• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.loggers;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 
24 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
25 import com.android.tradefed.device.DeviceNotAvailableException;
26 import com.android.tradefed.invoker.IInvocationContext;
27 import com.android.tradefed.result.CollectingTestListener;
28 import com.android.tradefed.result.TestResult;
29 import com.android.tradefed.result.TestRunResult;
30 import com.android.tradefed.targetprep.TargetSetupError;
31 import com.android.tradefed.testtype.AndroidJUnitTest;
32 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
33 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 import java.util.Arrays;
40 import java.util.Collection;
41 
42 /**
43  * Test the device side file logger to ensure we receive the information from the device
44  * instrumentation.
45  */
46 @RunWith(DeviceJUnit4ClassRunner.class)
47 public class DeviceFileLoggerHostTest extends BaseHostJUnit4Test {
48     private static final String TEST_APK = "CollectorDeviceLibTest.apk";
49     private static final String PACKAGE_NAME = "android.device.collectors";
50     private static final String AJUR_RUNNER = "androidx.test.runner.AndroidJUnitRunner";
51 
52     private static final String FILE_LOGGER = "android.device.loggers.LogFileLogger";
53 
54     private RemoteAndroidTestRunner mTestRunner;
55     private IInvocationContext mContext;
56 
57     @Before
setUp()58     public void setUp() throws DeviceNotAvailableException, TargetSetupError {
59         installPackage(TEST_APK);
60         assertTrue(isPackageInstalled(PACKAGE_NAME));
61         mTestRunner =
62                 new RemoteAndroidTestRunner(PACKAGE_NAME, AJUR_RUNNER, getDevice().getIDevice());
63         // Set the new runListener order to ensure test cases can show their metrics.
64         mTestRunner.addInstrumentationArg(AndroidJUnitTest.NEW_RUN_LISTENER_ORDER_KEY, "true");
65         mContext = mock(IInvocationContext.class);
66         doReturn(Arrays.asList(getDevice())).when(mContext).getDevices();
67         doReturn(Arrays.asList(getBuild())).when(mContext).getBuildInfos();
68     }
69 
70     @Test
testFileIsLogged()71     public void testFileIsLogged() throws Exception {
72         mTestRunner.addInstrumentationArg("listener", FILE_LOGGER);
73         mTestRunner.setClassName("android.device.loggers.test.StubInstrumentationAnnotatedTest");
74         CollectingTestListener listener = new CollectingTestListener();
75         assertTrue(getDevice().runInstrumentationTests(mTestRunner, listener));
76 
77         Collection<TestRunResult> results = listener.getRunResults();
78         assertEquals(1, results.size());
79         TestRunResult result = results.iterator().next();
80         assertFalse(result.isRunFailure());
81         assertFalse(result.hasFailedTests());
82         // Check that each test case has results with the metrics associated.
83         for (TestResult res : result.getTestResults().values()) {
84             assertTrue(res.getMetrics().containsKey("fake_file"));
85             assertEquals("/fake/path", res.getMetrics().get("fake_file"));
86         }
87     }
88 }
89