Code cleanup: EndToEndTestRunner and RunConfiguration

This commit is contained in:
Jimmy Shih
2014-01-28 11:11:30 -08:00
parent e8c9661776
commit bb5758f5bd
9 changed files with 140 additions and 69 deletions
@@ -13,6 +13,7 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.google.android.apps.mytracks.endtoendtest; package com.google.android.apps.mytracks.endtoendtest;
import android.os.Bundle; import android.os.Bundle;
@@ -20,38 +21,45 @@ import android.test.InstrumentationTestRunner;
import android.util.Log; import android.util.Log;
/** /**
* A test runner can be used to run end-to-end test. * End-to-end test test runner.
* *
* @author Youtao Liu * @author Youtao Liu
*/ */
public class EndToEndTestRunner extends InstrumentationTestRunner { public class EndToEndTestRunner extends InstrumentationTestRunner {
/** private static final String PORT_KEY = "port";
* Gets the parameter port which is used to fix GPS signal to emulators. private static final String RESOURCE_KEY = "resource";
*/ private static final String SENSOR_KEY = "sensor";
private static final String STRESS_KEY = "stress";
private static final String TRUE_VALUE = "true";
@Override @Override
public void onCreate(Bundle arguments) { public void onCreate(Bundle bundle) {
String portNumber = arguments.getString("port");
if (portNumber != null) { // Get the emulator port parameter for GPS signals
String port = bundle.getString(PORT_KEY);
if (port != null) {
try { try {
EndToEndTestUtils.emulatorPort = Integer.parseInt(portNumber); EndToEndTestUtils.emulatorPort = Integer.parseInt(port);
} catch (Exception e) { } catch (Exception e) {
Log.e(EndToEndTestUtils.LOG_TAG, "Unable to get port parameter, use default value." Log.e(EndToEndTestUtils.LOG_TAG,
+ EndToEndTestUtils.emulatorPort, e); "Unable to get emulator port parameter, use the default value", e);
} }
} }
Log.i(EndToEndTestUtils.LOG_TAG, "Emulator port: " + EndToEndTestUtils.emulatorPort);
RunConfiguration.runStressTest = "true".equalsIgnoreCase(arguments.getString("stress")); RunConfiguration runConfiguration = RunConfiguration.getInstance();
RunConfiguration.runSensorTest = "true".equalsIgnoreCase(arguments.getString("sensor"));
RunConfiguration.runResourceUsageTest = "true"
.equalsIgnoreCase(arguments.getString("resource"));
Log.d(EndToEndTestUtils.LOG_TAG, "Use port number when run test on emulator:" runConfiguration.setRunResourceTest(
+ EndToEndTestUtils.emulatorPort); TRUE_VALUE.equalsIgnoreCase(bundle.getString(RESOURCE_KEY)));
Log.i(EndToEndTestUtils.LOG_TAG, "Run stress test:" + RunConfiguration.runStressTest); Log.i(EndToEndTestUtils.LOG_TAG, "Run resource test: " + runConfiguration.getRunResourceTest());
Log.i(EndToEndTestUtils.LOG_TAG, "Run sensor test:" + RunConfiguration.runSensorTest);
super.onCreate(arguments); runConfiguration.setRunSensorTest(TRUE_VALUE.equalsIgnoreCase(bundle.getString(SENSOR_KEY)));
Log.i(EndToEndTestUtils.LOG_TAG, "Run sensor test: " + runConfiguration.getRunSensorTest());
runConfiguration.setRunStressTest(TRUE_VALUE.equalsIgnoreCase(bundle.getString(STRESS_KEY)));
Log.i(EndToEndTestUtils.LOG_TAG, "Run stress test: " + runConfiguration.getRunStressTest());
super.onCreate(bundle);
} }
} }
@@ -13,30 +13,23 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.google.android.apps.mytracks.endtoendtest; package com.google.android.apps.mytracks.endtoendtest;
import com.google.android.maps.mytracks.R;
/** /**
* Places all the test configuration in this class. * Run configuration for various tests.
* *
* @author Youtao Liu * @author Youtao Liu
*/ */
public class RunConfiguration { public class RunConfiguration {
private static RunConfiguration instance = null;
private static RunConfiguration instance;
/** /**
* Set to false as default. True to run the test. Default to false since this * Gets an instance of {@link RunConfiguration}.
* test can take a long time.
*/ */
public static boolean runStressTest = false;
public static boolean runResourceUsageTest = false;
public static boolean runSensorTest = false;
public boolean runSyncTest = false;
private RunConfiguration() {
runSyncTest = canRunSyncTest();
}
public static RunConfiguration getInstance() { public static RunConfiguration getInstance() {
if (instance == null) { if (instance == null) {
instance = new RunConfiguration(); instance = new RunConfiguration();
@@ -44,27 +37,97 @@ public class RunConfiguration {
return instance; return instance;
} }
/** /*
* Runs sync tests when both test accounts are bound with the devices. * Set to false as default. True to run the test. Default to false since these
* * tests can take a long time.
* @return true means can run sync tests in this device
*/ */
public static boolean canRunSyncTest() { private boolean runResourceTest = false;
return false; private boolean runSensorTest = false;
// EndToEndTestUtils.findMenuItem( private boolean runStressTest = false;
// EndToEndTestUtils.activityMytracks.getString(R.string.menu_settings), true); private boolean runSyncTest = false;
// EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
// .getString(R.string.settings_google)); private RunConfiguration() {
// EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // Sets run sync test in run-time
// .getString(R.string.settings_google_account_title)); setRunSyncTest(canRunSyncTest());
// boolean canRunSyncE2ETest = EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_1, 1, }
// EndToEndTestUtils.SHORT_WAIT_TIME)
// && EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_2, 1, /**
// EndToEndTestUtils.TINY_WAIT_TIME); * Returns true to run the resource test.
// EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks */
// .getString(R.string.generic_cancel)); public boolean getRunResourceTest() {
// EndToEndTestUtils.SOLO.goBack(); return runResourceTest;
// EndToEndTestUtils.SOLO.goBack(); }
// return canRunSyncE2ETest;
/**
* Sets run resource test.
*
* @param run true to run
*/
public void setRunResourceTest(boolean run) {
runResourceTest = run;
}
/**
* Returns true to run the sensor test.
*/
public boolean getRunSensorTest() {
return runSensorTest;
}
/**
* Sets run sensor test.
*
* @param run true to run
*/
public void setRunSensorTest(boolean run) {
runSensorTest = run;
}
/**
* Returns true to run the stress test.
*/
public boolean getRunStressTest() {
return runStressTest;
}
/**
* Sets run stress test.
*
* @param run true to run
*/
public void setRunStressTest(boolean run) {
runStressTest = run;
}
/**
* Returns true to run the sync test.
*/
public boolean getRunSyncTest() {
return runSyncTest;
}
/**
* Sets run sync test.
*
* @param run true to run
*/
public void setRunSyncTest(boolean run) {
runSyncTest = run;
}
/**
* Returns true if can run the sync test. Needs two specific test accounts on
* the device.
*/
private boolean canRunSyncTest() {
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_drive), true);
boolean canRun = EndToEndTestUtils.SOLO.waitForText(
GoogleUtils.ACCOUNT_NAME_1, 1, EndToEndTestUtils.SHORT_WAIT_TIME)
&& EndToEndTestUtils.SOLO.waitForText(
GoogleUtils.ACCOUNT_NAME_2, 1, EndToEndTestUtils.TINY_WAIT_TIME);
EndToEndTestUtils.SOLO.clickOnText(
EndToEndTestUtils.activityMytracks.getString(R.string.generic_cancel));
return canRun;
} }
} }
@@ -55,7 +55,7 @@ public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackLis
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
runTest = RunConfiguration.runResourceUsageTest; runTest = RunConfiguration.getInstance().getRunResourceTest();
if (!runTest) { if (!runTest) {
return; return;
} }
@@ -47,7 +47,7 @@ public class SensorTest extends ActivityInstrumentationTestCase2<TrackListActivi
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
runTest = RunConfiguration.runSensorTest; runTest = RunConfiguration.getInstance().getRunSensorTest();
super.setUp(); super.setUp();
if (!runTest) { if (!runTest) {
return; return;
@@ -48,7 +48,7 @@ public class StressTest extends ActivityInstrumentationTestCase2<TrackListActivi
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
runTest = RunConfiguration.runStressTest; runTest = RunConfiguration.getInstance().getRunStressTest();
super.setUp(); super.setUp();
if (!runTest) { if (!runTest) {
return; return;
@@ -68,7 +68,7 @@ public class MultiAccountsSyncTest extends ActivityInstrumentationTestCase2<Trac
* </ul> * </ul>
*/ */
public void testSyncTracksWithMultiAccounts() throws Exception { public void testSyncTracksWithMultiAccounts() throws Exception {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1); SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1);
@@ -111,7 +111,7 @@ public class MultiAccountsSyncTest extends ActivityInstrumentationTestCase2<Trac
* </ul> * </ul>
*/ */
public void testDeleteTracksWithMultiAccounts() throws Exception { public void testDeleteTracksWithMultiAccounts() throws Exception {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
EndToEndTestUtils.createSimpleTrack(0, true); EndToEndTestUtils.createSimpleTrack(0, true);
@@ -61,7 +61,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
* @throws IOException * @throws IOException
*/ */
public void testDeleteAllTracksInMyTracks() throws IOException { public void testDeleteAllTracksInMyTracks() throws IOException {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
EndToEndTestUtils.findMenuItem( EndToEndTestUtils.findMenuItem(
@@ -75,7 +75,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
* @throws IOException * @throws IOException
*/ */
public void testDeleteOneTracksInMyTracks() throws IOException { public void testDeleteOneTracksInMyTracks() throws IOException {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
EndToEndTestUtils.createTrackIfEmpty(2, false); EndToEndTestUtils.createTrackIfEmpty(2, false);
@@ -95,7 +95,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
* @throws IOException * @throws IOException
*/ */
public void testCreateTracksInMyTracks() throws IOException { public void testCreateTracksInMyTracks() throws IOException {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
EndToEndTestUtils.createSimpleTrack(0, true); EndToEndTestUtils.createSimpleTrack(0, true);
@@ -111,7 +111,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
* @throws IOException * @throws IOException
*/ */
public void testEditTrackInMyTracks() throws IOException { public void testEditTrackInMyTracks() throws IOException {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
EndToEndTestUtils.createTrackIfEmpty(3, true); EndToEndTestUtils.createTrackIfEmpty(3, true);
@@ -61,7 +61,7 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2<
* @throws IOException * @throws IOException
*/ */
public void testDeleteAllTracksOnDrive() throws IOException { public void testDeleteAllTracksOnDrive() throws IOException {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
EndToEndTestUtils.createTrackIfEmpty(0, true); EndToEndTestUtils.createTrackIfEmpty(0, true);
@@ -81,7 +81,7 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2<
* @throws IOException * @throws IOException
*/ */
public void testDeleteOneFileOnDrive() throws IOException { public void testDeleteOneFileOnDrive() throws IOException {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
@@ -106,7 +106,7 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2<
* @throws IOException * @throws IOException
*/ */
public void testCreateMyTracksOnDrive() throws IOException { public void testCreateMyTracksOnDrive() throws IOException {
if (!RunConfiguration.getInstance().runSyncTest) { if (!RunConfiguration.getInstance().getRunSyncTest()) {
return; return;
} }
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
@@ -65,13 +65,13 @@ public class SyncTestUtils {
*/ */
public static Drive setUpForSyncTest(Instrumentation instrumentation, public static Drive setUpForSyncTest(Instrumentation instrumentation,
TrackListActivity trackListActivity) throws IOException, GoogleAuthException { TrackListActivity trackListActivity) throws IOException, GoogleAuthException {
if (!isCheckedRunSyncTest || RunConfiguration.getInstance().runSyncTest) { if (!isCheckedRunSyncTest || RunConfiguration.getInstance().getRunSyncTest()) {
EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity); EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity);
} }
if (!isCheckedRunSyncTest) { if (!isCheckedRunSyncTest) {
isCheckedRunSyncTest = true; isCheckedRunSyncTest = true;
} }
if (RunConfiguration.getInstance().runSyncTest) { if (RunConfiguration.getInstance().getRunSyncTest()) {
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1); SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1);
Drive drive1 = SyncTestUtils.getGoogleDrive(EndToEndTestUtils.activityMytracks Drive drive1 = SyncTestUtils.getGoogleDrive(EndToEndTestUtils.activityMytracks
.getApplicationContext()); .getApplicationContext());