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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
# Copyright (c) 2013 - The IBus Cangjie authors
#
# This file is part of ibus-cangjie, the IBus Cangjie input method engine.
#
# ibus-cangjie is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ibus-cangjie is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ibus-cangjie. If not, see <http://www.gnu.org/licenses/>.
import math
import unittest
from gi.repository import IBus # type: ignore
from ibus_cangjie.engine import * # type: ignore
class QuickTestCase(unittest.TestCase):
def setUp(self):
self.engine = EngineQuick()
def tearDown(self):
del self.engine
def test_single_key(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
def test_single_key_space_single_char(self):
self.engine.do_process_key_event(IBus.d, 0, 0)
self.engine.do_process_key_event(IBus.space, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 1)
def test_single_key_space_two_candidates(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.space, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 2)
def test_two_candidates_space(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.space, 0, 0)
# Keep track of the first candidate, check later if it was committed
expected = self.engine.lookuptable.get_candidate(0).text
self.engine.do_process_key_event(IBus.space, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
self.assertEqual(self.engine._mock_committed_text, expected)
def test_two_candidates_continue_input(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.space, 0, 0)
# Keep track of the first candidate, check later if it was committed
expected = self.engine.lookuptable.get_candidate(0).text
self.engine.do_process_key_event(IBus.a, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
self.assertEqual(self.engine._mock_committed_text, expected)
def test_auto_candidates(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.a, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 2)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertTrue(self.engine.lookuptable.get_number_of_candidates() > 1)
def test_inexistent_combination(self):
self.engine.do_process_key_event(IBus.x, 0, 0)
self.engine.do_process_key_event(IBus.z, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 2)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
self.assertEqual(len(self.engine.sounds._mock_played_events), 1)
def test_nowildcard(self):
self.engine.do_process_key_event(IBus.d, 0, 0)
self.engine.do_process_key_event(IBus.asterisk, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 2)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
self.engine.do_process_key_event(IBus.d, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
self.assertEqual(len(self.engine._mock_committed_text), 2)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
def test_nowildcard_first(self):
self.engine.do_process_key_event(IBus.asterisk, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
def test_backspace(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.BackSpace, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
def test_backspace_on_multiple_keys(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.BackSpace, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
def test_backspace_on_candidates(self):
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.space, 0, 0)
self.engine.do_process_key_event(IBus.BackSpace, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
def test_escape(self):
self.engine.do_process_key_event(IBus.d, 0, 0)
self.engine.do_process_key_event(IBus.d, 0, 0)
self.engine.do_process_key_event(IBus.Escape, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
def test_symbol(self):
self.engine.do_process_key_event(IBus.at, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
def test_multiple_punctuation(self):
self.engine.do_process_key_event(IBus.comma, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
self.assertEqual(len(self.engine._mock_committed_text), 0)
self.assertTrue(self.engine.lookuptable.get_number_of_candidates() > 1)
def test_char_then_multiple_punctuation(self):
self.engine.do_process_key_event(IBus.d, 0, 0)
self.engine.do_process_key_event(IBus.comma, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertTrue(self.engine.lookuptable.get_number_of_candidates() > 1)
def test_punctuation_then_punctuation(self):
self.engine.do_process_key_event(IBus.comma, 0, 0)
self.engine.do_process_key_event(IBus.comma, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 1)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertTrue(self.engine.lookuptable.get_number_of_candidates() > 1)
def test_commit_with_numpad(self):
self.engine.do_process_key_event(IBus.h, 0, 0)
self.engine.do_process_key_event(IBus.i, 0, 0)
self.engine.do_process_key_event(getattr(IBus, "7"), 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
# Reset the committed text, but keep the value first
expected = self.engine._mock_committed_text[:]
self.engine._mock_committed_text = ""
self.engine.do_process_key_event(IBus.h, 0, 0)
self.engine.do_process_key_event(IBus.i, 0, 0)
self.engine.do_process_key_event(IBus.KP_7, 0, 0)
self.assertEqual(len(self.engine._mock_auxiliary_text), 0)
self.assertEqual(len(self.engine._mock_committed_text), 1)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 0)
# Now check that the same character was committed
self.assertEqual(expected, self.engine._mock_committed_text)
def test_modified_key_events(self):
self.assertFalse(
self.engine.do_process_key_event(IBus.a, 0, IBus.ModifierType.CONTROL_MASK),
"Should not have been processed Ctrl+a",
)
self.assertFalse(
self.engine.do_process_key_event(IBus.a, 0, IBus.ModifierType.MOD1_MASK),
"Should not have been processed Alt+a",
)
self.assertFalse(
self.engine.do_process_key_event(IBus.a, 0, IBus.ModifierType.MOD4_MASK),
"Should not have been processed Super_L+a",
)
self.assertFalse(
self.engine.do_process_key_event(IBus.a, 0, IBus.ModifierType.MOD4_MASK),
"Should not have been processed Mode_switch+a",
)
def test_lookup_with_zero(self):
# Type "ab" that prodcues 14 candidates
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.b, 0, 0)
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 14)
# Key in "0" should return True because it is handled
self.assertTrue(
self.engine.do_process_key_event(IBus.KP_0, 0, 0),
"Pressing '0' in candidate selection should have been handled",
)
self.assertGreater(
self.engine.lookuptable.get_number_of_candidates(),
0,
"Pressing '0' should not clear the lookuptable",
)
self.assertEqual(
len(self.engine._mock_committed_text),
0,
"Pressing '0' should not have committed any text",
)
def test_lookup_with_out_of_bound_index(self):
# Type "ab" that prodcues 14 candidates, 2 pages by default
# with the second page of only 5 candidates.
#
# So pressing page down to the next page and then pressing '6'
# will be out of bound.
self.engine.do_process_key_event(IBus.a, 0, 0)
self.engine.do_process_key_event(IBus.b, 0, 0)
# Checks the number of candidates to see if there is any systematic
# changes.
self.assertEqual(self.engine.lookuptable.get_number_of_candidates(), 14)
# Calculate the out of bound index
num_candidates = self.engine.lookuptable.get_number_of_candidates()
page_size = self.engine.lookuptable.get_page_size()
num_pages = math.ceil(num_candidates / page_size)
remainder = num_candidates % page_size
num_last_candidate = remainder if remainder > 0 else page_size
num_out_of_bound = num_last_candidate + 1
if num_out_of_bound > 9:
# Test won't make sense if the out of bound index is greater than 9.
# This is for the fringe case where the system default changes and
# the last page of the selected input radicals has exactly 9
# candidates.
#
# In which case, please rewrite the test case with other input
# radicals.
self.fail(
f"Test case is invalid, the out of bound index '{num_out_of_bound}' is greater than 9"
)
return
# Press Page_Down until the last page
for _ in range(num_pages - 1):
self.engine.do_process_key_event(IBus.Page_Down, 0, 0)
# The number of item should not be divisible by page size (i.e.
# reminder > 0) to allow the last page to have an out-of-bound num-key.
self.assertGreaterEqual(
remainder,
1,
"The last page should have at least 1 candidate",
)
# Press the number key of the out of bound index
keyval = getattr(IBus, f"KP_{num_out_of_bound}")
self.assertTrue(
self.engine.do_process_key_event(keyval, 0, 0),
f"Pressing '{num_out_of_bound}' in candidate selection should have been handled",
)
self.assertGreater(
self.engine.lookuptable.get_number_of_candidates(),
0,
f"Pressing '{num_out_of_bound}' should not clear the lookuptable",
)
self.assertEqual(
len(self.engine._mock_committed_text),
0,
f"Pressing '{num_out_of_bound}' should not have committed any text",
)
|