From 9960e8f01ab8477151465af936d7cb14b84fb125 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Mon, 27 Apr 2015 16:21:38 +0100 Subject: Combine sync.coffee with settings.coffee --- tests/unit_tests/exclusion_test.coffee | 1 - tests/unit_tests/settings_test.coffee | 1 - tests/unit_tests/utils_test.coffee | 1 - 3 files changed, 3 deletions(-) (limited to 'tests/unit_tests') diff --git a/tests/unit_tests/exclusion_test.coffee b/tests/unit_tests/exclusion_test.coffee index b3ed7194..217f2309 100644 --- a/tests/unit_tests/exclusion_test.coffee +++ b/tests/unit_tests/exclusion_test.coffee @@ -14,7 +14,6 @@ root.Marks = extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' -extend(global,require "../../background_scripts/sync.js") extend(global,require "../../background_scripts/settings.js") Sync.init() extend(global, require "../../background_scripts/exclusions.js") diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index 4cd20211..e85c45f2 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -4,7 +4,6 @@ extend global, require "./test_chrome_stubs.js" extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' global.localStorage = {} -extend(global,require "../../background_scripts/sync.js") extend(global,require "../../background_scripts/settings.js") Sync.init() diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee index bfe066c3..e7febe13 100644 --- a/tests/unit_tests/utils_test.coffee +++ b/tests/unit_tests/utils_test.coffee @@ -2,7 +2,6 @@ require "./test_helper.js" extend global, require "./test_chrome_stubs.js" extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.43' -extend(global, require "../../background_scripts/sync.js") extend(global, require "../../background_scripts/settings.js") Sync.init() -- cgit v1.2.3 From 31873e39772c538cab418d03c244b4cac1addba0 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Mon, 27 Apr 2015 17:07:16 +0100 Subject: Remove all direct calls to Sync, stop exporting it This stops Sync from being referred to from anywhere except settings.coffee and settings_test.coffee. --- tests/unit_tests/exclusion_test.coffee | 2 +- tests/unit_tests/settings_test.coffee | 14 +++++++------- tests/unit_tests/utils_test.coffee | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'tests/unit_tests') diff --git a/tests/unit_tests/exclusion_test.coffee b/tests/unit_tests/exclusion_test.coffee index 217f2309..097e1f30 100644 --- a/tests/unit_tests/exclusion_test.coffee +++ b/tests/unit_tests/exclusion_test.coffee @@ -15,7 +15,7 @@ root.Marks = extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' extend(global,require "../../background_scripts/settings.js") -Sync.init() +Settings.init() extend(global, require "../../background_scripts/exclusions.js") extend(global, require "../../background_scripts/commands.js") extend(global, require "../../background_scripts/main.js") diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index e85c45f2..b44a904f 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -5,12 +5,13 @@ extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' global.localStorage = {} extend(global,require "../../background_scripts/settings.js") -Sync.init() context "settings", setup -> stub global, 'localStorage', {} + Settings.postUpdateHooks = {} # Avoid running update hooks which include calls to outside of settings. + Settings.init() should "save settings in localStorage as JSONified strings", -> Settings.set 'dummy', "" @@ -38,24 +39,22 @@ context "settings", should "propagate non-default value via synced storage listener", -> Settings.set 'scrollStepSize', 20 assert.equal Settings.get('scrollStepSize'), 20 - Sync.handleStorageUpdate { scrollStepSize: { newValue: "40" } } + Settings.Sync.handleStorageUpdate { scrollStepSize: { newValue: "40" } } assert.equal Settings.get('scrollStepSize'), 40 should "propagate default value via synced storage listener", -> Settings.set 'scrollStepSize', 20 assert.equal Settings.get('scrollStepSize'), 20 - Sync.handleStorageUpdate { scrollStepSize: { newValue: "60" } } + Settings.Sync.handleStorageUpdate { scrollStepSize: { newValue: "60" } } assert.isFalse Settings.has 'scrollStepSize' should "propagate non-default values from synced storage", -> chrome.storage.sync.set { scrollStepSize: JSON.stringify(20) } - Sync.fetchAsync() assert.equal Settings.get('scrollStepSize'), 20 should "propagate default values from synced storage", -> Settings.set 'scrollStepSize', 20 chrome.storage.sync.set { scrollStepSize: JSON.stringify(60) } - Sync.fetchAsync() assert.isFalse Settings.has 'scrollStepSize' should "clear a setting from synced storage", -> @@ -65,9 +64,10 @@ context "settings", should "trigger a postUpdateHook", -> message = "Hello World" - Settings.postUpdateHooks['scrollStepSize'] = (value) -> Sync.message = value + receivedMessage = "" + Settings.postUpdateHooks['scrollStepSize'] = (value) -> receivedMessage = value chrome.storage.sync.set { scrollStepSize: JSON.stringify(message) } - assert.equal message, Sync.message + assert.equal message, receivedMessage should "sync a key which is not a known setting (without crashing)", -> chrome.storage.sync.set { notASetting: JSON.stringify("notAUsefullValue") } diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee index e7febe13..283f0ca0 100644 --- a/tests/unit_tests/utils_test.coffee +++ b/tests/unit_tests/utils_test.coffee @@ -3,7 +3,7 @@ extend global, require "./test_chrome_stubs.js" extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.43' extend(global, require "../../background_scripts/settings.js") -Sync.init() +Settings.init() context "isUrl", should "accept valid URLs", -> -- cgit v1.2.3 From d7a0daf5fa2c0a3302a8fc6b9fa0744cfa17ab42 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Mon, 27 Apr 2015 19:29:39 +0100 Subject: Move registration of postUpdateHooks to the corresponding source files This completely decouples settings.coffee from all other background source files, so that it can (eventually) also be used in the frontend. --- tests/unit_tests/commands_test.coffee | 1 + 1 file changed, 1 insertion(+) (limited to 'tests/unit_tests') diff --git a/tests/unit_tests/commands_test.coffee b/tests/unit_tests/commands_test.coffee index daaef016..e55dc0f2 100644 --- a/tests/unit_tests/commands_test.coffee +++ b/tests/unit_tests/commands_test.coffee @@ -1,5 +1,6 @@ require "./test_helper.js" extend global, require "./test_chrome_stubs.js" +global.Settings = {postUpdateHooks: {}} {Commands} = require "../../background_scripts/commands.js" context "Key mappings", -- cgit v1.2.3 From bfe304932b13eb1bfe65662490d3d6b830eefec7 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Mon, 27 Apr 2015 19:58:10 +0100 Subject: Only perform settings migration in the background page --- tests/unit_tests/test_chrome_stubs.coffee | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/unit_tests') diff --git a/tests/unit_tests/test_chrome_stubs.coffee b/tests/unit_tests/test_chrome_stubs.coffee index 60f3a890..16f0e144 100644 --- a/tests/unit_tests/test_chrome_stubs.coffee +++ b/tests/unit_tests/test_chrome_stubs.coffee @@ -19,6 +19,9 @@ exports.chrome = onInstalled: addListener: -> + extension: + getURL: (path) -> path + tabs: onSelectionChanged: addListener: () -> true -- cgit v1.2.3 From 0c12810c4c49ade77a1c8a8b3172857e19eb01f0 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Mon, 27 Apr 2015 20:08:11 +0100 Subject: Move settings.coffee from background_scripts/ to lib/ --- tests/unit_tests/exclusion_test.coffee | 2 +- tests/unit_tests/settings_test.coffee | 2 +- tests/unit_tests/utils_test.coffee | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/unit_tests') diff --git a/tests/unit_tests/exclusion_test.coffee b/tests/unit_tests/exclusion_test.coffee index 097e1f30..28c17a2f 100644 --- a/tests/unit_tests/exclusion_test.coffee +++ b/tests/unit_tests/exclusion_test.coffee @@ -14,7 +14,7 @@ root.Marks = extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' -extend(global,require "../../background_scripts/settings.js") +extend(global,require "../../lib/settings.js") Settings.init() extend(global, require "../../background_scripts/exclusions.js") extend(global, require "../../background_scripts/commands.js") diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index b44a904f..946a1688 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -4,7 +4,7 @@ extend global, require "./test_chrome_stubs.js" extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' global.localStorage = {} -extend(global,require "../../background_scripts/settings.js") +extend(global,require "../../lib/settings.js") context "settings", diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee index 283f0ca0..f9ed3636 100644 --- a/tests/unit_tests/utils_test.coffee +++ b/tests/unit_tests/utils_test.coffee @@ -2,7 +2,7 @@ require "./test_helper.js" extend global, require "./test_chrome_stubs.js" extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.43' -extend(global, require "../../background_scripts/settings.js") +extend(global, require "../../lib/settings.js") Settings.init() context "isUrl", -- cgit v1.2.3 From 7ff17b8a6f63b0f46fac2be27c2a2f7d82c8d458 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Mon, 27 Apr 2015 20:44:23 +0100 Subject: Make Settings explicitly use a cache The Settings object used by the background page now uses 1 of 3 caches, depending on the context it is available in: * localStorage - in the background page * a copy of localStorage - in non-background extension pages (options.html, popup.html, etc.) * an empty object - in all other pages (where localStorage doesn't point to the extension's localStorage object). For any extension page which is *not* the background page, a copy of localStorage is used instead of true localStorage: * Once localStorage is updated by one background page, the others can only see the updated copy. - Pages with an updated cache can't tell which changes are new, and so don't know which postUpdateHooks to run. * By copying localStorage's contents into a new object, extension pages can still access settings synchronously. - This is especially important to options.html and popup.html; they will not work without it. --- tests/unit_tests/settings_test.coffee | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/unit_tests') diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index 946a1688..ded7b5f8 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -3,6 +3,8 @@ extend global, require "./test_chrome_stubs.js" extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' +Utils.isBackgroundPage = -> true +Utils.isExtensionPage = -> true global.localStorage = {} extend(global,require "../../lib/settings.js") @@ -10,6 +12,7 @@ context "settings", setup -> stub global, 'localStorage', {} + Settings.cache = global.localStorage # Point the settings cache to the new localStorage object. Settings.postUpdateHooks = {} # Avoid running update hooks which include calls to outside of settings. Settings.init() -- cgit v1.2.3