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 {UnitTestUtils} from 'test/unit/utils'; 19import {Parser} from 'trace/parser'; 20import {Trace} from 'trace/trace'; 21import {Traces} from 'trace/traces'; 22import {TraceType} from 'trace/trace_type'; 23import {SEARCH_VIEWS, TraceSearchInitializer} from './trace_search_initializer'; 24 25describe('TraceSearchInitializer', () => { 26 it('robust to no searchable traces', async () => { 27 const views = await TraceSearchInitializer.createSearchViews(new Traces()); 28 expect(views).toEqual([]); 29 }); 30 31 it('initializes surface flinger', async () => { 32 const parser = await UnitTestUtils.getPerfettoParser( 33 TraceType.SURFACE_FLINGER, 34 'traces/perfetto/layers_trace.perfetto-trace', 35 ); 36 await createViewsAndTestExamples(parser, [ 37 'sf_layer_search', 38 'sf_hierarchy_root_search', 39 ]); 40 const queryResult = await UnitTestUtils.runQueryAndGetResult(` 41 SELECT * FROM sf_layer_search 42 WHERE layer_name LIKE 'Task%' 43 AND property='flags' 44 AND value!=previous_value 45 `); 46 expect(queryResult.numRows()).toEqual(2); 47 48 const queryResultEntry = await UnitTestUtils.runQueryAndGetResult(` 49 SELECT * FROM sf_hierarchy_root_search 50 WHERE property LIKE 'displays[1]%' 51 AND ( 52 flat_property='displays.layer_stack' 53 OR flat_property='displays.is_virtual' 54 ) 55 `); 56 expect(queryResultEntry.numRows()).toEqual(40); 57 }); 58 59 it('initializes transactions', async () => { 60 const parser = await UnitTestUtils.getPerfettoParser( 61 TraceType.TRANSACTIONS, 62 'traces/perfetto/transactions_trace.perfetto-trace', 63 ); 64 await createViewsAndTestExamples(parser, ['transactions_search']); 65 const queryResultTransaction = await UnitTestUtils.runQueryAndGetResult(` 66 SELECT * FROM transactions_search 67 WHERE flat_property='transactions.layer_changes.x' 68 AND value!='0.0' 69 `); 70 expect(queryResultTransaction.numRows()).toEqual(3); 71 72 const queryResultAddedLayer = await UnitTestUtils.runQueryAndGetResult(` 73 SELECT * FROM transactions_search 74 WHERE flat_property='added_layers.name' 75 AND value='ImeContainer' 76 `); 77 expect(queryResultAddedLayer.numRows()).toEqual(1); 78 }); 79 80 it('initializes protolog', async () => { 81 const parser = await UnitTestUtils.getPerfettoParser( 82 TraceType.PROTO_LOG, 83 'traces/perfetto/protolog.perfetto-trace', 84 ); 85 await createViewsAndTestExamples(parser, ['protolog']); 86 const queryResult = await UnitTestUtils.runQueryAndGetResult(` 87 SELECT * FROM protolog WHERE message LIKE '%string%' 88 `); 89 expect(queryResult.numRows()).toEqual(2); 90 }); 91 92 it('initializes transitions', async () => { 93 const parser = await UnitTestUtils.getPerfettoParser( 94 TraceType.TRANSITION, 95 'traces/perfetto/shell_transitions_trace.perfetto-trace', 96 ); 97 await createViewsAndTestExamples(parser, ['transitions_search']); 98 const queryResult = await UnitTestUtils.runQueryAndGetResult(` 99 SELECT * FROM transitions_search 100 WHERE flat_property='handler' 101 AND value LIKE '%DefaultMixedHandler' 102 `); 103 expect(queryResult.numRows()).toEqual(2); 104 }); 105 106 it('initializes view capture', async () => { 107 const parser = await UnitTestUtils.getPerfettoParser( 108 TraceType.VIEW_CAPTURE, 109 'traces/perfetto/viewcapture.perfetto-trace', 110 ); 111 await createViewsAndTestExamples(parser, ['viewcapture_search']); 112 const queryResult = await UnitTestUtils.runQueryAndGetResult(` 113 SELECT * FROM viewcapture_search 114 WHERE class_name LIKE '%SearchContainerView' 115 AND flat_property='translation_y' 116 AND value!=previous_value 117 `); 118 expect(queryResult.numRows()).toEqual(28); 119 }); 120 121 async function createViewsAndTestExamples( 122 parser: Parser<object>, 123 expectedViews: string[], 124 ) { 125 const trace = Trace.fromParser(parser); 126 const traces = new Traces(); 127 traces.addTrace(trace); 128 const views = await TraceSearchInitializer.createSearchViews(traces); 129 expect(views).toEqual(expectedViews); 130 for (const viewName of views) { 131 const view = assertDefined( 132 SEARCH_VIEWS.find((view) => view.name === viewName), 133 ); 134 for (const example of view.examples) { 135 await expectAsync( 136 UnitTestUtils.runQueryAndGetResult(example.query), 137 ).not.toBeRejected(); 138 } 139 } 140 } 141}); 142