Unity 8
 All Classes Functions Properties
test_lock_screen.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2012, 2013, 2014 Canonical
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 
20 
21 from __future__ import absolute_import
22 
23 from unity8.process_helpers import unlock_unity
24 from unity8.shell import with_lightdm_mock
25 from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
26 
27 from autopilot.matchers import Eventually
28 import sys
29 from testtools.matchers import Equals
30 import logging
31 
32 logger = logging.getLogger(__name__)
33 
34 # py2 compatible alias for py3
35 if sys.version >= '3':
36  basestring = str
37 
38 
40 
41  """Tests for the lock screen."""
42 
43  scenarios = _get_device_emulation_scenarios()
44 
45  @with_lightdm_mock("single-pin")
47  """Must be able to unlock the PIN entry lock screen."""
48  unity_proxy = self.launch_unity()
49  greeter = self.main_window.get_greeter()
50 
51  if greeter.narrowMode:
52  unlock_unity(unity_proxy)
53  lockscreen = self._wait_for_lockscreen()
54  self._enter_pincode("1234")
55  self.assertThat(lockscreen.shown, Eventually(Equals(False)))
56  else:
57  self._enter_prompt_passphrase("1234")
58  self.assertThat(greeter.shown, Eventually(Equals(False)))
59 
60  @with_lightdm_mock("single-passphrase")
62  """Must be able to unlock the passphrase entry screen."""
63  unity_proxy = self.launch_unity()
64  greeter = self.main_window.get_greeter()
65 
66  if greeter.narrowMode:
67  unlock_unity(unity_proxy)
68  lockscreen = self._wait_for_lockscreen()
69  self._enter_pin_passphrase("password")
70  self.assertThat(lockscreen.shown, Eventually(Equals(False)))
71  else:
72  self._enter_prompt_passphrase("password")
73  self.assertThat(greeter.shown, Eventually(Equals(False)))
74 
75  @with_lightdm_mock("single-pin")
77  """Entering the wrong pin code must not dismiss the lock screen."""
78  unity_proxy = self.launch_unity()
79  greeter = self.main_window.get_greeter()
80 
81  if greeter.narrowMode:
82  unlock_unity(unity_proxy)
83  lockscreen = self._wait_for_lockscreen()
84  self._enter_pincode("4321")
85  pinentryField = self.main_window.get_pinentryField()
86  self.assertThat(pinentryField.text, Eventually(Equals("")))
87  self.assertThat(lockscreen.shown, Eventually(Equals(True)))
88  else:
89  self._enter_prompt_passphrase("4231")
90  prompt = self.main_window.get_greeter().get_prompt()
91  self.assertThat(prompt.text, Eventually(Equals("")))
92  self.assertThat(greeter.shown, Eventually(Equals(True)))
93 
94  @with_lightdm_mock("single-passphrase")
96  """Entering the wrong password must not dismiss the lock screen."""
97  unity_proxy = self.launch_unity()
98  greeter = self.main_window.get_greeter()
99 
100  if greeter.narrowMode:
101  unlock_unity(unity_proxy)
102  lockscreen = self._wait_for_lockscreen()
103  self._enter_pin_passphrase("foobar")
104  pinentryField = self.main_window.get_pinentryField()
105  self.assertThat(pinentryField.text, Eventually(Equals("")))
106  self.assertThat(lockscreen.shown, Eventually(Equals(True)))
107  else:
108  self._enter_prompt_passphrase("foobar")
109  prompt = self.main_window.get_greeter().get_prompt()
110  self.assertThat(prompt.text, Eventually(Equals("")))
111  self.assertThat(greeter.shown, Eventually(Equals(True)))
112 
113  def _wait_for_lockscreen(self):
114  """Wait for the lock screen to load, and return it."""
115  pinPadLoader = self.main_window.get_pinPadLoader()
116  self.assertThat(pinPadLoader.progress, Eventually(Equals(1)))
117  lockscreen = self.main_window.get_lockscreen()
118  self.assertThat(lockscreen.shown, Eventually(Equals(True)))
119  return lockscreen
120 
121  def _enter_pincode(self, code):
122  """Enter code 'code' into the single-pin lightdm pincode entry
123  screen.
124 
125  :param code: must be a string of numeric characters.
126  :raises: TypeError if code is not a string.
127  :raises: ValueError if code contains non-numeric characters.
128 
129  """
130 
131  if not isinstance(code, basestring):
132  raise TypeError(
133  "'code' parameter must be a string, not %r."
134  % type(code)
135  )
136  for num in code:
137  if not num.isdigit():
138  raise ValueError(
139  "'code' parameter contains non-numeric characters."
140  )
141  self.touch.tap_object(self.main_window.get_pinPadButton(int(num)))
142 
143  def _enter_pin_passphrase(self, passphrase):
144  """Enter the password specified in 'passphrase' into the password entry
145  field of the pin lock screen.
146 
147  :param passphrase: The string you want to enter.
148  :raises: TypeError if passphrase is not a string.
149 
150  """
151  if not isinstance(passphrase, basestring):
152  raise TypeError(
153  "'passphrase' parameter must be a string, not %r."
154  % type(passphrase)
155  )
156 
157  pinentryField = self.main_window.get_pinentryField()
158  self.touch.tap_object(pinentryField)
159  self.assertThat(pinentryField.activeFocus, Eventually(Equals(True)))
160  for character in passphrase:
161  self._type_character(character, pinentryField)
162  logger.debug("Typed passphrase: %s", pinentryField.text)
163  self.keyboard.type("\n")
164 
165  def _enter_prompt_passphrase(self, passphrase):
166  """Enter the password specified in 'passphrase' into the password entry
167  field of the main user list's prompt.
168 
169  :param passphrase: The string you want to enter.
170  :raises: TypeError if passphrase is not a string.
171 
172  """
173  if not isinstance(passphrase, basestring):
174  raise TypeError(
175  "'passphrase' parameter must be a string, not %r."
176  % type(passphrase)
177  )
178 
179  prompt = self.main_window.get_greeter().get_prompt()
180  self.touch.tap_object(prompt)
181  self.assertThat(prompt.activeFocus, Eventually(Equals(True)))
182  for character in passphrase:
183  self._type_character(character, prompt)
184  logger.debug("Typed passphrase: %s", prompt.text)
185  self.keyboard.type("\n")
186 
187  def _type_character(self, character, prompt, retries=5):
188  current_text = prompt.text
189  self.keyboard.type(character)
190  try:
191  self.assertThat(
192  prompt.text, Eventually(Equals(current_text + character)))
193  except AssertionError:
194  if retries > 0:
195  self._type_character(character, prompt, retries-1)
196  else:
197  raise