Bug 1498892 - Part 1: Clean up existing scroll position test. r=gbrown
authorJan Henning <jh+bugzilla@buttercookie.de>
Mon, 15 Oct 2018 21:11:13 +0000
changeset 489693 fb3685a86f81baeebbf47aabdf2e0f28aa2bf221
parent 489692 7b5cc9b80a3e2630baf823181caca717e2c80a8a
child 489694 8e482a5a51ad99eb7173e1bf1a2410c1a70d6825
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersgbrown
bugs1498892
milestone64.0a1
Bug 1498892 - Part 1: Clean up existing scroll position test. r=gbrown Extract some repetitive logic into common helper functions/variables and pre- pare support for setting/checking the scroll position on frames, too. Additionally, I'm not sure why I ever thought it was necessary to manually fake "scroll" events after calling scrollTo(x,y), but in any case it is no longer necessary. Differential Revision: https://phabricator.services.mozilla.com/D8674
mobile/android/tests/browser/chrome/test_session_scroll_position.html
--- a/mobile/android/tests/browser/chrome/test_session_scroll_position.html
+++ b/mobile/android/tests/browser/chrome/test_session_scroll_position.html
@@ -24,48 +24,72 @@ https://bugzilla.mozilla.org/show_bug.cg
   ChromeUtils.import("resource://gre/modules/Services.jsm");
   ChromeUtils.import("resource://gre/modules/Messaging.jsm");
   ChromeUtils.import("resource://gre/modules/Task.jsm");
 
   // The chrome window and friends.
   let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
   let BrowserApp = chromeWin.BrowserApp;
 
+  const BASE = "http://example.org/chrome/mobile/android/tests/browser/chrome/";
   // Use something with ample space for scrolling and zooming.
-  const URL = "http://example.org/chrome/mobile/android/tests/browser/chrome/browser_scrollPositions_sample.html";
+  const URL = BASE + "browser_scrollPositions_sample.html";
   // Same content but under a different URL, so we have something to navigate to and from.
-  const URL2 = "http://example.org/chrome/mobile/android/tests/browser/chrome/browser_scrollPositions_sample2.html";
+  const URL2 = BASE + "browser_scrollPositions_sample2.html";
   // Reader mode URL
   const URL_reader = "about:reader?url=http%3A%2F%2Fexample.org%2Fchrome%2Fmobile%2Fandroid%2Ftests%2Fbrowser%2Fchrome%2Fbasic_article_mobile.html";
 
   // Randomized set of scroll positions and zoom factors we will use in this test.
   const SCROLL_X = Math.round(100 * (1 + Math.random()));
   const SCROLL_Y = Math.round(200 * (1 + Math.random()));
   const SCROLL_STR = SCROLL_X + "," + SCROLL_Y;
   const SCROLL_STR_Y_ONLY = 0 + "," + SCROLL_Y;
   const ZOOM = 1 + 0.5 * Math.random();
 
   const SCROLL2_X = Math.round(300 * (1 + Math.random()));
   const SCROLL2_Y = Math.round(400 * (1 + Math.random()));
   const SCROLL2_STR = SCROLL2_X + "," + SCROLL2_Y;
   const ZOOM2 = 1.5 + 0.5 * Math.random();
 
