1 /* 2 * Copyright (C) 2018 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 com.android.server.wm.flicker.monitor; 18 19 import static android.os.SystemClock.sleep; 20 import static android.surfaceflinger.nano.Layerstrace.LayersTraceFileProto.MAGIC_NUMBER_H; 21 import static android.surfaceflinger.nano.Layerstrace.LayersTraceFileProto.MAGIC_NUMBER_L; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import android.surfaceflinger.nano.Layerstrace.LayersTraceFileProto; 26 27 import com.google.common.io.Files; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.junit.Test; 32 33 import java.io.File; 34 35 /** 36 * Contains {@link LayersTraceMonitor} tests. 37 * To run this test: {@code atest FlickerLibTest:LayersTraceMonitorTest} 38 */ 39 public class LayersTraceMonitorTest { 40 private LayersTraceMonitor mLayersTraceMonitor; 41 42 @Before 43 public void setup() { 44 mLayersTraceMonitor = new LayersTraceMonitor(); 45 } 46 47 @After 48 public void teardown() { 49 mLayersTraceMonitor.stop(); 50 mLayersTraceMonitor.getOutputTraceFilePath("captureLayersTrace", 0).toFile().delete(); 51 } 52 53 @Test 54 public void canStartLayersTrace() throws Exception { 55 mLayersTraceMonitor.start(); 56 assertThat(mLayersTraceMonitor.isEnabled()).isTrue(); 57 } 58 59 @Test 60 public void canStopLayersTrace() throws Exception { 61 mLayersTraceMonitor.start(); 62 assertThat(mLayersTraceMonitor.isEnabled()).isTrue(); 63 mLayersTraceMonitor.stop(); 64 assertThat(mLayersTraceMonitor.isEnabled()).isFalse(); 65 } 66 67 @Test 68 public void captureLayersTrace() throws Exception { 69 mLayersTraceMonitor.start(); 70 mLayersTraceMonitor.stop(); 71 File testFile = mLayersTraceMonitor.saveTraceFile("captureLayersTrace", 0).toFile(); 72 assertThat(testFile.exists()).isTrue(); 73 byte[] trace = Files.toByteArray(testFile); 74 assertThat(trace.length).isGreaterThan(0); 75 LayersTraceFileProto mLayerTraceFileProto = LayersTraceFileProto.parseFrom(trace); 76 assertThat(mLayerTraceFileProto.magicNumber).isEqualTo( 77 (long) MAGIC_NUMBER_H << 32 | MAGIC_NUMBER_L); 78 } 79 } 80