Bug 715369 - Work around Solo.getCurrentActivity() possibly returning the wrong object. r=jmaher
authorKartikaya Gupta <kgupta@mozilla.com>
Thu, 05 Jan 2012 21:36:17 -0500
changeset 85139 f5afa1e3faa9a5b96f645acb34c963087188c0ff
parent 85138 aae720a3208821564bd7ceed7a049f6c79d67c6f
child 85140 6c3cea75e5bac20af1850f1de0d73876a5797806
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmaher
bugs715369
milestone12.0a1
Bug 715369 - Work around Solo.getCurrentActivity() possibly returning the wrong object. r=jmaher
build/mobile/robocop/Driver.java.in
build/mobile/robocop/FennecNativeDriver.java.in
build/mobile/robocop/FennecNativeElement.java.in
mobile/android/base/tests/BaseTest.java.in
mobile/android/base/tests/testNewTab.java.in
--- a/build/mobile/robocop/Driver.java.in
+++ b/build/mobile/robocop/Driver.java.in
@@ -33,27 +33,30 @@
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package @ANDROID_PACKAGE_NAME@;
+
 import java.util.List;
+import android.app.Activity;
 
 public interface Driver {
   /**
    * Find the first Element using the given method.
    * 
+   * @param activity The activity the element belongs to
    * @param name The name of the element
    * @return The first matching element on the current context
    * @throws RoboCopException If no matching elements are found
    */
-  Element findElement(String name);
+  Element findElement(Activity activity, String name);
 
   /**
    * Sets up scroll handling so that data is received from the extension.
    */
   void setupScrollHandling();
 
   int getPageHeight();
   int getScrollHeight();
--- a/build/mobile/robocop/FennecNativeDriver.java.in
+++ b/build/mobile/robocop/FennecNativeDriver.java.in
@@ -162,18 +162,17 @@ public class FennecNativeDriver implemen
   }
   public int getGeckoWidth() {
     if(!geckoInfo) {
       getGeckoInfo();
     }
     return geckoWidth;
   }
 
