1 /*
2  * Copyright (C) 2011 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.cts.tradefed.testtype;
17 
18 import com.android.cts.tradefed.build.CtsBuildHelper;
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.log.LogUtil.CLog;
22 import com.android.tradefed.result.ITestInvocationListener;
23 import com.android.tradefed.testtype.IBuildReceiver;
24 import com.android.tradefed.testtype.UiAutomatorTest;
25 
26 import java.io.FileNotFoundException;
27 import java.util.Arrays;
28 
29 import junit.framework.Assert;
30 
31 /**
32  * A {@link UiAutomatorTest} that will install a uiautomator jar before test
33  * execution, and uninstall on execution completion.
34  */
35 public class UiAutomatorJarTest extends UiAutomatorTest implements IBuildReceiver {
36 
37     // TODO: expose this in parent
38     private static final String SHELL_EXE_BASE = "/data/local/tmp/";
39 
40     /** the file names of the CTS jar to install */
41     private String mTestJarFileName;
42 
43     private CtsBuildHelper mCtsBuild = null;
44 
45     /**
46      * {@inheritDoc}
47      */
48     @Override
setBuild(IBuildInfo build)49     public void setBuild(IBuildInfo build) {
50         mCtsBuild  = CtsBuildHelper.createBuildHelper(build);
51     }
52 
53     /**
54      * Setter for CTS build files needed to perform the test.
55      *
56      * @param testJarName the file name of the jar containing the uiautomator tests
57      */
setInstallArtifacts(String testJarName)58     public void setInstallArtifacts(String testJarName) {
59         mTestJarFileName = testJarName;
60     }
61 
62     /**
63      * {@inheritDoc}
64      */
65     @Override
run(final ITestInvocationListener listener)66     public void run(final ITestInvocationListener listener)
67             throws DeviceNotAvailableException {
68         Assert.assertNotNull("missing device", getDevice());
69         Assert.assertNotNull("missing build", mCtsBuild);
70         Assert.assertNotNull("missing jar to install", mTestJarFileName);
71 
72         installJar();
73 
74         super.run(listener);
75 
76         uninstallJar();
77     }
78 
installJar()79     private void installJar() throws DeviceNotAvailableException {
80         CLog.d("Installing %s on %s", mTestJarFileName, getDevice().getSerialNumber());
81         String fullJarPath = String.format("%s%s", SHELL_EXE_BASE, mTestJarFileName);
82         try {
83             boolean result = getDevice().pushFile(mCtsBuild.getTestApp(mTestJarFileName),
84                     fullJarPath);
85             Assert.assertTrue(String.format("Failed to push file to %s", fullJarPath), result);
86             setTestJarPaths(Arrays.asList(fullJarPath));
87         }  catch (FileNotFoundException e) {
88             Assert.fail(String.format("Could not find file %s", mTestJarFileName));
89         }
90     }
91 
uninstallJar()92     private void uninstallJar() throws DeviceNotAvailableException {
93         CLog.d("Uninstalling %s on %s", mTestJarFileName, getDevice().getSerialNumber());
94         String fullJarPath = String.format("%s%s", SHELL_EXE_BASE, mTestJarFileName);
95         getDevice().executeShellCommand(String.format("rm %s", fullJarPath));
96     }
97 }
98