Bug 1064004 - Add Espresso instrumentation test support to Gradle. r?mcomella draft
authorNick Alexander <nalexander@mozilla.com>
Wed, 09 Mar 2016 14:55:33 -0800
changeset 338757 7feb36e1d792de5996b5efddfa886fc90615751a
parent 338756 a068e9be4d853f3e9f5f4e08e985788efbd00cb2
child 515858 d0aa9a6b0eb85a131b5d3812fbb45cda3aebfd00
push id12579
push usernalexander@mozilla.com
push dateWed, 09 Mar 2016 23:56:43 +0000
reviewersmcomella
bugs1064004
milestone48.0a1
Bug 1064004 - Add Espresso instrumentation test support to Gradle. r?mcomella MozReview-Commit-ID: H3mdpemiJ53
mobile/android/app/build.gradle
mobile/android/app/src/androidTest/AndroidManifest.xml
mobile/android/app/src/androidTest/java/org/mozilla/gecko/db/TestTabsProvider.java
mobile/android/config/tooltool-manifests/android-frontend/releng.manifest
mobile/android/tests/browser/junit3/src/org/mozilla/tests/browser/junit3/TestRemoteTabs.java
--- a/mobile/android/app/build.gradle
+++ b/mobile/android/app/build.gradle
@@ -7,17 +7,17 @@ android {
     compileSdkVersion 23
     buildToolsVersion "23.0.1"
 
     defaultConfig {
         targetSdkVersion 23
         minSdkVersion 15 
         applicationId mozconfig.substs.ANDROID_PACKAGE_NAME
         testApplicationId 'org.mozilla.roboexample.test'
-        testInstrumentationRunner 'org.mozilla.gecko.FennecInstrumentationTestRunner'
+        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
         manifestPlaceholders = [
             ANDROID_PACKAGE_NAME: mozconfig.substs.ANDROID_PACKAGE_NAME,
             MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
             MOZ_ANDROID_SHARED_ID: "${mozconfig.substs.ANDROID_PACKAGE_NAME}.sharedID",
         ]
         // Used by Robolectric based tests; see TestRunner.
         buildConfigField 'String', 'BUILD_DIR', "\"${project.buildDir}\""
     }
@@ -126,28 +126,28 @@ android {
 
         test {
             java {
                 srcDir "${topsrcdir}/mobile/android/tests/background/junit4/src"
             }
         }
 
         androidTest {
-            java {
-                srcDir "${topsrcdir}/mobile/android/tests/browser/robocop/src"
-                srcDir "${topsrcdir}/mobile/android/tests/background/junit3/src"
-                srcDir "${topsrcdir}/mobile/android/tests/browser/junit3/src"
-                srcDir "${topsrcdir}/mobile/android/tests/javaddons/src"
-            }
-            res {
-                srcDir "${topsrcdir}/mobile/android/tests/browser/robocop/res"
-            }
-            assets {
-                srcDir "${topsrcdir}/mobile/android/tests/browser/robocop/assets"
-            }
+            // java {
+            //     srcDir "${topsrcdir}/mobile/android/tests/browser/robocop/src"
+            //     srcDir "${topsrcdir}/mobile/android/tests/background/junit3/src"
+            //     srcDir "${topsrcdir}/mobile/android/tests/browser/junit3/src"
+            //     srcDir "${topsrcdir}/mobile/android/tests/javaddons/src"
+            // }
+            // res {
+            //     srcDir "${topsrcdir}/mobile/android/tests/browser/robocop/res"
+            // }
+            // assets {
+            //     srcDir "${topsrcdir}/mobile/android/tests/browser/robocop/assets"
+            // }
         }
     }
 
     testOptions {
         unitTests.all {
             // We'd like to use (Runtime.runtime.availableProcessors()/2), but
             // we have tests that start test servers and the bound ports
             // collide.  We'll fix this soon to have much faster test cycles.
@@ -189,18 +189,24 @@ dependencies {
 
     compile project(':thirdparty')
 
     testCompile 'junit:junit:4.12'
     testCompile 'org.robolectric:robolectric:3.0'
     testCompile 'org.simpleframework:simple-http:6.0.1'
     testCompile 'org.mockito:mockito-core:1.10.19'
 
-    // Including the Robotium JAR directly can cause issues with dexing.
-    androidTestCompile 'com.jayway.android.robotium:robotium-solo:4.3.1'
+    androidTestCompile 'junit:junit:4.12'
+    androidTestCompile 'com.android.support.test:runner:0.4'
+    // Set this dependency to use JUnit 4 rules.
+    androidTestCompile 'com.android.support.test:rules:0.4'
+    // Set this dependency to build and run Espresso tests.
+    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
+    // Set this dependency to build and run UI Automator tests.
+    androidTestLocalCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
 }
 
 task syncOmnijarFromDistDir(type: Sync) {
     into("${project.buildDir}/generated/omnijar")
     from("${topobjdir}/dist/fennec/assets") {
         include 'omni.ja'
     }
 }
@@ -327,39 +333,43 @@ android.applicationVariants.all { varian
 
 apply plugin: 'spoon'
 
 spoon {
     // For now, let's be verbose.
     debug = true
     // It's not helpful to pass when we don't have a device connected.
     failIfNoDeviceConnected = true
+    // For now, wait longer than we'd like.
+    adbTimeout = 10 * 60 // Seconds.  Both for startup and for output during test.  Be chatty!
 
-    def spoonPackageName
-    if (gradle.startParameter.taskNames.contains('runBrowserTests')) {
-        spoonPackageName = 'org.mozilla.tests.browser.junit3'
-    }
-    if (gradle.startParameter.taskNames.contains('runBackgroundTests')) {
-        spoonPackageName = 'org.mozilla.gecko.background'
-    }
-    if (project.hasProperty('spoonPackageName')) {
-        // Command line overrides everything.
-        spoonPackageName = project.spoonPackageName
-    }
-    if (spoonPackageName) {
-        instrumentationArgs = ['-e', "package=${spoonPackageName}".toString()]
-    }
+    // def spoonPackageName
+    // if (gradle.startParameter.taskNames.contains('runBrowserTests')) {
+    //     spoonPackageName = 'org.mozilla.tests.browser.junit3'
+    // }
+    // if (gradle.startParameter.taskNames.contains('runBackgroundTests')) {
+    //     spoonPackageName = 'org.mozilla.gecko.background'
+    // }
+    // if (project.hasProperty('spoonPackageName')) {
+    //     // Command line overrides everything.
+    //     spoonPackageName = project.spoonPackageName
+    // }
+    // if (spoonPackageName) {
+    //     instrumentationArgs = ['-e', "package=${spoonPackageName}".toString()]
+    // }
+
+    // instrumentationArgs = ['-e', 'package=org.mozilla.tests.browser.junit3']
 }
 
-// See discussion at https://github.com/stanfy/spoon-gradle-plugin/issues/9.
-afterEvaluate {
-    tasks["spoonLocal${android.testBuildType.capitalize()}AndroidTest"].outputs.upToDateWhen { false }
+// // See discussion at https://github.com/stanfy/spoon-gradle-plugin/issues/9.
+// afterEvaluate {
+//     tasks["spoonAutomation${android.testBuildType.capitalize()}AndroidTest"].outputs.upToDateWhen { false }
 
-    // This is an awkward way to define different sets of instrumentation tests.
-    // The task name itself is fished at runtime and the package name configured
-    // in the spoon configuration.
-    task runBrowserTests {
-        dependsOn tasks["spoonLocalOldDebugAndroidTest"]
-    }
-    task runBackgroundTests {
-        dependsOn tasks["spoonLocalOldDebugAndroidTest"]
-    }
-}
+//     // This is an awkward way to define different sets of instrumentation tests.
+//     // The task name itself is fished at runtime and the package name configured
+//     // in the spoon configuration.
+//     task runBrowserTests {
+//         dependsOn tasks["spoonAutomationDebugAndroidTest"]
+//     }
+//     task runBackgroundTests {
+//         dependsOn tasks["spoonAutomationDebugAndroidTest"]
+//     }
+// }
--- a/mobile/android/app/src/androidTest/AndroidManifest.xml
+++ b/mobile/android/app/src/androidTest/AndroidManifest.xml
@@ -9,20 +9,16 @@
               android:targetSdkVersion="23"/>
     <!-- TODO: re-instate maxSdkVersion. -->
 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
 
-    <instrumentation
-        android:name="org.mozilla.gecko.FennecInstrumentationTestRunner"
-        android:targetPackage="${ANDROID_PACKAGE_NAME}" />
-
     <application
         android:label="@string/app_name">
 
         <uses-library android:name="android.test.runner" />
 
         <!-- Fake handlers to ensure that we have some share intents to show in our share handler list -->
         <activity android:name="org.mozilla.gecko.RobocopShare1"
                   android:label="Robocop fake activity">
rename from mobile/android/tests/browser/junit3/src/org/mozilla/tests/browser/junit3/TestRemoteTabs.java
rename to mobile/android/app/src/androidTest/java/org/mozilla/gecko/db/TestTabsProvider.java
--- a/mobile/android/tests/browser/junit3/src/org/mozilla/tests/browser/junit3/TestRemoteTabs.java
+++ b/mobile/android/app/src/androidTest/java/org/mozilla/gecko/db/TestTabsProvider.java
@@ -1,35 +1,48 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
-package org.mozilla.tests.browser.junit3;
+package org.mozilla.gecko.db;
 
 import android.content.ContentProviderClient;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.net.Uri;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
 import android.test.InstrumentationTestCase;
 
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.background.db.CursorDumper;
-import org.mozilla.gecko.db.BrowserContract;
-import org.mozilla.gecko.db.LocalTabsAccessor;
-import org.mozilla.gecko.db.RemoteClient;
 import org.mozilla.gecko.sync.repositories.android.BrowserContractHelpers;
 
 import java.util.List;
 
-public class TestRemoteTabs extends InstrumentationTestCase {
+@RunWith(AndroidJUnit4.class)
+public class TestTabsProvider extends InstrumentationTestCase {
     private static final long ONE_DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
     private static final long ONE_WEEK_IN_MILLISECONDS = 7 * ONE_DAY_IN_MILLISECONDS;
     private static final long THREE_WEEKS_IN_MILLISECONDS = 3 * ONE_WEEK_IN_MILLISECONDS;
 
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        // Injecting the Instrumentation instance is required
+        // for your test to run with AndroidJUnitRunner.
+        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
+    }
+
+    @Test
     public void testGetClientsWithoutTabsByRecencyFromCursor() throws Exception {
         final Uri uri = BrowserContractHelpers.CLIENTS_CONTENT_URI;
         final ContentResolver cr = getInstrumentation().getTargetContext().getContentResolver();
         final ContentProviderClient cpc = cr.acquireContentProviderClient(uri);
         final LocalTabsAccessor accessor = new LocalTabsAccessor("test"); // The profile name given doesn't matter.
 
         try {
             // Delete all tabs to begin with.
@@ -124,16 +137,17 @@ public class TestRemoteTabs extends Inst
             } finally {
                 allClients.close();
             }
         } finally {
             cpc.release();
         }
     }
 
+    @Test
     public void testGetRecentRemoteClientsUpToOneWeekOld() throws Exception {
         final Uri uri = BrowserContractHelpers.CLIENTS_CONTENT_URI;
         final Context context = getInstrumentation().getTargetContext();
         final String profileName = GeckoProfile.get(context).getName();
         final ContentResolver cr = context.getContentResolver();
         final ContentProviderClient cpc = cr.acquireContentProviderClient(uri);
         final LocalTabsAccessor accessor = new LocalTabsAccessor(profileName);
 
--- a/mobile/android/config/tooltool-manifests/android-frontend/releng.manifest
+++ b/mobile/android/config/tooltool-manifests/android-frontend/releng.manifest
@@ -30,18 +30,18 @@
 "filename": "java_home-1.7.0-openjdk-1.7.0.85.x86_64.tar.xz",
 "unpack": true
 },
 {
 "algorithm": "sha512",
 "visibility": "public",
 "filename": "jcentral.tar.xz",
 "unpack": true,
-"digest": "b5d85a917785e1c034318f7495fef27a6274b04d8640245726b0cf1331b7ac374f5757868901c3fadd930bf10603173a706be653d769dde8ddfdb8673b143363",
-"size": 38596168
+"digest": "86d5906761715f7c9deeefdf2ded6f7f3e303166261c9a293404208c008863c58a46a3b513ad3a9b881988a9bb1071b5f2e5fb3641768af4d094697d10323f1c",
+"size": 38604136
 },
 {
 "algorithm": "sha512",
 "visibility": "public",
 "filename": "gradle.tar.xz",
 "unpack": true,
 "digest": "ef1d0038da879cc6840fced87671f8f6a18c51375498804f64d21fa48d7089ded4da2be36bd06a1457083e9110e59c0884f1e074dc609d29617c131caea8f234",
 "size": 50542140