Bug 1398499: Part 3 - Add Marionette tests for global sharing. r=mccr8
authorKris Maglione <maglione.k@gmail.com>
Sat, 09 Sep 2017 17:33:01 -0700
changeset 429434 952de83710f9b3a9757916cbe77081d9d08a7b85
parent 429433 f8528dbd0e708bc0f26dd4823ef8ee113c03636c
child 429435 2fbb500ac8d0941644e498f53100cf6021ff74fe
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1398499
milestone57.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 1398499: Part 3 - Add Marionette tests for global sharing. r=mccr8 This tests both that the settings have the desired effect and that switching between sharing enabled and sharing disabled without a startup cache flush does not cause any issues. Tests for user pref changes are currently non-fatal, since they're known not to work reliably. MozReview-Commit-ID: 1ZFwyiNf3da
js/xpconnect/tests/marionette/manifest.ini
js/xpconnect/tests/marionette/moz.build
js/xpconnect/tests/marionette/test_loader_global_sharing.py
js/xpconnect/tests/moz.build
testing/marionette/harness/marionette_harness/tests/unit-tests.ini
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/marionette/manifest.ini
@@ -0,0 +1,3 @@
+
+[test_loader_global_sharing.py]
+skip-if = !manage_instance || appname == 'fennec'
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/marionette/moz.build
@@ -0,0 +1,7 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+MARIONETTE_UNIT_MANIFESTS += ['manifest.ini']
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/marionette/test_loader_global_sharing.py
@@ -0,0 +1,119 @@
+from __future__ import print_function
+
+from marionette_harness import MarionetteTestCase
+
+
+GLOBAL_SHARING_PREF = 'jsloader.shareGlobal'
+GLOBAL_SHARING_VAR = 'MOZ_LOADER_SHARE_GLOBAL'
+
+
+class TestLoaderGlobalSharing(MarionetteTestCase):
+    sandbox_name = 'loader-global-sharing'
+
+    def execute_script(self, code, *args, **kwargs):
+        with self.marionette.using_context(self.marionette.CONTEXT_CHROME):
+            return self.marionette.execute_script(code,
+                                                  new_sandbox=False,
+                                                  sandbox=self.sandbox_name,
+                                                  *args, **kwargs)
+
+    def get_global_sharing_enabled(self):
+        return self.execute_script(r'''
+          Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+          return (Cu.getGlobalForObject(Services) ===
+                  Cu.getGlobalForObject(XPCOMUtils))
+        ''')
+
+    def set_env(self, env, value):
+        self.execute_script('env.set(arguments[0], arguments[1]);',
+                            script_args=(env, value))
+
+    def get_env(self, env):
+        return self.execute_script('return env.get(arguments[0]);',
+                                   script_args=(env,))
+
+    def restart(self, prefs=None, env=None):
+        if prefs:
+            self.marionette.set_prefs(prefs)
+
+        if env:
+            for name, value in env.items():
+                self.set_env(name, value)
+
+        self.marionette.restart(in_app=True, clean=False)
+        self.setUpSession()
+
+        # Sanity check our environment.
+        if prefs:
+            for key, val in prefs.items():
+                self.assertEqual(self.marionette.get_pref(key), val)
+        if env:
+            for key, val in env.items():
+                self.assertEqual(self.get_env(key), val or '')
+
+    def setUpSession(self):
+        self.marionette.set_context(self.marionette.CONTEXT_CHROME)
+
+        self.execute_script(r'''
+          const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} =
+                    Components;
+
+          // We're running in a function, in a sandbox, that inherits from an
+          // X-ray wrapped window. Anything we want to be globally available
+          // needs to be defined on that window.
+          Object.assign(window, {Cc, Ci, Cu, Cr});
+
+          Cu.import("resource://gre/modules/Services.jsm");
+          window.env = Cc["@mozilla.org/process/environment;1"]
+                    .getService(Ci.nsIEnvironment);
+        ''')
+
+    def setUp(self):
+        super(TestLoaderGlobalSharing, self).setUp()
+
+        self.default_pref_value = self.marionette.get_pref(GLOBAL_SHARING_PREF)
+
+        self.setUpSession()
+
+    def tearDown(self):
+        self.marionette.restart(clean=True)
+
+        super(TestLoaderGlobalSharing, self).tearDown()
+
+    def test_global_sharing_settings(self):
+        # The different cases to test, with the first element being the value
+        # of the MOZ_LOADER_SHARE_GLOBAL environment variable, and the second
+        # being the value of the "jsloader.shareGlobal" preference.
+        #
+        # The browser is restarted after each change, but the profile is not
+        # reset.
+        CASES = (
+            (None, False),
+            (None, True),
+            ('0', True),
+            ('1', True),
+            ('0', False),
+            ('1', False),
+        )
+
+        for var, pref in CASES:
+            print('Testing %s=%r %s=%r' % (GLOBAL_SHARING_VAR, var,
+                                           GLOBAL_SHARING_PREF, pref))
+
+            # The value of the environment variable takes precedence if it's
+            # defined.
+            expect_sharing = pref if var is None else var != '0'
+
+            self.restart(prefs={GLOBAL_SHARING_PREF: pref},
+                         env={GLOBAL_SHARING_VAR: var})
+
+            have_sharing = self.get_global_sharing_enabled()
+
+            # FIXME: User preference values currently do not always take
+            # effect early enough to influence loader behavior.
+            msg = ('Global sharing state should match settings: %r != %r'
+                   % (have_sharing, expect_sharing))
+            if var is not None or pref == self.default_pref_value:
+                self.assertEqual(have_sharing, expect_sharing, msg)
+            elif have_sharing != expect_sharing:
+                print('TEST-EXPECTED-FAIL: ' + msg)
--- a/js/xpconnect/tests/moz.build
+++ b/js/xpconnect/tests/moz.build
@@ -1,15 +1,16 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
 TEST_DIRS += [
+    'marionette',
     'mochitest',
     'chrome',
     'browser',
     'components/native',
 ]
 
 if CONFIG['COMPILE_ENVIRONMENT']:
     TEST_DIRS += [
--- a/testing/marionette/harness/marionette_harness/tests/unit-tests.ini
+++ b/testing/marionette/harness/marionette_harness/tests/unit-tests.ini
@@ -5,8 +5,11 @@ skip-if = asan # Bug 1223277, 1348961
 # layout tests
 [include:../../../../../layout/base/tests/marionette/manifest.ini]
 
 # microformats tests
 [include:../../../../../toolkit/components/microformats/manifest.ini]
 
 # migration tests
 [include:../../../../../browser/components/migration/tests/marionette/manifest.ini]
+
+# xpconnect tests
+[include:../../../../../js/xpconnect/tests/marionette/manifest.ini]