• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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 */
16import {assertDefined} from 'common/assert_utils';
17import {
18  TimestampConverterUtils,
19  timestampEqualityTester,
20} from 'common/time/test_utils';
21import {getFixtureFile} from 'test/unit/fixture_utils';
22import {UnitTestUtils} from 'test/unit/utils';
23import {Parser} from 'trace/parser';
24import {TraceFile} from 'trace/trace_file';
25import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
26import {ParserFactory} from './parser_factory';
27
28describe('Parser', () => {
29  beforeAll(() => {
30    jasmine.addCustomEqualityTester(timestampEqualityTester);
31  });
32
33  it('is robust to empty trace file', async () => {
34    const trace = new TraceFile(
35      await getFixtureFile('traces/empty.pb'),
36      undefined,
37    );
38    const parsers = await new ParserFactory().createParsers(
39      [trace],
40      TimestampConverterUtils.TIMESTAMP_CONVERTER,
41      {},
42    );
43    expect(parsers.length).toEqual(0);
44  });
45
46  it('is robust to trace with no entries', async () => {
47    const trace = new TraceFile(
48      await getFixtureFile('traces/no_entries_InputMethodClients.pb'),
49      undefined,
50    );
51    const parsers = await new ParserFactory().createParsers(
52      [trace],
53      TimestampConverterUtils.TIMESTAMP_CONVERTER,
54      {},
55    );
56    expect(parsers.length).toEqual(0);
57  });
58
59  it('is robust to view capture trace with no entries', async () => {
60    const trace = new TraceFile(
61      await getFixtureFile('traces/no_entries_view_capture.vc'),
62      undefined,
63    );
64    const parsers = await new ParserFactory().createParsers(
65      [trace],
66      TimestampConverterUtils.TIMESTAMP_CONVERTER,
67      {},
68    );
69    expect(parsers.length).toEqual(0);
70  });
71
72  describe('real timestamp', () => {
73    let parser: Parser<HierarchyTreeNode>;
74
75    beforeAll(async () => {
76      parser = (await UnitTestUtils.getParser(
77        'traces/elapsed_and_real_timestamp/WindowManager.pb',
78      )) as Parser<HierarchyTreeNode>;
79    });
80
81    it('has expected descriptors', () => {
82      expect(parser.getDescriptors()).toEqual(['WindowManager.pb']);
83    });
84
85    it('provides timestamps', () => {
86      const expected = [
87        TimestampConverterUtils.makeRealTimestamp(1659107089075566202n),
88        TimestampConverterUtils.makeRealTimestamp(1659107089999048990n),
89        TimestampConverterUtils.makeRealTimestamp(1659107090010194213n),
90      ];
91      expect(assertDefined(parser.getTimestamps()).slice(0, 3)).toEqual(
92        expected,
93      );
94    });
95
96    it('retrieves trace entries', async () => {
97      let entry = await parser.getEntry(0);
98      expect(
99        assertDefined(entry.getEagerPropertyByName('focusedApp')).getValue(),
100      ).toEqual('com.google.android.apps.nexuslauncher/.NexusLauncherActivity');
101
102      entry = await parser.getEntry(parser.getLengthEntries() - 1);
103      expect(
104        assertDefined(entry.getEagerPropertyByName('focusedApp')).getValue(),
105      ).toEqual('com.google.android.apps.nexuslauncher/.NexusLauncherActivity');
106    });
107  });
108
109  describe('elapsed timestamp', () => {
110    let parser: Parser<HierarchyTreeNode>;
111
112    beforeAll(async () => {
113      parser = (await UnitTestUtils.getParser(
114        'traces/elapsed_timestamp/WindowManager.pb',
115      )) as Parser<HierarchyTreeNode>;
116    });
117
118    it('provides timestamps', () => {
119      const expected = [
120        TimestampConverterUtils.makeElapsedTimestamp(850254319343n),
121        TimestampConverterUtils.makeElapsedTimestamp(850763506110n),
122        TimestampConverterUtils.makeElapsedTimestamp(850782750048n),
123      ];
124      expect(parser.getTimestamps()).toEqual(expected);
125    });
126
127    it('retrieves trace entries', async () => {
128      let entry = await parser.getEntry(0);
129      expect(
130        assertDefined(entry.getEagerPropertyByName('focusedApp')).getValue(),
131      ).toEqual('com.google.android.apps.nexuslauncher/.NexusLauncherActivity');
132
133      entry = await parser.getEntry(parser.getLengthEntries() - 1);
134      expect(
135        assertDefined(entry.getEagerPropertyByName('focusedApp')).getValue(),
136      ).toEqual('com.google.android.apps.nexuslauncher/.NexusLauncherActivity');
137    });
138  });
139});
140