Back to index

unity  6.0.0
test_keynav.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 import logging
00014 from testtools.matchers import Equals, GreaterThan
00015 
00016 from unity.tests.launcher import LauncherTestCase
00017 
00018 logger = logging.getLogger(__name__)
00019 
00020 
00021 class LauncherKeyNavTests(LauncherTestCase):
00022     """Test the launcher key navigation"""
00023 
00024     def start_keynav_with_cleanup_cancel(self):
00025         """Start keynav mode safely.
00026 
00027         This adds a cleanup action that cancels keynav mode at the end of the
00028         test if it's still running (but does nothing otherwise).
00029 
00030         """
00031         self.launcher_instance.key_nav_start()
00032         self.addCleanup(self.safe_quit_keynav)
00033 
00034     def safe_quit_keynav(self):
00035         """Quit the keynav mode if it's engaged."""
00036         if self.launcher.key_nav_is_active:
00037             self.launcher_instance.key_nav_cancel()
00038 
00039     def test_launcher_keynav_initiate(self):
00040         """Tests we can initiate keyboard navigation on the launcher."""
00041         self.start_keynav_with_cleanup_cancel()
00042         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(True)))
00043         self.assertThat(self.launcher.key_nav_is_grabbed, Eventually(Equals(True)))
00044 
00045     def test_launcher_keynav_cancel(self):
00046         """Test that we can exit keynav mode."""
00047         self.launcher_instance.key_nav_start()
00048         self.launcher_instance.key_nav_cancel()
00049         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(False)))
00050         self.assertThat(self.launcher.key_nav_is_grabbed, Eventually(Equals(False)))
00051 
00052     def test_launcher_keynav_cancel_resume_focus(self):
00053         """Test that ending the launcher keynav resume the focus."""
00054         calc = self.start_app("Calculator")
00055         self.assertTrue(calc.is_active)
00056 
00057         self.start_keynav_with_cleanup_cancel()
00058         self.assertFalse(calc.is_active)
00059 
00060         self.launcher_instance.key_nav_cancel()
00061         self.assertTrue(calc.is_active)
00062 
00063     def test_launcher_keynav_starts_at_index_zero(self):
00064         """Test keynav mode starts at index 0."""
00065         self.start_keynav_with_cleanup_cancel()
00066         self.assertThat(self.launcher.key_nav_selection, Eventually(Equals(0)))
00067 
00068     def test_launcher_keynav_forward(self):
00069         """Must be able to move forwards while in keynav mode."""
00070         self.start_keynav_with_cleanup_cancel()
00071         self.launcher_instance.key_nav_next()
00072         # The launcher model has hidden items, so the keynav indexes do not
00073         # increase by 1 each time. This test was failing because the 2nd icon
00074         # had an index of 2, not 1 as expected. The best we can do here is to
00075         # make sure that the index has increased. This opens us to the
00076         # possibility that the launcher really is skipping forward more than one
00077         # icon at a time, but we can't do much about that.
00078         self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(0)))
00079 
00080     def test_launcher_keynav_prev_works(self):
00081         """Must be able to move backwards while in keynav mode."""
00082         self.start_keynav_with_cleanup_cancel()
00083         self.launcher_instance.key_nav_next()
00084         self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(0)))
00085         self.launcher_instance.key_nav_prev()
00086         self.assertThat(self.launcher.key_nav_selection, Eventually(Equals(0)))
00087 
00088     def test_launcher_keynav_cycling_forward(self):
00089         """Launcher keynav must loop through icons when cycling forwards"""
00090         self.start_keynav_with_cleanup_cancel()
00091         prev_icon = 0
00092         for icon in range(1, self.launcher.model.num_launcher_icons()):
00093             self.launcher_instance.key_nav_next()
00094             # FIXME We can't directly check for selection/icon number equalty
00095             # since the launcher model also contains "hidden" icons that aren't
00096             # shown, so the selection index can increment by more than 1.
00097             self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(prev_icon)))
00098             prev_icon = self.launcher.key_nav_selection
00099 
00100         self.launcher_instance.key_nav_next()
00101         self.assertThat(self.launcher.key_nav_selection, Eventually(Equals(0)))
00102 
00103     def test_launcher_keynav_cycling_backward(self):
00104         """Launcher keynav must loop through icons when cycling backwards"""
00105         self.start_keynav_with_cleanup_cancel()
00106         self.launcher_instance.key_nav_prev()
00107         # FIXME We can't directly check for self.launcher.num_launcher_icons - 1
00108         self.assertThat(self.launcher.key_nav_selection, Eventually(GreaterThan(1)))
00109 
00110     def test_launcher_keynav_can_open_and_close_quicklist(self):
00111         """Tests that we can open and close a quicklist from keynav mode."""
00112         self.start_keynav_with_cleanup_cancel()
00113         self.launcher_instance.key_nav_next()
00114         self.launcher_instance.key_nav_enter_quicklist()
00115         self.assertThat(self.launcher_instance.quicklist_open, Eventually(Equals(True)))
00116         self.launcher_instance.key_nav_exit_quicklist()
00117         self.assertThat(self.launcher_instance.quicklist_open, Eventually(Equals(False)))
00118         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(True)))
00119         self.assertThat(self.launcher.key_nav_is_grabbed, Eventually(Equals(True)))
00120 
00121     def test_launcher_keynav_mode_toggles(self):
00122         """Tests that keynav mode toggles with Alt+F1."""
00123         # was initiated in setup.
00124         self.start_keynav_with_cleanup_cancel()
00125         self.keybinding("launcher/keynav")
00126         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(False)))
00127 
00128     def test_launcher_keynav_activate_keep_focus(self):
00129         """Activating a running launcher icon must focus it."""
00130         calc = self.start_app("Calculator")
00131         mahjongg = self.start_app("Mahjongg")
00132         self.assertTrue(mahjongg.is_active)
00133         self.assertFalse(calc.is_active)
00134 
00135         self.start_keynav_with_cleanup_cancel()
00136 
00137         self.launcher_instance.keyboard_select_icon(tooltip_text=calc.name)
00138         self.launcher_instance.key_nav_activate()
00139 
00140         self.assertTrue(calc.is_active)
00141         self.assertFalse(mahjongg.is_active)
00142 
00143     def test_launcher_keynav_expo_focus(self):
00144         """When entering expo mode from KeyNav the Desktop must get focus."""
00145         self.start_keynav_with_cleanup_cancel()
00146 
00147         self.launcher_instance.keyboard_select_icon(tooltip_text="Workspace Switcher")
00148         self.launcher_instance.key_nav_activate()
00149         self.addCleanup(self.keybinding, "expo/cancel")
00150 
00151         self.assertThat(self.panels.get_active_panel().title, Eventually(Equals("Ubuntu Desktop")))
00152 
00153     def test_launcher_keynav_expo_exit_on_esc(self):
00154         """Esc should quit expo when entering it from KeyNav."""
00155         self.start_keynav_with_cleanup_cancel()
00156 
00157         self.launcher_instance.keyboard_select_icon(tooltip_text="Workspace Switcher")
00158         self.launcher_instance.key_nav_activate()
00159 
00160         self.keyboard.press_and_release("Escape")
00161         self.assertThat(self.window_manager.expo_active, Eventually(Equals(False)))
00162 
00163     def test_launcher_keynav_alt_tab_quits(self):
00164         """Tests that alt+tab exits keynav mode."""
00165         self.start_keynav_with_cleanup_cancel()
00166 
00167         self.keybinding("switcher/reveal_normal")
00168         self.addCleanup(self.switcher.terminate)
00169         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(False)))
00170 
00171     def test_launcher_keynav_alt_grave_quits(self):
00172         """Tests that alt+` exits keynav mode."""
00173         self.start_keynav_with_cleanup_cancel()
00174         # Can't use switcher emulat here since the switcher won't appear.
00175         self.keybinding("switcher/reveal_details")
00176         self.assertThat(self.launcher.key_nav_is_active, Eventually(Equals(False)))
00177 
00178     def test_launcher_keynav_cancel_doesnt_activate_icon(self):
00179         """This tests when canceling keynav the current icon doesnt activate."""
00180         self.start_keynav_with_cleanup_cancel()
00181         self.keyboard.press_and_release("Escape")
00182         self.assertThat(self.dash.visible, Eventually(Equals(False)))
00183 
00184     def test_alt_f1_closes_dash(self):
00185         """Pressing Alt+F1 when the Dash is open must close the Dash and start keynav."""
00186         self.dash.ensure_visible()
00187 
00188         self.start_keynav_with_cleanup_cancel()
00189 
00190         self.assertThat(self.dash.visible, Equals(False))
00191         self.assertThat(self.launcher.key_nav_is_active, Equals(True))
00192 
00193     def test_alt_f1_closes_hud(self):
00194         """Pressing Alt+F1 when the HUD is open must close the HUD and start keynav."""
00195         self.hud.ensure_visible()
00196 
00197         self.start_keynav_with_cleanup_cancel()
00198 
00199         self.assertThat(self.hud.visible, Equals(False))
00200         self.assertThat(self.launcher.key_nav_is_active, Equals(True))