diff --git a/testing/marionette/client/marionette/marionette_test.py b/testing/marionette/client/marionette/marionette_test.py index d59d897cb52b..973685841c2e 100644 --- a/testing/marionette/client/marionette/marionette_test.py +++ b/testing/marionette/client/marionette/marionette_test.py @@ -382,9 +382,10 @@ permissions.forEach(function (perm) { self.marionette.timeouts(self.marionette.TIMEOUT_PAGE, self.marionette.timeout) else: self.marionette.timeouts(self.marionette.TIMEOUT_PAGE, 30000) + if hasattr(self, 'test_container') and self.test_container: self.switch_into_test_container() - else: + elif hasattr(self, 'test_container') and self.test_container is False: if self.marionette.session_capabilities.has_key('b2g') \ and self.marionette.session_capabilities['b2g'] == True: self.close_test_container() @@ -416,56 +417,18 @@ permissions.forEach(function (perm) { self.marionette = None def switch_into_test_container(self): - self.marionette.set_context("content") - frame = None - try: - frame = self.marionette.find_element( - 'css selector', - 'iframe[src*="app://test-container.gaiamobile.org/index.html"]' - ) - except NoSuchElementException: - result = self.marionette.execute_async_script(""" -if((navigator.mozSettings == undefined) || (navigator.mozSettings == null) || (navigator.mozApps == undefined) || (navigator.mozApps == null)) { - marionetteScriptFinished(false); - return; -} -let setReq = navigator.mozSettings.createLock().set({'lockscreen.enabled': false}); -setReq.onsuccess = function() { - let appsReq = navigator.mozApps.mgmt.getAll(); - appsReq.onsuccess = function() { - let apps = appsReq.result; - for (let i = 0; i < apps.length; i++) { - let app = apps[i]; - if (app.manifest.name === 'Test Container') { - app.launch(); - window.addEventListener('apploadtime', function apploadtime(){ - window.removeEventListener('apploadtime', apploadtime); - marionetteScriptFinished(true); - }); - return; - } - } - marionetteScriptFinished(false); - } - appsReq.onerror = function() { - marionetteScriptFinished(false); - } -} -setReq.onerror = function() { - marionetteScriptFinished(false); -}""", script_timeout=60000) + self.marionette.set_context(self.marionette.CONTEXT_CONTENT) - self.assertTrue(result) - frame = Wait(self.marionette, timeout=10, interval=0.2).until(element_present( - 'css selector', - 'iframe[src*="app://test-container.gaiamobile.org/index.html"]' - )) + frame = Wait(self.marionette, timeout=10, interval=0.2).until(element_present( + 'css selector', + 'iframe[src*="app://test-container.gaiamobile.org/index.html"]' + )) self.marionette.switch_to_frame(frame) def close_test_container(self): - self.marionette.set_context("content") - self.marionette.switch_to_frame() + self.marionette.set_context(self.marionette.CONTEXT_CONTENT) + result = self.marionette.execute_async_script(""" if((navigator.mozSettings == undefined) || (navigator.mozSettings == null) || (navigator.mozApps == undefined) || (navigator.mozApps == null)) { marionetteScriptFinished(false); @@ -499,7 +462,10 @@ setReq.onerror = function() { marionetteScriptFinished(false); }""", script_timeout=60000) - frame = Wait(self.marionette, timeout=10, interval=0.2).until(element_not_present( + if not result: + raise Exception('Failed to close Test Container app') + + Wait(self.marionette, timeout=10, interval=0.2).until(element_not_present( 'css selector', 'iframe[src*="app://test-container.gaiamobile.org/index.html"]' )) @@ -517,7 +483,7 @@ class MarionetteTestCase(CommonTestCase): self.methodName = methodName self.filepath = filepath self.testvars = kwargs.pop('testvars', None) - self.test_container = kwargs.pop('test_container', False) + self.test_container = kwargs.pop('test_container', None) CommonTestCase.__init__(self, methodName, **kwargs) @classmethod @@ -588,7 +554,7 @@ class MarionetteJSTestCase(CommonTestCase): self.jsFile = jsFile self._marionette_weakref = marionette_weakref self.marionette = None - self.test_container = kwargs.pop('test_container', False) + self.test_container = kwargs.pop('test_container', None) CommonTestCase.__init__(self, methodName) @classmethod diff --git a/testing/marionette/client/marionette/runner/base.py b/testing/marionette/client/marionette/runner/base.py index 6ba6aab272c3..61534e4d417c 100644 --- a/testing/marionette/client/marionette/runner/base.py +++ b/testing/marionette/client/marionette/runner/base.py @@ -638,6 +638,53 @@ class BaseMarionetteTestRunner(object): def start_marionette(self): self.marionette = Marionette(**self._build_kwargs()) + def launch_test_container(self): + if self.marionette.session is None: + self.marionette.start_session() + self.marionette.set_context(self.marionette.CONTEXT_CONTENT) + + result = self.marionette.execute_async_script(""" +if((navigator.mozSettings == undefined) || (navigator.mozSettings == null) || (navigator.mozApps == undefined) || (navigator.mozApps == null)) { + marionetteScriptFinished(false); + return; +} +let setReq = navigator.mozSettings.createLock().set({'lockscreen.enabled': false}); +setReq.onsuccess = function() { + let appName = 'Test Container'; + let activeApp = window.wrappedJSObject.System.currentApp; + + // if the Test Container is already open then do nothing + if(activeApp.name === appName){ + marionetteScriptFinished(true); + } + + let appsReq = navigator.mozApps.mgmt.getAll(); + appsReq.onsuccess = function() { + let apps = appsReq.result; + for (let i = 0; i < apps.length; i++) { + let app = apps[i]; + if (app.manifest.name === appName) { + app.launch(); + window.addEventListener('appopen', function apploadtime(){ + window.removeEventListener('appopen', apploadtime); + marionetteScriptFinished(true); + }); + return; + } + } + marionetteScriptFinished(false); + } + appsReq.onerror = function() { + marionetteScriptFinished(false); + } +} +setReq.onerror = function() { + marionetteScriptFinished(false); +}""", script_timeout=60000) + + if not result: + raise Exception("Could not launch test container app") + def run_tests(self, tests): self.reset_test_stats() self.start_time = time.time() @@ -724,7 +771,7 @@ class BaseMarionetteTestRunner(object): self.logger.suite_end() - def add_test(self, test, expected='pass', test_container=False): + def add_test(self, test, expected='pass', test_container=None): filepath = os.path.abspath(test) if os.path.isdir(filepath): @@ -778,9 +825,13 @@ class BaseMarionetteTestRunner(object): raise IOError("test file: %s does not exist" % i["path"]) file_ext = os.path.splitext(os.path.split(i['path'])[-1])[-1] - test_container = False - if i.get('test_container') and i.get('test_container') == 'true' and testarg_b2g: - test_container = True + test_container = None + if i.get('test_container') and testarg_b2g: + if i.get('test_container') == "true": + test_container = True + elif i.get('test_container') == "false": + test_container = False + self.add_test(i["path"], i["expected"], test_container) return @@ -811,6 +862,9 @@ class BaseMarionetteTestRunner(object): logcat_stdout=self.logcat_stdout, result_callbacks=self.result_callbacks) + if test_container: + self.launch_test_container() + results = runner.run(suite) self.results.append(results)