1// Copyright (C) 2018 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 {dingus} from 'dingusjs'; 16 17import {TraceConfig} from '../common/protos'; 18import {createEmptyRecordConfig, RecordConfig} from '../common/state'; 19 20import {App} from './globals'; 21import { 22 genConfigProto, 23 RecordController, 24 toPbtxt, 25 uint8ArrayToBase64 26} from './record_controller'; 27 28test('uint8ArrayToBase64', () => { 29 const bytes = [...'Hello, world'].map(c => c.charCodeAt(0)); 30 const buffer = new Uint8Array(bytes); 31 expect(uint8ArrayToBase64(buffer)).toEqual('SGVsbG8sIHdvcmxk'); 32}); 33 34test('encodeConfig', () => { 35 const config = createEmptyRecordConfig(); 36 config.durationSeconds = 10; 37 const result = TraceConfig.decode(genConfigProto(config)); 38 expect(result.durationMs).toBe(10000); 39}); 40 41test('toPbtxt', () => { 42 const config = { 43 durationMs: 1000, 44 buffers: [ 45 { 46 sizeKb: 42, 47 }, 48 ], 49 dataSources: [{ 50 config: { 51 name: 'linux.ftrace', 52 targetBuffer: 1, 53 ftraceConfig: { 54 ftraceEvents: ['sched_switch', 'print'], 55 }, 56 }, 57 }], 58 producers: [ 59 { 60 producerName: 'perfetto.traced_probes', 61 }, 62 ], 63 }; 64 65 const text = toPbtxt(TraceConfig.encode(config).finish()); 66 67 expect(text).toEqual(`buffers: { 68 size_kb: 42 69} 70data_sources: { 71 config { 72 name: "linux.ftrace" 73 target_buffer: 1 74 ftrace_config { 75 ftrace_events: "sched_switch" 76 ftrace_events: "print" 77 } 78 } 79} 80duration_ms: 1000 81producers: { 82 producer_name: "perfetto.traced_probes" 83} 84`); 85}); 86 87test('RecordController', () => { 88 const app = dingus<App>('globals'); 89 (app.state.recordConfig as RecordConfig) = createEmptyRecordConfig(); 90 const controller = new RecordController({app}); 91 controller.run(); 92 controller.run(); 93 controller.run(); 94 // tslint:disable-next-line no-any 95 const calls = app.calls.filter((call: any) => call[0] === 'publish()'); 96 expect(calls.length).toBe(1); 97 // TODO(hjd): Fix up dingus to have a more sensible API. 98 expect(calls[0][1][0]).toEqual('TrackData'); 99}); 100