1 /* 2 * Copyright (C) 2023 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 17 package android.tracing.perfetto; 18 19 import android.util.proto.ProtoInputStream; 20 21 import java.util.UUID; 22 import java.util.function.Consumer; 23 24 public class TestDataSource extends DataSource<TestDataSource.TestDataSourceInstance, 25 TestDataSource.TestTlsState, TestDataSource.TestIncrementalState> { 26 private final DataSourceInstanceProvider mDataSourceInstanceProvider; 27 private final TlsStateProvider mTlsStateProvider; 28 private final IncrementalStateProvider mIncrementalStateProvider; 29 30 interface DataSourceInstanceProvider { provide( TestDataSource dataSource, int instanceIndex, ProtoInputStream configStream)31 TestDataSourceInstance provide( 32 TestDataSource dataSource, int instanceIndex, ProtoInputStream configStream); 33 } 34 35 interface TlsStateProvider { provide(CreateTlsStateArgs<TestDataSourceInstance> args)36 TestTlsState provide(CreateTlsStateArgs<TestDataSourceInstance> args); 37 } 38 39 interface IncrementalStateProvider { provide(CreateIncrementalStateArgs<TestDataSourceInstance> args)40 TestIncrementalState provide(CreateIncrementalStateArgs<TestDataSourceInstance> args); 41 } 42 TestDataSource()43 public TestDataSource() { 44 this((ds, idx, config) -> new TestDataSourceInstance(ds, idx), 45 args -> new TestTlsState(), args -> new TestIncrementalState()); 46 } 47 TestDataSource( DataSourceInstanceProvider dataSourceInstanceProvider, TlsStateProvider tlsStateProvider, IncrementalStateProvider incrementalStateProvider )48 public TestDataSource( 49 DataSourceInstanceProvider dataSourceInstanceProvider, 50 TlsStateProvider tlsStateProvider, 51 IncrementalStateProvider incrementalStateProvider 52 ) { 53 super("android.tracing.perfetto.TestDataSource#" + UUID.randomUUID().toString()); 54 this.mDataSourceInstanceProvider = dataSourceInstanceProvider; 55 this.mTlsStateProvider = tlsStateProvider; 56 this.mIncrementalStateProvider = incrementalStateProvider; 57 } 58 59 @Override createInstance(ProtoInputStream configStream, int instanceIndex)60 public TestDataSourceInstance createInstance(ProtoInputStream configStream, int instanceIndex) { 61 return mDataSourceInstanceProvider.provide(this, instanceIndex, configStream); 62 } 63 64 @Override createTlsState(CreateTlsStateArgs args)65 public TestTlsState createTlsState(CreateTlsStateArgs args) { 66 return mTlsStateProvider.provide(args); 67 } 68 69 @Override createIncrementalState(CreateIncrementalStateArgs args)70 public TestIncrementalState createIncrementalState(CreateIncrementalStateArgs args) { 71 return mIncrementalStateProvider.provide(args); 72 } 73 74 public static class TestTlsState { 75 public int testStateValue; 76 public int stateIndex = lastIndex++; 77 78 public static int lastIndex = 0; 79 } 80 81 public static class TestIncrementalState { 82 public int testStateValue; 83 } 84 85 public static class TestDataSourceInstance extends DataSourceInstance { 86 public Object testObject; 87 Consumer<StartCallbackArguments> mStartCallback; 88 Consumer<FlushCallbackArguments> mFlushCallback; 89 Consumer<StopCallbackArguments> mStopCallback; 90 TestDataSourceInstance(DataSource dataSource, int instanceIndex)91 public TestDataSourceInstance(DataSource dataSource, int instanceIndex) { 92 this(dataSource, instanceIndex, args -> {}, args -> {}, args -> {}); 93 } 94 TestDataSourceInstance( DataSource dataSource, int instanceIndex, Consumer<StartCallbackArguments> startCallback, Consumer<FlushCallbackArguments> flushCallback, Consumer<StopCallbackArguments> stopCallback)95 public TestDataSourceInstance( 96 DataSource dataSource, 97 int instanceIndex, 98 Consumer<StartCallbackArguments> startCallback, 99 Consumer<FlushCallbackArguments> flushCallback, 100 Consumer<StopCallbackArguments> stopCallback) { 101 super(dataSource, instanceIndex); 102 this.mStartCallback = startCallback; 103 this.mFlushCallback = flushCallback; 104 this.mStopCallback = stopCallback; 105 } 106 107 @Override onStart(StartCallbackArguments args)108 public void onStart(StartCallbackArguments args) { 109 this.mStartCallback.accept(args); 110 } 111 112 @Override onFlush(FlushCallbackArguments args)113 public void onFlush(FlushCallbackArguments args) { 114 this.mFlushCallback.accept(args); 115 } 116 117 @Override onStop(StopCallbackArguments args)118 public void onStop(StopCallbackArguments args) { 119 this.mStopCallback.accept(args); 120 } 121 } 122 } 123