| <!DOCTYPE html> |
| <!-- |
| Copyright 2015 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. |
| --> |
| |
| <link rel="import" href="/tracing/value/time_display_mode.html"> |
| <link rel="import" href="/tracing/value/unit.html"> |
| |
| <script> |
| 'use strict'; |
| |
| tr.b.unittest.testSuite(function() { |
| var Unit = tr.v.Unit; |
| var ImprovementDirection = tr.v.ImprovementDirection; |
| |
| test('Unit.display-mode-changed', function() { |
| var Unit = tr.v.Unit; |
| var TimeDisplayModes = tr.v.TimeDisplayModes; |
| |
| var listenerWasCalled = false; |
| function listener(e) { |
| listenerWasCalled = true; |
| } |
| |
| try { |
| Unit.currentTimeDisplayMode = TimeDisplayModes.ms; |
| Unit.addEventListener('display-mode-changed', listener); |
| |
| listenerWasCalled = false; |
| Unit.currentTimeDisplayMode = TimeDisplayModes.ns; |
| assert.isTrue(listenerWasCalled); |
| assert.equal(Unit.currentTimeDisplayMode, TimeDisplayModes.ns); |
| } finally { |
| Unit.removeEventListener('display-mode-changed', listener); |
| Unit.reset(); |
| } |
| }); |
| |
| function checkTimeUnit(unit) { |
| try { |
| // Use milliseconds to display time (default behavior). |
| Unit.currentTimeDisplayMode = tr.v.TimeDisplayModes.ms; |
| |
| assert.strictEqual(unit.format(0), '0.000 ms'); |
| assert.strictEqual(unit.format(0.02), '0.020 ms'); |
| assert.strictEqual(unit.format(0.001), '0.001 ms'); |
| assert.strictEqual(unit.format(0.0005), '0.001 ms'); |
| assert.strictEqual(unit.format(0.00049), '0.000 ms'); |
| assert.strictEqual(unit.format(999.999), '999.999 ms'); |
| assert.strictEqual(unit.format(1000.001), '1,000.001 ms'); |
| assert.strictEqual(unit.format(123456789), '123,456,789.000 ms'); |
| assert.strictEqual(unit.format(-0.00051), '-0.001 ms'); |
| assert.strictEqual(unit.format(-123456789), '-123,456,789.000 ms'); |
| |
| // Change the unit to nanoseconds. |
| Unit.currentTimeDisplayMode = tr.v.TimeDisplayModes.ns; |
| |
| assert.strictEqual(unit.format(0), '0 ns'); |
| assert.strictEqual(unit.format(1), '1,000,000 ns'); |
| assert.strictEqual(unit.format(0.000042), '42 ns'); |
| assert.strictEqual(unit.format(0.000001), '1 ns'); |
| assert.strictEqual(unit.format(0.0000005), '1 ns'); |
| assert.strictEqual(unit.format(0.00000049), '0 ns'); |
| assert.strictEqual(unit.format(123.456), '123,456,000 ns'); |
| assert.strictEqual(unit.format(-0.07), '-70,000 ns'); |
| } finally { |
| Unit.reset(); |
| } |
| } |
| |
| test('timeStampInMs', function() { |
| assert.strictEqual(Unit.byName.timeStampInMs.unitName, 'timeStampInMs'); |
| assert.strictEqual(Unit.byName.timeStampInMs.asJSON(), 'tsMs'); |
| checkTimeUnit(Unit.byName.timeStampInMs); |
| }); |
| |
| test('timeDurationInMs', function() { |
| assert.strictEqual(Unit.byName.timeDurationInMs.unitName, |
| 'timeDurationInMs'); |
| assert.strictEqual(Unit.byName.timeDurationInMs.asJSON(), 'ms'); |
| checkTimeUnit(Unit.byName.timeDurationInMs); |
| }); |
| |
| test('sizeInBytes', function() { |
| var SOURCE_VALUES = [0, 1, 1536, 424.5 * 1024 * 1024, |
| 1025 * 1024 * 1024 * 1024 * 1024, -2.5 * 1024 * 1024]; |
| var EXPECTED_REGULAR_FORMATTED_VALUES = ['0.0 B', '1.0 B', '1.5 KiB', |
| '424.5 MiB', '1025.0 TiB', '-2.5 MiB']; |
| var EXPECTED_DELTA_FORMATTED_VALUES = ['\u00B10.0 B', '+1.0 B', '+1.5 KiB', |
| '+424.5 MiB', '+1025.0 TiB', '-2.5 MiB']; |
| |
| function checkSizeUnit(unit, expectation) { |
| assert.strictEqual(unit.unitName, expectation.unitName); |
| assert.strictEqual(unit.asJSON(), expectation.asJSON); |
| assert.strictEqual(unit.isDelta, expectation.isDelta); |
| assert.strictEqual(unit.correspondingDeltaUnit, |
| expectation.correspondingDeltaUnit); |
| assert.strictEqual(unit.improvementDirection, |
| expectation.improvementDirection); |
| assert.deepEqual(SOURCE_VALUES.map(unit.format.bind(unit)), |
| expectation.formattedValues); |
| } |
| |
| // Regular (non-delta). |
| checkSizeUnit(Unit.byName.sizeInBytes, { |
| unitName: 'sizeInBytes', |
| asJSON: 'sizeInBytes', |
| isDelta: false, |
| correspondingDeltaUnit: Unit.byName.sizeInBytesDelta, |
| improvementDirection: ImprovementDirection.DONT_CARE, |
| formattedValues: EXPECTED_REGULAR_FORMATTED_VALUES |
| }); |
| checkSizeUnit(Unit.byName.sizeInBytes_smallerIsBetter, { |
| unitName: 'sizeInBytes_smallerIsBetter', |
| asJSON: 'sizeInBytes_smallerIsBetter', |
| isDelta: false, |
| correspondingDeltaUnit: Unit.byName.sizeInBytesDelta_smallerIsBetter, |
| improvementDirection: ImprovementDirection.SMALLER_IS_BETTER, |
| formattedValues: EXPECTED_REGULAR_FORMATTED_VALUES |
| }); |
| checkSizeUnit(Unit.byName.sizeInBytes_biggerIsBetter, { |
| unitName: 'sizeInBytes_biggerIsBetter', |
| asJSON: 'sizeInBytes_biggerIsBetter', |
| isDelta: false, |
| correspondingDeltaUnit: Unit.byName.sizeInBytesDelta_biggerIsBetter, |
| improvementDirection: ImprovementDirection.BIGGER_IS_BETTER, |
| formattedValues: EXPECTED_REGULAR_FORMATTED_VALUES |
| }); |
| |
| // Delta. |
| checkSizeUnit(Unit.byName.sizeInBytesDelta, { |
| unitName: 'sizeInBytesDelta', |
| asJSON: 'sizeInBytesDelta', |
| isDelta: true, |
| correspondingDeltaUnit: Unit.byName.sizeInBytesDelta, |
| improvementDirection: ImprovementDirection.DONT_CARE, |
| formattedValues: EXPECTED_DELTA_FORMATTED_VALUES |
| }); |
| checkSizeUnit(Unit.byName.sizeInBytesDelta_smallerIsBetter, { |
| unitName: 'sizeInBytesDelta_smallerIsBetter', |
| asJSON: 'sizeInBytesDelta_smallerIsBetter', |
| isDelta: true, |
| correspondingDeltaUnit: Unit.byName.sizeInBytesDelta_smallerIsBetter, |
| improvementDirection: ImprovementDirection.SMALLER_IS_BETTER, |
| formattedValues: EXPECTED_DELTA_FORMATTED_VALUES |
| }); |
| checkSizeUnit(Unit.byName.sizeInBytesDelta_biggerIsBetter, { |
| unitName: 'sizeInBytesDelta_biggerIsBetter', |
| asJSON: 'sizeInBytesDelta_biggerIsBetter', |
| isDelta: true, |
| correspondingDeltaUnit: Unit.byName.sizeInBytesDelta_biggerIsBetter, |
| improvementDirection: ImprovementDirection.BIGGER_IS_BETTER, |
| formattedValues: EXPECTED_DELTA_FORMATTED_VALUES |
| }); |
| }); |
| |
| test('energyInJoules', function() { |
| assert.equal(Unit.byName.energyInJoules.format(1000), '1,000.000 J'); |
| assert.equal(Unit.byName.energyInJoules.format(1), '1.000 J'); |
| assert.equal(Unit.byName.energyInJoules.format(.005), '0.005 J'); |
| assert.equal(Unit.byName.energyInJoules.format(.0005), '0.001 J'); |
| assert.equal(Unit.byName.energyInJoules.format(.0004), '0.000 J'); |
| }); |
| |
| test('powerInWatts', function() { |
| assert.equal(Unit.byName.powerInWatts.format(1000), '1,000.000 W'); |
| assert.equal(Unit.byName.powerInWatts.format(1), '1.000 W'); |
| assert.equal(Unit.byName.powerInWatts.format(.001), '0.001 W'); |
| assert.equal(Unit.byName.powerInWatts.format(.001005), '0.001 W'); |
| }); |
| }); |
| </script> |