-  @Override
-  public Element findElement(String name) {
+  public Element findElement(Activity activity, String name) {
     if (name == null)
       throw new IllegalArgumentException("Can not findElements when passed a null");
     if (locators.containsKey(name)){
       return new FennecNativeElement(Integer.decode((String)locators.get(name)), activity, solo);
     }
     throw new RoboCopException("Element does not exist in the list");
   }
 
--- a/build/mobile/robocop/FennecNativeElement.java.in
+++ b/build/mobile/robocop/FennecNativeElement.java.in
@@ -51,64 +51,59 @@ import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.TextSwitcher;
 import android.app.Instrumentation;
 import android.util.Log;
 import com.jayway.android.robotium.solo.Solo;
 import java.util.concurrent.SynchronousQueue;
 
 public class FennecNativeElement implements Element {
+  private final Activity mActivity;
   private Integer id;
-  private Activity currentActivity;
   private Solo robocop;
 
   public FennecNativeElement(Integer id, Activity activity, Solo solo){
     this.id = id;
+    mActivity = activity;
     robocop = solo;
-    currentActivity = activity;
   }
 
   public Integer getId() {
     return id;
   }
 
-  @Override
   public void click() {
     final SynchronousQueue syncQueue = new SynchronousQueue();
-    currentActivity = robocop.getCurrentActivity();
-    currentActivity.runOnUiThread(
+    mActivity.runOnUiThread(
         new Runnable() {
           public void run() {
-            View view = (View)currentActivity.findViewById(id);
+            View view = (View)mActivity.findViewById(id);
             if(view != null) {
               view.performClick();
             } else {
               throw new RoboCopException("click: unable to find view "+id); 
             }
             syncQueue.offer(new Object());
           }
         });
     try {
       syncQueue.take();
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
 
   private Object text;
-  private Activity elementActivity;
 
-  @Override
   public String getText() {
-    elementActivity = robocop.getCurrentActivity();
     final SynchronousQueue syncQueue = new SynchronousQueue();
-    elementActivity.runOnUiThread(
+    mActivity.runOnUiThread(
         new Runnable() {
           public void run() {
-            View v = elementActivity.findViewById(id);
+            View v = mActivity.findViewById(id);
             if(v instanceof EditText) {
               EditText et = (EditText)v;
               text = et.getEditableText();
             }else if(v instanceof TextSwitcher) {
               TextSwitcher ts = (TextSwitcher)v;
               ts.getNextView();
               text = ((TextView)ts.getCurrentView()).getText();
             }else if(v instanceof ViewGroup) {
@@ -138,17 +133,16 @@ public class FennecNativeElement impleme
     if(text == null) {
       throw new RoboCopException("getText: Text is null for view "+id);
     }
     return text.toString();
   }
 
   private boolean displayed;
 
-  @Override
   public boolean isDisplayed() {
     final SynchronousQueue syncQueue = new SynchronousQueue();
     currentActivity = robocop.getCurrentActivity();
     displayed = false;
     currentActivity.runOnUiThread(
         new Runnable() {
           public void run() {
             View view = (View)currentActivity.findViewById(id);
--- a/mobile/android/base/tests/BaseTest.java.in
+++ b/mobile/android/base/tests/BaseTest.java.in
@@ -1,15 +1,16 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import com.jayway.android.robotium.solo.Solo;
 import @ANDROID_PACKAGE_NAME@.*;
 
 import android.app.Activity;
+import android.app.Instrumentation;
 import android.test.ActivityInstrumentationTestCase2;
 import android.content.Intent;
 import java.util.HashMap;
 
 abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
     private static final String TARGET_PACKAGE_ID = "org.mozilla.gecko";
     private static final String LAUNCH_ACTIVITY_FULL_CLASSNAME="@ANDROID_PACKAGE_NAME@.App";
 
@@ -42,17 +43,17 @@ abstract class BaseTest extends Activity
         Intent i = new Intent(Intent.ACTION_MAIN);
         i.putExtra("args", "-no-remote -profile " + (String)config.get("profile"));
 
         // Start the activity
         setActivityIntent(i);
         mActivity = getActivity();
 
         // Set up Robotium.solo and Driver objects
-        mSolo = new Solo(getInstrumentation(), getActivity());
+        mSolo = new Solo(getInstrumentation());
         mDriver = new FennecNativeDriver(mActivity, mSolo);
         mActions = new FennecNativeActions(mActivity, mSolo, getInstrumentation());
         mDriver.setLogFile((String)config.get("logfile"));
 
         mAsserter = new FennecNativeAssert();
         mAsserter.setLogFile((String)config.get("logfile"));
     }
 
@@ -62,23 +63,30 @@ abstract class BaseTest extends Activity
             mSolo.finalize();
         } catch (Throwable e) {
             e.printStackTrace();
         }
         getActivity().finish();
         super.tearDown();
     }
 
+    protected final Activity getActivityFromClick(Element element) {
+        Instrumentation inst = getInstrumentation();
+        Instrumentation.ActivityMonitor monitor = inst.addMonitor((String)null, null, false);
+        element.click();
+        return inst.waitForMonitor(monitor);
+    }
+
     protected final void enterUrl(String url) {
         mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
-        Element awesomebar = mDriver.findElement("awesome_bar");
-        awesomebar.click();
+        Element awesomebar = mDriver.findElement(mActivity, "awesome_bar");
+        Activity awesomeBarActivity = getActivityFromClick(awesomebar);
         getInstrumentation().waitForIdleSync();
 
-        Element urlbar = mDriver.findElement("awesomebar_text");
+        Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
         mActions.sendKeys(url);
         mAsserter.is(urlbar.getText(), url, "Awesomebar URL typed properly");
     }
 
     protected final void hitEnterAndWait() {
         Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
         mActions.sendSpecialKey(Actions.SpecialKey.ENTER);
         // wait for screen to load
@@ -86,15 +94,16 @@ abstract class BaseTest extends Activity
     }
 
     protected final void loadUrl(String url) {
         enterUrl(url);
         hitEnterAndWait();
     }
 
     protected final void verifyUrl(String url) {
-        Element awesomebar = mDriver.findElement("awesome_bar");
-        Element urlbar = mDriver.findElement("awesomebar_text");
-        awesomebar.click();
+        Element awesomebar = mDriver.findElement(mActivity, "awesome_bar");
+        Activity awesomeBarActivity = getActivityFromClick(awesomebar);
         getInstrumentation().waitForIdleSync();
+
+        Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
         mAsserter.is(urlbar.getText(), url, "Awesomebar URL stayed the same");
     }
 }
--- a/mobile/android/base/tests/testNewTab.java.in
+++ b/mobile/android/base/tests/testNewTab.java.in
@@ -1,36 +1,37 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
+import android.app.Activity;
 
 public class testNewTab extends BaseTest {
     public void testNewTab() {
         // TODO: find a better way to not hardcode this url
         String url = "http://mochi.test:8888/tests/robocop/robocop_blank_01.html";
         String url2 = "http://mochi.test:8888/tests/robocop/robocop_blank_02.html";
         mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
-        Element tabs = mDriver.findElement("tabs");
+        Element tabs = mDriver.findElement(getActivity(), "tabs");
         // Add one tab
-        tabs.click();
+        Activity awesomeBarActivity = getActivityFromClick(tabs);
 
-        Element urlbar = mDriver.findElement("awesomebar_text");
         getInstrumentation().waitForIdleSync();
+        Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
         mActions.sendKeys(url);
         mAsserter.is(urlbar.getText(), url, "Awesomebar url is fine");
         hitEnterAndWait();
 
         // See tab count
-        Element tabCount = mDriver.findElement("tabs_count");
+        Element tabCount = mDriver.findElement(getActivity(), "tabs_count");
         mAsserter.is(tabCount.getText(), "2", "Number of tabs has increased");
 
         // Click tab list
-        tabs.click();
-        Element addTab = mDriver.findElement("add_tab");
+        Activity tabTray = getActivityFromClick(tabs);
+        Element addTab = mDriver.findElement(tabTray, "add_tab");
 
         //Add another tab
         addTab.click();
         getInstrumentation().waitForIdleSync();
         mActions.sendKeys(url2);
         getInstrumentation().waitForIdleSync();
         mAsserter.is(urlbar.getText(), url2, "URL is still fine");