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 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 from autopilot.platform import model
29 import sys
30 from testtools import skipUnless
31 from testtools.matchers import Equals
32 import logging
33 
34 logger = logging.getLogger(__name__)
35 
36 # py2 compatible alias for py3
37 if sys.version >= '3':
38  basestring = str
39 
40 
42 
43  """Tests for the lock screen."""
44 
45  scenarios = _get_device_emulation_scenarios()
46 
47  @with_lightdm_mock("single-pin")
49  """Must be able to unlock the PIN entry lock screen."""
50  unity_proxy = self.launch_unity()
51  greeter = self.main_window.get_greeter()
52 
53  if greeter.narrowMode:
54  unlock_unity(unity_proxy)
55  lockscreen = self._wait_for_lockscreen()
56  self._enter_pincode("1234")
57  self.assertThat(lockscreen.shown, Eventually(Equals(False)))
58  else:
59  self._enter_prompt_passphrase("1234")
60  self.assertThat(greeter.shown, Eventually(Equals(False)))
61 
62  @with_lightdm_mock("single-passphrase")
64  """Must be able to unlock the passphrase entry screen."""
65  unity_proxy = self.launch_unity()
66  greeter = self.main_window.get_greeter()
67 
68  if greeter.narrowMode:
69  unlock_unity(unity_proxy)
70  lockscreen = self._wait_for_lockscreen()
71  self._enter_pin_passphrase("password")
72  self.assertThat(lockscreen.shown, Eventually(Equals(False)))
73  else:
74  self._enter_prompt_passphrase("password")
75  self.assertThat(greeter.shown, Eventually(Equals(False)))
76 
77  @with_lightdm_mock("single-pin")
79  """Entering the wrong pin code must not dismiss the lock screen."""
80  unity_proxy = self.launch_unity()
81  greeter = self.main_window.get_greeter()
82 
83  if greeter.narrowMode:
84  unlock_unity(unity_proxy)
85  lockscreen = self._wait_for_lockscreen()
86  self._enter_pincode("4321")
87  pinentryField = self.main_window.get_pinentryField()
88  self.assertThat(pinentryField.text, Eventually(Equals("")))
89  self.assertThat(lockscreen.shown, Eventually(Equals(True)))
90  else:
91  self._enter_prompt_passphrase("4231")
92  prompt = self.main_window.get_greeter().get_prompt()
93  self.assertThat(prompt.text, Eventually(Equals("")))
94  self.assertThat(greeter.shown, Eventually(Equals(True)))
95 
96  @with_lightdm_mock("single-passphrase")
98  """Entering the wrong password must not dismiss the lock screen."""
99  unity_proxy = self.launch_unity()
100  greeter = self.main_window.get_greeter()
101 
102  if greeter.narrowMode:
103  unlock_unity(unity_proxy)
104  lockscreen = self._wait_for_lockscreen()
105  self._enter_pin_passphrase("foobar")
106  pinentryField = self.main_window.get_pinentryField()
107  self.assertThat(pinentryField.text, Eventually(Equals("")))
108  self.assertThat(lockscreen.shown, Eventually(Equals(True)))
109  else:
110  self._enter_prompt_passphrase("foobar")
111  prompt = self.main_window.get_greeter().get_prompt()
112  self.assertThat(prompt.text, Eventually(Equals("")))
113  self.assertThat(greeter.shown, Eventually(Equals(True)))
114 
115  def _wait_for_lockscreen(self):
116  """Wait for the lock screen to load, and return it."""
117  pinPadLoader = self.main_window.get_pinPadLoader()
118  self.assertThat(pinPadLoader.progress, Eventually(Equals(1)))
119  lockscreen = self.main_window.get_lockscreen()
120  self.assertThat(lockscreen.shown, Eventually(Equals(True)))
121  return lockscreen
122 
123  def _enter_pincode(self, code):
124  """Enter code 'code' into the single-pin lightdm pincode entry
125  screen.
126 
127  :param code: must be a string of numeric characters.
128  :raises: TypeError if code is not a string.
129  :raises: ValueError if code contains non-numeric characters.
130 
131  """
132 
133  if not isinstance(code, basestring):
134  raise TypeError(
135  "'code' parameter must be a string, not %r."
136  % type(passphrase)
137  )
138  for num in code:
139  if not num.isdigit():
140  raise ValueError(
141  "'code' parameter contains non-numeric characters."
142  )
143  self.touch.tap_object(self.main_window.get_pinPadButton(int(num)))
144 
145  def _enter_pin_passphrase(self, passphrase):
146  """Enter the password specified in 'passphrase' into the password entry
147  field of the pin lock screen.
148 
149  :param passphrase: The string you want to enter.
150  :raises: TypeError if passphrase is not a string.
151 
152  """
153  if not isinstance(passphrase, basestring):
154  raise TypeError(
155  "'passphrase' parameter must be a string, not %r."
156  % type(passphrase)
157  )
158 
159  pinentryField = self.main_window.get_pinentryField()
160  self.touch.tap_object(pinentryField)
161  self.assertThat(pinentryField.activeFocus, Eventually(Equals(True)))
162  for character in passphrase:
163  self._type_character(character, pinentryField)
164  logger.debug("Typed passphrase: %s", pinentryField.text)
165  self.keyboard.type("\n")
166 
167  def _enter_prompt_passphrase(self, passphrase):
168  """Enter the password specified in 'passphrase' into the password entry
169  field of the main user list's prompt.
170 
171  :param passphrase: The string you want to enter.
172  :raises: TypeError if passphrase is not a string.
173 
174  """
175  if not isinstance(passphrase, basestring):
176  raise TypeError(
177  "'passphrase' parameter must be a string, not %r."
178  % type(passphrase)
179  )
180 
181  prompt = self.main_window.get_greeter().get_prompt()
182  self.touch.tap_object(prompt)
183  self.assertThat(prompt.activeFocus, Eventually(Equals(True)))
184  for character in passphrase:
185  self._type_character(character, prompt)
186  logger.debug("Typed passphrase: %s", prompt.text)
187  self.keyboard.type("\n")
188 
189  def _type_character(self, character, prompt, retries=5):
190  current_text = prompt.text
191  self.keyboard.type(character)
192  try:
193  self.assertThat(prompt.text, Eventually(Equals(current_text + character)))
194  except AssertionError:
195  if retries > 0:
196  self._type_character(character, prompt, retries-1)
197  else:
198  raise