1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
# Copyright 2012 Canonical
3
# Author: Marco Trevisan (Treviño)
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
from __future__ import absolute_import
11
from autopilot.matchers import Eventually
13
from time import sleep
14
from testtools.matchers import Equals, NotEquals
16
from unity.tests import UnityTestCase
17
from unity.emulators.screen import Screen
20
class SpreadTests(UnityTestCase):
23
def start_test_application_windows(self, app_name, num_windows=2):
24
"""Start a given number of windows of the requested application"""
25
self.close_all_app(app_name)
28
for i in range(num_windows):
29
win = self.start_app_window(app_name)
31
self.assertThat(win.application, Equals(windows[-1].application))
35
self.assertThat(len(windows), Equals(num_windows))
39
def initiate_spread_for_screen(self):
40
"""Initiate the Spread for all windows"""
41
self.addCleanup(self.keybinding, "spread/cancel")
42
self.keybinding("spread/start")
44
self.assertThat(self.window_manager.scale_active, Eventually(Equals(True)))
47
def initiate_spread_for_application(self, desktop_id):
48
"""Initiate the Spread for windows of the given app"""
49
icon = self.launcher.model.get_icon(desktop_id=desktop_id)
50
self.assertThat(lambda: icon, Eventually(NotEquals(None)))
51
launcher = self.launcher.get_launcher_for_monitor(self.screen_geo.get_primary_monitor())
53
self.addCleanup(self.keybinding, "spread/cancel")
54
launcher.click_launcher_icon(icon)
55
self.assertThat(self.window_manager.scale_active_for_group, Eventually(Equals(True)))
57
def assertWindowIsNotScaled(self, xid):
58
"""Assert that a window is not scaled"""
59
refresh_fn = lambda: xid in [w.xid for w in self.screen.scaled_windows]
60
self.assertThat(refresh_fn, Eventually(Equals(False)))
62
def assertWindowIsClosed(self, xid):
63
"""Assert that a window is not in the list of the open windows"""
64
refresh_fn = lambda: xid in [w.x_id for w in self.bamf.get_open_windows()]
65
self.assertThat(refresh_fn, Eventually(Equals(False)))
68
def test_scale_application_windows(self):
69
"""Test if all the windows of an application are scaled when application spread is initiated"""
70
[win1, win2] = self.start_test_application_windows("Calculator")
71
self.initiate_spread_for_application(win1.application.desktop_file)
73
self.assertThat(lambda: len(self.screen.scaled_windows), Eventually(Equals(2)))
74
self.assertThat(lambda: (win1.x_id and win2.x_id) in [w.xid for w in self.screen.scaled_windows],
75
Eventually(Equals(True)))
77
def test_scaled_window_is_focused_on_click(self):
78
"""Test that a window is focused when clicked in spread"""
79
windows = self.start_test_application_windows("Calculator", 3)
80
self.initiate_spread_for_application(windows[0].application.desktop_file)
82
not_focused = [w for w in windows if not w.is_focused][0]
84
target_xid = not_focused.x_id
85
[target_win] = [w for w in self.screen.scaled_windows if w.xid == target_xid]
87
(x, y, w, h) = target_win.geometry
88
self.mouse.move(x + w / 2, y + h / 2)
92
self.assertThat(lambda: not_focused.is_focused, Eventually(Equals(True)))
94
def test_scaled_window_closes_on_close_button_click(self):
95
"""Test that a window is closed when its close button is clicked in spread"""
96
win = self.start_test_application_windows("Calculator", 1)[0]
97
self.initiate_spread_for_screen()
100
[target_win] = [w for w in self.screen.scaled_windows if w.xid == target_xid]
102
(x, y, w, h) = target_win.scale_close_geometry
103
self.mouse.move(x + w / 2, y + h / 2)
107
self.assertWindowIsNotScaled(target_xid)
108
self.assertWindowIsClosed(target_xid)