~3v1n0/autopilot/badwindow-errors-protect

« back to all changes in this revision

Viewing changes to autopilot/tests/test_ibus.py

  • Committer: Thomi Richards
  • Date: 2012-05-06 22:45:27 UTC
  • Revision ID: thomi.richards@canonical.com-20120506224527-xh6wixqiw0rarkmh
Imported code from unity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2012 Canonical
 
3
# Author: Thomi Richards, Martin Mrazik
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
 
 
9
"""Tests to ensure unity is compatible with ibus input method."""
 
10
 
 
11
from testtools.matchers import Equals, NotEquals
 
12
 
 
13
from autopilot.emulators.ibus import (
 
14
    set_active_engines,
 
15
    get_available_input_engines,
 
16
    )
 
17
from autopilot.matchers import Eventually
 
18
from autopilot.tests import AutopilotTestCase, multiply_scenarios
 
19
 
 
20
 
 
21
class IBusTests(AutopilotTestCase):
 
22
    """Base class for IBus tests."""
 
23
 
 
24
    def setUp(self):
 
25
        super(IBusTests, self).setUp()
 
26
 
 
27
    def tearDown(self):
 
28
        super(IBusTests, self).tearDown()
 
29
 
 
30
    @classmethod
 
31
    def setUpClass(cls):
 
32
        cls._old_engines = None
 
33
        cls.activate_input_engine_or_skip(cls.engine_name)
 
34
 
 
35
    @classmethod
 
36
    def tearDownClass(cls):
 
37
        if cls._old_engines is not None:
 
38
            set_active_engines(cls._old_engines)
 
39
 
 
40
    @classmethod
 
41
    def activate_input_engine_or_skip(cls, engine_name):
 
42
        available_engines = get_available_input_engines()
 
43
        if engine_name in available_engines:
 
44
            cls._old_engines = set_active_engines([engine_name])
 
45
        else:
 
46
            raise AutopilotTestCase.skipException("This test requires the '%s' engine to be installed." % (engine_name))
 
47
 
 
48
    def activate_ibus(self, widget):
 
49
        """Activate IBus, and wait till it's actived on 'widget'"""
 
50
        self.assertThat(widget.im_active, Equals(False))
 
51
        self.keyboard.press_and_release('Ctrl+Space', 0.05)
 
52
        self.assertThat(widget.im_active, Eventually(Equals(True)))
 
53
 
 
54
    def deactivate_ibus(self, widget):
 
55
        """Deactivate ibus, and wait till it's inactive on 'widget'"""
 
56
        self.assertThat(widget.im_active, Equals(True))
 
57
        self.keyboard.press_and_release('Ctrl+Space', 0.05)
 
58
        self.assertThat(widget.im_active, Eventually(Equals(False)))
 
59
 
 
60
    def do_dash_test_with_engine(self):
 
61
        self.dash.ensure_visible()
 
62
        self.addCleanup(self.dash.ensure_hidden)
 
63
        self.activate_ibus(self.dash.searchbar)
 
64
        self.keyboard.type(self.input)
 
65
        commit_key = getattr(self, 'commit_key', None)
 
66
        if commit_key:
 
67
            self.keyboard.press_and_release(commit_key)
 
68
        self.deactivate_ibus(self.dash.searchbar)
 
69
        self.assertThat(self.dash.search_string, Eventually(Equals(self.result)))
 
70
 
 
71
    def do_hud_test_with_engine(self):
 
72
        self.hud.ensure_visible()
 
73
        self.addCleanup(self.hud.ensure_hidden)
 
74
        self.activate_ibus(self.hud.searchbar)
 
75
        self.keyboard.type(self.input)
 
76
        commit_key = getattr(self, 'commit_key', None)
 
77
        if commit_key:
 
78
            self.keyboard.press_and_release(commit_key)
 
79
        self.deactivate_ibus(self.hud.searchbar)
 
80
        self.assertThat(self.hud.search_string, Eventually(Equals(self.result)))
 
81
 
 
82
 
 
83
class IBusTestsPinyin(IBusTests):
 
84
    """Tests for the Pinyin(Chinese) input engine."""
 
85
 
 
86
    engine_name = "pinyin"
 
87
 
 
88
    scenarios = [
 
89
        ('basic', {'input': 'abc1', 'result': u'\u963f\u5e03\u4ece'}),
 
90
        ('photo', {'input': 'zhaopian ', 'result': u'\u7167\u7247'}),
 
91
        ('internet', {'input': 'hulianwang ', 'result': u'\u4e92\u8054\u7f51'}),
 
92
        ('disk', {'input': 'cipan ', 'result': u'\u78c1\u76d8'}),
 
93
        ('disk_management', {'input': 'cipan guanli ', 'result': u'\u78c1\u76d8\u7ba1\u7406'}),
 
94
    ]
 
95
 
 
96
    def test_simple_input_dash(self):
 
97
        self.do_dash_test_with_engine()
 
98
 
 
99
    def test_simple_input_hud(self):
 
