From 0de6b076271b673d0e1dcc2b74b2ddd1646bf08e Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Sun, 31 May 2015 16:35:09 +0100 Subject: Rewrite settings as a tight wrapper around Settings, tweaks for tests --- tests/dom_tests/chrome.coffee | 6 +++++- tests/dom_tests/dom_tests.coffee | 30 +++++++++++++++++++----------- tests/dom_tests/dom_tests.html | 1 + 3 files changed, 25 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/dom_tests/chrome.coffee b/tests/dom_tests/chrome.coffee index 4c9bfa52..4de85876 100644 --- a/tests/dom_tests/chrome.coffee +++ b/tests/dom_tests/chrome.coffee @@ -7,6 +7,9 @@ root.chromeMessages = [] document.hasFocus = -> true +fakeManifest = + version: "1.51" + root.chrome = runtime: connect: -> @@ -18,7 +21,7 @@ root.chrome = onMessage: addListener: -> sendMessage: (message) -> chromeMessages.unshift message - getManifest: -> + getManifest: -> fakeManifest getURL: (url) -> "../../#{url}" storage: local: @@ -31,3 +34,4 @@ root.chrome = addListener: -> extension: inIncognitoContext: false + getURL: (url) -> chrome.runtime.getURL url diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index 8c2b73c3..e57f3eab 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -34,12 +34,20 @@ initializeModeState = -> handlerStack.bubbleEvent "registerKeyQueue", keyQueue: "" +# Tell Settings that it's been loaded. +Settings.isLoaded = true + +# Shoulda.js doesn't support async code, so we try not to use any. +Utils.nextTick = (func) -> func() + # # Retrieve the hint markers as an array object. # getHintMarkers = -> Array::slice.call document.getElementsByClassName("vimiumHintMarker"), 0 +stubSettings = (key, value) -> stub Settings.cache, key, JSON.stringify value + # # Generate tests that are common to both default and filtered # link hinting modes. @@ -52,8 +60,8 @@ createGeneralHintTests = (isFilteredMode) -> initializeModeState() testContent = "test" + "tress" document.getElementById("test-div").innerHTML = testContent - stub settings.values, "filterLinkHints", false - stub settings.values, "linkHintCharacters", "ab" + stubSettings "filterLinkHints", false + stubSettings "linkHintCharacters", "ab" tearDown -> document.getElementById("test-div").innerHTML = "" @@ -92,8 +100,8 @@ context "Test link hints for focusing input elements correctly", testDiv = document.getElementById("test-div") testDiv.innerHTML = "" - stub settings.values, "filterLinkHints", false - stub settings.values, "linkHintCharacters", "ab" + stubSettings "filterLinkHints", false + stubSettings "linkHintCharacters", "ab" # Every HTML5 input type except for hidden. We should be able to activate all of them with link hints. inputTypes = ["button", "checkbox", "color", "date", "datetime", "datetime-local", "email", "file", @@ -129,8 +137,8 @@ context "Alphabetical link hints", setup -> initializeModeState() - stub settings.values, "filterLinkHints", false - stub settings.values, "linkHintCharacters", "ab" + stubSettings "filterLinkHints", false + stubSettings "linkHintCharacters", "ab" # Three hints will trigger double hint chars. createLinks 3 @@ -161,8 +169,8 @@ context "Filtered link hints", # elements. setup -> - stub settings.values, "filterLinkHints", true - stub settings.values, "linkHintNumbers", "0123456789" + stubSettings "filterLinkHints", true + stubSettings "linkHintNumbers", "0123456789" context "Text hints", @@ -289,7 +297,7 @@ context "Find prev / next links", nextcorrupted next page """ - stub settings.values, "nextPatterns", "next" + stubSettings "nextPatterns", "next" goNext() assert.equal '#second', window.location.hash @@ -297,7 +305,7 @@ context "Find prev / next links", document.getElementById("test-div").innerHTML = """ >> """ - stub settings.values, "nextPatterns", ">>" + stubSettings "nextPatterns", ">>" goNext() assert.equal '#first', window.location.hash @@ -306,7 +314,7 @@ context "Find prev / next links", lorem ipsum next next! """ - stub settings.values, "nextPatterns", "next" + stubSettings "nextPatterns", "next" goNext() assert.equal '#second', window.location.hash diff --git a/tests/dom_tests/dom_tests.html b/tests/dom_tests/dom_tests.html index 5ccd39e7..f7cc430d 100644 --- a/tests/dom_tests/dom_tests.html +++ b/tests/dom_tests/dom_tests.html @@ -35,6 +35,7 @@ + -- cgit v1.2.3 From c62ffa33ad5230b89f44cb8f3268e6a4e48afd52 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 1 Jun 2015 07:04:43 +0100 Subject: Re-work unified settings. This is a minor re-working of #1705 from @mrmr1993. The main changes are: - Simplify initialisation logic. - Always initialise Settings immediately and automatically (ie. don't initialise Settings separately and manually in the background, content scripts, options and tests). - Get rid of addEventListener (it's only being used for on "load"). - Add Settings.use() in its place. --- tests/dom_tests/chrome.coffee | 2 +- tests/unit_tests/exclusion_test.coffee | 1 - tests/unit_tests/settings_test.coffee | 1 - tests/unit_tests/utils_test.coffee | 1 - 4 files changed, 1 insertion(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/dom_tests/chrome.coffee b/tests/dom_tests/chrome.coffee index 4de85876..d4e6930d 100644 --- a/tests/dom_tests/chrome.coffee +++ b/tests/dom_tests/chrome.coffee @@ -28,7 +28,7 @@ root.chrome = get: -> set: -> sync: - get: -> + get: (_, callback) -> callback? {} set: -> onChanged: addListener: -> diff --git a/tests/unit_tests/exclusion_test.coffee b/tests/unit_tests/exclusion_test.coffee index 28c17a2f..0e4b87bc 100644 --- a/tests/unit_tests/exclusion_test.coffee +++ b/tests/unit_tests/exclusion_test.coffee @@ -15,7 +15,6 @@ root.Marks = extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.44' extend(global,require "../../lib/settings.js") -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 ded7b5f8..a2aca6fd 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -14,7 +14,6 @@ context "settings", 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() should "save settings in localStorage as JSONified strings", -> Settings.set 'dummy', "" diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee index f9ed3636..67c3b333 100644 --- a/tests/unit_tests/utils_test.coffee +++ b/tests/unit_tests/utils_test.coffee @@ -3,7 +3,6 @@ extend global, require "./test_chrome_stubs.js" extend(global, require "../../lib/utils.js") Utils.getCurrentVersion = -> '1.43' extend(global, require "../../lib/settings.js") -Settings.init() context "isUrl", should "accept valid URLs", -> -- cgit v1.2.3 From e8476682362b9648aba874e8581fe9076479f734 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 1 Jun 2015 09:46:41 +0100 Subject: Remove LinkHints.init()... LinkHints.init() isn't doing anything. --- tests/dom_tests/dom_tests.coffee | 2 -- 1 file changed, 2 deletions(-) (limited to 'tests') diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index e57f3eab..8f293075 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -142,7 +142,6 @@ context "Alphabetical link hints", # Three hints will trigger double hint chars. createLinks 3 - LinkHints.init() LinkHints.activateMode() tearDown -> @@ -178,7 +177,6 @@ context "Filtered link hints", initializeModeState() testContent = "test" + "tress" + "trait" + "trackalt text" document.getElementById("test-div").innerHTML = testContent - LinkHints.init() LinkHints.activateMode() tearDown -> -- cgit v1.2.3 From 5f0400ebac5867df74225b987ea1238bdaeb40b2 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 1 Jun 2015 10:53:42 +0100 Subject: Refactor and eliminate Sync object. --- tests/unit_tests/settings_test.coffee | 11 +++++++++-- tests/unit_tests/test_chrome_stubs.coffee | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index a2aca6fd..08145190 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -38,16 +38,23 @@ context "settings", Settings.clear 'scrollStepSize' assert.equal Settings.get('scrollStepSize'), 60 +context "synced 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. + should "propagate non-default value via synced storage listener", -> Settings.set 'scrollStepSize', 20 assert.equal Settings.get('scrollStepSize'), 20 - Settings.Sync.handleStorageUpdate { scrollStepSize: { newValue: "40" } } + Settings.propagateChangesFromChromeStorage { 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 - Settings.Sync.handleStorageUpdate { scrollStepSize: { newValue: "60" } } + Settings.propagateChangesFromChromeStorage { scrollStepSize: { newValue: "60" } } assert.isFalse Settings.has 'scrollStepSize' should "propagate non-default values from synced storage", -> diff --git a/tests/unit_tests/test_chrome_stubs.coffee b/tests/unit_tests/test_chrome_stubs.coffee index 16f0e144..fe2fc298 100644 --- a/tests/unit_tests/test_chrome_stubs.coffee +++ b/tests/unit_tests/test_chrome_stubs.coffee @@ -70,14 +70,14 @@ exports.chrome = chrome.runtime.lastError = undefined key_value = {} key_value[key] = { newValue: value } - @func(key_value,'synced storage stub') if @func + @func(key_value,'sync') if @func callEmpty: (key) -> chrome.runtime.lastError = undefined if @func items = {} items[key] = {} - @func(items,'synced storage stub') + @func(items,'sync') session: MAX_SESSION_RESULTS: 25 -- cgit v1.2.3