• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {globals} from '../frontend/globals';
16import {Plugin} from '../public';
17import {EngineBase} from '../trace_processor/engine';
18
19import {createEmptyState} from './empty_state';
20import {PluginManager, PluginRegistry} from './plugins';
21
22class FakeEngine extends EngineBase {
23  id: string = 'TestEngine';
24
25  rpcSendRequestBytes(_data: Uint8Array) {}
26}
27
28function makeMockPlugin(): Plugin {
29  return {
30    onActivate: jest.fn(),
31    onDeactivate: jest.fn(),
32    onTraceLoad: jest.fn(),
33    onTraceUnload: jest.fn(),
34  };
35}
36
37const engine = new FakeEngine();
38globals.initStore(createEmptyState());
39
40let mockPlugin: Plugin;
41let manager: PluginManager;
42
43describe('PluginManger', () => {
44  beforeEach(() => {
45    mockPlugin = makeMockPlugin();
46    const registry = new PluginRegistry();
47    registry.register({
48      pluginId: 'foo',
49      plugin: mockPlugin,
50    });
51    manager = new PluginManager(registry);
52  });
53
54  it('can activate plugin', async () => {
55    await manager.activatePlugin('foo');
56
57    expect(manager.isActive('foo')).toBe(true);
58    expect(mockPlugin.onActivate).toHaveBeenCalledTimes(1);
59  });
60
61  it('can deactivate plugin', async () => {
62    await manager.activatePlugin('foo');
63    await manager.deactivatePlugin('foo');
64
65    expect(manager.isActive('foo')).toBe(false);
66    expect(mockPlugin.onDeactivate).toHaveBeenCalledTimes(1);
67  });
68
69  it('invokes onTraceLoad when trace is loaded', async () => {
70    await manager.activatePlugin('foo');
71    await manager.onTraceLoad(engine);
72
73    expect(mockPlugin.onTraceLoad).toHaveBeenCalledTimes(1);
74  });
75
76  it('invokes onTraceLoad when plugin activated while trace loaded', async () => {
77    await manager.onTraceLoad(engine);
78    await manager.activatePlugin('foo');
79
80    expect(mockPlugin.onTraceLoad).toHaveBeenCalledTimes(1);
81  });
82
83  it('invokes onTraceUnload when plugin deactivated while trace loaded', async () => {
84    await manager.activatePlugin('foo');
85    await manager.onTraceLoad(engine);
86    await manager.deactivatePlugin('foo');
87
88    expect(mockPlugin.onTraceUnload).toHaveBeenCalledTimes(1);
89  });
90});
91