xref: /aosp_15_r20/development/tools/winscope/src/common/time_duration_test.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {TimeDuration} from './time_duration';
18import {TIME_UNIT_TO_NANO} from './time_units';
19
20describe('TimeDuration', () => {
21  const MILLISECOND = BigInt(TIME_UNIT_TO_NANO.ms);
22  const SECOND = BigInt(TIME_UNIT_TO_NANO.s);
23  const MINUTE = BigInt(TIME_UNIT_TO_NANO.m);
24
25  it('formats to nearest ms, using locale format for number', () => {
26    const expected0 = makeExpectedString(0);
27    expect(new TimeDuration(0n).format()).toEqual(expected0);
28    expect(new TimeDuration(1000n).format()).toEqual(expected0);
29    expect(new TimeDuration(10n * MILLISECOND).format()).toEqual(
30      makeExpectedString(10),
31    );
32
33    const expected1000 = makeExpectedString(1000);
34    expect(new TimeDuration(SECOND - 1n).format()).toEqual(expected1000);
35    expect(new TimeDuration(SECOND).format()).toEqual(expected1000);
36    expect(new TimeDuration(SECOND + MILLISECOND).format()).toEqual(
37      makeExpectedString(1001),
38    );
39
40    const expected60000 = makeExpectedString(60000);
41    expect(new TimeDuration(MINUTE - 1n).format()).toEqual(expected60000);
42    expect(new TimeDuration(MINUTE).format()).toEqual(expected60000);
43
44    const expected61001 = makeExpectedString(61001);
45    expect(new TimeDuration(MINUTE + SECOND + MILLISECOND).format()).toEqual(
46      expected61001,
47    );
48    expect(
49      new TimeDuration(MINUTE + SECOND + MILLISECOND + 1n).format(),
50    ).toEqual(expected61001);
51  });
52
53  function makeExpectedString(value: number) {
54    return BigInt(value).toLocaleString() + ' ms';
55  }
56});
57