Bug 786094 - Track the current chrome window during a session, r=mdas, DONTBUILD because NPOTB
authorJonathan Griffin <jgriffin@mozilla.com>
Tue, 28 Aug 2012 11:14:30 -0700
changeset 105736 ff1f9b1ddc2ac162e2fa266288406c5b1852d468
parent 105735 089fd92d1553f5b5970c2e388053609a82047d9f
child 105737 e461878f0567a55a1eee4f118f8c970f618929e7
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersmdas, DONTBUILD
bugs786094
milestone18.0a1
Bug 786094 - Track the current chrome window during a session, r=mdas, DONTBUILD because NPOTB
testing/marionette/client/marionette/tests/unit/test_click.py
testing/marionette/client/marionette/tests/unit/test_elementState.py
testing/marionette/client/marionette/tests/unit/test_findelement.py
testing/marionette/client/marionette/tests/unit/test_getattr.py
testing/marionette/client/marionette/tests/unit/test_pagesource.py
testing/marionette/client/marionette/tests/unit/test_switch_frame.py
testing/marionette/client/marionette/tests/unit/test_text.py
testing/marionette/client/marionette/tests/unit/test_window_management.py
testing/marionette/marionette-actors.js
--- a/testing/marionette/client/marionette/tests/unit/test_click.py
+++ b/testing/marionette/client/marionette/tests/unit/test_click.py
@@ -12,26 +12,31 @@ class TestClick(MarionetteTestCase):
         link = self.marionette.find_element("id", "mozLink")
         link.click()
         self.assertEqual("Clicked", self.marionette.execute_script("return document.getElementById('mozLink').innerHTML;"))
 
     def testClickingALinkMadeUpOfNumbersIsHandledCorrectly(self):
         test_html = self.marionette.absolute_url("clicks.html")
         self.marionette.navigate(test_html)
         self.marionette.find_element("link text", "333333").click()
+        self.marionette.set_search_timeout(5000)
+        self.marionette.find_element("id", "username")
         self.assertEqual(self.marionette.title, "XHTML Test Page")
 
 class TestClickChrome(MarionetteTestCase):
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context("chrome")
         self.win = self.marionette.current_window_handle
-        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', '_blank', 'chrome,centerscreen');")
+        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
 
     def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
         self.marionette.execute_script("window.close();")
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def test_click(self):
         wins = self.marionette.window_handles
         wins.remove(self.win)
         newWin = wins.pop()
--- a/testing/marionette/client/marionette/tests/unit/test_elementState.py
+++ b/testing/marionette/client/marionette/tests/unit/test_elementState.py
@@ -22,19 +22,22 @@ class TestState(MarionetteTestCase):
         self.marionette.execute_script("arguments[0].hidden = true;", [l])
         self.assertFalse(l.is_displayed())
 
 class TestStateChrome(MarionetteTestCase):
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context("chrome")
         self.win = self.marionette.current_window_handle
-        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', '_blank', 'chrome,centerscreen');")
+        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
 
     def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
         self.marionette.execute_script("window.close();")
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def test_isEnabled(self):
         l = self.marionette.find_element("id", "textInput")
         self.assertTrue(l.is_enabled())
         self.marionette.execute_script("arguments[0].disabled = true;", [l])
--- a/testing/marionette/client/marionette/tests/unit/test_findelement.py
+++ b/testing/marionette/client/marionette/tests/unit/test_findelement.py
@@ -129,19 +129,22 @@ class TestElements(MarionetteTestCase):
         self.marionette.navigate(test_html)
         self.assertEqual(HTMLElement, type(self.marionette.find_element("id", "newDiv")))
 
 class TestElementsChrome(MarionetteTestCase):
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context("chrome")
         self.win = self.marionette.current_window_handle
-        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', '_blank', 'chrome,centerscreen');")
+        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
 
     def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
         self.marionette.execute_script("window.close();")
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def test_id(self):
         el = self.marionette.execute_script("return window.document.getElementById('textInput');")
         found_el = self.marionette.find_element("id", "textInput")
         self.assertEqual(HTMLElement, type(found_el))
