Skip to content

Colota - Android GPS Location tracker

14.3 KB
Raw Blame
import { Platform, PermissionsAndroid } from "react-native"
import {
  ensurePermissions,
  checkPermissions,
  ensureLocalNetworkPermission,
  registerDisclosureCallback,
  registerLocalNetworkDisclosureCallback
} from "../LocationServicePermission"
import { showAlert } from "../modalService"

jest.mock("../modalService", () => ({
  showAlert: jest.fn(),
  showConfirm: jest.fn()
}))

// Mock NativeLocationService
jest.mock("../NativeLocationService", () => ({
  isIgnoringBatteryOptimizations: jest.fn().mockResolvedValue(true),
  requestIgnoreBatteryOptimizations: jest.fn().mockResolvedValue(undefined)
}))

import NativeLocationService from "../NativeLocationService"

const mockIsIgnoring = NativeLocationService.isIgnoringBatteryOptimizations as jest.Mock
const mockRequestIgnore = NativeLocationService.requestIgnoreBatteryOptimizations as jest.Mock

let requestSpy: jest.SpyInstance
let checkSpy: jest.SpyInstance

function setPlatform(os: string, version: number) {
  Object.defineProperty(Platform, "OS", { get: () => os, configurable: true })
  Object.defineProperty(Platform, "Version", { get: () => version, configurable: true })
}

const originalOS = Platform.OS
const originalVersion = Platform.Version

beforeEach(() => {
  jest.clearAllMocks()
  jest.spyOn(console, "error").mockImplementation()
  requestSpy = jest.spyOn(PermissionsAndroid, "request").mockResolvedValue(PermissionsAndroid.RESULTS.GRANTED)
  // Default: permissions not granted (so disclosure + request flow runs)
  checkSpy = jest.spyOn(PermissionsAndroid, "check").mockResolvedValue(false)
  // Register auto-agree disclosure for tests
  registerDisclosureCallback(() => Promise.resolve(true))
})

afterEach(() => {
  jest.restoreAllMocks()
  setPlatform(originalOS, originalVersion as number)
  // Reset disclosure callback
  registerDisclosureCallback(() => Promise.resolve(true))
})

