• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package android.host.retaildemo;
17 
18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
19 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
20 import com.android.ddmlib.testrunner.TestIdentifier;
21 import com.android.ddmlib.testrunner.TestResult;
22 import com.android.ddmlib.testrunner.TestResult.TestStatus;
23 import com.android.ddmlib.testrunner.TestRunResult;
24 import com.android.tradefed.build.IBuildInfo;
25 import com.android.tradefed.device.DeviceNotAvailableException;
26 import com.android.tradefed.log.LogUtil.CLog;
27 import com.android.tradefed.result.CollectingTestListener;
28 import com.android.tradefed.testtype.DeviceTestCase;
29 import com.android.tradefed.testtype.IBuildReceiver;
30 
31 import java.io.File;
32 import java.io.FileNotFoundException;
33 import java.util.ArrayList;
34 import java.util.Map;
35 
36 public class BaseTestCase extends DeviceTestCase implements IBuildReceiver {
37     private static final String RETAIL_DEMO_TEST_PKG = "com.android.cts.retaildemo";
38     private static final String RUNNER = "android.support.test.runner.AndroidJUnitRunner";
39 
40     private IBuildInfo mBuildInfo;
41     private CompatibilityBuildHelper mBuildHelper;
42 
43     private ArrayList<Integer> mTestUsers;
44 
45     @Override
setBuild(IBuildInfo buildInfo)46     public void setBuild(IBuildInfo buildInfo) {
47         mBuildInfo = buildInfo;
48         mBuildHelper = new CompatibilityBuildHelper(mBuildInfo);
49     }
50 
51     @Override
setUp()52     protected void setUp() throws Exception {
53         super.setUp();
54         assertNotNull(mBuildInfo); // ensure build has been set before test is run.
55         mTestUsers = new ArrayList<>();
56     }
57 
58     @Override
tearDown()59     protected void tearDown() throws Exception {
60         for (int userId : mTestUsers) {
61             getDevice().removeUser(userId);
62         }
63         super.tearDown();
64     }
65 
createDemoUser()66     protected int createDemoUser() throws DeviceNotAvailableException, IllegalStateException {
67         final String command = "pm create-user --ephemeral --demo "
68                 + "TestUser_" + System.currentTimeMillis();
69         CLog.d("Starting command: " + command);
70         final String output = getDevice().executeShellCommand(command);
71         CLog.d("Output for command " + command + ": " + output);
72 
73         if (output.startsWith("Success")) {
74             try {
75                 int userId = Integer.parseInt(output.substring(output.lastIndexOf(" ")).trim());
76                 mTestUsers.add(userId);
77                 return userId;
78             } catch (NumberFormatException e) {
79                 CLog.e("Failed to parse result: %s", output);
80             }
81         } else {
82             CLog.e("Failed to create demo user: %s", output);
83         }
84         throw new IllegalStateException();
85     }
86 
installAppAsUser(String appFileName, int userId)87     protected void installAppAsUser(String appFileName, int userId)
88             throws FileNotFoundException, DeviceNotAvailableException {
89         CLog.d("Installing app " + appFileName + " for user " + userId);
90         File apkFile = new File(mBuildHelper.getTestsDir(), appFileName);
91         final String result = getDevice().installPackageForUser(
92                 apkFile, true, true, userId, "-t");
93         assertNull("Failed to install " + appFileName + " for user " + userId + ": " + result,
94                 result);
95     }
96 
runDeviceTestsAsUser(String testClassName, String testMethodName, int userId)97     protected boolean runDeviceTestsAsUser(String testClassName, String testMethodName, int userId)
98             throws Exception {
99         if (testClassName != null && testClassName.startsWith(".")) {
100             testClassName = RETAIL_DEMO_TEST_PKG + testClassName;
101         }
102 
103         RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(
104                 RETAIL_DEMO_TEST_PKG, RUNNER, getDevice().getIDevice());
105         if (testClassName != null && testMethodName != null) {
106             testRunner.setMethodName(testClassName, testMethodName);
107         } else if (testClassName != null) {
108             testRunner.setClassName(testClassName);
109         }
110 
111         CollectingTestListener listener = new CollectingTestListener();
112         assertTrue(getDevice().runInstrumentationTestsAsUser(testRunner, userId, listener));
113 
114         TestRunResult runResult = listener.getCurrentRunResults();
115         printTestResult(runResult);
116         return !runResult.hasFailedTests() && runResult.getNumTestsInState(TestStatus.PASSED) > 0;
117     }
118 
printTestResult(TestRunResult runResult)119     private void printTestResult(TestRunResult runResult) {
120         for (Map.Entry<TestIdentifier, TestResult> testEntry :
121                 runResult.getTestResults().entrySet()) {
122             TestResult testResult = testEntry.getValue();
123             CLog.d("Test " + testEntry.getKey() + ": " + testResult.getStatus());
124             if (testResult.getStatus() != TestStatus.PASSED) {
125                 CLog.d(testResult.getStackTrace());
126             }
127         }
128     }
129 }
130