aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit_tests/test_chrome_stubs.coffee
blob: 396a2e5573e95d8f4d6ba2fb9ab889a59515b723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#
# This is a stub for chrome.strorage.sync for testing.
# It does what chrome.storage.sync should do (roughly), but does so synchronously.
# It also provides stubs for a number of other chrome APIs.
#

exports.window = {}
exports.localStorage = {}

exports.chrome =
  runtime:
    getManifest: () ->
      version: "1.2.3"
    onConnect:
      addListener: () -> true
    onMessage:
      addListener: () -> true

  tabs:
    onSelectionChanged:
      addListener: () -> true
    onUpdated:
      addListener: () -> true
    onAttached:
      addListener: () -> true
    onMoved:
      addListener: () -> true
    onRemoved:
      addListener: () -> true
    onActiveChanged:
      addListener: () -> true
    onActivated:
      addListener: () -> true
    onReplaced:
      addListener: () -> true
    query: () -> true

  windows:
    onRemoved:
      addListener: () -> true
    getAll: () -> true

  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')

    session:
      MAX_SESSION_RESULTS: 25

    # 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)

exports.setTimeout = (callback,timeout) -> callback()