Html程序  |  94行  |  2.69 KB

<!DOCTYPE html>
<html>
<!--
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.
-->
<head>
<title>Settings tests</title>
<script src="base.js"></script>
</head>
<body>
<script>
'use strict';

base.require('unittest');
base.require('test_utils');
base.require('settings');

function testSetGetString() {
  var settings = new base.Settings();
  settings.set('my_key', 'my_val');
  assertEquals('my_val', settings.get('my_key'));
}

function testSetGetNumber() {
  var settings = new base.Settings();
  settings.set('my_key', 5);
  // Numbers are converted to strings.
  assertEquals('5', settings.get('my_key'));
}

function testSetGetBool() {
  var settings = new base.Settings();
  settings.set('my_key', false);
  // Booleans are converted to strings.
  assertEquals('false', settings.get('my_key'));
}

function testSetGetObject() {
  var settings = new base.Settings();
  settings.set('my_key', {'hello': 5});
  // Objects are converted to strings.
  assertEquals('[object Object]', settings.get('my_key'));
}

function testGetUnset() {
  var settings = new base.Settings();
  // Undefined should be returned if value isn't set.
  assertUndefined(settings.get('my_key'));
}

function testGetDefault() {
  var settings = new base.Settings();
  // default_val should be returned if value isn't set.
  assertEquals('default_val', settings.get('my_key', 'default_val'));
}

function testSetGetPrefix() {
  var settings = new base.Settings();
  settings.set('key_a', 'foo', 'my_prefix');
  assertEquals('foo', settings.get('key_a', undefined, 'my_prefix'));
  assertEquals('foo', settings.get('key_a', 'bar', 'my_prefix'));
  assertEquals(undefined, settings.get('key_a'));
  assertEquals('bar', settings.get('key_a', 'bar'));
}

function testKeys() {
  var settings = new base.Settings();
  settings.set('key_a', 'foo');
  settings.set('key_b', 'bar');
  settings.set('key_c', 'baz');
  assertArrayEquals(['key_a', 'key_b', 'key_c'], settings.keys());
}

function testKeysPrefix() {
  var settings = new base.Settings();
  settings.set('key_a', 'foo', 'prefix1');
  settings.set('key_b', 'bar', 'prefix1');
  settings.set('key_c', 'baz', 'prefix1');
  settings.set('key_a', 'foo', 'prefix2');
  settings.set('key_b', 'bar', 'prefix2');
  settings.set('key_C', 'baz', 'prefix2');
  assertArrayEquals(['key_a', 'key_b', 'key_c'], settings.keys('prefix1'));
  assertArrayEquals(['key_C', 'key_a', 'key_b'], settings.keys('prefix2'));
  assertArrayEquals(['prefix1.key_a', 'prefix1.key_b', 'prefix1.key_c',
                     'prefix2.key_C', 'prefix2.key_a', 'prefix2.key_b'],
                    settings.keys());
}

</script>
</body>
</html>