Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rugk committed Nov 28, 2021
2 parents 4a86ad5 + b3a4ff5 commit 9f18a12
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
33 changes: 25 additions & 8 deletions EnvironmentalOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@
*/

/**
* Returns whether the current runtime is a mobile one (true) or not (false).
* Return the browser's browser info or fallback to an empty object.
*
* This is a workaround for Chrome/ium browsers that do not support runtime.getBrowserInfo().
* See https://github.com/TinyWebEx/AutomaticSettings/issues/18
*
* @private
* @returns {Promise<bool>}
*/
async function isMobile() {
async function getBrowserInfo() {
if (!browser.runtime.getBrowserInfo) {
return {};
}

return await browser.runtime.getBrowserInfo();
}

/**
* Returns whether the current runtime is a mobile one (true) or not (false).
*
* @public
* @returns {Promise<bool>}
*/
export async function isMobile() {
const platformInfo = await browser.runtime.getPlatformInfo();

return platformInfo.os === "android";
Expand All @@ -29,11 +46,11 @@ async function isMobile() {
*
* This includes Firefox and Thunderbird e.g.
*
* @private
* @public
* @returns {Promise<bool>}
*/
async function isMozilla() {
const browserInfo = await browser.runtime.getBrowserInfo();
export async function isMozilla() {
const browserInfo = await getBrowserInfo();

// Thunderbird is explicitly checked as a workaround as Thunderbird does not return the vendor correctly
// see https://bugzilla.mozilla.org/show_bug.cgi?id=1702722
Expand All @@ -46,11 +63,11 @@ async function isMozilla() {
*
* This does not include Thunderbird!
*
* @private
* @public
* @returns {Promise<bool>}
*/
async function isFirefox() {
const browserInfo = await browser.runtime.getBrowserInfo();
export async function isFirefox() {
const browserInfo = await getBrowserInfo();

return browserInfo.name === "Firefox";
}
Expand Down
31 changes: 31 additions & 0 deletions tests/environmentalOptions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "https://unpkg.com/[email protected]/mocha.js"; /* globals mocha */
import "https://unpkg.com/[email protected]/chai.js"; /* globals chai */
import "https://unpkg.com/[email protected]/pkg/sinon.js"; /* globals sinon */

import * as EnvironmentalOptions from "../EnvironmentalOptions.js";

describe("options module: AutomaticSettings.EnvironmentalOptions", function () {
describe("isMobile()", function () {
it("returns true as browser is a mobile browser", async function () {
const result = await EnvironmentalOptions.isMobile();

chai.assert.isTrue(result);
});
});

describe("isMozilla()", function () {
it("returns true as browser is a Mozilla browser", async function () {
const result = await EnvironmentalOptions.isMozilla();

chai.assert.isTrue(result);
});
});

describe("isFirefox()", function () {
it("returns true as browser is a Mozilla Firefox", async function () {
const result = await EnvironmentalOptions.isFirefox();

chai.assert.isTrue(result);
});
});
});
1 change: 1 addition & 0 deletions tests/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "https://unpkg.com/[email protected]/mocha.js"; /* globals mocha */

/* tests */
import "./automaticSettings.test.js";
import "./environmentalOptions.test.js";

mocha.checkLeaks();
mocha.run();
1 change: 1 addition & 0 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "https://unpkg.com/[email protected]/mocha.js"; /* globals mocha */
import "https://unpkg.com/[email protected]/dist/browser-polyfill.js"
// import "./node_modules/sinon/pkg/sinon.js";

mocha.setup("bdd");

0 comments on commit 9f18a12

Please sign in to comment.