--- a/testing/marionette/client/marionette/tests/unit/test_getattr.py
+++ b/testing/marionette/client/marionette/tests/unit/test_getattr.py
@@ -12,19 +12,22 @@ class TestGetAttribute(MarionetteTestCas
         l = self.marionette.find_element("id", "mozLink")
         self.assertEqual("mozLink", l.get_attribute("id"))
 
 class TestGetAttributeChrome(MarionetteTestCase):
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context("chrome")
         self.win = self.marionette.current_window_handle
-        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', '_blank', 'chrome,centerscreen');")
+        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
 
     def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
         self.marionette.execute_script("window.close();")
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def test_getAttribute(self):
         el = self.marionette.execute_script("return window.document.getElementById('textInput');")
         self.assertEqual(el.get_attribute("id"), "textInput")
 
--- a/testing/marionette/client/marionette/tests/unit/test_pagesource.py
+++ b/testing/marionette/client/marionette/tests/unit/test_pagesource.py
@@ -19,19 +19,22 @@ class TestPageSource(MarionetteTestCase)
         import re
         self.assertEqual(re.sub("\s", "", source), "<xml><foo><bar>baz</bar></foo></xml>")
 
 class TestPageSourceChrome(MarionetteTestCase):
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context("chrome")
         self.win = self.marionette.current_window_handle
-        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', '_blank', 'chrome,centerscreen');")
+        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
 
     def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
         self.marionette.execute_script("window.close();")
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def testShouldReturnXULDetails(self):
         wins = self.marionette.window_handles
         wins.remove(self.win)
         newWin = wins.pop()
--- a/testing/marionette/client/marionette/tests/unit/test_switch_frame.py
+++ b/testing/marionette/client/marionette/tests/unit/test_switch_frame.py
@@ -32,19 +32,22 @@ class TestSwitchFrame(MarionetteTestCase
         #test that we're using the right window object server-side
         self.assertTrue("test_nested_iframe.html" in self.marionette.execute_script("return window.location.href;"))
 
 class TestSwitchFrameChrome(MarionetteTestCase):
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context("chrome")
         self.win = self.marionette.current_window_handle
-        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', '_blank', 'chrome,centerscreen');")
+        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
 
     def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
         self.marionette.execute_script("window.close();")
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def test_switch_simple(self):
         self.assertTrue("test.xul" in self.marionette.get_url())
         self.marionette.switch_to_frame(0)
         self.assertTrue("test2.xul" in self.marionette.get_url())
--- a/testing/marionette/client/marionette/tests/unit/test_text.py
+++ b/testing/marionette/client/marionette/tests/unit/test_text.py
@@ -28,19 +28,22 @@ class TestText(MarionetteTestCase):
         l.send_keys("o")
         self.assertEqual("asdfo", self.marionette.execute_script("return arguments[0].value;", [l]))
 
 class TestTextChrome(MarionetteTestCase):
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.set_context("chrome")
         self.win = self.marionette.current_window_handle
-        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', '_blank', 'chrome,centerscreen');")
+        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
 
     def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
         self.marionette.execute_script("window.close();")
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def test_getText(self):
         wins = self.marionette.window_handles
         wins.remove(self.win)
         newWin = wins.pop()
--- a/testing/marionette/client/marionette/tests/unit/test_window_management.py
+++ b/testing/marionette/client/marionette/tests/unit/test_window_management.py
@@ -5,35 +5,35 @@
 import os
 from marionette_test import MarionetteTestCase
 
 class TestSwitchWindow(MarionetteTestCase):
     def open_new_window(self):
         self.marionette.set_context("chrome")
         self.marionette.set_script_timeout(5000)
         self.marionette.execute_async_script("""
-                                        var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
-                                                 .getService(Components.interfaces.nsIWindowWatcher); 
-                                        var win = ww.openWindow(null, "chrome://browser/content/browser.xul", "testWin", null, null);
-                                        win.addEventListener("load", function() { 
-                                                                        win.removeEventListener("load", arguments.callee, true); 
-                                                                        marionetteScriptFinished();
-                                                                        }, null);
-                                        """)
+var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
+                   .getService(Components.interfaces.nsIWindowWatcher); 
+var win = ww.openWindow(null, "chrome://browser/content/browser.xul", "testWin", null, null);
+win.addEventListener("load", function() { 
+  win.removeEventListener("load", arguments.callee, true); 
+  marionetteScriptFinished();
+}, null);
+""")
         self.marionette.set_context("content")
 
     def close_new_window(self):
         self.marionette.set_context("chrome")
         self.marionette.execute_script("""
-                                        var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
-                                                 .getService(Components.interfaces.nsIWindowWatcher); 
-                                        var win = ww.getWindowByName("testWin", null);
-                                        if (win != null)
-                                          win.close();
-                                        """)
+var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
+                   .getService(Components.interfaces.nsIWindowWatcher); 
+var win = ww.getWindowByName("testWin", null);
+if (win != null)
+  win.close();
+""")
         self.marionette.set_context("content")
 
     def test_windows(self):
         orig_win = self.marionette.current_window_handle
         orig_available = self.marionette.window_handles
         self.open_new_window()
         #assert we're still in the original window
         self.assertEqual(self.marionette.current_window_handle, orig_win)
@@ -76,21 +76,24 @@ class TestSwitchWindow(MarionetteTestCas
         self.marionette.close()
         self.marionette.switch_to_window(current)
         self.assertEqual(1, len(self.marionette.window_handles))
 
     def testShouldCauseAWindowToLoadAndCheckItIsOpenThenCloseIt(self):
         test_html = self.marionette.absolute_url("test_windows.html")
         self.marionette.navigate(test_html)
         current = self.marionette.current_window_handle
-        
+
         self.marionette.find_element('link text',"Open new window").click()
-        self.assertEqual(2, len(self.marionette.window_handles))
+        all_handles = self.marionette.window_handles
+        self.assertEqual(2, len(all_handles))
+        self.marionette.switch_to_window([x for x in all_handles if x != current][0])
 
         # Let's close and check
         self.marionette.close()
         self.marionette.switch_to_window(current)
         self.assertEqual(1, len(self.marionette.window_handles))
 
     def tearDown(self):
         #ensure that we close the window, regardless of pass/failure
         self.close_new_window()
         MarionetteTestCase.tearDown(self)
+
--- a/testing/marionette/marionette-actors.js
+++ b/testing/marionette/marionette-actors.js
@@ -229,20 +229,25 @@ MarionetteDriverActor.prototype = {
   /**
    * Gets the current active window
    * 
    * @return nsIDOMWindow
    */
   getCurrentWindow: function MDA_getCurrentWindow() {
     let type = null;
     if (this.curFrame == null) {
-      if (appName != "B2G" && this.context == "content") {
-        type = 'navigator:browser';
+      if (this.curBrowser == null) {
+        if (appName != "B2G" && this.context == "content") {
+          type = 'navigator:browser';
+        }
+        return Services.wm.getMostRecentWindow(type);
       }
-      return Services.wm.getMostRecentWindow(type);
+      else {
+        return this.curBrowser.window;
+      }
     }
     else {
       return this.curFrame;
     }
   },
 
   /**
    * Gets the the window enumerator
@@ -852,28 +857,29 @@ MarionetteDriverActor.prototype = {
    *
    * @param object aRequest
    *        'value' member holds the name or id of the window to switch to
    */
   switchToWindow: function MDA_switchToWindow(aRequest) {
     let winEn = this.getWinEnumerator(); 
     while(winEn.hasMoreElements()) {
       let foundWin = winEn.getNext();
-      let winId = foundWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
+      let winId = foundWin.QueryInterface(Ci.nsIInterfaceRequestor)
+                          .getInterface(Ci.nsIDOMWindowUtils)
+                          .outerWindowID;
       winId = winId + ((appName == "B2G") ? '-b2g' : '');
       if (aRequest.value == foundWin.name || aRequest.value == winId) {
         if (this.browsers[winId] == undefined) {
           //enable Marionette in that browser window
           this.startBrowser(foundWin, false);
         }
         else {
           utils.window = foundWin;
           this.curBrowser = this.browsers[winId];
         }
-        foundWin.focus();
         this.sendOk();
         return;
       }
     }
     this.sendError("Unable to locate window " + aRequest.value, 23, null);
   },
  
   /**
@@ -1512,16 +1518,17 @@ MarionetteDriverActor.prototype.requestT
  *        The window whose browser needs to be accessed
  */
 
 function BrowserObj(win) {
   this.DESKTOP = "desktop";
   this.B2G = "B2G";
   this.browser;
   this.tab = null;
+  this.window = win;
   this.knownFrames = [];
   this.curFrameId = null;
   this.startPage = "about:blank";
   this.mainContentId = null; // used in B2G to identify the homescreen content page
   this.messageManager = Cc["@mozilla.org/globalmessagemanager;1"]
                           .getService(Ci.nsIMessageBroadcaster);
   this.newSession = true; //used to set curFrameId upon new session
   this.elementManager = new ElementManager([SELECTOR, NAME, LINK_TEXT, PARTIAL_LINK_TEXT]);
@@ -1555,18 +1562,16 @@ BrowserObj.prototype = {
     if (appName == "B2G") {
       return;
     }
     if (newTab) {
       this.addTab(this.startPage);
       //if we have a new tab, make it the selected tab and give it focus
       this.browser.selectedTab = this.tab;
       let newTabBrowser = this.browser.getBrowserForTab(this.tab);
-      //focus the tab
-      newTabBrowser.ownerDocument.defaultView.focus();
     }
     else {
       //set this.tab to the currently focused tab
       if (this.browser != undefined && this.browser.selectedTab != undefined) {
         this.tab = this.browser.selectedTab;
       }
     }
   },