1 /*
2  * Copyright (C) 2010 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.tradefed.device;
17 
18 import com.android.ddmlib.IDevice;
19 import com.android.ddmlib.Log;
20 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
21 import com.android.ddmlib.testrunner.TestResult.TestStatus;
22 import com.android.tradefed.TestAppConstants;
23 import com.android.tradefed.config.Option;
24 import com.android.tradefed.result.CollectingTestListener;
25 import com.android.tradefed.testtype.DeviceTestCase;
26 import com.android.tradefed.util.FileUtil;
27 
28 import java.io.File;
29 import java.io.IOException;
30 
31 /**
32  * Long running functional tests for {@link TestDevice} that verify an operation can be run
33  * many times in sequence
34  * <p/>
35  * Requires a physical device to be connected.
36  */
37 public class TestDeviceStressTest extends DeviceTestCase {
38 
39     @Option(name = "iterations", description = "number of iterations to test")
40     private int mIterations = 50;
41 
42     @Option(name = "stop-on-failure", description =
43             "stops the rest of the iteration on a failure")
44     private boolean mStopOnFailure = true;
45 
46     private static final String LOG_TAG = "TestDeviceStressTest";
47     private static final int TEST_FILE_COUNT= 200;
48     private TestDevice mTestDevice;
49     private IDeviceStateMonitor mMonitor;
50 
51     @Override
setUp()52     protected void setUp() throws Exception {
53         super.setUp();
54         mTestDevice = (TestDevice)getDevice();
55         mMonitor = mTestDevice.getDeviceStateMonitor();
56     }
57 
createTempTestFiles()58     private File createTempTestFiles() throws IOException {
59         File tmpDir = null;
60         File tmpFile = null;
61 
62         tmpDir = FileUtil.createTempDir("testDir");
63 
64         final String fileContents = "this is the test file contents";
65         for (int i = 0; i < TEST_FILE_COUNT; i++) {
66             tmpFile = FileUtil.createTempFile(String.format("tmp_%d", i), ".txt", tmpDir);
67             FileUtil.writeToFile(fileContents, tmpFile);
68         }
69         return tmpDir;
70 
71     }
72 
testManyReboots()73     public void testManyReboots() throws DeviceNotAvailableException {
74         for (int i=0; i < mIterations; i++) {
75             Log.i(LOG_TAG, String.format("testReboot attempt %d", i));
76             mTestDevice.reboot();
77             assertEquals(TestDeviceState.ONLINE, mMonitor.getDeviceState());
78         }
79     }
80 
testManyRebootBootloaders()81     public void testManyRebootBootloaders() throws DeviceNotAvailableException {
82         for (int i=0; i < mIterations; i++) {
83             Log.i(LOG_TAG, String.format("testRebootBootloader attempt %d", i));
84             mTestDevice.rebootIntoBootloader();
85             assertEquals(TestDeviceState.FASTBOOT, mMonitor.getDeviceState());
86             mTestDevice.reboot();
87             assertEquals(TestDeviceState.ONLINE, mMonitor.getDeviceState());
88         }
89     }
90 
testManyDisableKeyguard()91     public void testManyDisableKeyguard() throws DeviceNotAvailableException {
92         int passed = 0;
93         boolean iterationPassed;
94         for (int i=0; i < mIterations; i++) {
95             Log.i(LOG_TAG, String.format("testDisableKeyguard attempt %d", i));
96             mTestDevice.reboot();
97             iterationPassed = runUITests();
98             if (mStopOnFailure){
99                 assertTrue(iterationPassed);
100             }
101             passed += iterationPassed? 1 : 0;
102         }
103         assertEquals(mIterations, passed);
104     }
105 
106     /**
107      * Stress test to push a folder which contains 200 text file to device
108      * internal storage.
109      */
testPushFolderWithManyFiles()110     public void testPushFolderWithManyFiles() throws IOException, DeviceNotAvailableException {
111         File tmpDir = null;
112         String deviceFilePath = null;
113         String externalStorePath = mTestDevice.getMountPoint(IDevice.MNT_EXTERNAL_STORAGE);
114         assertNotNull(externalStorePath);
115         deviceFilePath = String.format("%s/%s", externalStorePath, "testDir");
116 
117         // start the stress test
118         try {
119             // Create the test folder and make sure the test folder doesn't exist in
120             // device before the test start.
121             tmpDir = createTempTestFiles();
122             for (int i = 0; i < mIterations; i++) {
123                 mTestDevice.deleteFile(deviceFilePath);
124                 assertFalse(String.format("%s exists", deviceFilePath),
125                         mTestDevice.doesFileExist(deviceFilePath));
126                 assertTrue(mTestDevice.pushDir(tmpDir, deviceFilePath));
127                 assertTrue(mTestDevice.doesFileExist(deviceFilePath));
128             }
129         } finally {
130             if (tmpDir != null) {
131                 FileUtil.recursiveDelete(tmpDir);
132             }
133             mTestDevice.deleteFile(deviceFilePath);
134             assertFalse(String.format("%s exists", deviceFilePath),
135                     mTestDevice.doesFileExist(deviceFilePath));
136         }
137     }
138 
139     /**
140      * Test to sync a host side folder to device
141      */
testSyncDataWithManyFiles()142     public void testSyncDataWithManyFiles() throws IOException, DeviceNotAvailableException {
143         File tmpDir = null;
144         String deviceFilePath = null;
145         String externalStorePath = mTestDevice.getMountPoint(IDevice.MNT_EXTERNAL_STORAGE);
146         assertNotNull(externalStorePath);
147         deviceFilePath = String.format("%s/%s", externalStorePath, "testDir");
148 
149         // start the stress test
150         try {
151             // Create the test folder and make sure the test folder doesn't exist in
152             // device before the test start.
153             tmpDir = createTempTestFiles();
154             assertTrue(String.format(
155                     "failed to sync test data from local-data-path %s to %s on device %s",
156                     tmpDir.getAbsolutePath(), deviceFilePath, mTestDevice.getSerialNumber()),
157                     mTestDevice.syncFiles(tmpDir, deviceFilePath));
158         } finally {
159             if (tmpDir != null) {
160                 FileUtil.recursiveDelete(tmpDir);
161             }
162             mTestDevice.deleteFile(deviceFilePath);
163             assertFalse(String.format("%s exists", deviceFilePath),
164                     mTestDevice.doesFileExist(deviceFilePath));
165         }
166     }
167 
168     /**
169      * Run the test app UI tests and return true if they all pass.
170      */
runUITests()171     private boolean runUITests() throws DeviceNotAvailableException {
172         RemoteAndroidTestRunner uirunner = new RemoteAndroidTestRunner(
173                 TestAppConstants.UITESTAPP_PACKAGE, getDevice().getIDevice());
174         CollectingTestListener uilistener = new CollectingTestListener();
175         getDevice().runInstrumentationTests(uirunner, uilistener);
176         return TestAppConstants.UI_TOTAL_TESTS == uilistener.getNumTestsInState(TestStatus.PASSED);
177     }
178 
179     /**
180      * Return the number of iterations.
181      * <p/>
182      * Exposed for unit testing
183      */
getIterations()184     public int getIterations() {
185         return mIterations;
186     }
187 
188     /**
189      * Set the iterations
190      */
setIterations(int iterations)191     void setIterations(int iterations) {
192         mIterations = iterations;
193     }
194 }
195