Bug 1157319 - Add a 'Request Desktop Site' Robocop test r=nalexander,margaret
authorMark Finkle <mfinkle@mozilla.com>
Wed, 01 Jul 2015 15:52:49 -0400
changeset 251097 c4a1f11886771b37493a85c317e0c3755e3f414f
parent 251096 138bfb083317b9834a4776e28a71c710c6d73181
child 251098 9976683b3e5a4492419fd7b2673acf74a8a6ec8d
push id61766
push usercbook@mozilla.com
push dateThu, 02 Jul 2015 13:47:40 +0000
treeherdermozilla-inbound@8ee689f2899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander, margaret
bugs1157319
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1157319 - Add a 'Request Desktop Site' Robocop test r=nalexander,margaret
mobile/android/tests/browser/robocop/desktopmode_user_agent.sjs
mobile/android/tests/browser/robocop/robocop.ini
mobile/android/tests/browser/robocop/testDesktopUserAgent.java
mobile/android/tests/browser/robocop/testDesktopUserAgent.js
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/robocop/desktopmode_user_agent.sjs
@@ -0,0 +1,11 @@
+function handleRequest(request, response)
+{
+  // avoid confusing cache behaviors
+  response.setHeader("Cache-Control", "no-cache", false);
+  response.setHeader("Content-Type", "text/plain", false);
+  response.setHeader("Access-Control-Allow-Origin", "*", false);
+
+  // used by mobile/desktop user agent tests
+  response.write(request.getHeader("User-Agent"));
+}
+
--- a/mobile/android/tests/browser/robocop/robocop.ini
+++ b/mobile/android/tests/browser/robocop/robocop.ini
@@ -113,16 +113,17 @@ skip-if = android_version == "10" || and
 
 # Using JavascriptTest
 [testAccounts.java]
 [testAndroidLog.java]
 [testBrowserDiscovery.java]
 # disabled on 4.3, bug 1158384
 skip-if = android_version == "18"
 [testDebuggerServer.java]
+[testDesktopUserAgent.java]
 [testDeviceSearchEngine.java]
 [testFilePicker.java]
 [testHistoryService.java]
 # disabled on 4.3, bug 1116036
 skip-if = android_version == "18"
 [testJavaAddons.java]
 [testJNI.java]
 # [testMozPay.java] # see bug 945675
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/robocop/testDesktopUserAgent.java
@@ -0,0 +1,11 @@
+/* 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;
+
+public class testDesktopUserAgent extends JavascriptTest {
+    public testDesktopUserAgent() {
+        super("testDesktopUserAgent.js");
+    }
+}
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/robocop/testDesktopUserAgent.js
@@ -0,0 +1,71 @@
+// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
+/* 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/. */
+
+"use strict";
+
+const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+function ok(passed, text) {
+  do_report_result(passed, text, Components.stack.caller, false);
+}
+
+function is(lhs, rhs, text) {
+  do_report_result(lhs === rhs, text, Components.stack.caller, false);
+}
+
+function promiseBrowserEvent(browser, eventType) {
+  return new Promise((resolve) => {
+    function handle(event) {
+      // Since we'll be redirecting, don't make assumptions about the given URL and the loaded URL
+      if (event.target != browser.contentDocument || event.target.location.href == "about:blank") {
+        do_print("Skipping spurious '" + eventType + "' event" + " for " + event.target.location.href);
+        return;
+      }
+      do_print("Received event " + eventType + " from browser");
+      browser.removeEventListener(eventType, handle, true);
+      resolve(event);
+    }
+
+    browser.addEventListener(eventType, handle, true);
+    do_print("Now waiting for " + eventType + " event from browser");
+  });
+}
+
+// Load a custom sjs script that echos our "User-Agent" header back at us
+const TestURI = Services.io.newURI("http://mochi.test:8888/tests/robocop/desktopmode_user_agent.sjs", null, null);
+
+add_task(function* test_desktopmode() {
+  let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+  let BrowserApp = chromeWin.BrowserApp;
+
+  // Add a new 'desktop mode' tab with our test page
+  let desktopBrowser = BrowserApp.addTab(TestURI.spec, { selected: true, parentId: BrowserApp.selectedTab.id, desktopMode: true }).browser;
+  yield promiseBrowserEvent(desktopBrowser, "load");
+
+  // Some debugging
+  do_print("desktop: " + desktopBrowser.contentWindow.navigator.userAgent);
+  do_print("desktop: " + desktopBrowser.contentDocument.body.innerHTML);
+
+  // Check the server UA and the navigator UA for 'desktop'
+  ok(desktopBrowser.contentWindow.navigator.userAgent.indexOf("Linux x86_64") != -1, "window.navigator.userAgent has 'Linux' in it");
+  ok(desktopBrowser.contentDocument.body.innerHTML.indexOf("Linux x86_64") != -1, "HTTP header 'User-Agent' has 'Linux' in it");
+
+  // Add a new 'mobile mode' tab with our test page
+  let mobileBrowser = BrowserApp.addTab(TestURI.spec, { selected: true, parentId: BrowserApp.selectedTab.id }).browser;
+  yield promiseBrowserEvent(mobileBrowser, "load");
+
+  // Some debugging
+  do_print("mobile: " + mobileBrowser.contentWindow.navigator.userAgent);
+  do_print("mobile: " + mobileBrowser.contentDocument.body.innerHTML);
+
+  // Check the server UA and the navigator UA for 'mobile'
+  // We only check for 'Android' because we don't know the version or if it's phone or tablet
+  ok(mobileBrowser.contentWindow.navigator.userAgent.indexOf("Android") != -1, "window.navigator.userAgent has 'Android' in it");
+  ok(mobileBrowser.contentDocument.body.innerHTML.indexOf("Android") != -1, "HTTP header 'User-Agent' has 'Android' in it");
+});
+
+run_next_test();