xref: /aosp_15_r20/development/tools/winscope/src/viewers/viewer_search/search_result_presenter_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 {assertDefined} from 'common/assert_utils';
18import {TracePositionUpdate} from 'messaging/winscope_event';
19import {TimestampConverterUtils} from 'test/unit/timestamp_converter_utils';
20import {TraceBuilder} from 'test/unit/trace_builder';
21import {UnitTestUtils} from 'test/unit/utils';
22import {Trace} from 'trace/trace';
23import {TraceType} from 'trace/trace_type';
24import {QueryResult, Row, RowIterator} from 'trace_processor/query_result';
25import {NotifyLogViewCallbackType} from 'viewers/common/abstract_log_viewer_presenter';
26import {AbstractLogViewerPresenterTest} from 'viewers/common/abstract_log_viewer_presenter_test';
27import {LogHeader} from 'viewers/common/ui_data_log';
28import {SearchResultPresenter} from './search_result_presenter';
29import {SearchResult} from './ui_data';
30
31class SearchResultPresenterTest extends AbstractLogViewerPresenterTest<SearchResult> {
32  override readonly expectedHeaders = [
33    {
34      header: new LogHeader({
35        name: 'ts',
36        cssClass: 'search-result',
37      }),
38    },
39    {
40      header: new LogHeader({
41        name: 'property',
42        cssClass: 'search-result',
43      }),
44    },
45  ];
46  private trace: Trace<QueryResult> | undefined;
47  private positionUpdate: TracePositionUpdate | undefined;
48  private spyIter: jasmine.SpyObj<RowIterator<Row>> | undefined;
49
50  override async setUpTestEnvironment(): Promise<void> {
51    const time100 = TimestampConverterUtils.makeRealTimestamp(100n);
52    const [spyQueryResult, spyIter] =
53      UnitTestUtils.makeSearchTraceSpies(time100);
54    this.spyIter = spyIter;
55    this.trace = new TraceBuilder<QueryResult>()
56      .setEntries([spyQueryResult])
57      .setTimestamps([time100])
58      .setType(TraceType.SEARCH)
59      .build();
60    this.positionUpdate = TracePositionUpdate.fromTraceEntry(
61      this.trace.getEntry(0),
62    );
63  }
64
65  override resetTestEnvironment() {
66    assertDefined(this.spyIter).valid.and.returnValue(true);
67  }
68
69  override async createPresenterWithEmptyTrace(
70    callback: NotifyLogViewCallbackType<SearchResult>,
71  ): Promise<SearchResultPresenter> {
72    const time100 = TimestampConverterUtils.makeRealTimestamp(100n);
73    const [spyQueryResult, spyIter] =
74      UnitTestUtils.makeSearchTraceSpies(time100);
75    this.spyIter = spyIter;
76    const trace = UnitTestUtils.makeEmptyTrace(TraceType.SEARCH);
77    return new SearchResultPresenter(
78      'fake query',
79      trace,
80      callback,
81      spyQueryResult,
82    );
83  }
84
85  override async createPresenter(
86    callback: NotifyLogViewCallbackType<SearchResult>,
87    trace = assertDefined(this.trace),
88    positionUpdate = assertDefined(this.getPositionUpdate()),
89  ): Promise<SearchResultPresenter> {
90    const presenter = new SearchResultPresenter(
91      'successful query',
92      trace,
93      callback,
94      await trace.getEntry(0).getValue(),
95    );
96    await presenter.onAppEvent(positionUpdate); // trigger initialization
97    return presenter;
98  }
99
100  override getPositionUpdate(): TracePositionUpdate {
101    return assertDefined(this.positionUpdate);
102  }
103
104  override executePropertiesChecksAfterPositionUpdate(result: SearchResult) {
105    const firstEntry = assertDefined(this.trace).getEntry(0);
106    expect(result.entries).toEqual([
107      {
108        traceEntry: firstEntry,
109        fields: [
110          {
111            spec: this.expectedHeaders[0].header.spec,
112            value: firstEntry.getTimestamp(),
113          },
114          {spec: this.expectedHeaders[1].header.spec, value: 'test_value'},
115        ],
116        propertiesTree: undefined,
117      },
118    ]);
119  }
120}
121
122describe('SearchResultPresenterTest', () => {
123  new SearchResultPresenterTest().execute();
124});
125