• 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 {Store} from 'common/store/store';
19import {Trace} from 'trace/trace';
20import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
21import {
22  AbstractLogViewerPresenter,
23  NotifyLogViewCallbackType,
24} from 'viewers/common/abstract_log_viewer_presenter';
25import {LogSelectFilter, LogTextFilter} from 'viewers/common/log_filters';
26import {LogPresenter} from 'viewers/common/log_presenter';
27import {PropertiesPresenter} from 'viewers/common/properties_presenter';
28import {TextFilter} from 'viewers/common/text_filter';
29import {LogEntry, LogHeader, UiDataLog} from 'viewers/common/ui_data_log';
30import {UiPropertyTreeNode} from 'viewers/common/ui_property_tree_node';
31import {UserOptions} from 'viewers/common/user_options';
32
33export class MockPresenter extends AbstractLogViewerPresenter<
34  UiDataLog,
35  PropertyTreeNode
36> {
37  protected override logPresenter = new LogPresenter<LogEntry>();
38  protected override propertiesPresenter = new PropertiesPresenter(
39    {
40      showDefaults: {
41        name: 'Show defaults',
42        enabled: false,
43        tooltip: `If checked, shows the value of all properties.
44Otherwise, hides all properties whose value is
45the default for its data type.`,
46      },
47    },
48    new TextFilter(),
49    [],
50  );
51  stringColumn = {
52    name: 'String Column',
53    cssClass: 'string-column',
54  };
55  numberColumn = {
56    name: 'Number Column',
57    cssClass: 'number-column',
58  };
59  timestampColumn = {
60    name: 'Timestamp Column',
61    cssClass: 'timestamp-column',
62  };
63
64  constructor(
65    trace: Trace<PropertyTreeNode>,
66    readonly storage: Store,
67    notifyViewCallback: NotifyLogViewCallbackType<UiDataLog>,
68  ) {
69    super(trace, notifyViewCallback, MockData.createEmpty());
70  }
71
72  protected override async makeUiDataEntries(): Promise<LogEntry[]> {
73    if (this.trace.lengthEntries === 0) return [];
74    const entries: LogEntry[] = [
75      {
76        traceEntry: this.trace.getEntry(0),
77        fields: [
78          {spec: this.stringColumn, value: 'stringValue'},
79          {spec: this.numberColumn, value: 0},
80          {
81            spec: this.timestampColumn,
82            value: this.trace.getEntry(0).getTimestamp(),
83          },
84        ],
85        propertiesTree: await this.trace.getEntry(0).getValue(),
86      },
87      {
88        traceEntry: this.trace.getEntry(1),
89        fields: [
90          {spec: this.stringColumn, value: 'differentValue'},
91          {spec: this.numberColumn, value: 1},
92          {
93            spec: this.timestampColumn,
94            value: this.trace.getEntry(1).getTimestamp(),
95          },
96        ],
97        propertiesTree: await this.trace.getEntry(1).getValue(),
98      },
99      {
100        traceEntry: this.trace.getEntry(2),
101        fields: [
102          {spec: this.stringColumn, value: 'stringValue'},
103          {spec: this.numberColumn, value: 2},
104          {
105            spec: this.timestampColumn,
106            value: this.trace.getEntry(2).getTimestamp(),
107          },
108        ],
109        propertiesTree: await this.trace.getEntry(2).getValue(),
110      },
111      {
112        traceEntry: this.trace.getEntry(3),
113        fields: [
114          {spec: this.stringColumn, value: 'differentValue'},
115          {spec: this.numberColumn, value: 3},
116          {
117            spec: this.timestampColumn,
118            value: this.trace.getEntry(3).getTimestamp(),
119          },
120        ],
121        propertiesTree: await this.trace.getEntry(3).getValue(),
122      },
123    ];
124    return entries;
125  }
126
127  protected override makeHeaders(): LogHeader[] {
128    const stringFilter = new LogTextFilter(new TextFilter());
129    const numberFilter = new LogSelectFilter(['0', '1', '2', '3']);
130    const headers = [
131      new LogHeader(this.stringColumn, stringFilter),
132      new LogHeader(this.numberColumn, numberFilter),
133      new LogHeader(this.timestampColumn),
134    ];
135    return headers;
136  }
137
138  protected override updateFiltersInHeaders(
139    headers: LogHeader[],
140    allEntries: LogEntry[],
141  ) {
142    for (const header of headers) {
143      if (header.spec === this.stringColumn) {
144        (assertDefined(header.filter) as LogSelectFilter).options = [
145          'stringValue',
146          'differentValue',
147        ];
148      }
149    }
150  }
151}
152
153export class MockData implements UiDataLog {
154  isFetchingData = false;
155  constructor(
156    public headers: LogHeader[],
157    public entries: LogEntry[],
158    public currentIndex: undefined | number,
159    public selectedIndex: undefined | number,
160    public scrollToIndex: undefined | number,
161    public propertiesTree: undefined | UiPropertyTreeNode,
162    public propertiesUserOptions: UserOptions,
163    public isDarkMode = false,
164  ) {}
165
166  static createEmpty(): MockData {
167    return new MockData([], [], undefined, undefined, undefined, undefined, {});
168  }
169}
170