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 {TimeSpan} from '../common/time';
16
17import {computeZoom, TimeScale} from './time_scale';
18
19test('time scale to work', () => {
20  const scale = new TimeScale(new TimeSpan(0, 100), [200, 1000]);
21
22  expect(scale.timeToPx(0)).toEqual(200);
23  expect(scale.timeToPx(100)).toEqual(1000);
24  expect(scale.timeToPx(50)).toEqual(600);
25
26  expect(scale.pxToTime(200)).toEqual(0);
27  expect(scale.pxToTime(1000)).toEqual(100);
28  expect(scale.pxToTime(600)).toEqual(50);
29
30  expect(scale.deltaPxToDuration(400)).toEqual(50);
31
32  expect(scale.timeInBounds(50)).toEqual(true);
33  expect(scale.timeInBounds(0)).toEqual(true);
34  expect(scale.timeInBounds(100)).toEqual(true);
35  expect(scale.timeInBounds(-1)).toEqual(false);
36  expect(scale.timeInBounds(101)).toEqual(false);
37});
38
39
40test('time scale to be updatable', () => {
41  const scale = new TimeScale(new TimeSpan(0, 100), [100, 1000]);
42
43  expect(scale.timeToPx(0)).toEqual(100);
44
45  scale.setLimitsPx(200, 1000);
46  expect(scale.timeToPx(0)).toEqual(200);
47  expect(scale.timeToPx(100)).toEqual(1000);
48
49  scale.setTimeBounds(new TimeSpan(0, 200));
50  expect(scale.timeToPx(0)).toEqual(200);
51  expect(scale.timeToPx(100)).toEqual(600);
52  expect(scale.timeToPx(200)).toEqual(1000);
53});
54
55test('it zooms', () => {
56  const span = new TimeSpan(0, 20);
57  const scale = new TimeScale(span, [0, 100]);
58  const newSpan = computeZoom(scale, span, 0.5, 50);
59  expect(newSpan.start).toEqual(5);
60  expect(newSpan.end).toEqual(15);
61});
62
63test('it zooms an offset scale and span', () => {
64  const span = new TimeSpan(1000, 1020);
65  const scale = new TimeScale(span, [200, 300]);
66  const newSpan = computeZoom(scale, span, 0.5, 250);
67  expect(newSpan.start).toEqual(1005);
68  expect(newSpan.end).toEqual(1015);
69});
70
71test('it clamps zoom in', () => {
72  const span = new TimeSpan(1000, 1040);
73  const scale = new TimeScale(span, [200, 300]);
74  const newSpan = computeZoom(scale, span, 0.0000000001, 225);
75  expect((newSpan.end - newSpan.start) / 2 + newSpan.start).toBeCloseTo(1010);
76  expect(newSpan.end - newSpan.start).toBeCloseTo(1e-6, 8);
77});
78