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/ui/base/dom_helpers.html"> 8 <script> 9 'use strict'; 10 11 tr.b.unittest.testSuite(function() { 12 var THIS_DOC = document._currentScript.ownerDocument; 13 14 test('simpleSpanAndDiv', function() { 15 var divEl = tr.ui.b.createDiv({ 16 className: 'a-div-class', parent: document.body 17 }); 18 var testText = 'some span text'; 19 var spanEl = tr.ui.b.createSpan({ 20 className: 'a-span-class', 21 textContent: testText, 22 parent: divEl 23 }); 24 var eltInDocument = document.querySelector('.a-div-class>.a-span-class'); 25 assert.equal(eltInDocument.textContent, testText); 26 eltInDocument.parentElement.removeChild(eltInDocument); 27 }); 28 29 test('createSpan_ownerDocument', function() { 30 var spanEl = tr.ui.b.createSpan({ 31 className: 'a-span-class', 32 bold: true, 33 ownerDocument: THIS_DOC 34 }); 35 assert.strictEqual(spanEl.ownerDocument, THIS_DOC); 36 }); 37 38 test('checkboxFromDefaults', function() { 39 var target = {foo: undefined}; 40 var cb = tr.ui.b.createCheckBox(target, 'foo', 'myCheckBox', false, 'Foo'); 41 assert.isFalse(target.foo); 42 }); 43 44 test('checkboxFromSettings', function() { 45 tr.b.Settings.set('myCheckBox', true); 46 var target = {foo: undefined}; 47 var cb = tr.ui.b.createCheckBox(target, 'foo', 'myCheckBox', false, 'Foo'); 48 assert.isTrue(target.foo); 49 }); 50 51 test('checkboxChanged', function() { 52 var target = {foo: undefined}; 53 var cb = tr.ui.b.createCheckBox(target, 'foo', 'myCheckBox', false, 'Foo'); 54 cb.checked = true; 55 56 assert.isTrue(tr.b.Settings.get('myCheckBox', undefined)); 57 assert.isTrue(target.foo); 58 }); 59 60 test('selectorSettingsAlreaySet', function() { 61 tr.b.Settings.set('myScale', 0.25); 62 63 var target = { 64 scale: 314 65 }; 66 var sel = tr.ui.b.createSelector( 67 target, 'scale', 68 'myScale', 0.375, 69 [{label: '6.25%', value: 0.0625}, 70 {label: '12.5%', value: 0.125}, 71 {label: '25%', value: 0.25}, 72 {label: '37.5%', value: 0.375}, 73 {label: '50%', value: 0.5}, 74 {label: '75%', value: 0.75}, 75 {label: '100%', value: 1}, 76 {label: '200%', value: 2} 77 ]); 78 assert.equal(target.scale, 0.25); 79 assert.equal(sel.selectedIndex, 2); 80 }); 81 82 test('selectorSettingsDefault', function() { 83 var target = { 84 scale: 314 85 }; 86 var sel = tr.ui.b.createSelector( 87 target, 'scale', 88 'myScale', 0.375, 89 [{label: '6.25%', value: 0.0625}, 90 {label: '12.5%', value: 0.125}, 91 {label: '25%', value: 0.25}, 92 {label: '37.5%', value: 0.375}, 93 {label: '50%', value: 0.5}, 94 {label: '75%', value: 0.75}, 95 {label: '100%', value: 1}, 96 {label: '200%', value: 2} 97 ]); 98 assert.equal(target.scale, 0.375); 99 assert.equal(sel.selectedIndex, 3); 100 }); 101 102 test('selectorSettingsChanged', function() { 103 var target = { 104 scale: 314 105 }; 106 var sel = tr.ui.b.createSelector( 107 target, 'scale', 108 'myScale', 0.375, 109 [{label: '6.25%', value: 0.0625}, 110 {label: '12.5%', value: 0.125}, 111 {label: '25%', value: 0.25}, 112 {label: '37.5%', value: 0.375}, 113 {label: '50%', value: 0.5}, 114 {label: '75%', value: 0.75}, 115 {label: '100%', value: 1}, 116 {label: '200%', value: 2} 117 ]); 118 assert.equal(sel.selectedValue, 0.375); 119 sel.selectedValue = 0.75; 120 assert.equal(target.scale, 0.75); 121 assert.equal(sel.selectedValue, 0.75); 122 assert.equal(undefined), 0.75, tr.b.Settings.get('myScale'); 123 }); 124 125 test('asHTMLOrTextNode_string', function() { 126 // Default owner document. 127 var node = tr.ui.b.asHTMLOrTextNode('Hello, World!'); 128 assert.instanceOf(node, Node); 129 assert.equal(node.textContent, 'Hello, World!'); 130 assert.strictEqual(node.ownerDocument, document); 131 132 // Custom owner document. 133 var node = tr.ui.b.asHTMLOrTextNode('Bye, World!', THIS_DOC); 134 assert.instanceOf(node, Node); 135 assert.equal(node.textContent, 'Bye, World!'); 136 assert.strictEqual(node.ownerDocument, THIS_DOC); 137 }); 138 139 test('asHTMLOrTextNode_node', function() { 140 // Node object. Owner document should NOT be modified. 141 var node = document.createTextNode('Hi', THIS_DOC); 142 assert.strictEqual(tr.ui.b.asHTMLOrTextNode(node), node); 143 assert.strictEqual(node.ownerDocument, document); 144 145 // HTMLElement object. Owner document should NOT be modified. 146 var node = THIS_DOC.createElement('div'); 147 assert.strictEqual(tr.ui.b.asHTMLOrTextNode(node), node); 148 assert.strictEqual(node.ownerDocument, THIS_DOC); 149 }); 150 }); 151 </script> 152