• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {assertDefined} from 'common/assert_utils';
18import {TimestampConverterUtils} from 'common/time/test_utils';
19import {MockLong} from 'test/unit/mock_long';
20import {PropertyTreeBuilder} from 'test/unit/property_tree_builder';
21import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
22import {TransformToTimestamp} from './transform_to_timestamp';
23
24describe('TransformToTimestamp', () => {
25  const timestampProperties = ['timestamp'];
26  const longTimestamp = new MockLong(10, 0);
27  let propertyRoot: PropertyTreeNode;
28
29  beforeEach(() => {
30    propertyRoot = new PropertyTreeBuilder()
31      .setIsRoot(true)
32      .setRootId('test')
33      .setName('node')
34      .setChildren([
35        {name: 'timestamp', value: longTimestamp},
36        {name: 'otherTimestamp', value: longTimestamp},
37      ])
38      .build();
39  });
40
41  it('transforms only specified property to real timestamp', () => {
42    const operation = new TransformToTimestamp(
43      timestampProperties,
44      makeRealTimestampStrategy,
45    );
46    operation.apply(propertyRoot);
47    expect(
48      assertDefined(propertyRoot.getChildByName('timestamp')).getValue(),
49    ).toEqual(TimestampConverterUtils.makeRealTimestamp(10n));
50    expect(
51      assertDefined(propertyRoot.getChildByName('otherTimestamp')).getValue(),
52    ).toEqual(longTimestamp);
53  });
54
55  it('transforms only specified property to elapsed timestamp', () => {
56    const operation = new TransformToTimestamp(
57      timestampProperties,
58      makeElapsedTimestampStrategy,
59    );
60    operation.apply(propertyRoot);
61    expect(
62      assertDefined(propertyRoot.getChildByName('timestamp')).getValue(),
63    ).toEqual(TimestampConverterUtils.makeElapsedTimestamp(10n));
64    expect(
65      assertDefined(propertyRoot.getChildByName('otherTimestamp')).getValue(),
66    ).toEqual(longTimestamp);
67  });
68
69  function makeRealTimestampStrategy(valueNs: bigint) {
70    return TimestampConverterUtils.makeRealTimestamp(valueNs);
71  }
72
73  function makeElapsedTimestampStrategy(valueNs: bigint) {
74    return TimestampConverterUtils.makeElapsedTimestamp(valueNs);
75  }
76});
77