| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var assertEq = chrome.test.assertEq; |
| 6 var assertTrue = chrome.test.assertTrue; |
| 7 var succeed = chrome.test.succeed; |
| 8 |
| 5 function test(stage0) { | 9 function test(stage0) { |
| 6 var apis = [ | 10 var apis = [ |
| 7 chrome.storage.sync, | 11 chrome.storage.sync, |
| 8 chrome.storage.local | 12 chrome.storage.local |
| 9 ]; | 13 ]; |
| 10 apis.forEach(function(api) { | 14 apis.forEach(function(api) { |
| 11 api.succeed = chrome.test.callbackPass(api.clear.bind(api)); | 15 api.succeed = chrome.test.callbackPass(api.clear.bind(api)); |
| 12 stage0.call(api); | 16 stage0.call(api); |
| 13 }); | 17 }); |
| 14 } | 18 } |
| 15 | 19 |
| 16 chrome.test.runTests([ | 20 chrome.test.runTests([ |
| 17 function getWhenEmpty() { | 21 function getWhenEmpty() { |
| 18 function stage0() { | 22 function stage0() { |
| 19 this.get('foo', stage1.bind(this)); | 23 this.get('foo', stage1.bind(this)); |
| 20 } | 24 } |
| 21 function stage1(settings) { | 25 function stage1(settings) { |
| 22 chrome.test.assertEq({}, settings); | 26 assertEq({}, settings); |
| 23 this.get(['foo', 'bar'], stage2.bind(this)); | 27 this.get(['foo', 'bar'], stage2.bind(this)); |
| 24 } | 28 } |
| 25 function stage2(settings) { | 29 function stage2(settings) { |
| 26 chrome.test.assertEq({}, settings); | 30 assertEq({}, settings); |
| 27 this.get(undefined, stage3.bind(this)); | 31 this.get(undefined, stage3.bind(this)); |
| 28 } | 32 } |
| 29 function stage3(settings) { | 33 function stage3(settings) { |
| 30 chrome.test.assertEq({}, settings); | 34 assertEq({}, settings); |
| 31 this.succeed(); | 35 this.succeed(); |
| 32 } | 36 } |
| 33 test(stage0); | 37 test(stage0); |
| 34 }, | 38 }, |
| 35 | 39 |
| 36 function getWhenNonempty() { | 40 function getWhenNonempty() { |
| 37 function stage0() { | 41 function stage0() { |
| 38 this.set({ | 42 this.set({ |
| 39 'foo' : 'bar', | 43 'foo' : 'bar', |
| 40 'baz' : 'qux', | 44 'baz' : 'qux', |
| 41 'hello': 'world' | 45 'hello': 'world' |
| 42 }, stage1.bind(this)); | 46 }, stage1.bind(this)); |
| 43 } | 47 } |
| 44 function stage1() { | 48 function stage1() { |
| 45 this.get(['foo', 'baz'], stage2.bind(this)); | 49 this.get(['foo', 'baz'], stage2.bind(this)); |
| 46 } | 50 } |
| 47 function stage2(settings) { | 51 function stage2(settings) { |
| 48 chrome.test.assertEq({ | 52 assertEq({ |
| 49 'foo': 'bar', | 53 'foo': 'bar', |
| 50 'baz': 'qux' | 54 'baz': 'qux' |
| 51 }, settings); | 55 }, settings); |
| 52 this.get(['nothing', 'baz', 'hello', 'ignore'], stage3.bind(this)); | 56 this.get(['nothing', 'baz', 'hello', 'ignore'], stage3.bind(this)); |
| 53 } | 57 } |
| 54 function stage3(settings) { | 58 function stage3(settings) { |
| 55 chrome.test.assertEq({ | 59 assertEq({ |
| 56 'baz' : 'qux', | 60 'baz' : 'qux', |
| 57 'hello': 'world' | 61 'hello': 'world' |
| 58 }, settings); | 62 }, settings); |
| 59 this.get(null, stage4.bind(this)); | 63 this.get(null, stage4.bind(this)); |
| 60 } | 64 } |
| 61 function stage4(settings) { | 65 function stage4(settings) { |
| 62 chrome.test.assertEq({ | 66 assertEq({ |
| 63 'foo' : 'bar', | 67 'foo' : 'bar', |
| 64 'baz' : 'qux', | 68 'baz' : 'qux', |
| 65 'hello': 'world' | 69 'hello': 'world' |
| 66 }, settings); | 70 }, settings); |
| 67 this.succeed(); | 71 this.succeed(); |
| 68 } | 72 } |
| 69 test(stage0); | 73 test(stage0); |
| 70 }, | 74 }, |
| 71 | 75 |
| 72 function removeWhenEmpty() { | 76 function removeWhenEmpty() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 87 'hello': 'world' | 91 'hello': 'world' |
| 88 }, stage1.bind(this)); | 92 }, stage1.bind(this)); |
| 89 } | 93 } |
| 90 function stage1() { | 94 function stage1() { |
| 91 this.remove('foo', stage2.bind(this)); | 95 this.remove('foo', stage2.bind(this)); |
| 92 } | 96 } |
| 93 function stage2() { | 97 function stage2() { |
| 94 this.get(null, stage3.bind(this)); | 98 this.get(null, stage3.bind(this)); |
| 95 } | 99 } |
| 96 function stage3(settings) { | 100 function stage3(settings) { |
| 97 chrome.test.assertEq({ | 101 assertEq({ |
| 98 'baz' : 'qux', | 102 'baz' : 'qux', |
| 99 'hello': 'world' | 103 'hello': 'world' |
| 100 }, settings); | 104 }, settings); |
| 101 this.remove(['baz', 'nothing'], stage4.bind(this)); | 105 this.remove(['baz', 'nothing'], stage4.bind(this)); |
| 102 } | 106 } |
| 103 function stage4() { | 107 function stage4() { |
| 104 this.get(null, stage5.bind(this)); | 108 this.get(null, stage5.bind(this)); |
| 105 } | 109 } |
| 106 function stage5(settings) { | 110 function stage5(settings) { |
| 107 chrome.test.assertEq({ | 111 assertEq({ |
| 108 'hello': 'world' | 112 'hello': 'world' |
| 109 }, settings); | 113 }, settings); |
| 110 this.remove('hello', stage6.bind(this)); | 114 this.remove('hello', stage6.bind(this)); |
| 111 } | 115 } |
| 112 function stage6() { | 116 function stage6() { |
| 113 this.get(null, stage7.bind(this)); | 117 this.get(null, stage7.bind(this)); |
| 114 } | 118 } |
| 115 function stage7(settings) { | 119 function stage7(settings) { |
| 116 chrome.test.assertEq({}, settings); | 120 assertEq({}, settings); |
| 117 this.succeed(); | 121 this.succeed(); |
| 118 } | 122 } |
| 119 test(stage0); | 123 test(stage0); |
| 120 }, | 124 }, |
| 121 | 125 |
| 122 function setWhenOverwriting() { | 126 function setWhenOverwriting() { |
| 123 function stage0() { | 127 function stage0() { |
| 124 this.set({ | 128 this.set({ |
| 125 'foo' : 'bar', | 129 'foo' : 'bar', |
| 126 'baz' : 'qux', | 130 'baz' : 'qux', |
| 127 'hello': 'world' | 131 'hello': 'world' |
| 128 }, stage1.bind(this)); | 132 }, stage1.bind(this)); |
| 129 } | 133 } |
| 130 function stage1() { | 134 function stage1() { |
| 131 this.set({ | 135 this.set({ |
| 132 'foo' : 'otherBar', | 136 'foo' : 'otherBar', |
| 133 'baz' : 'otherQux' | 137 'baz' : 'otherQux' |
| 134 }, stage2.bind(this)); | 138 }, stage2.bind(this)); |
| 135 } | 139 } |
| 136 function stage2() { | 140 function stage2() { |
| 137 this.get(null, stage3.bind(this)); | 141 this.get(null, stage3.bind(this)); |
| 138 } | 142 } |
| 139 function stage3(settings) { | 143 function stage3(settings) { |
| 140 chrome.test.assertEq({ | 144 assertEq({ |
| 141 'foo' : 'otherBar', | 145 'foo' : 'otherBar', |
| 142 'baz' : 'otherQux', | 146 'baz' : 'otherQux', |
| 143 'hello': 'world' | 147 'hello': 'world' |
| 144 }, settings); | 148 }, settings); |
| 145 this.set({ | 149 this.set({ |
| 146 'baz' : 'anotherQux', | 150 'baz' : 'anotherQux', |
| 147 'hello': 'otherWorld', | 151 'hello': 'otherWorld', |
| 148 'some' : 'value' | 152 'some' : 'value' |
| 149 }, stage4.bind(this)); | 153 }, stage4.bind(this)); |
| 150 } | 154 } |
| 151 function stage4() { | 155 function stage4() { |
| 152 this.get(null, stage5.bind(this)); | 156 this.get(null, stage5.bind(this)); |
| 153 } | 157 } |
| 154 function stage5(settings) { | 158 function stage5(settings) { |
| 155 chrome.test.assertEq({ | 159 assertEq({ |
| 156 'foo' : 'otherBar', | 160 'foo' : 'otherBar', |
| 157 'baz' : 'anotherQux', | 161 'baz' : 'anotherQux', |
| 158 'hello': 'otherWorld', | 162 'hello': 'otherWorld', |
| 159 'some' : 'value' | 163 'some' : 'value' |
| 160 }, settings); | 164 }, settings); |
| 161 this.succeed(); | 165 this.succeed(); |
| 162 } | 166 } |
| 163 test(stage0); | 167 test(stage0); |
| 164 }, | 168 }, |
| 165 | 169 |
| 166 function clearWhenEmpty() { | 170 function clearWhenEmpty() { |
| 167 function stage0() { | 171 function stage0() { |
| 168 this.clear(stage1.bind(this)); | 172 this.clear(stage1.bind(this)); |
| 169 } | 173 } |
| 170 function stage1() { | 174 function stage1() { |
| 171 this.get(null, stage2.bind(this)); | 175 this.get(null, stage2.bind(this)); |
| 172 } | 176 } |
| 173 function stage2(settings) { | 177 function stage2(settings) { |
| 174 chrome.test.assertEq({}, settings); | 178 assertEq({}, settings); |
| 175 this.succeed(); | 179 this.succeed(); |
| 176 } | 180 } |
| 177 test(stage0); | 181 test(stage0); |
| 178 }, | 182 }, |
| 179 | 183 |
| 180 function clearWhenNonempty() { | 184 function clearWhenNonempty() { |
| 181 function stage0() { | 185 function stage0() { |
| 182 this.set({ | 186 this.set({ |
| 183 'foo' : 'bar', | 187 'foo' : 'bar', |
| 184 'baz' : 'qux', | 188 'baz' : 'qux', |
| 185 'hello': 'world' | 189 'hello': 'world' |
| 186 }, stage1.bind(this)); | 190 }, stage1.bind(this)); |
| 187 } | 191 } |
| 188 function stage1() { | 192 function stage1() { |
| 189 this.clear(stage2.bind(this)); | 193 this.clear(stage2.bind(this)); |
| 190 } | 194 } |
| 191 function stage2() { | 195 function stage2() { |
| 192 this.get(null, stage3.bind(this)); | 196 this.get(null, stage3.bind(this)); |
| 193 } | 197 } |
| 194 function stage3(settings) { | 198 function stage3(settings) { |
| 195 chrome.test.assertEq({}, settings); | 199 assertEq({}, settings); |
| 196 this.succeed(); | 200 this.succeed(); |
| 197 } | 201 } |
| 198 test(stage0); | 202 test(stage0); |
| 199 }, | 203 }, |
| 200 | 204 |
| 201 function keysWithDots() { | 205 function keysWithDots() { |
| 202 function stage0() { | 206 function stage0() { |
| 203 this.set({ | 207 this.set({ |
| 204 'foo.bar' : 'baz', | 208 'foo.bar' : 'baz', |
| 205 'one' : {'two': 'three'} | 209 'one' : {'two': 'three'} |
| 206 }, stage1.bind(this)); | 210 }, stage1.bind(this)); |
| 207 } | 211 } |
| 208 function stage1() { | 212 function stage1() { |
| 209 this.get(['foo.bar', 'one'], stage2.bind(this)); | 213 this.get(['foo.bar', 'one'], stage2.bind(this)); |
| 210 } | 214 } |
| 211 function stage2(settings) { | 215 function stage2(settings) { |
| 212 chrome.test.assertEq({ | 216 assertEq({ |
| 213 'foo.bar' : 'baz', | 217 'foo.bar' : 'baz', |
| 214 'one' : {'two': 'three'} | 218 'one' : {'two': 'three'} |
| 215 }, settings); | 219 }, settings); |
| 216 this.get('one.two', stage3.bind(this)); | 220 this.get('one.two', stage3.bind(this)); |
| 217 } | 221 } |
| 218 function stage3(settings) { | 222 function stage3(settings) { |
| 219 chrome.test.assertEq({}, settings); | 223 assertEq({}, settings); |
| 220 this.remove(['foo.bar', 'one.two'], stage4.bind(this)); | 224 this.remove(['foo.bar', 'one.two'], stage4.bind(this)); |
| 221 } | 225 } |
| 222 function stage4() { | 226 function stage4() { |
| 223 this.get(null, stage5.bind(this)); | 227 this.get(null, stage5.bind(this)); |
| 224 } | 228 } |
| 225 function stage5(settings) { | 229 function stage5(settings) { |
| 226 chrome.test.assertEq({ | 230 assertEq({ |
| 227 'one' : {'two': 'three'} | 231 'one' : {'two': 'three'} |
| 228 }, settings); | 232 }, settings); |
| 229 this.succeed(); | 233 this.succeed(); |
| 230 } | 234 } |
| 231 test(stage0); | 235 test(stage0); |
| 232 }, | 236 }, |
| 233 | 237 |
| 234 function getWithDefaultValues() { | 238 function getWithDefaultValues() { |
| 235 function stage0() { | 239 function stage0() { |
| 236 this.get({ | 240 this.get({ |
| 237 'foo': 'defaultBar', | 241 'foo': 'defaultBar', |
| 238 'baz': [1, 2, 3] | 242 'baz': [1, 2, 3] |
| 239 }, stage1.bind(this)); | 243 }, stage1.bind(this)); |
| 240 } | 244 } |
| 241 function stage1(settings) { | 245 function stage1(settings) { |
| 242 chrome.test.assertEq({ | 246 assertEq({ |
| 243 'foo': 'defaultBar', | 247 'foo': 'defaultBar', |
| 244 'baz': [1, 2, 3] | 248 'baz': [1, 2, 3] |
| 245 }, settings); | 249 }, settings); |
| 246 this.get(null, stage2.bind(this)); | 250 this.get(null, stage2.bind(this)); |
| 247 } | 251 } |
| 248 function stage2(settings) { | 252 function stage2(settings) { |
| 249 chrome.test.assertEq({}, settings); | 253 assertEq({}, settings); |
| 250 this.set({'foo': 'bar'}, stage3.bind(this)); | 254 this.set({'foo': 'bar'}, stage3.bind(this)); |
| 251 } | 255 } |
| 252 function stage3() { | 256 function stage3() { |
| 253 this.get({ | 257 this.get({ |
| 254 'foo': 'defaultBar', | 258 'foo': 'defaultBar', |
| 255 'baz': [1, 2, 3] | 259 'baz': [1, 2, 3] |
| 256 }, stage4.bind(this)); | 260 }, stage4.bind(this)); |
| 257 } | 261 } |
| 258 function stage4(settings) { | 262 function stage4(settings) { |
| 259 chrome.test.assertEq({ | 263 assertEq({ |
| 260 'foo': 'bar', | 264 'foo': 'bar', |
| 261 'baz': [1, 2, 3] | 265 'baz': [1, 2, 3] |
| 262 }, settings); | 266 }, settings); |
| 263 this.set({'baz': {}}, stage5.bind(this)); | 267 this.set({'baz': {}}, stage5.bind(this)); |
| 264 } | 268 } |
| 265 function stage5() { | 269 function stage5() { |
| 266 this.get({ | 270 this.get({ |
| 267 'foo': 'defaultBar', | 271 'foo': 'defaultBar', |
| 268 'baz': [1, 2, 3] | 272 'baz': [1, 2, 3] |
| 269 }, stage6.bind(this)); | 273 }, stage6.bind(this)); |
| 270 } | 274 } |
| 271 function stage6(settings) { | 275 function stage6(settings) { |
| 272 chrome.test.assertEq({ | 276 assertEq({ |
| 273 'foo': 'bar', | 277 'foo': 'bar', |
| 274 'baz': {} | 278 'baz': {} |
| 275 }, settings); | 279 }, settings); |
| 276 this.remove('foo', stage7.bind(this)); | 280 this.remove('foo', stage7.bind(this)); |
| 277 } | 281 } |
| 278 function stage7() { | 282 function stage7() { |
| 279 this.get({ | 283 this.get({ |
| 280 'foo': 'defaultBar', | 284 'foo': 'defaultBar', |
| 281 'baz': [1, 2, 3] | 285 'baz': [1, 2, 3] |
| 282 }, stage8.bind(this)); | 286 }, stage8.bind(this)); |
| 283 } | 287 } |
| 284 function stage8(settings) { | 288 function stage8(settings) { |
| 285 chrome.test.assertEq({ | 289 assertEq({ |
| 286 'foo': 'defaultBar', | 290 'foo': 'defaultBar', |
| 287 'baz': {} | 291 'baz': {} |
| 288 }, settings); | 292 }, settings); |
| 289 this.succeed(); | 293 this.succeed(); |
| 290 } | 294 } |
| 291 test(stage0); | 295 test(stage0); |
| 292 }, | 296 }, |
| 293 | 297 |
| 294 | 298 |
| 295 function quota() { | 299 function quota() { |
| 296 // Just check that the constants are defined; no need to be forced to | 300 // Just check that the constants are defined; no need to be forced to |
| 297 // update them here as well if/when they change. | 301 // update them here as well if/when they change. |
| 298 chrome.test.assertTrue(chrome.storage.sync.QUOTA_BYTES > 0); | 302 assertTrue(chrome.storage.sync.QUOTA_BYTES > 0); |
| 299 chrome.test.assertTrue(chrome.storage.sync.QUOTA_BYTES_PER_ITEM > 0); | 303 assertTrue(chrome.storage.sync.QUOTA_BYTES_PER_ITEM > 0); |
| 300 chrome.test.assertTrue(chrome.storage.sync.MAX_ITEMS > 0); | 304 assertTrue(chrome.storage.sync.MAX_ITEMS > 0); |
| 301 | 305 |
| 302 chrome.test.assertTrue(chrome.storage.local.QUOTA_BYTES > 0); | 306 assertTrue(chrome.storage.local.QUOTA_BYTES > 0); |
| 303 chrome.test.assertEq('undefined', | 307 assertEq('undefined', |
| 304 typeof chrome.storage.local.QUOTA_BYTES_PER_ITEM); | 308 typeof chrome.storage.local.QUOTA_BYTES_PER_ITEM); |
| 305 chrome.test.assertEq('undefined', | 309 assertEq('undefined', |
| 306 typeof chrome.storage.local.MAX_ITEMS); | 310 typeof chrome.storage.local.MAX_ITEMS); |
| 307 | 311 |
| 308 var area = chrome.storage.sync; | 312 var area = chrome.storage.sync; |
| 309 function stage0() { | 313 function stage0() { |
| 310 area.getBytesInUse(null, stage1); | 314 area.getBytesInUse(null, stage1); |
| 311 } | 315 } |
| 312 function stage1(bytesInUse) { | 316 function stage1(bytesInUse) { |
| 313 chrome.test.assertEq(0, bytesInUse); | 317 assertEq(0, bytesInUse); |
| 314 area.set({ a: 42, b: 43, c: 44 }, stage2); | 318 area.set({ a: 42, b: 43, c: 44 }, stage2); |
| 315 } | 319 } |
| 316 function stage2() { | 320 function stage2() { |
| 317 area.getBytesInUse(null, stage3); | 321 area.getBytesInUse(null, stage3); |
| 318 } | 322 } |
| 319 function stage3(bytesInUse) { | 323 function stage3(bytesInUse) { |
| 320 chrome.test.assertEq(9, bytesInUse); | 324 assertEq(9, bytesInUse); |
| 321 area.getBytesInUse('a', stage4); | 325 area.getBytesInUse('a', stage4); |
| 322 } | 326 } |
| 323 function stage4(bytesInUse) { | 327 function stage4(bytesInUse) { |
| 324 chrome.test.assertEq(3, bytesInUse); | 328 assertEq(3, bytesInUse); |
| 325 area.getBytesInUse(['a', 'b'], stage5); | 329 area.getBytesInUse(['a', 'b'], stage5); |
| 326 } | 330 } |
| 327 function stage5(bytesInUse) { | 331 function stage5(bytesInUse) { |
| 328 chrome.test.assertEq(6, bytesInUse); | 332 assertEq(6, bytesInUse); |
| 329 chrome.test.succeed(); | 333 succeed(); |
| 330 } | 334 } |
| 331 area.clear(stage0); | 335 area.clear(stage0); |
| 332 }, | 336 }, |
| 337 |
| 338 function nullsInArgs() { |
| 339 var area = chrome.storage.local; |
| 340 function stage0() { |
| 341 area.get({ |
| 342 foo: 'foo', |
| 343 bar: null, |
| 344 baz: undefined |
| 345 }, stage1); |
| 346 } |
| 347 function stage1(values) { |
| 348 assertEq({ |
| 349 foo: 'foo', |
| 350 bar: null, |
| 351 baz: null |
| 352 }, values); |
| 353 area.set({ |
| 354 foo: 'foo', |
| 355 bar: null, |
| 356 baz: undefined |
| 357 }, area.get.bind(area, stage2)); |
| 358 } |
| 359 function stage2(values) { |
| 360 assertEq({ |
| 361 foo: 'foo', |
| 362 bar: null, |
| 363 baz: null |
| 364 }, values); |
| 365 succeed(); |
| 366 } |
| 367 area.clear(stage0); |
| 368 }, |
| 333 | 369 |
| 334 // NOTE: throttling test must come last, since each test runs with a single | 370 // NOTE: throttling test must come last, since each test runs with a single |
| 335 // quota. | 371 // quota. |
| 336 function throttling() { | 372 function throttling() { |
| 337 // Test script is as so: | 373 // Test script is as so: |
| 338 // 1 - storage.local shouldn't be exceeded. | 374 // 1 - storage.local shouldn't be exceeded. |
| 339 // 2 - storage.sync should be exceeded. | 375 // 2 - storage.sync should be exceeded. |
| 340 // 3 - storage.local still shouldn't be exceeded. | 376 // 3 - storage.local still shouldn't be exceeded. |
| 341 // 4 - storage.sync should still be exceeded. | 377 // 4 - storage.sync should still be exceeded. |
| 342 // | 378 // |
| (...skipping 15 matching lines...) Expand all Loading... |
| 358 var test = chrome.test; | 394 var test = chrome.test; |
| 359 var quotaError = "This request exceeds available quota."; | 395 var quotaError = "This request exceeds available quota."; |
| 360 | 396 |
| 361 clearNTimes(local, 1001, test.callbackPass(function() { | 397 clearNTimes(local, 1001, test.callbackPass(function() { |
| 362 clearNTimes(sync, 1001, test.callbackFail(quotaError, function() { | 398 clearNTimes(sync, 1001, test.callbackFail(quotaError, function() { |
| 363 clearNTimes(local, 1, test.callbackPass(function() { | 399 clearNTimes(local, 1, test.callbackPass(function() { |
| 364 clearNTimes(sync, 1, test.callbackFail(quotaError, test.succeed)); | 400 clearNTimes(sync, 1, test.callbackFail(quotaError, test.succeed)); |
| 365 })); | 401 })); |
| 366 })); | 402 })); |
| 367 })); | 403 })); |
| 368 } | 404 }, |
| 369 ]); | 405 ]); |
| OLD | NEW |