summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/marionette_test/decorators.py
blob: cc3aa091d82465be89c628f9a4de6a16ed522913 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 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/.

import functools
import types
from unittest.case import SkipTest


def parameterized(func_suffix, *args, **kwargs):
    r"""Decorator which generates methods given a base method and some data.

    **func_suffix** is used as a suffix for the new created method and must be
    unique given a base method. if **func_suffix** countains characters that
    are not allowed in normal python function name, these characters will be
    replaced with "_".

    This decorator can be used more than once on a single base method. The class
    must have a metaclass of :class:`MetaParameterized`.

    Example::

      # This example will generate two methods:
      #
      # - MyTestCase.test_it_1
      # - MyTestCase.test_it_2
      #
      class MyTestCase(MarionetteTestCase):
          @parameterized("1", 5, named='name')
          @parameterized("2", 6, named='name2')
          def test_it(self, value, named=None):
              print value, named

    :param func_suffix: will be used as a suffix for the new method
    :param \*args: arguments to pass to the new method
    :param \*\*kwargs: named arguments to pass to the new method
    """

    def wrapped(func):
        if not hasattr(func, "metaparameters"):
            func.metaparameters = []
        func.metaparameters.append((func_suffix, args, kwargs))
        return func

    return wrapped


def run_if_manage_instance(reason):
    """Decorator which runs a test if Marionette manages the application instance."""

    def decorator(test_item):
        if not isinstance(test_item, types.FunctionType):
            raise Exception("Decorator only supported for functions")

        @functools.wraps(test_item)
        def skip_wrapper(self, *args, **kwargs):
            if self.marionette.instance is None:
                raise SkipTest(reason)
            return test_item(self, *args, **kwargs)

        return skip_wrapper

    return decorator


def skip_if_chrome(reason):
    """Decorator which skips a test if chrome context is active."""

    def decorator(test_item):
        if not isinstance(test_item, types.FunctionType):
            raise Exception("Decorator only supported for functions")

        @functools.wraps(test_item)
        def skip_wrapper(self, *args, **kwargs):
            if self.marionette._send_message("getContext", key="value") == "chrome":
                raise SkipTest(reason)
            return test_item(self, *args, **kwargs)

        return skip_wrapper

    return decorator


def skip_if_desktop(reason):
    """Decorator which skips a test if run on desktop."""

    def decorator(test_item):
        if not isinstance(test_item, types.FunctionType):
            raise Exception("Decorator only supported for functions")

        @functools.wraps(test_item)
        def skip_wrapper(self, *args, **kwargs):
            if self.marionette.session_capabilities.get("browserName") == "firefox":
                raise SkipTest(reason)
            return test_item(self, *args, **kwargs)

        return skip_wrapper

    return decorator


def skip_unless_browser_pref(reason, pref, predicate=bool):
    """Decorator which skips a test based on the value of a browser preference.

    :param reason: Message describing why the test need to be skipped.
    :param pref: the preference name
    :param predicate: a function that should return false to skip the test.
                      The function takes one parameter, the preference value.
                      Defaults to the python built-in bool function.

    Note that the preference must exist, else a failure is raised.

    Example: ::

      class TestSomething(MarionetteTestCase):
          @skip_unless_browser_pref("Sessionstore needs to be enabled for crashes",
                                    "browser.sessionstore.resume_from_crash",
                                    lambda value: value is True,
                                    )
          def test_foo(self):
              pass  # test implementation here

    """

    def decorator(test_item):
        if not isinstance(test_item, types.FunctionType):
            raise Exception("Decorator only supported for functions")
        if not callable(predicate):
            raise ValueError("predicate must be callable")

        @functools.wraps(test_item)
        def skip_wrapper(self, *args, **kwargs):
            value = self.marionette.get_pref(pref)
            if value is None:
                self.fail("No such browser preference: {0!r}".format(pref))
            if not predicate(value):
                raise SkipTest(reason)
            return test_item(self, *args, **kwargs)

        return skip_wrapper

    return decorator


def skip_unless_protocol(reason, predicate):
    """Decorator which skips a test if the predicate does not match the current protocol level."""

    def decorator(test_item):
        if not isinstance(test_item, types.FunctionType):
            raise Exception("Decorator only supported for functions")
        if not callable(predicate):
            raise ValueError("predicate must be callable")

        @functools.wraps(test_item)
        def skip_wrapper(self, *args, **kwargs):
            level = self.marionette.client.protocol
            if not predicate(level):
                raise SkipTest(reason)
            return test_item(self, *args, **kwargs)

        return skip_wrapper

    return decorator


def with_parameters(parameters):
    """Decorator which generates methods given a base method and some data.

    Acts like :func:`parameterized`, but define all methods in one call.

    Example::

      # This example will generate two methods:
      #
      # - MyTestCase.test_it_1
      # - MyTestCase.test_it_2
      #

      DATA = [("1", [5], {'named':'name'}), ("2", [6], {'named':'name2'})]

      class MyTestCase(MarionetteTestCase):
          @with_parameters(DATA)
          def test_it(self, value, named=None):
              print value, named

    :param parameters: list of tuples (**func_suffix**, **args**, **kwargs**)
                       defining parameters like in :func:`todo`.
    """

    def wrapped(func):
        func.metaparameters = parameters
        return func

    return wrapped