xref: /aosp_15_r20/development/tools/winscope/src/test/unit/mock_adb_connection.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1/*
2 * Copyright 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 {FunctionUtils} from 'common/function_utils';
18import {AdbConnection} from 'trace_collection/adb_connection';
19import {AdbDevice} from 'trace_collection/adb_device';
20import {ConnectionState} from 'trace_collection/connection_state';
21import {TraceRequest} from 'trace_collection/trace_request';
22
23export class MockAdbConnection extends AdbConnection {
24  state: ConnectionState = ConnectionState.CONNECTING;
25  errorText = '';
26  files = [new File([], 'test_file')];
27  devices: AdbDevice[] = [];
28  availableTracesChangeCallback: (traces: string[]) => void =
29    FunctionUtils.DO_NOTHING;
30  devicesChangeCallback: (devices: AdbDevice[]) => void =
31    FunctionUtils.DO_NOTHING;
32  private detectStateChangeInUi: () => Promise<void> =
33    FunctionUtils.DO_NOTHING_ASYNC;
34
35  async initialize(
36    detectStateChangeInUi: () => Promise<void>,
37    availableTracesChangeCallback: (traces: string[]) => void,
38    devicesChangeCallback: (devices: AdbDevice[]) => void,
39  ) {
40    this.detectStateChangeInUi = detectStateChangeInUi;
41    this.availableTracesChangeCallback = availableTracesChangeCallback;
42    this.devicesChangeCallback = devicesChangeCallback;
43    this.setState(ConnectionState.CONNECTING);
44  }
45
46  setState(state: ConnectionState) {
47    this.state = state;
48    this.detectStateChangeInUi();
49  }
50
51  async restartConnection(): Promise<void> {
52    this.setState(ConnectionState.CONNECTING);
53  }
54
55  setSecurityToken(token: string) {
56    // do nothing
57  }
58
59  getDevices(): AdbDevice[] {
60    return this.devices;
61  }
62
63  getState(): ConnectionState {
64    return this.state;
65  }
66
67  getErrorText(): string {
68    return this.errorText;
69  }
70
71  onDestroy() {
72    // do nothing
73  }
74
75  async startTrace(
76    device: AdbDevice,
77    requestedTraces: TraceRequest[],
78  ): Promise<void> {
79    this.setState(ConnectionState.STARTING_TRACE);
80  }
81
82  async endTrace() {
83    this.setState(ConnectionState.ENDING_TRACE);
84  }
85
86  async dumpState(
87    device: AdbDevice,
88    requestedDumps: TraceRequest[],
89  ): Promise<void> {
90    this.setState(ConnectionState.DUMPING_STATE);
91  }
92
93  async fetchLastTracingSessionData(device: AdbDevice): Promise<File[]> {
94    this.setState(ConnectionState.LOADING_DATA);
95    return this.files;
96  }
97}
98