100
        self.do_hud_test_with_engine()
 
101
 
 
102
 
 
103
class IBusTestsHangul(IBusTests):
 
104
    """Tests for the Hangul(Korean) input engine."""
 
105
 
 
106
    engine_name = "hangul"
 
107
 
 
108
    scenarios = [
 
109
        ('transmission', {'input': 'xmfostmaltus ', 'result': u'\ud2b8\ub79c\uc2a4\ubbf8\uc158 '}),
 
110
        ('social', {'input': 'httuf ', 'result': u'\uc18c\uc15c '}),
 
111
        ('document', {'input': 'anstj ', 'result': u'\ubb38\uc11c '}),
 
112
        ]
 
113
 
 
114
    def test_simple_input_dash(self):
 
115
        self.do_dash_test_with_engine()
 
116
 
 
117
    def test_simple_input_hud(self):
 
118
        self.do_hud_test_with_engine()
 
119
 
 
120
 
 
121
class IBusTestsAnthy(IBusTests):
 
122
    """Tests for the Anthy(Japanese) input engine."""
 
123
 
 
124
    engine_name = "anthy"
 
125
 
 
126
    scenarios = multiply_scenarios(
 
127
        [
 
128
            ('system', {'input': 'shisutemu ', 'result': u'\u30b7\u30b9\u30c6\u30e0'}),
 
129
            ('game', {'input': 'ge-mu ', 'result': u'\u30b2\u30fc\u30e0'}),
 
130
            ('user', {'input': 'yu-za- ', 'result': u'\u30e6\u30fc\u30b6\u30fc'}),
 
131
        ],
 
132
        [
 
133
            ('commit_j', {'commit_key': 'Ctrl+j'}),
 
134
            ('commit_enter', {'commit_key': 'Enter'}),
 
135
        ]
 
136
        )
 
137
 
 
138
    def test_simple_input_dash(self):
 
139
        self.do_dash_test_with_engine()
 
140
 
 
141
    def test_simple_input_hud(self):
 
142
        self.do_hud_test_with_engine()
 
143
 
 
144
 
 
145
class IBusTestsPinyinIgnore(IBusTests):
 
146
    """Tests for ignoring key events while the Pinyin input engine is active."""
 
147
 
 
148
    engine_name = "pinyin"
 
149
 
 
150
    def test_ignore_key_events_on_dash(self):
 
151
        self.dash.ensure_visible()
 
152
        self.addCleanup(self.dash.ensure_hidden)
 
153
        self.activate_ibus(self.dash.searchbar)
 
154
        self.keyboard.type("cipan")
 
155
        self.keyboard.press_and_release("Tab")
 
156
        self.keyboard.type("  ")
 
157
        self.deactivate_ibus(self.dash.searchbar)
 
158
        self.assertThat(self.dash.search_string, Eventually(NotEquals("  ")))
 
159
 
 
160
    def test_ignore_key_events_on_hud(self):
 
161
        self.hud.ensure_visible()
 
162
        self.addCleanup(self.hud.ensure_hidden)
 
163
 
 
164
        self.keyboard.type("a")
 
165
        self.activate_ibus(self.hud.searchbar)
 
166
        self.keyboard.type("riqi")
 
167
        old_selected = self.hud.selected_button
 
168
        self.keyboard.press_and_release("Down")
 
169
        new_selected = self.hud.selected_button
 
170
        self.deactivate_ibus(self.hud.searchbar)
 
171
 
 
172
        self.assertEqual(old_selected, new_selected)
 
173
 
 
174
 
 
175
class IBusTestsAnthyIgnore(IBusTests):
 
176
    """Tests for ignoring key events while the Anthy input engine is active."""
 
177
 
 
178
    engine_name = "anthy"
 
179
 
 
180
    def test_ignore_key_events_on_dash(self):
 
181
        self.dash.ensure_visible()
 
182
        self.addCleanup(self.dash.ensure_hidden)
 
183
        self.activate_ibus(self.dash.searchbar)
 
184
        self.keyboard.type("shisutemu ")
 
185
        self.keyboard.press_and_release("Tab")
 
186
        self.keyboard.press_and_release("Ctrl+j")
 
187
        self.deactivate_ibus(self.dash.searchbar)
 
188
        dash_search_string = self.dash.search_string
 
189
 
 
190
        self.assertNotEqual("", dash_search_string)
 
191
 
 
192
    def test_ignore_key_events_on_hud(self):
 
193
        self.hud.ensure_visible()
 
194
        self.addCleanup(self.hud.ensure_hidden)
 
195
        self.keyboard.type("a")
 
196
        self.activate_ibus(self.hud.searchbar)
 
197
        self.keyboard.type("hiduke")
 
198
        old_selected = self.hud.selected_button
 
199
        self.keyboard.press_and_release("Down")
 
200
        new_selected = self.hud.selected_button
 
201
        self.deactivate_ibus(self.hud.searchbar)
 
202
 
 
203
        self.assertEqual(old_selected, new_selected)