summaryrefslogtreecommitdiffstats
path: root/xpcom/components/test/test_gen_static_components.py
blob: 85f731c082f9b490c6514c7b4fbd45c2e92c8222 (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
# 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 os
import sys
import unittest

import mozunit

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import gen_static_components
from gen_static_components import BackgroundTasksSelector


class TestGenStaticComponents(unittest.TestCase):
    def test_string(self):
        # A string: we default to NO_TASKS.
        clas = {
            "cid": "{a8566880-0bc7-4822-adb9-748c9af5cce7}",
            "contract_ids": ["@mozilla.org/dummy-class;1"],
            "jsm": "resource:///modules/DummyClass.jsm",
            "js_name": "dummyClass",
            "constructor": "DummyClassImpl",
            "categories": {
                "dummy1": ["m-dummy1", "m-dummy2"],
            },
            "protocol_config": {
                "scheme": "dummy",
                "flags": [],
            },
        }

        substs = gen_static_components.gen_substs([{"Classes": [clas]}])

        self.assertEqual(substs["category_count"], 1)
        self.assertEqual(
            [s.strip() for s in substs["categories"].splitlines()],
            [
                '{ { 0x0 } /* "dummy1" */,',
                "0, 2 },",
            ],
        )
        self.assertEqual(
            [s.strip() for s in substs["category_entries"].splitlines()],
            [
                '/* "dummy1" */',
                '{ { 0x7 } /* "m-dummy1" */,',
                '{ 0x10 } /* "@mozilla.org/dummy-class;1" */,',
                "Module::BackgroundTasksSelector::NO_TASKS,",
                "Module::ProcessSelector::ANY_PROCESS },",
                '{ { 0x2b } /* "m-dummy2" */,',
                '{ 0x10 } /* "@mozilla.org/dummy-class;1" */,',
                "Module::BackgroundTasksSelector::NO_TASKS,",
                "Module::ProcessSelector::ANY_PROCESS },",
            ],
        )

    def test_dict(self):
        # A dict, but no backgroundtasks selector: we default to NO_TASKS.
        clas = {
            "cid": "{a8566880-0bc7-4822-adb9-748c9af5cce7}",
            "contract_ids": ["@mozilla.org/dummy-class;1"],
            "jsm": "resource:///modules/DummyClass.jsm",
            "js_name": "dummyClass",
            "constructor": "DummyClassImpl",
            "categories": {
                "dummy1": {
                    "name": ["m-dummy1", "m-dummy2"],
                },
            },
            "protocol_config": {
                "scheme": "dummy",
                "flags": [],
            },
        }

        substs = gen_static_components.gen_substs([{"Classes": [clas]}])

        self.assertEqual(substs["category_count"], 1)
        self.assertEqual(
            [s.strip() for s in substs["categories"].splitlines()],
            [
                '{ { 0x0 } /* "dummy1" */,',
                "0, 2 },",
            ],
        )
        self.assertEqual(
            [s.strip() for s in substs["category_entries"].splitlines()],
            [
                '/* "dummy1" */',
                '{ { 0x7 } /* "m-dummy1" */,',
                '{ 0x10 } /* "@mozilla.org/dummy-class;1" */,',
                "Module::BackgroundTasksSelector::NO_TASKS,",
                "Module::ProcessSelector::ANY_PROCESS },",
                '{ { 0x2b } /* "m-dummy2" */,',
                '{ 0x10 } /* "@mozilla.org/dummy-class;1" */,',
                "Module::BackgroundTasksSelector::NO_TASKS,",
                "Module::ProcessSelector::ANY_PROCESS },",
            ],
        )

    def test_dict_with_selector(self):
        # A dict with a selector.
        clas = {
            "cid": "{a8566880-0bc7-4822-adb9-748c9af5cce7}",
            "contract_ids": ["@mozilla.org/dummy-class;1"],
            "jsm": "resource:///modules/DummyClass.jsm",
            "js_name": "dummyClass",
            "constructor": "DummyClassImpl",
            "categories": {
                "dummy1": {
                    "name": ["m-dummy1", "m-dummy2"],
                    "backgroundtasks": BackgroundTasksSelector.ALL_TASKS,
                },
            },
            "protocol_config": {
                "scheme": "dummy",
                "flags": [],
            },
        }

        substs = gen_static_components.gen_substs([{"Classes": [clas]}])

        self.assertEqual(substs["category_count"], 1)
        self.assertEqual(
            [s.strip() for s in substs["categories"].splitlines()],
            [
                '{ { 0x0 } /* "dummy1" */,',
                "0, 2 },",
            ],
        )
        self.assertEqual(
            [s.strip() for s in substs["category_entries"].splitlines()],
            [
                '/* "dummy1" */',
                '{ { 0x7 } /* "m-dummy1" */,',
                '{ 0x10 } /* "@mozilla.org/dummy-class;1" */,',
                "Module::BackgroundTasksSelector::ALL_TASKS,",
                "Module::ProcessSelector::ANY_PROCESS },",
                '{ { 0x2b } /* "m-dummy2" */,',
                '{ 0x10 } /* "@mozilla.org/dummy-class;1" */,',
                "Module::BackgroundTasksSelector::ALL_TASKS,",
                "Module::ProcessSelector::ANY_PROCESS },",
            ],
        )


if __name__ == "__main__":
    mozunit.main()