1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
# Copyright 2012 Canonical
3
# Author: Thomi Richards, Martin Mrazik
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.
9
"""Tests to ensure unity is compatible with ibus input method."""
11
from testtools.matchers import Equals, NotEquals
13
from autopilot.emulators.ibus import (
15
get_available_input_engines,
17
from autopilot.matchers import Eventually
18
from autopilot.tests import AutopilotTestCase, multiply_scenarios
21
class IBusTests(AutopilotTestCase):
22
"""Base class for IBus tests."""
25
super(IBusTests, self).setUp()
28
super(IBusTests, self).tearDown()
32
cls._old_engines = None
33
cls.activate_input_engine_or_skip(cls.engine_name)
36
def tearDownClass(cls):
37
if cls._old_engines is not None:
38
set_active_engines(cls._old_engines)
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])
46
raise AutopilotTestCase.skipException("This test requires the '%s' engine to be installed." % (engine_name))
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)))
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)))
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)
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)))
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)
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)))
83
class IBusTestsPinyin(IBusTests):
84
"""Tests for the Pinyin(Chinese) input engine."""
86
engine_name = "pinyin"
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'}),
96
def test_simple_input_dash(self):
97
self.do_dash_test_with_engine()
99
def test_simple_input_hud(self):
100
self.do_hud_test_with_engine()
103
class IBusTestsHangul(IBusTests):
104
"""Tests for the Hangul(Korean) input engine."""
106
engine_name = "hangul"
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 '}),
114
def test_simple_input_dash(self):
115
self.do_dash_test_with_engine()
117
def test_simple_input_hud(self):
118
self.do_hud_test_with_engine()
121
class IBusTestsAnthy(IBusTests):
122
"""Tests for the Anthy(Japanese) input engine."""
124
engine_name = "anthy"
126
scenarios = multiply_scenarios(
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'}),
133
('commit_j', {'commit_key': 'Ctrl+j'}),
134
('commit_enter', {'commit_key': 'Enter'}),
138
def test_simple_input_dash(self):
139
self.do_dash_test_with_engine()
141
def test_simple_input_hud(self):
142
self.do_hud_test_with_engine()
145
class IBusTestsPinyinIgnore(IBusTests):
146
"""Tests for ignoring key events while the Pinyin input engine is active."""
148
engine_name = "pinyin"
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(" ")))
160
def test_ignore_key_events_on_hud(self):
161
self.hud.ensure_visible()
162
self.addCleanup(self.hud.ensure_hidden)
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)
172
self.assertEqual(old_selected, new_selected)
175
class IBusTestsAnthyIgnore(IBusTests):
176
"""Tests for ignoring key events while the Anthy input engine is active."""
178
engine_name = "anthy"
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
190
self.assertNotEqual("", dash_search_string)
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)
203
self.assertEqual(old_selected, new_selected)