1// Copyright (C) 2018 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {Time} from '../base/time'; 16import {HighPrecisionTime} from '../common/high_precision_time'; 17 18import {PxSpan, TimeScale} from './time_scale'; 19 20describe('TimeScale', () => { 21 const ts = new TimeScale( 22 new HighPrecisionTime(40n), 23 100, 24 new PxSpan(200, 1000), 25 ); 26 27 it('converts timescales to pixels', () => { 28 expect(ts.timeToPx(Time.fromRaw(40n))).toEqual(200); 29 expect(ts.timeToPx(Time.fromRaw(140n))).toEqual(1000); 30 expect(ts.timeToPx(Time.fromRaw(90n))).toEqual(600); 31 32 expect(ts.timeToPx(Time.fromRaw(240n))).toEqual(1800); 33 expect(ts.timeToPx(Time.fromRaw(-60n))).toEqual(-600); 34 }); 35 36 it('converts pixels to HPTime objects', () => { 37 let result = ts.pxToHpTime(200); 38 expect(result.base).toEqual(40n); 39 expect(result.offset).toBeCloseTo(0); 40 41 result = ts.pxToHpTime(1000); 42 expect(result.base).toEqual(140n); 43 expect(result.offset).toBeCloseTo(0); 44 45 result = ts.pxToHpTime(600); 46 expect(result.base).toEqual(90n); 47 expect(result.offset).toBeCloseTo(0); 48 49 result = ts.pxToHpTime(1800); 50 expect(result.base).toEqual(240n); 51 expect(result.offset).toBeCloseTo(0); 52 53 result = ts.pxToHpTime(-600); 54 expect(result.base).toEqual(-60n); 55 expect(result.offset).toBeCloseTo(0); 56 }); 57 58 it('converts durations to pixels', () => { 59 expect(ts.durationToPx(0n)).toEqual(0); 60 expect(ts.durationToPx(1n)).toEqual(8); 61 expect(ts.durationToPx(1000n)).toEqual(8000); 62 }); 63 64 it('converts pxDeltaToDurations to HPTime durations', () => { 65 let result = ts.pxDeltaToDuration(0); 66 expect(result.base).toEqual(0n); 67 expect(result.offset).toBeCloseTo(0); 68 69 result = ts.pxDeltaToDuration(1); 70 expect(result.base).toEqual(0n); 71 expect(result.offset).toBeCloseTo(0.125); 72 73 result = ts.pxDeltaToDuration(100); 74 expect(result.base).toEqual(12n); 75 expect(result.offset).toBeCloseTo(0.5); 76 }); 77}); 78