File: gsettings_tests.py

package info (click to toggle)
keyman 18.0.246-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,316 kB
  • sloc: python: 52,784; cpp: 21,289; sh: 7,633; ansic: 4,823; xml: 3,617; perl: 959; makefile: 139; javascript: 138
file content (227 lines) | stat: -rw-r--r-- 9,207 bytes parent folder | download | duplicates (2)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/python3
import os
import tempfile
import unittest
from unittest.mock import patch

import keyman_config
from keyman_config import _set_dbus_started_for_session, file_cleanup
from keyman_config.gsettings import GSettings

# pylint: disable=unused-argument


class GSettingsTests(unittest.TestCase):
    def setUp(self):
        patcher = patch('keyman_config.gsettings.Gio.Settings.new')
        self.MockSettingsClass = patcher.start()
        self.addCleanup(patcher.stop)

    def tearDown(self) -> None:
        file_cleanup.unregister('keyfile')
        return super().tearDown()

    def test_ConvertArrayToVariantToArray_Empty_Gnome(self):
        # Setup
        children = []
        sut = GSettings('org.gnome.desktop.input-sources')
        # Execute
        variant = sut._convert_array_to_variant(children, 'a(ss)')
        array = sut._convert_variant_to_array(variant)

        # Verify
        self.assertEqual(array, children)

    def test_ConvertArrayToVariantToArray_OneElement_Gnome(self):
        # Setup
        children = [('t1', 'id1')]
        sut = GSettings('org.gnome.desktop.input-sources')
        # Execute
        variant = sut._convert_array_to_variant(children, 'a(ss)')
        array = sut._convert_variant_to_array(variant)

        # Verify
        self.assertEqual(array, children)

    def test_ConvertArrayToVariantToArray_MultipleElements_Gnome(self):
        # Setup
        children = [('t1', 'id1'), ('t2', 'id2')]
        sut = GSettings('org.gnome.desktop.input-sources')
        # Execute
        variant = sut._convert_array_to_variant(children, 'a(ss)')
        array = sut._convert_variant_to_array(variant)

        # Verify
        self.assertEqual(array, children)

    def test_ConvertArrayToVariantToArray_Empty_Ibus(self):
        # Setup
        children = []
        sut = GSettings('org.gnome.desktop.input-sources')
        # Execute
        variant = sut._convert_array_to_variant(children, 'as')
        array = sut._convert_variant_to_array(variant)

        # Verify
        self.assertEqual(array, children)

    def test_ConvertArrayToVariantToArray_OneElement_Ibus(self):
        # Setup
        children = ['t1']
        sut = GSettings('org.gnome.desktop.input-sources')
        # Execute
        variant = sut._convert_array_to_variant(children, 'as')
        array = sut._convert_variant_to_array(variant)

        # Verify
        self.assertEqual(array, children)

    def test_ConvertArrayToVariantToArray_MultipleElements_Ibus(self):
        # Setup
        children = ['t1', 'id1', 't2', 'id2']
        sut = GSettings('org.gnome.desktop.input-sources')
        # Execute
        variant = sut._convert_array_to_variant(children, 'as')
        array = sut._convert_variant_to_array(variant)

        # Verify
        self.assertEqual(array, children)

    @patch.object(GSettings, '_convert_variant_to_array')
    def test_GSettings_get(self, convertVariantToArrayMethod):
        # Setup
        keyboards = [('xkb', 'en')]
        mock_settingsInstance = self.MockSettingsClass.return_value
        mock_settingsInstance.get_value.return_value = keyboards
        convertVariantToArrayMethod.side_effect = lambda value: value
        sut = GSettings('org.gnome.desktop.input-sources')
        # Execute
        result = sut.get('sources')
        # Verify
        self.assertEqual(result, keyboards)

    @patch.object(GSettings, '_convert_array_to_variant')
    def test_GSettings_set_gnome(self, convertArrayToVariantMethod):
        # Setup
        convertArrayToVariantMethod.side_effect = lambda value, type: value
        sut = GSettings('org.gnome.desktop.input-sources')
        keyboards = [('xkb', 'en'), ('ibus', 'fooDir/foo1.kmx'), ('ibus', 'fooDir/foo2.kmx')]
        # Execute
        sut.set('sources', keyboards, 'a(ss)')
        # Verify
        self.MockSettingsClass.return_value.set_value.assert_called_once_with(
            'sources', keyboards)

    @patch.object(GSettings, '_convert_array_to_variant')
    def test_GSettings_set_ibus(self, convertArrayToVariantMethod):
        # Setup
        convertArrayToVariantMethod.side_effect = lambda value, type: value
        sut = GSettings('org.freedesktop.ibus.general')
        keyboards = ['xkb:us::eng', 'de:fooDir/foo1.kmx', 'fr:fooDir/foo2.kmx']
        # Execute
        sut.set('sources', keyboards, 'as')
        # Verify
        self.MockSettingsClass.return_value.set_value.assert_called_once_with(
            'sources', keyboards)

    @patch.object(GSettings, '_convert_array_to_variant')
    @patch('pathlib.Path.unlink')
    @patch('subprocess.run')
    def test_GSettings_set_ibus_keyfile(self, patched_run, patched_unlink,
                                        convertArrayToVariantMethod):
        with tempfile.TemporaryDirectory() as configdir:
            mock_xdg_config_home = patch.object(keyman_config.gsettings,
                                                'xdg_config_home', configdir)
            with mock_xdg_config_home:
                # Setup
                convertArrayToVariantMethod.side_effect = lambda value, type: value
                os.environ['XDG_CONFIG_HOME'] = configdir
                os.makedirs(os.path.join(configdir, 'dconf'))
                sut = GSettings('org.freedesktop.ibus.general')
                keyboards = ['xkb:us::eng', 'de:fooDir/foo1.kmx', 'fr:fooDir/foo2.kmx']
                # Simulate running under a different user account
                _set_dbus_started_for_session(True)

                # Execute
                sut.set('sources', keyboards, 'as')

                # Verify
                keyfile = os.path.join(configdir, 'dconf', 'user.d', 'keyman-settings')
                self.assertTrue(os.path.exists(keyfile))
                with open(keyfile, encoding='utf-8') as file:
                    content = file.read()
                    self.assertEqual(content, '''[org/freedesktop/ibus/general]
sources = @as ['xkb:us::eng', 'de:fooDir/foo1.kmx', 'fr:fooDir/foo2.kmx']

''')

    @patch.object(GSettings, '_convert_array_to_variant')
    @patch('pathlib.Path.unlink')
    @patch('subprocess.run')
    def test_GSettings_set_ibus_keyfile_update(self, patched_run, patched_unlink,
                                               convertArrayToVariantMethod):
        with tempfile.TemporaryDirectory() as configdir:
            mock_xdg_config_home = patch.object(keyman_config.gsettings,
                                                'xdg_config_home', configdir)
            with mock_xdg_config_home:
                # Setup
                convertArrayToVariantMethod.side_effect = lambda value, type: value
                # Simulate running under a different user account
                _set_dbus_started_for_session(True)
                os.environ['XDG_CONFIG_HOME'] = configdir
                os.makedirs(os.path.join(configdir, 'dconf', 'user.d'))
                keyfile = os.path.join(configdir, 'dconf', 'user.d', 'keyman-settings')
                with open(keyfile, mode='w', encoding='utf-8') as file:
                    file.write('''[com/keyman/engine]
additional-keyboards = []

[org/freedesktop/ibus/general]
sources=['xkb:us::eng']
''')
                sut = GSettings('org.freedesktop.ibus.general')
                keyboards = ['xkb:us::eng', 'de:fooDir/foo1.kmx', 'fr:fooDir/foo2.kmx']

                # Execute
                sut.set('sources', keyboards, 'as')

                # Verify
                with open(keyfile, encoding='utf-8') as file:
                    content = file.read()
                    self.assertEqual(content, '''[com/keyman/engine]
additional-keyboards = []

[org/freedesktop/ibus/general]
sources = @as ['xkb:us::eng', 'de:fooDir/foo1.kmx', 'fr:fooDir/foo2.kmx']

''')

    @patch.object(GSettings, '_convert_array_to_variant')
    @patch('pathlib.Path.unlink')
    @patch('subprocess.run')
    def test_GSettings_set_ibus_keyfile_empty(self, patched_run, patched_unlink,
                                              convertArrayToVariantMethod):
        with tempfile.TemporaryDirectory() as configdir:
            mock_xdg_config_home = patch.object(keyman_config.gsettings,
                                                'xdg_config_home', configdir)
            with mock_xdg_config_home:
                # Setup
                convertArrayToVariantMethod.side_effect = lambda value, type: value
                os.environ['XDG_CONFIG_HOME'] = configdir
                os.makedirs(os.path.join(configdir, 'dconf'))
                sut = GSettings('org.freedesktop.ibus.general')
                keyboards = []
                # Simulate running under a different user account
                _set_dbus_started_for_session(True)

                # Execute
                sut.set('sources', keyboards, 'as')

                # Verify
                keyfile = os.path.join(configdir, 'dconf', 'user.d', 'keyman-settings')
                self.assertTrue(os.path.exists(keyfile))
                with open(keyfile, encoding='utf-8') as file:
                    content = file.read()
                    self.assertEqual(content, '''[org/freedesktop/ibus/general]
sources = @as []

''')