Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright (c) 2015 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 | |
| 8 | <link rel="import" href="/core/test_utils.html"> |
| 9 | <link rel="import" href="/core/trace_model/attribute.html"> |
| 10 | |
| 11 | <script> |
| 12 | 'use strict'; |
| 13 | |
| 14 | tv.b.unittest.testSuite(function() { |
| 15 | var Attribute = tv.c.trace_model.Attribute; |
| 16 | var ScalarAttribute = tv.c.trace_model.ScalarAttribute; |
| 17 | var StringAttribute = tv.c.trace_model.StringAttribute; |
| 18 | var UnknownAttribute = tv.c.trace_model.UnknownAttribute; |
| 19 | |
| 20 | test('findCommonTraits', function() { |
| 21 | // Empty list. |
| 22 | var traits = Attribute.findCommonTraits([]); |
| 23 | assert.isUndefined(traits); |
| 24 | |
| 25 | // List containing no defined attribute values. |
| 26 | var traits = Attribute.findCommonTraits([undefined, undefined]); |
| 27 | assert.isUndefined(traits); |
| 28 | |
| 29 | // Singleton list. |
| 30 | var traits = Attribute.findCommonTraits([ |
| 31 | new ScalarAttribute('ms', 24) |
| 32 | ]); |
| 33 | assert.strictEqual(traits.constructor, ScalarAttribute); |
| 34 | assert.strictEqual(traits.units, 'ms'); |
| 35 | |
| 36 | // Long list. |
| 37 | var traits = Attribute.findCommonTraits([ |
| 38 | undefined, |
| 39 | new ScalarAttribute('km', 15), |
| 40 | new ScalarAttribute('km', 16), |
| 41 | undefined, |
| 42 | new ScalarAttribute('km', 17), |
| 43 | undefined |
| 44 | ]); |
| 45 | assert.strictEqual(traits.constructor, ScalarAttribute); |
| 46 | assert.strictEqual(traits.units, 'km'); |
| 47 | |
| 48 | // List containing attribute values of different types. |
| 49 | var traits = Attribute.findCommonTraits([ |
| 50 | new ScalarAttribute('km/h', 15), |
| 51 | undefined, |
| 52 | new StringAttribute('km/h', 'speed-of-light') |
| 53 | ]); |
| 54 | assert.strictEqual(traits.constructor, UnknownAttribute); |
| 55 | assert.isUndefined(traits.units); |
| 56 | |
| 57 | // List containing attribute values with different units. |
| 58 | var traits = Attribute.findCommonTraits([ |
| 59 | new ScalarAttribute('m', 10), |
| 60 | new ScalarAttribute('ft', 10) |
| 61 | ]); |
| 62 | assert.strictEqual(traits.constructor, UnknownAttribute); |
| 63 | assert.isUndefined(traits.units); |
| 64 | }); |
| 65 | |
| 66 | test('aggregate', function() { |
| 67 | // No parent or children. |
| 68 | var aggregatedAttr = Attribute.aggregate([], undefined); |
| 69 | assert.isUndefined(aggregatedAttr); |
| 70 | |
| 71 | // No parent, children with a single type. |
| 72 | var aggregatedAttr = Attribute.aggregate([ |
| 73 | undefined, |
| 74 | new ScalarAttribute('bytes', 128), |
| 75 | undefined, |
| 76 | new ScalarAttribute('bytes', 64), |
| 77 | undefined |
| 78 | ], undefined); |
| 79 | assert.isDefined(aggregatedAttr); |
| 80 | assert.equal(aggregatedAttr.value, 192); |
| 81 | assert.equal(aggregatedAttr.units, 'bytes'); |
| 82 | assert.instanceOf(aggregatedAttr, ScalarAttribute); |
| 83 | |
| 84 | // No parent, children with multiple types. |
| 85 | var aggregatedAttr = Attribute.aggregate([ |
| 86 | new StringAttribute('Hz', 128), |
| 87 | new ScalarAttribute('Hz', 64) |
| 88 | ], undefined); |
| 89 | assert.isUndefined(aggregatedAttr); |
| 90 | |
| 91 | // No parent, children with multiple units. |
| 92 | var aggregatedAttr = Attribute.aggregate([ |
| 93 | new ScalarAttribute('s', 10), |
| 94 | new ScalarAttribute('Hz', 0.1) |
| 95 | ], undefined); |
| 96 | assert.isUndefined(aggregatedAttr); |
| 97 | |
| 98 | // No parent, children which do not support merging. |
| 99 | var aggregatedAttr = Attribute.aggregate([ |
| 100 | new StringAttribute('items', 'a piece of text'), |
| 101 | new StringAttribute('items', 'another piece of text') |
| 102 | ], undefined); |
| 103 | assert.isUndefined(aggregatedAttr); |
| 104 | |
| 105 | // Defined parent, no children. |
| 106 | var aggregatedAttr = Attribute.aggregate([], new ScalarAttribute('C', -12)); |
| 107 | assert.isDefined(aggregatedAttr); |
| 108 | assert.equal(aggregatedAttr.value, -12); |
| 109 | assert.equal(aggregatedAttr.units, 'C'); |
| 110 | assert.instanceOf(aggregatedAttr, ScalarAttribute); |
| 111 | |
| 112 | // Defined parent, children with the same type. |
| 113 | var aggregatedAttr = Attribute.aggregate([ |
| 114 | new ScalarAttribute('W', 110), |
| 115 | new ScalarAttribute('W', 13) |
| 116 | ], new ScalarAttribute('W', -123)); |
| 117 | assert.isDefined(aggregatedAttr); |
| 118 | assert.equal(aggregatedAttr.value, -123); |
| 119 | assert.equal(aggregatedAttr.units, 'W'); |
| 120 | assert.instanceOf(aggregatedAttr, ScalarAttribute); |
| 121 | |
| 122 | // Defined parent, children with a different type. |
| 123 | var aggregatedAttr = Attribute.aggregate([ |
| 124 | new StringAttribute('colors', 640), |
| 125 | new StringAttribute('colors', 640) |
| 126 | ], new ScalarAttribute('colors', -1234)); |
| 127 | assert.isDefined(aggregatedAttr); |
| 128 | assert.equal(aggregatedAttr.value, -1234); |
| 129 | assert.equal(aggregatedAttr.units, 'colors'); |
| 130 | assert.instanceOf(aggregatedAttr, ScalarAttribute); |
| 131 | |
| 132 | // Defined parent, children with multiple types. |
| 133 | var aggregatedAttr = Attribute.aggregate([ |
| 134 | new ScalarAttribute('mm', 999), |
| 135 | new StringAttribute('mm', 640) |
| 136 | ], new ScalarAttribute('mm', -12345)); |
| 137 | assert.isDefined(aggregatedAttr); |
| 138 | assert.equal(aggregatedAttr.value, -12345); |
| 139 | assert.equal(aggregatedAttr.units, 'mm'); |
| 140 | assert.instanceOf(aggregatedAttr, ScalarAttribute); |
| 141 | |
| 142 | // Defined parent, children which do not support merging. |
| 143 | var aggregatedAttr = Attribute.aggregate([ |
| 144 | new StringAttribute('m', 'X'), |
| 145 | new StringAttribute('m', 'Y') |
| 146 | ], new StringAttribute('m', 'Z')); |
| 147 | assert.isDefined(aggregatedAttr); |
| 148 | assert.equal(aggregatedAttr.value, 'Z'); |
| 149 | assert.equal(aggregatedAttr.units, 'm'); |
| 150 | assert.instanceOf(aggregatedAttr, StringAttribute); |
| 151 | }); |
| 152 | |
| 153 | test('useMergedAttribute', function() { |
| 154 | var importWarningCallbackFired; |
| 155 | var model = { |
| 156 | importWarning: function() { |
| 157 | importWarningCallbackFired = true; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | // Same type. |
| 162 | var attr = new ScalarAttribute('C', 42); |
| 163 | importWarningCallbackFired = false; |
| 164 | attr.useMergedAttribute(new ScalarAttribute('C', 24), model); |
| 165 | assert.isFalse(importWarningCallbackFired); |
| 166 | |
| 167 | // Different type. |
| 168 | var attr = new ScalarAttribute('C', 42); |
| 169 | importWarningCallbackFired = false; |
| 170 | attr.useMergedAttribute(new UnknownAttribute('C'), model); |
| 171 | assert.isTrue(importWarningCallbackFired); |
| 172 | |
| 173 | // Different units. |
| 174 | var attr = new ScalarAttribute('C', 42); |
| 175 | importWarningCallbackFired = false; |
| 176 | attr.useMergedAttribute(new ScalarAttribute('F', 75.2), model); |
| 177 | assert.isTrue(importWarningCallbackFired); |
| 178 | }); |
| 179 | |
| 180 | test('scalar_construct', function() { |
| 181 | var attr = new ScalarAttribute('kHz', 1024); |
| 182 | assert.equal(attr.value, 1024); |
| 183 | assert.equal(attr.units, 'kHz'); |
| 184 | }); |
| 185 | |
| 186 | test('scalar_fromDict', function() { |
| 187 | var attr = Attribute.fromDictIfPossible({ |
| 188 | type: 'scalar', |
| 189 | units: 'kHz', |
| 190 | value: '400' |
| 191 | }); |
| 192 | assert.isDefined(attr); |
| 193 | assert.equal(attr.value, 1024); |
| 194 | assert.equal(attr.units, 'kHz'); |
| 195 | assert.instanceOf(attr, ScalarAttribute); |
| 196 | }); |
| 197 | |
| 198 | test('scalar_merge', function() { |
| 199 | var mergedAttr = ScalarAttribute.merge([ |
| 200 | new ScalarAttribute('objects', 10), |
| 201 | new ScalarAttribute('objects', 20), |
| 202 | new ScalarAttribute('objects', -3) |
| 203 | ], 'objects'); |
| 204 | assert.isDefined(mergedAttr); |
| 205 | assert.equal(mergedAttr.value, 27); |
| 206 | assert.equal(mergedAttr.units, 'objects'); |
| 207 | assert.instanceOf(mergedAttr, ScalarAttribute); |
| 208 | }); |
| 209 | |
| 210 | test('string_construct', function() { |
| 211 | var attr = new StringAttribute('C', 'absolute zero'); |
| 212 | assert.equal(attr.value, 'absolute zero'); |
| 213 | assert.equal(attr.units, 'C'); |
| 214 | }); |
| 215 | |
| 216 | test('string_fromDict', function() { |
| 217 | var attr = Attribute.fromDictIfPossible({ |
| 218 | type: 'string', |
| 219 | units: 'm/s', |
| 220 | value: 'almost zero' |
| 221 | }); |
| 222 | assert.isDefined(attr); |
| 223 | assert.equal(attr.value, 'almost zero'); |
| 224 | assert.equal(attr.units, 'm/s'); |
| 225 | assert.instanceOf(attr, StringAttribute); |
| 226 | }); |
| 227 | |
| 228 | test('unknown_construct', function() { |
| 229 | var attr = new UnknownAttribute('ml'); |
| 230 | assert.equal(attr.units, 'ml'); |
| 231 | }); |
| 232 | |
| 233 | test('unknown_fromDict', function() { |
| 234 | // Missing type. |
| 235 | var attr = Attribute.fromDictIfPossible({units: 'F'}); |
| 236 | assert.isDefined(attr); |
| 237 | assert.equal(attr.units, 'F'); |
| 238 | assert.instanceOf(attr, UnknownAttribute); |
| 239 | |
| 240 | // Non-existent type. |
| 241 | var attr = Attribute.fromDictIfPossible({type: 'hashmap'}); |
| 242 | assert.isDefined(attr); |
| 243 | assert.isUndefined(attr.units); |
| 244 | assert.instanceOf(attr, UnknownAttribute); |
| 245 | }); |
| 246 | }); |
| 247 | </script> |