1 <!DOCTYPE html> 2 <html> 3 <!-- 4 Copyright (c) 2012 The Chromium Authors. All rights reserved. 5 Use of this source code is governed by a BSD-style license that can be 6 found in the LICENSE file. 7 --> 8 <head> 9 <title>Settings tests</title> 10 <script src="base.js"></script> 11 </head> 12 <body> 13 <script> 14 'use strict'; 15 16 base.require('unittest'); 17 base.require('test_utils'); 18 base.require('settings'); 19 20 function testSetGetString() { 21 var settings = new base.Settings(); 22 settings.set('my_key', 'my_val'); 23 assertEquals('my_val', settings.get('my_key')); 24 } 25 26 function testSetGetNumber() { 27 var settings = new base.Settings(); 28 settings.set('my_key', 5); 29 // Numbers are converted to strings. 30 assertEquals('5', settings.get('my_key')); 31 } 32 33 function testSetGetBool() { 34 var settings = new base.Settings(); 35 settings.set('my_key', false); 36 // Booleans are converted to strings. 37 assertEquals('false', settings.get('my_key')); 38 } 39 40 function testSetGetObject() { 41 var settings = new base.Settings(); 42 settings.set('my_key', {'hello': 5}); 43 // Objects are converted to strings. 44 assertEquals('[object Object]', settings.get('my_key')); 45 } 46 47 function testGetUnset() { 48 var settings = new base.Settings(); 49 // Undefined should be returned if value isn't set. 50 assertUndefined(settings.get('my_key')); 51 } 52 53 function testGetDefault() { 54 var settings = new base.Settings(); 55 // default_val should be returned if value isn't set. 56 assertEquals('default_val', settings.get('my_key', 'default_val')); 57 } 58 59 function testSetGetPrefix() { 60 var settings = new base.Settings(); 61 settings.set('key_a', 'foo', 'my_prefix'); 62 assertEquals('foo', settings.get('key_a', undefined, 'my_prefix')); 63 assertEquals('foo', settings.get('key_a', 'bar', 'my_prefix')); 64 assertEquals(undefined, settings.get('key_a')); 65 assertEquals('bar', settings.get('key_a', 'bar')); 66 } 67 68 function testKeys() { 69 var settings = new base.Settings(); 70 settings.set('key_a', 'foo'); 71 settings.set('key_b', 'bar'); 72 settings.set('key_c', 'baz'); 73 assertArrayEquals(['key_a', 'key_b', 'key_c'], settings.keys()); 74 } 75 76 function testKeysPrefix() { 77 var settings = new base.Settings(); 78 settings.set('key_a', 'foo', 'prefix1'); 79 settings.set('key_b', 'bar', 'prefix1'); 80 settings.set('key_c', 'baz', 'prefix1'); 81 settings.set('key_a', 'foo', 'prefix2'); 82 settings.set('key_b', 'bar', 'prefix2'); 83 settings.set('key_C', 'baz', 'prefix2'); 84 assertArrayEquals(['key_a', 'key_b', 'key_c'], settings.keys('prefix1')); 85 assertArrayEquals(['key_C', 'key_a', 'key_b'], settings.keys('prefix2')); 86 assertArrayEquals(['prefix1.key_a', 'prefix1.key_b', 'prefix1.key_c', 87 'prefix2.key_C', 'prefix2.key_a', 'prefix2.key_b'], 88 settings.keys()); 89 } 90 91 </script> 92 </body> 93 </html> 94