diff options
| author | Stephen Blott | 2014-04-19 09:44:50 +0100 | 
|---|---|---|
| committer | Stephen Blott | 2014-04-19 09:44:50 +0100 | 
| commit | a2de5078f8d34c2b65b451556aff07a6a41399a5 (patch) | |
| tree | fc90b6a0e331906dc9ff74eb26443b540cb69477 | |
| parent | 64f290465aa14478200dc266ccc68b3475352e69 (diff) | |
| download | vimium-a2de5078f8d34c2b65b451556aff07a6a41399a5.tar.bz2 | |
Add test cases for "asynchronous" sync.
| -rw-r--r-- | background_scripts/sync.coffee | 2 | ||||
| -rw-r--r-- | tests/unit_tests/settings_test.coffee | 22 | ||||
| -rw-r--r-- | tests/unit_tests/test_chrome_stubs.coffee | 49 | 
3 files changed, 70 insertions, 3 deletions
| diff --git a/background_scripts/sync.coffee b/background_scripts/sync.coffee index afde3e70..3a829722 100644 --- a/background_scripts/sync.coffee +++ b/background_scripts/sync.coffee @@ -83,7 +83,7 @@ root.Sync = Sync =        return      #      if @isSyncKey key -      @storage.set @mkKeyValue(key,value), -> +      @storage.set @mkKeyValue(key,value), =>          if chrome.runtime.lastError            @log "chrome sync callback for Sync.set() indicates error: " + key            @log chrome.runtime.lastError diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index 57766c12..07b96a30 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -42,3 +42,25 @@ context "settings",      assert.equal Settings.get('scrollStepSize'), 20      Sync.listener { scrollStepSize: { newValue: "60" } }      assert.isFalse Settings.has 'scrollStepSize' + +  should "remote changes are propagated, non-default value", -> +    # Prime Sync. +    Settings.set 'scrollStepSize', 20 +    assert.equal Settings.get('scrollStepSize'), 20 +    # Set a bogus value in localStorage, bypassing Settings and Sync. +    localStorage['scrollStepSize'] = JSON.stringify(10) +    assert.equal Settings.get('scrollStepSize'), 10 +    # Pull Sync's version of scrollStepSize, this should reset it to the correct value (20). +    Sync.pull() +    assert.equal Settings.get('scrollStepSize'), 20 + +  should "remote changes are propagated, default value", -> +    # Prime Sync with a default value. +    chrome.storage.sync.set { scrollStepSize: JSON.stringify(60) } +    assert.isFalse Settings.has 'scrollStepSize' +    # Set a bogus value in localStorage, bypassing Settings and Sync. +    localStorage['scrollStepSize'] = JSON.stringify(10) +    assert.equal Settings.get('scrollStepSize'), 10 +    # Pull Sync's version of scrollStepSize, this should delete scrollStepSize in localStorage, because it's a default value. +    Sync.pull() +    assert.isFalse Settings.has 'scrollStepSize' diff --git a/tests/unit_tests/test_chrome_stubs.coffee b/tests/unit_tests/test_chrome_stubs.coffee index 86eef1a1..f32de24f 100644 --- a/tests/unit_tests/test_chrome_stubs.coffee +++ b/tests/unit_tests/test_chrome_stubs.coffee @@ -1,11 +1,56 @@  global.chrome ||= {} +global.runtime ||= {}  global.chrome.storage ||= {} +# +# 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.storage.onChanged ||= -  addListener: (changes,area) -> +  addListener: (func) -> @func = func + +  # Fake a callback from chrome.storage.sync. +  call: (key,value) -> +    chrome.runtime = { lastError: undefined } +    if @func +      @func( @mkKeyValue(key,value), 'synced storage stub' ) + +  mkKeyValue: (key, value) -> +    obj = {} +    obj[key] = { newValue: value } +    obj  global.chrome.storage.sync ||= -  set: (key,value,callback) -> +  store: {} + +  set: (items,callback) -> +    chrome.runtime = { lastError: undefined } +    for own key, value of items +      @store[key] = value +    if callback +      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 +    if callback +      callback items +    remove: (key,callback) -> +    chrome.runtime = { lastError: undefined } +    if key of @store +      delete @store[key] +    if callback +      callback() | 