-  function dispatchUIEvent(browser, type) {
-    let event = browser.contentDocument.createEvent("UIEvents");
-    event.initUIEvent(type, true, false, browser.contentDocument.defaultView, 0);
-    browser.dispatchEvent(event);
+  function getFrame(browser, data) {
+    let frame = browser.contentWindow;
+    if (data.hasOwnProperty("frame")) {
+      frame = browser.contentWindow.frames[data.frame];
+    }
+    return frame;
   }
 
-  function setScrollPosition(browser, x, y) {
-    browser.contentWindow.scrollTo(x, y);
-    dispatchUIEvent(browser, "scroll");
+  function setScrollPosition(browser, data) {
+    let {x, y, zoom} = data;
+    x = x || 0;
+    y = y || 0;
+    let frame = getFrame(browser, data);
+    if (data.hasOwnProperty("zoom")) {
+      browser.contentWindow.windowUtils.setResolutionAndScaleTo(zoom);
+    }
+    frame.scrollTo(x, y);
   }
 
-  function setZoomLevel(browser, zoom) {
-    browser.contentWindow.windowUtils.setResolutionAndScaleTo(zoom);
+  function checkScroll(browser, data) {
+    let {x, y, zoom} = data;
+    let utils = getFrame(browser, data).windowUtils;
+
+    let actualX = {}, actualY = {}, actualZoom = {};
+    utils.getResolution(actualZoom);
+    utils.getScrollXY(false, actualX, actualY);
+
+    if (data.hasOwnProperty("x")) {
+      is(actualX.value, x, "scrollX set correctly");
+    }
+    if (data.hasOwnProperty("y")) {
+      is(actualY.value, y, "scrollY set correctly");
+    }
+    if (data.hasOwnProperty("zoom")) {
+      ok(fuzzyEquals(actualZoom.value, zoom), "zoom set correctly");
+    }
   }
 
   // Track the tabs where the tests are happening.
   let tabScroll;
 
   function cleanupTabs() {
     if (tabScroll) {
       BrowserApp.closeTab(tabScroll);
@@ -75,34 +99,31 @@ https://bugzilla.mozilla.org/show_bug.cg
 
   SimpleTest.registerCleanupFunction(function() {
     cleanupTabs();
   });
 
   let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
 
   add_task(async function test_sessionStoreScrollPositionReaderMode() {
+    let testData = {x: 0, y: SCROLL_Y};
     // Creates a tab, sets a scroll position and closes the tab.
     function createAndRemoveReaderTab() {
       return Task.spawn(function* () {
         // Create a new tab.
         tabScroll = BrowserApp.addTab(URL_reader);
         let browser = tabScroll.browser;
         yield promiseBrowserEvent(browser, "AboutReaderContentReady");
 
         // Modify scroll position.
-        setScrollPosition(browser, 0, SCROLL_Y);
+        setScrollPosition(browser, testData);
         yield promiseTabEvent(browser, "SSTabScrollCaptured");
 
         // Check that we've actually scrolled.
-        let utils = browser.contentWindow.windowUtils;
-        let scrollX = {}, scrollY = {};
-        utils.getScrollXY(false, scrollX, scrollY);
-        is(scrollX.value, 0, "scrollX set correctly");
-        is(scrollY.value, SCROLL_Y, "scrollY set correctly");
+        checkScroll(browser, testData);
 
         // Remove the tab.
         BrowserApp.closeTab(tabScroll);
         yield promiseTabEvent(browser, "SSTabCloseProcessed");
       });
     }
 
     await createAndRemoveReaderTab();
@@ -111,62 +132,48 @@ https://bugzilla.mozilla.org/show_bug.cg
     is(scrolldata.scroll, SCROLL_STR_Y_ONLY, "stored scroll position is correct");
 
     // Restore the closed tab.
     let closedTabData = ss.getClosedTabs(chromeWin)[0];
     let browser = ss.undoCloseTab(chromeWin, closedTabData);
     await promiseBrowserEvent(browser, "AboutReaderContentReady");
 
     // Check the scroll position.
-    let utils = browser.contentWindow.windowUtils;
-    let scrollX = {}, scrollY = {};
-    utils.getScrollXY(false, scrollX, scrollY);
-    is(scrollX.value, 0, "scrollX restored correctly");
-    is(scrollY.value, SCROLL_Y, "scrollY restored correctly");
+    checkScroll(browser, testData);
 
     // Remove the tab.
     BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
   });
 
   add_task(async function test_sessionStoreScrollPositionAndZoomLevel() {
+    let testData1 = {x: SCROLL_X, y: SCROLL_Y, zoom: ZOOM};
+    let testData2 = {x: SCROLL2_X, y: SCROLL2_Y, zoom: ZOOM2};
     // Creates a tab, sets a scroll position and zoom level and closes the tab.
     function createAndRemoveTab() {
       return Task.spawn(function* () {
         // Create a new tab.
         tabScroll = BrowserApp.addTab(URL);
         let browser = tabScroll.browser;
         yield promiseBrowserEvent(browser, "pageshow");
 
         // Modify scroll position and zoom level.
-        setZoomLevel(browser, ZOOM);
-        setScrollPosition(browser, SCROLL_X, SCROLL_Y);
+        setScrollPosition(browser, testData1);
         yield promiseTabEvent(browser, "SSTabScrollCaptured");
 
         // Check that we've actually scrolled and zoomed.
-        let utils = browser.contentWindow.windowUtils;
-        let scrollX = {}, scrollY = {}, zoom = {};
-        utils.getResolution(zoom);
-        utils.getScrollXY(false, scrollX, scrollY);
-        ok(fuzzyEquals(zoom.value, ZOOM), "zoom set correctly");
-        is(scrollX.value, SCROLL_X, "scrollX set correctly");
-        is(scrollY.value, SCROLL_Y, "scrollY set correctly");
+        checkScroll(browser, testData1);
 
         // Navigate to a different page and scroll/zoom there as well.
         browser.loadURI(URL2);
         yield promiseBrowserEvent(browser, "pageshow");
 
-        setZoomLevel(browser, ZOOM2);
-        setScrollPosition(browser, SCROLL2_X, SCROLL2_Y);
+        setScrollPosition(browser, testData2);
         yield promiseTabEvent(browser, "SSTabScrollCaptured");
 
-        utils.getResolution(zoom);
-        utils.getScrollXY(false, scrollX, scrollY);
-        ok(fuzzyEquals(zoom.value, ZOOM2), "zoom set correctly");
-        is(scrollX.value, SCROLL2_X, "scrollX set correctly");
-        is(scrollY.value, SCROLL2_Y, "scrollY set correctly");
+        checkScroll(browser, testData2);
 
         // Remove the tab.
         BrowserApp.closeTab(tabScroll);
         yield promiseTabEvent(browser, "SSTabCloseProcessed");
       });
     }
 
     await createAndRemoveTab();
@@ -179,65 +186,49 @@ https://bugzilla.mozilla.org/show_bug.cg
     let closedTabData = ss.getClosedTabs(chromeWin)[0];
     let browser = ss.undoCloseTab(chromeWin, closedTabData);
     let pageshow = promiseBrowserEvent(browser, "pageshow");
     let scroll = promiseBrowserEvent(browser, "scroll");
     await pageshow;
     await scroll;
 
     // Check the scroll position and zoom level.
-    let utils = browser.contentWindow.windowUtils;
-    let scrollX = {}, scrollY = {}, zoom = {};
-    utils.getResolution(zoom);
-    utils.getScrollXY(false, scrollX, scrollY);
-    ok(fuzzyEquals(zoom.value, ZOOM2), "zoom restored correctly");
-    is(scrollX.value, SCROLL2_X, "scrollX restored correctly");
-    is(scrollY.value, SCROLL2_Y, "scrollY restored correctly");
+    checkScroll(browser, testData2);
 
     // Now go back in history and check that the scroll position
     // is restored there as well.
     is(browser.canGoBack, true, "can go back");
     pageshow = promiseBrowserEvent(browser, "pageshow");
     scroll = promiseBrowserEvent(browser, "scroll");
     browser.goBack();
     await pageshow;
     await scroll;
 
-    utils.getResolution(zoom);
-    utils.getScrollXY(false, scrollX, scrollY);
-    ok(fuzzyEquals(zoom.value, ZOOM), "zoom restored correctly");
-    is(scrollX.value, SCROLL_X, "scrollX restored correctly");
-    is(scrollY.value, SCROLL_Y, "scrollY restored correctly");
+    checkScroll(browser, testData1);
 
     // Remove the tab.
     BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
   });
 
   add_task(async function test_sessionStoreZoomLevelRecalc() {
+    let testData = {x: SCROLL_X, y: SCROLL_Y, zoom: ZOOM};
     // Creates a tab, sets a scroll position and zoom level and closes the tab.
     function createAndRemoveTab() {
       return Task.spawn(function* () {
         // Create a new tab.
         tabScroll = BrowserApp.addTab(URL);
         let browser = tabScroll.browser;
         yield promiseBrowserEvent(browser, "pageshow");
 
         // Modify scroll position and zoom level.
-        setZoomLevel(browser, ZOOM);
-        setScrollPosition(browser, SCROLL_X, SCROLL_Y);
+        setScrollPosition(browser, testData);
         yield promiseTabEvent(browser, "SSTabScrollCaptured");
 
         // Check that we've actually scrolled and zoomed.
-        let utils = browser.contentWindow.windowUtils;
-        let scrollX = {}, scrollY = {}, zoom = {};
-        utils.getResolution(zoom);
-        utils.getScrollXY(false, scrollX, scrollY);
-        ok(fuzzyEquals(zoom.value, ZOOM), "zoom set correctly");
-        is(scrollX.value, SCROLL_X, "scrollX set correctly");
-        is(scrollY.value, SCROLL_Y, "scrollY set correctly");
+        checkScroll(browser, testData);
 
         // Remove the tab.
         BrowserApp.closeTab(tabScroll);
         yield promiseTabEvent(browser, "SSTabCloseProcessed");
       });
     }
 
     await createAndRemoveTab();
@@ -253,23 +244,18 @@ https://bugzilla.mozilla.org/show_bug.cg
     closedTabData.scrolldata.zoom.displaySize.width = displayHeight;
     closedTabData.scrolldata.zoom.displaySize.height = displayWidth;
 
     // Restore the closed tab.
     let browser = ss.undoCloseTab(chromeWin, closedTabData);
     await promiseBrowserEvent(browser, "pageshow");
 
     // Check the scroll position and zoom level.
-    let utils = browser.contentWindow.windowUtils;
-    let scrollX = {}, scrollY = {}, zoom = {};
-    utils.getResolution(zoom);
-    utils.getScrollXY(false, scrollX, scrollY);
-    ok(fuzzyEquals(zoom.value, ZOOM * displayWidth / displayHeight), "recalculated zoom restored correctly");
-    is(scrollX.value, SCROLL_X, "scrollX restored correctly");
-    is(scrollY.value, SCROLL_Y, "scrollY restored correctly");
+    testData.zoom = ZOOM * displayWidth / displayHeight;
+    checkScroll(browser, testData);
 
     // Remove the tab.
     BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
   });
 
   </script>
 </head>
 <body>