From 56c739fa080359fe15cd874f0fa44ffc1c9baa9e Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 12 Aug 2021 15:41:52 -0500 Subject: [PATCH] os: add isAndroid() function This will be used to test if the app is running on Android. --- src/utils/os.test.ts | 13 ++++++++++++- src/utils/os.ts | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/utils/os.test.ts b/src/utils/os.test.ts index 90c3d947..c21b28da 100644 --- a/src/utils/os.test.ts +++ b/src/utils/os.test.ts @@ -1,12 +1,23 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2021 The Pybricks Authors -import { isMacOS, isWindows, prefersDarkMode } from './os'; +import { isAndroid, isMacOS, isWindows, prefersDarkMode } from './os'; afterEach(() => { jest.resetAllMocks(); }); +describe('isAndroid', () => { + test('is true', () => { + jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue('Android'); + expect(isAndroid()).toBeTruthy(); + }); + test('is false', () => { + jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue('Linux'); + expect(isAndroid()).toBeFalsy(); + }); +}); + describe('isMacOS', () => { test('is true', () => { jest.spyOn(navigator, 'platform', 'get').mockReturnValue('MacIntel'); diff --git a/src/utils/os.ts b/src/utils/os.ts index 12685d2e..5fe83dfd 100644 --- a/src/utils/os.ts +++ b/src/utils/os.ts @@ -3,6 +3,17 @@ // Utility functions for dealing with operating systems. +// TODO: replace with navigator.userAgentData when it is more widely available +// https://developer.mozilla.org/en-US/docs/Web/API/User-Agent_Client_Hints_API + +/** + * Tests if we are running on Android. + * @returns `true` if running on Android, otherwise `false`. + */ +export function isAndroid(): boolean { + return /android/i.test(navigator.userAgent); +} + /** * Tests if we are running on macOS. * @returns `true` if running on macOS, otherwise `false`.