• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 './time';
16import {HighPrecisionTime} from './high_precision_time';
17import {HighPrecisionTimeSpan} from './high_precision_time_span';
18import {TimeScale} from './time_scale';
19
20const t = Time.fromRaw;
21
22describe('TimeScale', () => {
23  const ts = new TimeScale(
24    new HighPrecisionTimeSpan(new HighPrecisionTime(t(40n)), 100),
25    {left: 200, right: 1000},
26  );
27
28  it('converts timescales to pixels', () => {
29    expect(ts.timeToPx(Time.fromRaw(40n))).toEqual(200);
30    expect(ts.timeToPx(Time.fromRaw(140n))).toEqual(1000);
31    expect(ts.timeToPx(Time.fromRaw(90n))).toEqual(600);
32
33    expect(ts.timeToPx(Time.fromRaw(240n))).toEqual(1800);
34    expect(ts.timeToPx(Time.fromRaw(-60n))).toEqual(-600);
35  });
36
37  it('converts pixels to HPTime objects', () => {
38    let result = ts.pxToHpTime(200);
39    expect(result.integral).toEqual(40n);
40    expect(result.fractional).toBeCloseTo(0);
41
42    result = ts.pxToHpTime(1000);
43    expect(result.integral).toEqual(140n);
44    expect(result.fractional).toBeCloseTo(0);
45
46    result = ts.pxToHpTime(600);
47    expect(result.integral).toEqual(90n);
48    expect(result.fractional).toBeCloseTo(0);
49
50    result = ts.pxToHpTime(1800);
51    expect(result.integral).toEqual(240n);
52    expect(result.fractional).toBeCloseTo(0);
53
54    result = ts.pxToHpTime(-600);
55    expect(result.integral).toEqual(-60n);
56    expect(result.fractional).toBeCloseTo(0);
57  });
58
59  it('converts durations to pixels', () => {
60    expect(ts.durationToPx(0n)).toEqual(0);
61    expect(ts.durationToPx(1n)).toEqual(8);
62    expect(ts.durationToPx(1000n)).toEqual(8000);
63  });
64
65  it('converts pxDeltaToDurations to HPTime durations', () => {
66    let result = ts.pxToDuration(0);
67    expect(result).toBeCloseTo(0);
68
69    result = ts.pxToDuration(1);
70    expect(result).toBeCloseTo(0.125);
71
72    result = ts.pxToDuration(100);
73    expect(result).toBeCloseTo(12.5);
74  });
75
76  it('converts px span to HPTimeSpan', () => {
77    const result = ts.pxSpanToHpTimeSpan({left: 200, right: 1000});
78    expect(result.start.integral).toBe(40n);
79    expect(result.start.fractional).toBeCloseTo(0);
80    expect(result.duration).toBeCloseTo(100);
81  });
82
83  it('converts HPTimeSpan to px span', () => {
84    const result = ts.hpTimeSpanToPxSpan(
85      new HighPrecisionTimeSpan(new HighPrecisionTime(Time.fromRaw(40n)), 100),
86    );
87    expect(result.left).toBe(200);
88    expect(result.right).toBeCloseTo(1000);
89  });
90});
91