blob: 519cbba8594d70f494a2a75f07f39c47adc5f160 [file] [log] [blame]
Michail Schwabbeb34522018-07-20 08:15:17 -04001// 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
Steve Golton278b7f02023-09-06 16:26:23 +010015import {Time} from '../base/time';
Steve Goltonf3897e22023-05-11 14:18:30 +010016import {HighPrecisionTime} from '../common/high_precision_time';
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020017
Steve Goltonf3897e22023-05-11 14:18:30 +010018import {PxSpan, TimeScale} from './time_scale';
Michail Schwabbeb34522018-07-20 08:15:17 -040019
Steve Goltonf3897e22023-05-11 14:18:30 +010020describe('TimeScale', () => {
21 const ts =
22 new TimeScale(new HighPrecisionTime(40n), 100, new PxSpan(200, 1000));
Michail Schwabbeb34522018-07-20 08:15:17 -040023
Steve Goltonf3897e22023-05-11 14:18:30 +010024 it('converts timescales to pixels', () => {
Steve Goltonb3a389d2023-07-10 11:03:17 +010025 expect(ts.timeToPx(Time.fromRaw(40n))).toEqual(200);
26 expect(ts.timeToPx(Time.fromRaw(140n))).toEqual(1000);
27 expect(ts.timeToPx(Time.fromRaw(90n))).toEqual(600);
Michail Schwabbeb34522018-07-20 08:15:17 -040028
Steve Goltonb3a389d2023-07-10 11:03:17 +010029 expect(ts.timeToPx(Time.fromRaw(240n))).toEqual(1800);
30 expect(ts.timeToPx(Time.fromRaw(-60n))).toEqual(-600);
Steve Goltonf3897e22023-05-11 14:18:30 +010031 });
Michail Schwabbeb34522018-07-20 08:15:17 -040032
Steve Goltonf3897e22023-05-11 14:18:30 +010033 it('converts pixels to HPTime objects', () => {
34 let result = ts.pxToHpTime(200);
35 expect(result.base).toEqual(40n);
36 expect(result.offset).toBeCloseTo(0);
Hector Dearmanea002ea2019-01-21 11:43:45 +000037
Steve Goltonf3897e22023-05-11 14:18:30 +010038 result = ts.pxToHpTime(1000);
39 expect(result.base).toEqual(140n);
40 expect(result.offset).toBeCloseTo(0);
Michail Schwabbeb34522018-07-20 08:15:17 -040041
Steve Goltonf3897e22023-05-11 14:18:30 +010042 result = ts.pxToHpTime(600);
43 expect(result.base).toEqual(90n);
44 expect(result.offset).toBeCloseTo(0);
Michail Schwabbeb34522018-07-20 08:15:17 -040045
Steve Goltonf3897e22023-05-11 14:18:30 +010046 result = ts.pxToHpTime(1800);
47 expect(result.base).toEqual(240n);
48 expect(result.offset).toBeCloseTo(0);
Michail Schwabbeb34522018-07-20 08:15:17 -040049
Steve Goltonf3897e22023-05-11 14:18:30 +010050 result = ts.pxToHpTime(-600);
51 expect(result.base).toEqual(-60n);
52 expect(result.offset).toBeCloseTo(0);
53 });
Michail Schwabbeb34522018-07-20 08:15:17 -040054
Steve Goltonf3897e22023-05-11 14:18:30 +010055 it('converts durations to pixels', () => {
56 expect(ts.durationToPx(0n)).toEqual(0);
57 expect(ts.durationToPx(1n)).toEqual(8);
58 expect(ts.durationToPx(1000n)).toEqual(8000);
59 });
Michail Schwabbeb34522018-07-20 08:15:17 -040060
Steve Goltonf3897e22023-05-11 14:18:30 +010061 it('converts pxDeltaToDurations to HPTime durations', () => {
62 let result = ts.pxDeltaToDuration(0);
63 expect(result.base).toEqual(0n);
64 expect(result.offset).toBeCloseTo(0);
Hector Dearman3e82d682019-01-09 15:55:10 +000065
Steve Goltonf3897e22023-05-11 14:18:30 +010066 result = ts.pxDeltaToDuration(1);
67 expect(result.base).toEqual(0n);
68 expect(result.offset).toBeCloseTo(0.125);
Hector Dearman3e82d682019-01-09 15:55:10 +000069
Steve Goltonf3897e22023-05-11 14:18:30 +010070 result = ts.pxDeltaToDuration(100);
71 expect(result.base).toEqual(12n);
72 expect(result.offset).toBeCloseTo(0.5);
73 });
Hector Dearman3e82d682019-01-09 15:55:10 +000074});