def test_try_custom_proxy_classes_zero_results(self):
     """_try_custom_proxy_classes must return None if no classes match."""
     proxy_class_dict = {'NeverSelected': self.NeverSelected}
     fake_id = self.getUniqueInteger()
     path = '/path/to/NeverSelected'
     state = {}
     with object_registry.patch_registry({fake_id: proxy_class_dict}):
         class_type = object_registry._try_custom_proxy_classes(
             fake_id, path, state)
     self.assertThat(class_type, Equals(None))
    def test_handles_using_app_cpo_base_class(self):
        # This test replicates an issue found in an application test suite
        # where using the App CPO caused an exception.
        with object_registry.patch_registry({}):
            class WindowMockerApp(CustomEmulatorBase):
                @classmethod
                def validate_dbus_object(cls, path, _state):
                    return path == b'/window-mocker'

            self.start_mock_app(WindowMockerApp)
 def test_try_custom_proxy_classes_one_result(self):
     """_try_custom_proxy_classes must return the matching class if there is
     exacly 1."""
     proxy_class_dict = {'DefaultSelector': self.DefaultSelector}
     fake_id = self.getUniqueInteger()
     path = '/path/to/DefaultSelector'
     state = {}
     with object_registry.patch_registry({fake_id: proxy_class_dict}):
         class_type = object_registry._try_custom_proxy_classes(
             fake_id, path, state)
     self.assertThat(class_type, Equals(self.DefaultSelector))
 def test_try_custom_proxy_classes_two_results(self):
     """_try_custom_proxy_classes must raise ValueError if multiple classes
     match."""
     proxy_class_dict = {
         'DefaultSelector': self.DefaultSelector,
         'AlwaysSelected': self.AlwaysSelected
     }
     path = '/path/to/DefaultSelector'
     state = {}
     object_id = self.getUniqueInteger()
     with object_registry.patch_registry({object_id: proxy_class_dict}):
         self.assertThat(
             lambda: object_registry._try_custom_proxy_classes(
                 object_id, path, state), raises(ValueError))
    def test_warns_when_using_incorrect_cpo_base_class(self):
        # Ensure the warning method is called when launching a proxy.
        with object_registry.patch_registry({}):
            class TestCPO(CustomEmulatorBase):
                pass

            class WindowMockerApp(TestCPO):
                @classmethod
                def validate_dbus_object(cls, path, _state):
                    return path == b'/window-mocker'

            with patch.object(_search, 'logger') as p_logger:
                self.start_mock_app(WindowMockerApp)
                self.assertTrue(p_logger.warning.called)
    def test_cpo_can_be_named_different_to_underlying_type(self):
        """A CPO with the correct name match method must be matched if the
        class name is different to the Type name.

        """
        with object_registry.patch_registry({}):
            class RandomNamedCPORectangle(CustomEmulatorBase):
                @classmethod
                def get_type_query_name(cls):
                    return 'QQuickRectangle'

            app = self.launch_simple_qml_script()
            rectangle = app.select_single(RandomNamedCPORectangle)

            self.assertThat(rectangle.objectName, Equals('ExampleRectangle'))
    def test_customised_proxy_classes_have_multiple_extension_classes(self):
        with object_registry.patch_registry({}):
            class SecondEmulatorBase(CustomEmulatorBase):
                pass

            class WindowMockerApp(EmulatorBase, SecondEmulatorBase):
                @classmethod
                def validate_dbus_object(cls, path, _state):
                    return path == b'/window-mocker'

            app = self.start_mock_app(EmulatorBase)
            self.assertThat(app.__class__.__bases__, Contains(EmulatorBase))
            self.assertThat(
                app.__class__.__bases__,
                Contains(SecondEmulatorBase)
            )
 def test_modifications_are_unwound(self):
     token = self.getUniqueString()
     with object_registry.patch_registry(dict()):
         object_registry._object_registry[token] = token
     self.assertFalse(token in object_registry._object_registry)
 def patch_reg():
     with object_registry.patch_registry({}):
         raise RuntimeError()
 def test_patch_registry_undoes_patch(self):
     old_registry = object_registry._object_registry.copy()
     with object_registry.patch_registry({}):
         pass
     self.assertEqual(object_registry._object_registry, old_registry)
 def test_patch_registry_sets_new_registry(self):
     new_registry = dict(foo=123)
     with object_registry.patch_registry(new_registry):
         self.assertEqual(object_registry._object_registry, new_registry)