1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 'use strict'; 6 7 base.require('base.settings'); 8 base.require('base.unittest'); 9 base.require('tracing.test_utils'); 10 11 base.unittest.testSuite('base.settings', function() { 12 function assertSettingIs(expectedValue, key) { 13 assertEquals(expectedValue, base.Settings.get('my_key'), 14 'Expected Settings.' + key + ' to be ' + expectedValue); 15 } 16 17 // Old settings versions used to stringify objects instead of putting them 18 // into JSON. This test makes sure that these old settings yield the default 19 // value instead of strings. 20 test('oldStyleSettingYieldsDefaultValue', function() { 21 var storage = base.Settings.getAlternativeStorageInstance(); 22 storage.setItem(base.Settings.namespace_('key'), 'hello world'); 23 24 assertEquals('value', base.Settings.get('key', 'value')); 25 }); 26 27 test('setGetString', function() { 28 var settings = new base.Settings(); 29 settings.set('my_key', 'my_val'); 30 assertEquals('my_val', settings.get('my_key')); 31 // base.Settings() is a singleton 32 assertEquals('my_val', base.Settings().get('my_key')); 33 }); 34 35 test('setGetNumber', function() { 36 var settings = new base.Settings(); 37 settings.set('my_key', 5); 38 assertSettingIs(5, 'my_key'); 39 }); 40 41 test('setGetBool', function() { 42 var settings = new base.Settings(); 43 settings.set('my_key', false); 44 assertSettingIs(false, 'my_key'); 45 }); 46 47 test('setGetObject', function() { 48 var settings = new base.Settings(); 49 settings.set('my_key', {'hello': 5}); 50 assertObjectEquals({'hello': 5}, settings.get('my_key')); 51 }); 52 53 test('setInvalidObject', function() { 54 var settings = new base.Settings(); 55 var obj = {'hello': undefined}; 56 obj.hello = obj; 57 assertThrows(function() { 58 settings.set('my_key', obj); 59 }); 60 }); 61 62 test('setUndefined', function() { 63 var settings = new base.Settings(); 64 assertThrows(function() { 65 settings.set('my_key', undefined); 66 }); 67 }); 68 69 test('getUnset', function() { 70 var settings = new base.Settings(); 71 // Undefined should be returned if value isn't set. 72 assertSettingIs(undefined, 'my_key'); 73 }); 74 75 test('getDefault', function() { 76 var settings = new base.Settings(); 77 // default_val should be returned if value isn't set. 78 assertEquals('default_val', settings.get('my_key', 'default_val')); 79 }); 80 81 test('setGetPrefix', function() { 82 var settings = new base.Settings(); 83 settings.set('key_a', 'foo', 'my_prefix'); 84 assertEquals('foo', settings.get('key_a', undefined, 'my_prefix')); 85 assertEquals('foo', settings.get('key_a', 'bar', 'my_prefix')); 86 assertEquals(undefined, settings.get('key_a')); 87 assertEquals('bar', settings.get('key_a', 'bar')); 88 }); 89 90 test('keys', function() { 91 var settings = new base.Settings(); 92 settings.set('key_a', 'foo'); 93 settings.set('key_b', 'bar'); 94 settings.set('key_c', 'baz'); 95 assertArrayEquals(['key_a', 'key_b', 'key_c'], settings.keys()); 96 }); 97 98 test('keysPrefix', function() { 99 var settings = new base.Settings(); 100 settings.set('key_a', 'foo', 'prefix1'); 101 settings.set('key_b', 'bar', 'prefix1'); 102 settings.set('key_c', 'baz', 'prefix1'); 103 settings.set('key_a', 'foo', 'prefix2'); 104 settings.set('key_b', 'bar', 'prefix2'); 105 settings.set('key_C', 'baz', 'prefix2'); 106 assertArrayEquals(['key_a', 'key_b', 'key_c'], settings.keys('prefix1')); 107 assertArrayEquals(['key_C', 'key_a', 'key_b'], settings.keys('prefix2')); 108 assertArrayEquals( 109 ['prefix1.key_a', 'prefix1.key_b', 'prefix1.key_c', 110 'prefix2.key_C', 'prefix2.key_a', 'prefix2.key_b'], 111 settings.keys()); 112 }); 113 }); 114