Debian Codegraph @

tests/autopilot/lomiriuitoolkit/_custom_proxy_objects/_textfield.py

raw
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Copyright (C) 2012, 2013, 2014 Canonical Ltd.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; version 3.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import logging
18
19 from autopilot import logging as autopilot_logging
20
21 from lomiriuitoolkit._custom_proxy_objects import _common, _mainview
22
23 logger = logging.getLogger(__name__)
24
25
26 class TextField(_common.LomiriUIToolkitCustomProxyObjectBase):
27 """TextField Autopilot custom proxy object."""
28
29 def __init__(self, *args):
30 super().__init__(*args)
31 self.keyboard = _common.get_keyboard()
32
33 @autopilot_logging.log_action(logger.info)
34 def write(self, text, clear=True):
35 """Write into the text field.
36
37 :parameter text: The text to write.
38 :parameter clear: If True, the text field will be cleared before
39 writing the text. If False, the text will be appended at the end
40 of the text field. Default is True.
41
42 """
43 self._ensure_focused()
44 if clear:
45 self.clear()
46 else:
47 if not self.is_empty():
48 self._go_to_end()
49 self.keyboard.type(text)
50
51 @autopilot_logging.log_action(logger.info)
52 def clear(self):
53 """Clear the text field."""
54 self._ensure_focused()
55 if not self.is_empty():
56 if self.hasClearButton:
57 self._click_clear_button()
58 else:
59 self._clear_with_keys()
60 self.text.wait_for('')
61
62 def is_empty(self):
63 """Return True if the text field is empty. False otherwise."""
64 return self.text == ''
65
66 @autopilot_logging.log_action(logger.debug)
67 def _click_clear_button(self):
68 clear_button = self.select_single(objectName='clear_button')
69 if not clear_button.visible:
70 self.pointing_device.click_object(self)
71 self.pointing_device.click_object(clear_button)
72
73 @autopilot_logging.log_action(logger.debug)
74 def _clear_with_keys(self):
75 self._select_all()
76 self.keyboard.press_and_release('\b')
77 if not self.is_empty():
78 raise _common.ToolkitException('Failed to clear the text field.')
79
80 @autopilot_logging.log_action(logger.debug)
81 def _select_all(self):
82 if not self._is_all_text_selected():
83 if self._is_keyboard_osk():
84 cursor = self.select_single('TextCursor', visible=True)
85 self.pointing_device.click_object(
86 cursor, time_between_events=0.5)
87 self.pointing_device.click_object(
88 cursor, time_between_events=0.5)
89 root = self.get_root_instance()
90 main_view = root.select_single(_mainview.MainView)
91 popover = main_view.get_text_input_context_menu(
92 'text_input_contextmenu')
93 popover.click_option_by_text('Select All')
94 else:
95 self._go_to_start()
96 self.keyboard.press('Shift')
97 self._go_to_end()
98 self.keyboard.release('Shift')
99
100 def _is_all_text_selected(self):
101 return self.text == self.selectedText
102
103 def _is_keyboard_osk(self):
104 """Return True if the keyboard instance is the OSK."""
105 return _common.is_maliit_process_running()
106
107 @autopilot_logging.log_action(logger.debug)
108 def _go_to_end(self):
109 if self._is_keyboard_osk():
110 from lomiri_keyboard.emulators.keyboard import Keyboard
111 Keyboard().send_end_key()
112 else:
113 self.keyboard.press_and_release('End')
114
115 @autopilot_logging.log_action(logger.debug)
116 def _go_to_start(self):
117 if self._is_keyboard_osk():
118 from lomiri_keyboard.emulators.keyboard import Keyboard
119 Keyboard().send_home_key()
120 else:
121 self.keyboard.press_and_release('Home')
122
123 @autopilot_logging.log_action(logger.debug)
124 def _ensure_focused(self):
125 if not self.activeFocus:
126 self.pointing_device.click_object(self)
127 self.activeFocus.wait_for(True)