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