Back to index

unity  6.0.0
test_switcher.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 # Authors: Thomi Richards,
00004 #          Marco Trevisan (TreviƱo)
00005 #
00006 # This program is free software: you can redistribute it and/or modify it
00007 # under the terms of the GNU General Public License version 3, as published
00008 # by the Free Software Foundation.
00009 
00010 from __future__ import absolute_import
00011 
00012 from autopilot.matchers import Eventually
00013 
00014 import logging
00015 from testtools.matchers import Equals, NotEquals, GreaterThan
00016 from time import sleep
00017 
00018 from unity.tests.launcher import LauncherTestCase
00019 
00020 
00021 logger = logging.getLogger(__name__)
00022 
00023 
00024 class LauncherSwitcherTests(LauncherTestCase):
00025     """ Tests the functionality of the launcher's switcher capability"""
00026 
00027     def start_switcher_with_cleanup_cancel(self):
00028         """Start switcher mode safely.
00029 
00030         This adds a cleanup action that cancels keynav mode at the end of the
00031         test if it's still running (but does nothing otherwise).
00032 
00033         """
00034         self.launcher_instance.switcher_start()
00035         self.addCleanup(self.safe_quit_switcher)
00036 
00037     def safe_quit_switcher(self):
00038         """Quit the keynav mode if it's engaged."""
00039         if self.launcher.key_nav_is_active:
00040             self.launcher_instance.switcher_cancel()
00041 
00042     def test_launcher_switcher_cancel(self):
00043         """Test that ending the launcher switcher actually works."""
00044         self.launcher_instance.switcher_start()
00045         self.launcher_instance.switcher_cancel()
00046         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(False)))
00047 
00048     def test_launcher_switcher_cancel_resume_focus(self):
00049         """Test that ending the launcher switcher resume the focus."""
00050         self.close_all_app("Calculator")
00051         calc = self.start_app("Calculator")
00052         self.assertTrue(calc.is_active)
00053 
00054         self.start_switcher_with_cleanup_cancel()
00055         sleep(.5)
00056         self.assertFalse(calc.is_active)
00057 
00058         self.launcher_instance.switcher_cancel()
00059         sleep(.5)
00060         self.assertTrue(calc.is_active)
00061 
00062     def test_launcher_switcher_starts_at_index_zero(self):
00063         """Test that starting the Launcher switcher puts the keyboard focus on item 0."""
00064         self.start_switcher_with_cleanup_cancel()
00065 
00066         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(True)))
00067         self.assertThat(self.launcher.key_nav_is_grabbed, Eventually(Equals(False)))
00068         self.assertThat(self.launcher.key_nav_selection, Eventually(Equals(0)))
00069 
00070     def test_launcher_switcher_next(self):
00071         """Moving to the next launcher item while switcher is activated must work."""
00072         self.start_switcher_with_cleanup_cancel()
00073         self.launcher_instance.switcher_next()
00074         # The launcher model has hidden items, so the keynav indexes do not
00075         # increase by 1 each time. This test was failing because the 2nd icon
00076         # had an index of 2, not 1 as expected. The best we can do here is to
00077         # make sure that the index has increased. This opens us to the
00078         # possibility that the launcher really is skipping forward more than one
00079         # icon at a time, but we can't do much about that.
00080         self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(0)))
00081 
00082     def test_launcher_switcher_prev(self):
00083         """Moving to the previous launcher item while switcher is activated must work."""
00084         self.start_switcher_with_cleanup_cancel()
00085         self.launcher_instance.switcher_prev()
00086         self.assertThat(self.launcher.key_nav_selection, Eventually(NotEquals(0)))
00087 
00088     def test_launcher_switcher_down(self):
00089         """Pressing the down arrow key while switcher is activated must work."""
00090         self.start_switcher_with_cleanup_cancel()
00091         self.launcher_instance.switcher_down()
00092         # The launcher model has hidden items, so the keynav indexes do not
00093         # increase by 1 each time. This test was failing because the 2nd icon
00094         # had an index of 2, not 1 as expected. The best we can do here is to
00095         # make sure that the index has increased. This opens us to the
00096         # possibility that the launcher really is skipping forward more than one
00097         # icon at a time, but we can't do much about that.
00098         self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(0)))
00099 
00100     def test_launcher_switcher_up(self):
00101         """Pressing the up arrow key while switcher is activated must work."""
00102         self.start_switcher_with_cleanup_cancel()
00103         self.launcher_instance.switcher_up()
00104         self.assertThat(self.launcher.key_nav_selection, Eventually(NotEquals(0)))
00105 
00106     def test_launcher_switcher_next_doesnt_show_shortcuts(self):
00107         """Moving forward in launcher switcher must not show launcher shortcuts."""
00108         self.start_switcher_with_cleanup_cancel()
00109         self.launcher_instance.switcher_next()
00110         # sleep so that the shortcut timeout could be triggered
00111         sleep(2)
00112         self.assertThat(self.launcher_instance.shortcuts_shown, Eventually(Equals(False)))
00113 
00114     def test_launcher_switcher_prev_doesnt_show_shortcuts(self):
00115         """Moving backward in launcher switcher must not show launcher shortcuts."""
00116         self.start_switcher_with_cleanup_cancel()
00117         self.launcher_instance.switcher_prev()
00118         # sleep so that the shortcut timeout could be triggered
00119         sleep(2)
00120         self.assertThat(self.launcher_instance.shortcuts_shown, Eventually(Equals(False)))
00121 
00122     def test_launcher_switcher_cycling_forward(self):
00123         """Launcher Switcher must loop through icons when cycling forwards"""
00124         self.start_switcher_with_cleanup_cancel()
00125         prev_icon = 0
00126         num_icons = self.launcher.model.num_launcher_icons()
00127         logger.info("This launcher has %d icons", num_icons)
00128         for icon in range(1, num_icons):
00129             self.launcher_instance.switcher_next()
00130             # FIXME We can't directly check for selection/icon number equalty
00131             # since the launcher model also contains "hidden" icons that aren't
00132             # shown, so the selection index can increment by more than 1.
00133             self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(prev_icon)))
00134             prev_icon = self.launcher.key_nav_selection
00135 
00136         self.launcher_instance.switcher_next()
00137         self.assertThat(self.launcher.key_nav_selection, Eventually(Equals(0)))
00138 
00139     def test_launcher_switcher_cycling_backward(self):
00140         """Launcher Switcher must loop through icons when cycling backwards"""
00141         self.start_switcher_with_cleanup_cancel()
00142         self.launcher_instance.switcher_prev()
00143         # FIXME We can't directly check for self.launcher.num_launcher_icons - 1
00144         self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(1)))
00145 
00146     def test_launcher_switcher_activate_keep_focus(self):
00147         """Activating a running launcher icon should focus the application."""
00148         calc = self.start_app("Calculator")
00149         mahjongg = self.start_app("Mahjongg")
00150         self.assertTrue(mahjongg.is_active)
00151         self.assertFalse(calc.is_active)
00152 
00153         self.start_switcher_with_cleanup_cancel()
00154 
00155         self.launcher_instance.keyboard_select_icon(tooltip_text=calc.name)
00156         self.launcher_instance.switcher_activate()
00157 
00158         self.assertThat(lambda: calc.is_active, Eventually(Equals(True)))
00159         self.assertThat(lambda: mahjongg.is_active, Eventually(Equals(False)))
00160 
00161     def test_launcher_switcher_using_shorcuts(self):
00162         """Using some other shortcut while switcher is active must cancel switcher."""
00163         self.start_switcher_with_cleanup_cancel()
00164         self.keyboard.press_and_release("s")
00165         sleep(.25)
00166         self.keyboard.press_and_release("Escape")
00167         sleep(.25)
00168         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(False)))