test_context.py (3272B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 from marionette_driver.decorators import using_context 6 from marionette_driver.errors import MarionetteException 7 from marionette_harness import MarionetteTestCase 8 9 10 class ContextTestCase(MarionetteTestCase): 11 def setUp(self): 12 super(ContextTestCase, self).setUp() 13 14 # shortcuts to improve readability of these tests 15 self.chrome = self.marionette.CONTEXT_CHROME 16 self.content = self.marionette.CONTEXT_CONTENT 17 18 self.assertEqual(self.get_context(), self.content) 19 20 test_url = self.marionette.absolute_url("empty.html") 21 self.marionette.navigate(test_url) 22 23 def get_context(self): 24 return self.marionette._send_message("Marionette:GetContext", key="value") 25 26 27 class TestSetContext(ContextTestCase): 28 def test_switch_context(self): 29 self.marionette.set_context(self.chrome) 30 self.assertEqual(self.get_context(), self.chrome) 31 32 self.marionette.set_context(self.content) 33 self.assertEqual(self.get_context(), self.content) 34 35 def test_invalid_context(self): 36 with self.assertRaises(ValueError): 37 self.marionette.set_context("foobar") 38 39 40 class TestUsingContext(ContextTestCase): 41 def test_set_different_context_using_with_block(self): 42 with self.marionette.using_context(self.chrome): 43 self.assertEqual(self.get_context(), self.chrome) 44 self.assertEqual(self.get_context(), self.content) 45 46 def test_set_same_context_using_with_block(self): 47 with self.marionette.using_context(self.content): 48 self.assertEqual(self.get_context(), self.content) 49 self.assertEqual(self.get_context(), self.content) 50 51 def test_nested_with_blocks(self): 52 with self.marionette.using_context(self.chrome): 53 self.assertEqual(self.get_context(), self.chrome) 54 with self.marionette.using_context(self.content): 55 self.assertEqual(self.get_context(), self.content) 56 self.assertEqual(self.get_context(), self.chrome) 57 self.assertEqual(self.get_context(), self.content) 58 59 def test_set_scope_while_in_with_block(self): 60 with self.marionette.using_context(self.chrome): 61 self.assertEqual(self.get_context(), self.chrome) 62 self.marionette.set_context(self.content) 63 self.assertEqual(self.get_context(), self.content) 64 self.assertEqual(self.get_context(), self.content) 65 66 def test_exception_raised_while_in_with_block_is_propagated(self): 67 with self.assertRaises(MarionetteException): 68 with self.marionette.using_context(self.chrome): 69 raise MarionetteException 70 self.assertEqual(self.get_context(), self.content) 71 72 def test_with_using_context_decorator(self): 73 @using_context("content") 74 def inner_content(m): 75 self.assertEqual(self.get_context(), "content") 76 77 @using_context("chrome") 78 def inner_chrome(m): 79 self.assertEqual(self.get_context(), "chrome") 80 81 inner_content(self.marionette) 82 inner_chrome(self.marionette)