1 /* 2 * Copyright (C) 2016 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.sample.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestMetrics; 25 import com.android.tradefed.testtype.IDeviceTest; 26 27 import org.junit.Rule; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.util.Scanner; 32 33 /** 34 * Test to check the APK logs to Logcat. 35 * 36 * When this test builds, it also builds {@link android.sample.app.SampleDeviceActivity} into an 37 * APK which it then installed at runtime and started. The activity simply prints a message to 38 * Logcat and then gets uninstalled. 39 * 40 * Instead of extending DeviceTestCase, this JUnit4 test extends IDeviceTest and is run with 41 * tradefed's DeviceJUnit4ClassRunner 42 */ 43 @RunWith(DeviceJUnit4ClassRunner.class) 44 public class SampleHostJUnit4Test implements IDeviceTest { 45 46 /** 47 * The package name of the APK. 48 */ 49 private static final String PACKAGE = "android.sample.app"; 50 51 /** 52 * The class name of the main activity in the APK. 53 */ 54 private static final String CLASS = "SampleDeviceActivity"; 55 56 /** 57 * The command to launch the main activity. 58 */ 59 private static final String START_COMMAND = String.format( 60 "am start -W -a android.intent.action.MAIN -n %s/%s.%s", PACKAGE, PACKAGE, CLASS); 61 62 /** 63 * The command to clear the main activity. 64 */ 65 private static final String CLEAR_COMMAND = String.format("pm clear %s", PACKAGE); 66 67 /** 68 * The test string to look for. 69 */ 70 private static final String TEST_STRING = "SampleTestString"; 71 72 /** 73 * A rule annotation that allows to log metrics in test cases. 74 */ 75 @Rule public TestMetrics mMetrics = new TestMetrics(); 76 77 private ITestDevice mDevice; 78 79 @Override setDevice(ITestDevice device)80 public void setDevice(ITestDevice device) { 81 mDevice = device; 82 } 83 84 @Override getDevice()85 public ITestDevice getDevice() { 86 return mDevice; 87 } 88 89 /** 90 * Tests the string was successfully logged to Logcat from the activity. 91 * 92 * @throws Exception 93 */ 94 @Test testLogcat()95 public void testLogcat() throws Exception { 96 ITestDevice device = getDevice(); 97 assertNotNull("Device not set", device); 98 // Clear activity 99 device.executeShellCommand(CLEAR_COMMAND); 100 // Clear logcat. 101 device.executeAdbCommand("logcat", "-c"); 102 // Start the APK and wait for it to complete. 103 device.executeShellCommand(START_COMMAND); 104 // Dump logcat. 105 String logs = device.executeAdbCommand("logcat", "-v", "brief", "-d", CLASS + ":I", "*:S"); 106 // Search for string. 107 String testString = ""; 108 Scanner in = new Scanner(logs); 109 while (in.hasNextLine()) { 110 String line = in.nextLine(); 111 if(line.startsWith("I/"+CLASS)) { 112 testString = line.split(":")[1].trim(); 113 } 114 } 115 in.close(); 116 // Assert the logged string matches the test string. 117 assertEquals("Incorrect test string", TEST_STRING, testString); 118 } 119 120 /** 121 * Documentation: https://source.android.com/devices/tech/test_infra/tradefed/testing/through-tf/report-metrics 122 */ 123 @Test testMetrics()124 public void testMetrics() { 125 mMetrics.addTestMetric("somekey", "some_values"); 126 } 127 } 128