<commit_before>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

chromeSpec(function(runningInBackground) {
  describe('chrome.storage', function() {
    it('should contain definitions', function() {
      expect(chrome.storage).toBeDefined();
      expect(chrome.storage.local).toBeDefined();
      expect(chrome.storage.sync).toBeDefined();
      expect(chrome.storage.onChanged).toBeDefined();

      [chrome.storage.local, chrome.storage.sync].forEach(function(store) {
        expect(store.get).toBeDefined();
        expect(store.getBytesInUse).toBeDefined();
        expect(store.set).toBeDefined();
        expect(store.remove).toBeDefined();
        expect(store.clear).toBeDefined();
      });
    });

    it('should have properties', function() {
      expect(chrome.storage.sync.QUOTA_BYTES).toEqual(102400);
      expect(chrome.storage.sync.QUOTA_BYTES_PER_ITEM).toEqual(4096);
      expect(chrome.storage.sync.MAX_ITEMS).toEqual(512);
      expect(chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR).toEqual(1000);
      expect(chrome.storage.sync.MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE).toEqual(10);

      expect(chrome.storage.local.QUOTA_BYTES).toEqual(5242880);
    });

    it('onChanged() Event', function() {
      // TODO
    });

    function test_storagearea(type, storagearea) {
      var obj = {
        'a': 1,
        'b': 2.345,
        'c': 'test',
        'd': [1,2,3],
        'e': {'a':1,'b':'2','c':3.456}
      };

      describe('chrome.storage.' + type, function() {
        describe('testing set', function() {
          beforeEach(function() {
            storagearea.clear();
          });

          Object.keys(obj).forEach(function(key) {
            it('set(object) with single value of type: ' + typeof obj[key], function() {
              var temp = {};
              temp[key] = obj[key];
              storagearea.set(temp);
              storagearea.get(key, waitUntilCalled(function(items) {
                expect(items[key]).toEqual(obj[key]);

                storagearea.get(waitUntilCalled(function(items) {
                  expect(Object.keys(items).length).toEqual(1);
                }));
              }));
            });
          });

          it('set(object) with multiple values', function() {
            storagearea.set(obj);
            storagearea.get(Object.keys(obj), waitUntilCalled(function(items) {
              expect(items).toEqual(obj);

              storagearea.get(waitUntilCalled(function(items) {
                expect(Object.keys(items).length).toBeGreaterThan(0);
              }));
            }));
          });
        });

        describe('testing get', function() {
          beforeEach(function() {
            storagearea.clear();
            storagearea.set(obj);
          });

          it('get() should return all items', function() {
            storagearea.get(waitUntilCalled(function(items) {
              expect(items).toEqual(obj);
            }));
          });

          it('get(null) should return all items', function() {
            storagearea.get(null, waitUntilCalled(function(items) {
              expect(items).toEqual(obj);
            }));
          });

          it('get(string) should return item with that key', function() {
            storagearea.get('a', waitUntilCalled(function(items) {
              expect(items).toEqual({'a': obj.a});
            }));
          });

          it('get(object) should return all items with those keys, or items with default values provided', function() {
            var request = {
              'a': 'should ignore',
              'b': 'also ignore',
              'z': 'should not ignore'
            };
            var expected = {
              'a': obj.a,
              'b': obj.b,
              'z': request.z
            };
            storagearea.get(request, waitUntilCalled(function(items) {
              expect(items).toEqual(expected);
            }));
          });

          it('get([string, ...]) should return items with those keys, ignoring keys that arent found', function() {
            storagearea.get(['a','b','c','d','e','x','y','z'], waitUntilCalled(function(items) {
              expect(items).toEqual(obj);
            }));
            storagearea.get(['a','b','c'], waitUntilCalled(function(items) {
              expect(items).toNotEqual(obj);
            }));
            storagearea.get([], waitUntilCalled(function(items) {
              expect(items).toEqual({});
            }));
          });
        });

        describe('testing clear', function() {
          beforeEach(function() {
            storagearea.clear();
          });

          it('should delete all items', function() {
            storagearea.set(obj);
            storagearea.get(waitUntilCalled(function(items) {
              expect(Object.keys(items).length).toEqual(Object.keys(obj).length);

              storagearea.clear();
              storagearea.get(waitUntilCalled(function(items) {
                expect(Object.keys(items).length).toEqual(0);
              }));
            }));
          });
        });

        describe('testing remove', function() {
          beforeEach(function() {
            storagearea.clear();
            storagearea.set(obj);
          });

          it('remove(string) should remove item with key', function() {
            storagearea.remove('a');
            var expected = {
              'b': obj.b,
              'c': obj.c,
              'd': obj.d,
              'e': obj.e
            };
            storagearea.get(waitUntilCalled(function(items) {
              expect(items).not.toEqual(obj);
              expect(items).toEqual(expected);
            }));
          });

          it('remove(string) with invalid key should be ignored', function() {
            storagearea.remove('z');
            storagearea.get(waitUntilCalled(function(items) {
              expect(items).toEqual(obj);
            }));
          });

          it('remove([string, ...]) should remove item with key, ignoring invalid keys', function() {
            storagearea.remove(['a','b','z']);
            var expected = {
              'c': obj.c,
              'd': obj.d,
              'e': obj.e
            };
            storagearea.get(waitUntilCalled(function(items) {
              expect(items).toEqual(expected);
            }));
          });
        });<commit_msg>Add tests to chrome.storage for special-case values.<commit_after>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

chromeSpec(function(runningInBackground) {
  describe('chrome.storage', function() {
    it('should contain definitions', function() {
      expect(chrome.storage).toBeDefined();
      expect(chrome.storage.local).toBeDefined();
      expect(chrome.storage.sync).toBeDefined();
      expect(chrome.storage.onChanged).toBeDefined();

      [chrome.storage.local, chrome.storage.sync].forEach(function(store) {
        expect(store.get).toBeDefined();
        expect(store.getBytesInUse).toBeDefined();
        expect(store.set).toBeDefined();
        expect(store.remove).toBeDefined();
        expect(store.clear).toBeDefined();
      });
    });

    it('should have properties', function() {
      expect(chrome.storage.sync.QUOTA_BYTES).toEqual(102400);
      expect(chrome.storage.sync.QUOTA_BYTES_PER_ITEM).toEqual(4096);
      expect(chrome.storage.sync.MAX_ITEMS).toEqual(512);
      expect(chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR).toEqual(1000);
      expect(chrome.storage.sync.MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE).toEqual(10);

      expect(chrome.storage.local.QUOTA_BYTES).toEqual(5242880);
    });

    it('onChanged() Event', function() {
      // TODO
    });

    function test_storagearea(type, storagearea) {
      var obj = {
        'int': 1,
        'double': 2.345,
        'string': 'test',
        'String': Object('test'),
        'Array': [1,2,3],
        'object': {'a':1,'b':'2','c':3.456},
        'object_with_toJSON': {'a':1, 'toJSON': function() { return 'foo'; }},
        'Window': window,
        'RegExp': /hello/,
        'date': new Date(1000),
        'undefined': undefined, // This gets ignored by .set().
        'null': null,
        'function': function hello() { return 1; },
        'DivElement': document.createElement('div'),
        'Document': document
      };

      var expected = {
        'int': 1,
        'double': 2.345,
        'string': 'test',
        'String': {'0': 't', '1': 'e', '2': 's', '3': 't'},
        'Array': [1,2,3],
        'object': {'a':1,'b':'2','c':3.456},
        'object_with_toJSON': {'a':1, 'toJSON': {}},
        'Window': {},
        'RegExp': {},
        'date': {},
        'null': null,
        'function': {},
        'DivElement': {},
        'Document': {}
      };

      describe('chrome.storage.' + type, function() {
        describe('testing set', function() {
          beforeEach(function() {
            storagearea.clear();
          });

          Object.keys(obj).forEach(function(key) {
            it('set(object) with single value of type: ' + key, function() {
              var temp = {};
              temp[key] = obj[key];
              storagearea.set(temp);
              storagearea.get(key, waitUntilCalled(function(items) {
                expect(items[key]).toEqual(expected[key]);

                storagearea.get(waitUntilCalled(function(items) {
                  expect(Object.keys(items).length).toEqual(+(key in expected));
                }));
              }));
            });
          });

          it('set(object) with multiple values', function() {
            storagearea.set(obj);
            storagearea.get(Object.keys(obj), waitUntilCalled(function(items) {
              expect(items).toEqual(expected);

              storagearea.get(waitUntilCalled(function(items) {
                expect(items).toEqual(expected);
              }));
            }));
          });
        });

        describe('testing get', function() {
          beforeEach(function() {
            storagearea.clear();
            storagearea.set(obj);
          });

          it('get() should return all items', function() {
            storagearea.get(waitUntilCalled(function(items) {
              expect(items).toEqual(expected);
            }));
          });

          it('get(null) should return all items', function() {
            storagearea.get(null, waitUntilCalled(function(items) {
              expect(items).toEqual(expected);
            }));
          });

          it('get(string) should return item with that key', function() {
            storagearea.get('int', waitUntilCalled(function(items) {
              expect(items).toEqual({'int': expected.int});
            }));
          });

          it('get(object) should return all items with those keys, or items with default values provided', function() {
            var request = {
              'int': 'should ignore',
              'double': 'also ignore',
              'z': 'should not ignore'
            };
            var answer = {
              'int': expected.int,
              'double': expected.double,
              'z': request.z
            };
            storagearea.get(request, waitUntilCalled(function(items) {
              expect(items).toEqual(answer);
            }));
          });

          it('get([string, ...]) should return items with those keys, ignoring keys that arent found', function() {
            var answer = {
              'int': expected.int,
              'double': expected.double
            };
            storagearea.get(['int','double','x','y','z'], waitUntilCalled(function(items) {
              expect(items).toEqual(answer);
            }));
            storagearea.get([], waitUntilCalled(function(items) {
              expect(items).toEqual({});
            }));
          });
        });

        describe('testing clear', function() {
          beforeEach(function() {
            storagearea.clear();
          });

          it('should delete all items', function() {
            storagearea.set(obj);
            storagearea.get(waitUntilCalled(function(items) {
              expect(Object.keys(items).length).toEqual(Object.keys(expected).length);

              storagearea.clear();
              storagearea.get(waitUntilCalled(function(items) {
                expect(Object.keys(items).length).toEqual(0);
              }));
            }));
          });
        });

        describe('testing remove', function() {
          beforeEach(function() {
            storagearea.clear();
            storagearea.set(obj);
          });

          it('remove(string) should remove item with key', function() {
            storagearea.remove('int');
            storagearea.get(waitUntilCalled(function(items) {
              expect(items.int).toBeUndefined();
              expect(Object.keys(items).length).toEqual(Object.keys(expected).length - 1);
            }));
          });

          it('remove(string) with invalid key should be ignored', function() {
            storagearea.remove('z');
            storagearea.get(waitUntilCalled(function(items) {
              expect(items).toEqual(expected);
            }));
          });

          it('remove([string, ...]) should remove item with key, ignoring invalid keys', function() {
            storagearea.remove(['int','string','z']);
            storagearea.get(waitUntilCalled(function(items) {
              expect(items.int).toBeUndefined();
              expect(items.string).toBeUndefined();
              expect(Object.keys(items).length).toEqual(Object.keys(expected).length - 2);
            }));
          });
        });
{'fraction_non_alphanumeric': 0.15449902898654966, 'fraction_numerical': 0.008559303747392649, 'mean_word_length': 1.9333333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7458182', 'n_tokens_mistral': 3758, 'n_tokens_neox': 3718, 'n_words': 631}