Bug 929654 - Part 4: Fail earlier for local testers who can't reach mochi.test. r=nalexander
authorMark Capella <markcapella@twcny.rr.com>
Sat, 04 Oct 2014 01:41:22 -0400
changeset 208748 63061f730d01c8259956572de06d5636dab3969f
parent 208747 b1718e507da7dfc2610e460ff414c4f5fcc09bbf
child 208749 339a9e5c9303863cdbf8df9da0718fb59731f25c
push id9163
push usermarkcapella@twcny.rr.com
push dateSat, 04 Oct 2014 05:41:29 +0000
treeherderfx-team@63061f730d01 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs929654
milestone35.0a1
Bug 929654 - Part 4: Fail earlier for local testers who can't reach mochi.test. r=nalexander
mobile/android/base/tests/BaseRobocopTest.java
mobile/android/base/tests/BaseTest.java
mobile/android/base/tests/UITest.java
--- a/mobile/android/base/tests/BaseRobocopTest.java
+++ b/mobile/android/base/tests/BaseRobocopTest.java
@@ -1,24 +1,28 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.tests;
 
 import java.util.Map;
 
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.Assert;
 import org.mozilla.gecko.FennecInstrumentationTestRunner;
 import org.mozilla.gecko.FennecMochitestAssert;
 import org.mozilla.gecko.FennecNativeDriver;
 import org.mozilla.gecko.FennecTalosAssert;
 
-import org.mozilla.gecko.AppConstants;
-
 import android.app.Activity;
 import android.content.Context;
 import android.os.PowerManager;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
 
 @SuppressWarnings("unchecked")
 public abstract class BaseRobocopTest extends ActivityInstrumentationTestCase2<Activity> {
@@ -106,16 +110,43 @@ public abstract class BaseRobocopTest ex
         } else {
             mAsserter = new FennecMochitestAssert();
         }
         mAsserter.setLogFile(mLogFile);
         mAsserter.setTestName(getClass().getName());
     }
 
     /**
+     * Function to early abort if we can't reach the given HTTP server. Provides local testers
+     * with diagnostic information. Not currently available for TALOS tests, which are rarely run
+     * locally in any case.
+     */
+    public void throwIfHttpGetFails() {
+        if (getTestType() == Type.TALOS) {
+            return;
+        }
+
+        // rawURL to test fetching from. This should be a raw (IP) URL, not an alias
+        // (like mochi.test). We can't (easily) test fetching from the aliases, since
+        // those are managed by Fennec's proxy settings.
+        final String rawUrl = ((String) mConfig.get("rawhost")).replaceAll("(/$)", "");
+
+        try {
+            final HttpClient httpclient = new DefaultHttpClient();
+            final HttpResponse response = httpclient.execute(new HttpGet(rawUrl));
+            final int statusCode = response.getStatusLine().getStatusCode();
+            if (200 != statusCode) {
+                throw new IllegalStateException("Status code: " + statusCode);
+            }
+        } catch (Exception e) {
+            mAsserter.ok(false, "Robocop tests on your device need network/wifi access to reach: [" + rawUrl + "].", e.toString());
+        }
+    }
+
+    /**
      * Ensure that the screen on the test device is powered on during tests.
      */
     public void throwIfScreenNotOn() {
         final PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
         mAsserter.ok(pm.isScreenOn(),
             "Robocop tests need the test device screen to be powered on.", "");
     }
 }
--- a/mobile/android/base/tests/BaseTest.java
+++ b/mobile/android/base/tests/BaseTest.java
@@ -117,17 +117,18 @@ abstract class BaseTest extends BaseRobo
         mActivity = getActivity();
         // Set up Robotium.solo and Driver objects
         mSolo = new Solo(getInstrumentation(), mActivity);
         mDriver = new FennecNativeDriver(mActivity, mSolo, mRootPath);
         mActions = new FennecNativeActions(mActivity, mSolo, getInstrumentation(), mAsserter);
         mDevice = new Device();
         mDatabaseHelper = new DatabaseHelper(mActivity, mAsserter);
 
-        // Ensure Robocop tests are run with Display powered on.
+        // Ensure Robocop tests have access to network, and are run with Display powered on.
+        throwIfHttpGetFails();
         throwIfScreenNotOn();
     }
 
     protected void initializeProfile() {
         final GeckoProfile profile;
         if (mProfile.startsWith("/")) {
             profile = GeckoProfile.get(getActivity(), "default", mProfile);
         } else {
--- a/mobile/android/base/tests/UITest.java
+++ b/mobile/android/base/tests/UITest.java
@@ -70,17 +70,18 @@ abstract class UITest extends BaseRoboco
 
         mBaseHostnameUrl = mConfig.get("host").replaceAll("(/$)", "");
         mBaseIpUrl = mConfig.get("rawhost").replaceAll("(/$)", "");
 
         // Helpers depend on components so initialize them first.
         initComponents();
         initHelpers();
 
-        // Ensure Robocop tests are run with Display powered on.
+        // Ensure Robocop tests have access to network, and are run with Display powered on.
+        throwIfHttpGetFails();
         throwIfScreenNotOn();
     }
 
     @Override
     public void tearDown() throws Exception {
         try {
             mAsserter.endTest();
             // request a force quit of the browser and wait for it to take effect