aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPhil Crosby2014-04-30 00:42:42 -0700
committerPhil Crosby2014-04-30 00:42:42 -0700
commitfc23be221270dc11914244f5c93d900ad8e9225c (patch)
tree9d676401ce359a28ef76a552e514db590cee3ecd /tests
parentfa16722613cd60933258edac2c0aa1908fe387bd (diff)
parentdb65721aa67b2de75b1e279f01e721676e83b448 (diff)
downloadvimium-fc23be221270dc11914244f5c93d900ad8e9225c.tar.bz2
Merge branch 'smblott-github-sync-chrome-instances'
Conflicts: tests/unit_tests/utils_test.coffee
Diffstat (limited to 'tests')
-rw-r--r--tests/unit_tests/settings_test.coffee46
-rw-r--r--tests/unit_tests/test_chrome_stubs.coffee61
-rw-r--r--tests/unit_tests/utils_test.coffee4
3 files changed, 109 insertions, 2 deletions
diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee
index b2c5484b..25bb3628 100644
--- a/tests/unit_tests/settings_test.coffee
+++ b/tests/unit_tests/settings_test.coffee
@@ -1,15 +1,22 @@
require "./test_helper.js"
+require "./test_chrome_stubs.js"
extend(global, require "../../lib/utils.js")
Utils.getCurrentVersion = -> '1.44'
global.localStorage = {}
-{Settings} = require "../../background_scripts/settings.js"
+extend(global,require "../../background_scripts/sync.js")
+extend(global,require "../../background_scripts/settings.js")
+Sync.init()
context "settings",
setup ->
stub global, 'localStorage', {}
+ should "save settings in localStorage as JSONified strings", ->
+ Settings.set 'dummy', ""
+ assert.equal localStorage.dummy, '""'
+
should "obtain defaults if no key is stored", ->
assert.isFalse Settings.has 'scrollStepSize'
assert.equal Settings.get('scrollStepSize'), 60
@@ -28,3 +35,40 @@ context "settings",
Settings.set 'scrollStepSize', 20
Settings.clear 'scrollStepSize'
assert.equal Settings.get('scrollStepSize'), 60
+
+ should "propagate non-default value via synced storage listener", ->
+ Settings.set 'scrollStepSize', 20
+ assert.equal Settings.get('scrollStepSize'), 20
+ 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" } }
+ 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", ->
+ Settings.set 'scrollStepSize', 20
+ chrome.storage.sync.remove 'scrollStepSize'
+ assert.isFalse Settings.has 'scrollStepSize'
+
+ should "trigger a postUpdateHook", ->
+ message = "Hello World"
+ Settings.postUpdateHooks['scrollStepSize'] = (value) -> Sync.message = value
+ chrome.storage.sync.set { scrollStepSize: JSON.stringify(message) }
+ assert.equal message, Sync.message
+
+ 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/test_chrome_stubs.coffee b/tests/unit_tests/test_chrome_stubs.coffee
new file mode 100644
index 00000000..e9c48f31
--- /dev/null
+++ b/tests/unit_tests/test_chrome_stubs.coffee
@@ -0,0 +1,61 @@
+
+#
+# This is a stub for chrome.strorage.sync for testing.
+# It does what chrome.storage.sync should do (roughly), but does so synchronously.
+#
+
+global.chrome =
+ runtime: {}
+
+ storage:
+
+ # chrome.storage.onChanged
+ onChanged:
+ addListener: (func) -> @func = func
+
+ # Fake a callback from chrome.storage.sync.
+ call: (key, value) ->
+ chrome.runtime = { lastError: undefined }
+ key_value = {}
+ key_value[key] = { newValue: value }
+ @func(key_value,'synced storage stub') if @func
+
+ callEmpty: (key) ->
+ chrome.runtime = { lastError: undefined }
+ if @func
+ items = {}
+ items[key] = {}
+ @func(items,'synced storage stub')
+
+ # chrome.storage.sync
+ sync:
+ store: {}
+
+ set: (items, callback) ->
+ chrome.runtime = { lastError: undefined }
+ for own key, value of items
+ @store[key] = value
+ callback() if callback
+ # Now, generate (supposedly asynchronous) notifications for listeners.
+ for own key, value of items
+ global.chrome.storage.onChanged.call(key,value)
+
+ get: (keys, callback) ->
+ chrome.runtime = { lastError: undefined }
+ if keys == null
+ keys = []
+ for own key, value of @store
+ keys.push key
+ items = {}
+ for key in keys
+ items[key] = @store[key]
+ # Now, generate (supposedly asynchronous) callback
+ callback items if callback
+
+ remove: (key, callback) ->
+ chrome. runtime = { lastError: undefined }
+ if key of @store
+ delete @store[key]
+ callback() if callback
+ # Now, generate (supposedly asynchronous) notification for listeners.
+ global.chrome.storage.onChanged.callEmpty(key)
diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee
index 91a06135..c4139dbb 100644
--- a/tests/unit_tests/utils_test.coffee
+++ b/tests/unit_tests/utils_test.coffee
@@ -1,8 +1,10 @@
require "./test_helper.js"
+require "./test_chrome_stubs.js"
extend(global, require "../../lib/utils.js")
Utils.getCurrentVersion = -> '1.43'
-global.localStorage = {}
+extend(global, require "../../background_scripts/sync.js")
extend(global, require "../../background_scripts/settings.js")
+Sync.init()
context "isUrl",
should "accept valid URLs", ->