describe("ensurePermissions", () => {
  it("returns true immediately on non-Android platforms", async () => {
    setPlatform("ios", 0)

    const result = await ensurePermissions()

    expect(result).toBe(true)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("skips disclosure and requests if all permissions already granted", async () => {
    setPlatform("android", 33)
    checkSpy.mockResolvedValue(true)

    const disclosureSpy = jest.fn().mockResolvedValue(true)
    registerDisclosureCallback(disclosureSpy)

    const result = await ensurePermissions()

    expect(result).toBe(true)
    expect(disclosureSpy).not.toHaveBeenCalled()
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("skips the disclosure when only notification permission is missing", async () => {
    setPlatform("android", 33)
    checkSpy.mockImplementation((perm: string) =>
      Promise.resolve(perm !== PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
    )
    const disclosureSpy = jest.fn().mockResolvedValue(true)
    registerDisclosureCallback(disclosureSpy)

    const result = await ensurePermissions()

    expect(result).toBe(true)
    expect(disclosureSpy).not.toHaveBeenCalled()
    // No prompt without a disclosure before it: the notification ask only rides along with the location grants
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("starts tracking when notifications are denied even if the disclosure would be declined", async () => {
    setPlatform("android", 33)
    checkSpy.mockImplementation((perm: string) =>
      Promise.resolve(perm !== PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
    )
    requestSpy.mockResolvedValue(PermissionsAndroid.RESULTS.DENIED)
    registerDisclosureCallback(() => Promise.resolve(false))

    expect(await ensurePermissions()).toBe(true)
  })

  it("still shows the disclosure when only background location is missing", async () => {
    setPlatform("android", 33)
    checkSpy.mockImplementation((perm: string) =>
      Promise.resolve(perm !== PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION)
    )
    const disclosureSpy = jest.fn().mockResolvedValue(true)
    registerDisclosureCallback(disclosureSpy)

    await ensurePermissions()

    // Google Play requires the disclosure immediately before the background location request
    expect(disclosureSpy).toHaveBeenCalledTimes(1)
    expect(requestSpy).toHaveBeenCalledWith(PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION)
  })

  it("shows disclosure before requesting permissions", async () => {
    setPlatform("android", 28)
    const disclosureSpy = jest.fn().mockResolvedValue(true)
    registerDisclosureCallback(disclosureSpy)

    await ensurePermissions()

    expect(disclosureSpy).toHaveBeenCalledTimes(1)
    expect(requestSpy).toHaveBeenCalled()
  })

  it("returns false if user denies disclosure", async () => {
    setPlatform("android", 28)
    registerDisclosureCallback(() => Promise.resolve(false))

    const result = await ensurePermissions()

    expect(result).toBe(false)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("requests fine location permission", async () => {
    setPlatform("android", 28)

    await ensurePermissions()

    expect(requestSpy).toHaveBeenCalledWith(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
  })

  it("returns false if fine location denied", async () => {
    setPlatform("android", 28)
    requestSpy.mockResolvedValueOnce(PermissionsAndroid.RESULTS.DENIED)

    const result = await ensurePermissions()

    expect(result).toBe(false)
  })

  it("requests background location on Android 10+", async () => {
    setPlatform("android", 29)

    await ensurePermissions()

    expect(requestSpy).toHaveBeenCalledWith(PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION)
  })

  it("skips background location on Android < 10", async () => {
    setPlatform("android", 28)

    await ensurePermissions()

    const permissions = requestSpy.mock.calls.map((c: any[]) => c[0])
    expect(permissions).not.toContain(PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION)
  })

  it("requests notification permission on Android 13+", async () => {
    setPlatform("android", 33)

    await ensurePermissions()

    expect(requestSpy).toHaveBeenCalledWith(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
  })

  it("skips notification permission on Android < 13", async () => {
    setPlatform("android", 29)

    await ensurePermissions()

    const permissions = requestSpy.mock.calls.map((c: any[]) => c[0])
    expect(permissions).not.toContain(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
  })

  it("returns false if background location denied", async () => {
    setPlatform("android", 29)
    requestSpy
      .mockResolvedValueOnce(PermissionsAndroid.RESULTS.GRANTED) // fine location
      .mockResolvedValueOnce(PermissionsAndroid.RESULTS.DENIED) // background

    const result = await ensurePermissions()

    expect(result).toBe(false)
  })

  it("still returns true if notification permission denied (non-blocking)", async () => {
    setPlatform("android", 33)
    requestSpy
      .mockResolvedValueOnce(PermissionsAndroid.RESULTS.GRANTED) // fine location
      .mockResolvedValueOnce(PermissionsAndroid.RESULTS.GRANTED) // background
      .mockResolvedValueOnce(PermissionsAndroid.RESULTS.DENIED) // notification

    const result = await ensurePermissions()

    expect(result).toBe(true)
  })

  it("requests battery optimization exemption", async () => {
    setPlatform("android", 33)
    // First call from checkPermissions (checkBatteryOptimization), second from requestBatteryOptimizationExemption
    mockIsIgnoring.mockResolvedValueOnce(false).mockResolvedValueOnce(false)

    await ensurePermissions()

    expect(mockRequestIgnore).toHaveBeenCalled()
  })

  it("skips battery request if already exempted", async () => {
    setPlatform("android", 28)
    mockIsIgnoring.mockResolvedValueOnce(true)

    await ensurePermissions()

    expect(mockRequestIgnore).not.toHaveBeenCalled()
  })

  it("still returns true if battery optimization request fails", async () => {
    setPlatform("android", 28)
    mockIsIgnoring.mockRejectedValueOnce(new Error("battery check failed"))

    const result = await ensurePermissions()

    expect(result).toBe(true)
  })

  it("returns false and shows alert on unexpected error", async () => {
    setPlatform("android", 28)
    checkSpy.mockRejectedValueOnce(new Error("unexpected"))

    const result = await ensurePermissions()

    expect(result).toBe(false)
    expect(showAlert).toHaveBeenCalledWith("Permission Error", expect.any(String), "error")
  })

  // Play reviewed the modal's wording; a stand-in with other text must never reach a user.
  it("requests nothing when no disclosure is registered", async () => {
    setPlatform("android", 28)
    registerDisclosureCallback(undefined as any)

    expect(await ensurePermissions()).toBe(false)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("skips already-granted permissions", async () => {
    setPlatform("android", 33)
    // Fine location already granted, background and notifications not
    checkSpy.mockImplementation((permission: string) => {
      if (permission === PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION) {
        return Promise.resolve(true)
      }
      return Promise.resolve(false)
    })

    await ensurePermissions()

    const permissions = requestSpy.mock.calls.map((c: any[]) => c[0])
    expect(permissions).not.toContain(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
    expect(permissions).toContain(PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION)
  })
})

describe("checkPermissions", () => {
  it("returns all true on non-Android platforms", async () => {
    setPlatform("ios", 0)

    const result = await checkPermissions()

    expect(result).toEqual({
      location: true,
      background: true,
      notifications: true,
      batteryOptimized: true,
      localNetwork: true
    })
  })

  it("checks all permissions in parallel on Android", async () => {
    setPlatform("android", 33)

    checkSpy.mockImplementation((permission: string) => {
      if (permission === PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS) {
        return Promise.resolve(false)
      }
      return Promise.resolve(true)
    })
    mockIsIgnoring.mockResolvedValueOnce(true)

    const result = await checkPermissions()

    expect(result.location).toBe(true)
    expect(result.background).toBe(true)
    expect(result.notifications).toBe(false)
    expect(result.batteryOptimized).toBe(true)
  })

  it("returns true for background on Android < 10", async () => {
    setPlatform("android", 28)
    mockIsIgnoring.mockResolvedValueOnce(true)

    const result = await checkPermissions()

    expect(result.background).toBe(true)
  })

  it("returns true for notifications on Android < 13", async () => {
    setPlatform("android", 29)
    mockIsIgnoring.mockResolvedValueOnce(true)

    const result = await checkPermissions()

    expect(result.notifications).toBe(true)
  })

  it("returns false for battery if check throws", async () => {
    setPlatform("android", 28)
    mockIsIgnoring.mockRejectedValueOnce(new Error("failed"))

    const result = await checkPermissions()

    expect(result.batteryOptimized).toBe(false)
  })

  it("returns true for localNetwork on Android < 37", async () => {
    setPlatform("android", 36)
    mockIsIgnoring.mockResolvedValueOnce(true)

    const result = await checkPermissions()

    expect(result.localNetwork).toBe(true)
  })

  it("checks ACCESS_LOCAL_NETWORK on Android 37+", async () => {
    setPlatform("android", 37)
    checkSpy.mockImplementation((permission: string) => {
      if (permission === "android.permission.ACCESS_LOCAL_NETWORK") {
        return Promise.resolve(false)
      }
      return Promise.resolve(true)
    })
    mockIsIgnoring.mockResolvedValueOnce(true)

    const result = await checkPermissions()

    expect(result.localNetwork).toBe(false)
  })
})

describe("ensureLocalNetworkPermission", () => {
  it("returns true on non-Android platforms", async () => {
    setPlatform("ios", 0)

    const result = await ensureLocalNetworkPermission()

    expect(result).toBe(true)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("returns true on Android < 37 without requesting", async () => {
    setPlatform("android", 36)

    const result = await ensureLocalNetworkPermission()

    expect(result).toBe(true)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("returns true if already granted", async () => {
    setPlatform("android", 37)
    checkSpy.mockResolvedValue(true)

    const result = await ensureLocalNetworkPermission()

    expect(result).toBe(true)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("shows disclosure before requesting permission", async () => {
    setPlatform("android", 37)
    const disclosureSpy = jest.fn().mockResolvedValue(true)
    registerLocalNetworkDisclosureCallback(disclosureSpy)

    await ensureLocalNetworkPermission()

    expect(disclosureSpy).toHaveBeenCalledTimes(1)
    expect(requestSpy).toHaveBeenCalledWith("android.permission.ACCESS_LOCAL_NETWORK")
  })

  it("returns false if user denies disclosure", async () => {
    setPlatform("android", 37)
    registerLocalNetworkDisclosureCallback(() => Promise.resolve(false))

    const result = await ensureLocalNetworkPermission()

    expect(result).toBe(false)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("returns true if permission granted after request", async () => {
    setPlatform("android", 37)
    registerLocalNetworkDisclosureCallback(() => Promise.resolve(true))
    requestSpy.mockResolvedValueOnce(PermissionsAndroid.RESULTS.GRANTED)

    const result = await ensureLocalNetworkPermission()

    expect(result).toBe(true)
  })

  it("returns false if permission denied after request", async () => {
    setPlatform("android", 37)
    registerLocalNetworkDisclosureCallback(() => Promise.resolve(true))
    requestSpy.mockResolvedValueOnce(PermissionsAndroid.RESULTS.DENIED)

    const result = await ensureLocalNetworkPermission()

    expect(result).toBe(false)
  })

  it("requests nothing when no local network disclosure is registered", async () => {
    setPlatform("android", 37)
    registerLocalNetworkDisclosureCallback(undefined as any)

    expect(await ensureLocalNetworkPermission()).toBe(false)
    expect(requestSpy).not.toHaveBeenCalled()
  })

  it("returns false on unexpected error", async () => {
    setPlatform("android", 37)
    checkSpy.mockRejectedValueOnce(new Error("unexpected"))

    const result = await ensureLocalNetworkPermission()

    expect(result).toBe(false)
  })
})