Back to index

unity  6.0.0
test_ibus.py
Go to the documentation of this file.
00001 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
00002 # Copyright 2012 Canonical
00003 # Author: Thomi Richards, Martin Mrazik
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License version 3, as published
00007 # by the Free Software Foundation.
00008 
00009 """Tests to ensure unity is compatible with ibus input method."""
00010 
00011 from __future__ import absolute_import
00012 
00013 from autopilot.emulators.ibus import (
00014     get_active_input_engines,
00015     set_active_engines,
00016     get_available_input_engines,
00017     )
00018 from autopilot.matchers import Eventually
00019 from autopilot.testcase import multiply_scenarios
00020 from testtools.matchers import Equals, NotEquals
00021 
00022 from unity.tests import UnityTestCase
00023 
00024 
00025 class IBusTests(UnityTestCase):
00026     """Base class for IBus tests."""
00027 
00028     def setUp(self):
00029         super(IBusTests, self).setUp()
00030 
00031     def tearDown(self):
00032         super(IBusTests, self).tearDown()
00033 
00034     @classmethod
00035     def setUpClass(cls):
00036         cls._old_engines = None
00037 
00038     @classmethod
00039     def tearDownClass(cls):
00040         if cls._old_engines is not None:
00041             set_active_engines(cls._old_engines)
00042 
00043     def activate_input_engine_or_skip(self, engine_name):
00044         available_engines = get_available_input_engines()
00045         if engine_name in available_engines:
00046             if get_active_input_engines() != [engine_name]:
00047                 IBusTests._old_engines = set_active_engines([engine_name])
00048         else:
00049             self.skip("This test requires the '%s' engine to be installed." % (engine_name))
00050 
00051     def activate_ibus(self, widget):
00052         """Activate IBus, and wait till it's actived on 'widget'"""
00053         self.assertThat(widget.im_active, Equals(False))
00054         self.keyboard.press_and_release('Ctrl+Space', 0.05)
00055         self.assertThat(widget.im_active, Eventually(Equals(True)))
00056 
00057     def deactivate_ibus(self, widget):
00058         """Deactivate ibus, and wait till it's inactive on 'widget'"""
00059         self.assertThat(widget.im_active, Equals(True))
00060         self.keyboard.press_and_release('Ctrl+Space', 0.05)
00061         self.assertThat(widget.im_active, Eventually(Equals(False)))
00062 
00063     def do_dash_test_with_engine(self):
00064         self.dash.ensure_visible()
00065         self.addCleanup(self.dash.ensure_hidden)
00066         self.activate_ibus(self.dash.searchbar)
00067         self.keyboard.type(self.input)
00068         commit_key = getattr(self, 'commit_key', None)
00069         if commit_key:
00070             self.keyboard.press_and_release(commit_key)
00071         self.deactivate_ibus(self.dash.searchbar)
00072         self.assertThat(self.dash.search_string, Eventually(Equals(self.result)))
00073 
00074     def do_hud_test_with_engine(self):
00075         self.hud.ensure_visible()
00076         self.addCleanup(self.hud.ensure_hidden)
00077         self.activate_ibus(self.hud.searchbar)
00078         self.keyboard.type(self.input)
00079         commit_key = getattr(self, 'commit_key', None)
00080         if commit_key:
00081             self.keyboard.press_and_release(commit_key)
00082         self.deactivate_ibus(self.hud.searchbar)
00083         self.assertThat(self.hud.search_string, Eventually(Equals(self.result)))
00084 
00085 
00086 class IBusTestsPinyin(IBusTests):
00087     """Tests for the Pinyin(Chinese) input engine."""
00088 
00089     engine_name = "pinyin"
00090 
00091     scenarios = [
00092         ('basic', {'input': 'abc1', 'result': u'\u963f\u5e03\u4ece'}),
00093         ('photo', {'input': 'zhaopian ', 'result': u'\u7167\u7247'}),
00094         ('internet', {'input': 'hulianwang ', 'result': u'\u4e92\u8054\u7f51'}),
00095         ('disk', {'input': 'cipan ', 'result': u'\u78c1\u76d8'}),
00096         ('disk_management', {'input': 'cipan guanli ', 'result': u'\u78c1\u76d8\u7ba1\u7406'}),
00097     ]
00098 
00099     def setUp(self):
00100         super(IBusTestsPinyin, self).setUp()
00101         self.activate_input_engine_or_skip(self.engine_name)
00102 
00103     def test_simple_input_dash(self):
00104         self.do_dash_test_with_engine()
00105 
00106     def test_simple_input_hud(self):
00107         self.do_hud_test_with_engine()
00108 
00109 
00110 class IBusTestsHangul(IBusTests):
00111     """Tests for the Hangul(Korean) input engine."""
00112 
00113     engine_name = "hangul"
00114 
00115     scenarios = [
00116         ('transmission', {'input': 'xmfostmaltus ', 'result': u'\ud2b8\ub79c\uc2a4\ubbf8\uc158 '}),
00117         ('social', {'input': 'httuf ', 'result': u'\uc18c\uc15c '}),
00118         ('document', {'input': 'anstj ', 'result': u'\ubb38\uc11c '}),
00119         ]
00120 
00121     def setUp(self):
00122         super(IBusTestsHangul, self).setUp()
00123         self.activate_input_engine_or_skip(self.engine_name)
00124 
00125     def test_simple_input_dash(self):
00126         self.do_dash_test_with_engine()
00127 
00128     def test_simple_input_hud(self):
00129         self.do_hud_test_with_engine()
00130 
00131 
00132 class IBusTestsAnthy(IBusTests):
00133     """Tests for the Anthy(Japanese) input engine."""
00134 
00135     engine_name = "anthy"
00136 
00137     scenarios = multiply_scenarios(
00138         [
00139             ('system', {'input': 'shisutemu ', 'result': u'\u30b7\u30b9\u30c6\u30e0'}),
00140             ('game', {'input': 'ge-mu ', 'result': u'\u30b2\u30fc\u30e0'}),
00141             ('user', {'input': 'yu-za- ', 'result': u'\u30e6\u30fc\u30b6\u30fc'}),
00142         ],
00143         [
00144             ('commit_j', {'commit_key': 'Ctrl+j'}),
00145             ('commit_enter', {'commit_key': 'Enter'}),
00146         ]
00147         )
00148 
00149     def setUp(self):
00150         super(IBusTestsAnthy, self).setUp()
00151         self.activate_input_engine_or_skip(self.engine_name)
00152 
00153     def test_simple_input_dash(self):
00154         self.do_dash_test_with_engine()
00155 
00156     def test_simple_input_hud(self):
00157         self.do_hud_test_with_engine()
00158 
00159 
00160 class IBusTestsPinyinIgnore(IBusTests):
00161     """Tests for ignoring key events while the Pinyin input engine is active."""
00162 
00163     engine_name = "pinyin"
00164 
00165     def setUp(self):
00166         super(IBusTestsPinyinIgnore, self).setUp()
00167         self.activate_input_engine_or_skip(self.engine_name)
00168 
00169     def test_ignore_key_events_on_dash(self):
00170         self.dash.ensure_visible()
00171         self.addCleanup(self.dash.ensure_hidden)
00172         self.activate_ibus(self.dash.searchbar)
00173         self.keyboard.type("cipan")
00174         self.keyboard.press_and_release("Tab")
00175         self.keyboard.type("  ")
00176         self.deactivate_ibus(self.dash.searchbar)
00177         self.assertThat(self.dash.search_string, Eventually(NotEquals("  ")))
00178 
00179     def test_ignore_key_events_on_hud(self):
00180         self.hud.ensure_visible()
00181         self.addCleanup(self.hud.ensure_hidden)
00182 
00183         self.keyboard.type("a")
00184         self.activate_ibus(self.hud.searchbar)
00185         self.keyboard.type("riqi")
00186         old_selected = self.hud.selected_button
00187         self.keyboard.press_and_release("Down")
00188         new_selected = self.hud.selected_button
00189         self.deactivate_ibus(self.hud.searchbar)
00190 
00191         self.assertEqual(old_selected, new_selected)
00192 
00193 
00194 class IBusTestsAnthyIgnore(IBusTests):
00195     """Tests for ignoring key events while the Anthy input engine is active."""
00196 
00197     engine_name = "anthy"
00198 
00199     def setUp(self):
00200         super(IBusTestsAnthyIgnore, self).setUp()
00201         self.activate_input_engine_or_skip(self.engine_name)
00202 
00203     def test_ignore_key_events_on_dash(self):
00204         self.dash.ensure_visible()
00205         self.addCleanup(self.dash.ensure_hidden)
00206         self.activate_ibus(self.dash.searchbar)
00207         self.keyboard.type("shisutemu ")
00208         self.keyboard.press_and_release("Tab")
00209         self.keyboard.press_and_release("Ctrl+j")
00210         self.deactivate_ibus(self.dash.searchbar)
00211         dash_search_string = self.dash.search_string
00212 
00213         self.assertNotEqual("", dash_search_string)
00214 
00215     def test_ignore_key_events_on_hud(self):
00216         self.hud.ensure_visible()
00217         self.addCleanup(self.hud.ensure_hidden)
00218         self.keyboard.type("a")
00219         self.activate_ibus(self.hud.searchbar)
00220         self.keyboard.type("hiduke")
00221         old_selected = self.hud.selected_button
00222         self.keyboard.press_and_release("Down")
00223         new_selected = self.hud.selected_button
00224         self.deactivate_ibus(self.hud.searchbar)
00225 
00226         self.assertEqual(old_selected, new_selected)