on xenial help sign in
* Refactor code

* Add test for order-independent screenshot button presses

commitae03088a94ac49f0153f9fcba2633f67364793f1 authorJosh Arenson <joshua.arenson@canonical.com> dateWed 11 Feb 2015, 16:22 (12 years ago) parenteab826e
4 files changed+33−107
qml/Components/PhysicalKeysMapper.qml+6−18 qml/Shell.qml+3−3 tests/autopilot/unity8/shell/tests/test_screenshots.py−69 tests/qmltests/Components/tst_PhysicalKeysMapper.qml+24−17
qml/Components/PhysicalKeysMapper.qml+6−18
@@ -36,14 +36,15 @@ Item {
3636 id: root
3737
3838 signal powerKeyLongPress();
39− signal screenshotPressed();
40− signal volumeDownPressed();
41− signal volumeUpPressed();
39+
40+ property bool screenshotPressed: d.powerKeyPressed && d.volumeDownKeyPressed
41+ property bool volumeDownPressed: d.volumeDownKeyPressed && !d.powerKeyPressed
42+ property bool volumeUpPressed: d.volumeUpKeyPressed
43+ property int powerKeyLongPressTimeMs: 2000 // Is writable for testing purposes
4244
4345 QtObject {
4446 id: d
4547
46− property bool aPowerKeyWasReleased: true
4748 property bool powerKeyPressed: false
4849
4950 property bool volumeDownKeyPressed: false
@@ -56,7 +57,7 @@ Item {
5657 Timer {
5758 id: powerKeyLongPressTimer
5859
59− interval: 2000
60+ interval: powerKeyLongPressTimeMs
6061 repeat: false
6162 triggeredOnStart: false
6263 onTriggered: powerKeyLongPress();
@@ -92,18 +93,6 @@ Item {
9293 d.volumeUpKeyPressed = true;
9394 }
9495
95− /* Determine how to handle it */
96− if (d.volumeDownKeyPressed) {
97− if (d.powerKeyPressed && d.aPowerKeyWasReleased) {
98− screenshotPressed();
99− d.aPowerKeyWasReleased = false;
100− // Don't emit volumeDownPressed if power key is held
101− } else if (d.aPowerKeyWasReleased){
102− volumeDownPressed();
103− }
104− } else if (d.volumeUpKeyPressed) {
105− volumeUpPressed();
106− }
10796 return eventAccepted;
10897 }
10998
@@ -113,7 +102,6 @@ Item {
113102 if (key == Qt.Key_PowerDown || key == Qt.Key_PowerOff) {
114103 powerKeyLongPressTimer.stop();
115104 d.powerKeyPressed = false;
116− d.aPowerKeyWasReleased = true;
117105 eventAccepted = true;
118106 } else if (key == Qt.Key_VolumeDown) {
119107 d.volumeDownKeyPressed = false;
qml/Shell.qml+3−3
@@ -163,9 +163,9 @@ Item {
163163 id: physicalKeysMapper
164164
165165 onPowerKeyLongPress: dialogs.showPowerDialog()
166− onVolumeDownPressed: volumeControl.volumeDown()
167− onVolumeUpPressed: volumeControl.volumeUp()
168− onScreenshotPressed: screenGrabber.capture()
166+ onVolumeDownPressedChanged: if (volumeDownPressed) { volumeControl.volumeDown(); }
167+ onVolumeUpPressedChanged: if (volumeUpPressed) { volumeControl.volumeUp(); }
168+ onScreenshotPressedChanged: if (screenshotPressed) { screenGrabber.capture(); }
169169
170170 }
171171
tests/autopilot/unity8/shell/tests/test_screenshots.pydeleted−69
@@ -1,69 +0,0 @@
1−# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2−#
3−# Unity Autopilot Test Suite
4−# Copyright (C) 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−import evdev
21−import os
22−import time
23−
24−from gi.repository import Notify
25−from testtools.matchers import Equals
26−
27−from unity8.shell import (
28− fixture_setup,
29− tests
30−)
31−
32−from unity8.process_helpers import unlock_unity
33−
34−class ScreenShotTestCase(tests.UnityTestCase):
35−
36− AP_SCREENSHOT_DIR = '/home/phablet/Pictures/Screenshots'
37− SCREENSHOT_DELAY_SECS = 2
38−
39− def setUp(self):
40− super(ScreenShotTestCase, self).setUp()
41− unity_proxy = self.launch_unity()
42− unlock_unity(unity_proxy)
43−
44− def test_volume_is_hidden(self):
45− uinput = evdev.UInput(name='apinput', devnode='/dev/autopilot-uinput')
46− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_VOLUMEDOWN, 1)
47− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_VOLUMEDOWN, 0)
48− uinput.syn()
49− a=self.main_window.select_single("Notification", objectName="notification1")
50− print (a.icon)
51−
52− def test_take_screenshot(self):
53− num_screenshots_pre = len(os.listdir(self.AP_SCREENSHOT_DIR))
54− self._take_screenshot()
55− num_screenshots_post = len(os.listdir(self.AP_SCREENSHOT_DIR))
56− self.assertThat(num_screenshots_pre + 1, Equals(num_screenshots_post))
57−
58− def _take_screenshot(self):
59− uinput = evdev.UInput(name='apinput', devnode='/dev/autopilot-uinput')
60− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
61− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_VOLUMEDOWN, 1)
62− uinput.syn()
63−
64− time.sleep(self.SCREENSHOT_DELAY_SECS)
65−
66− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
67− uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_VOLUMEDOWN, 0)
68− uinput.syn()
69−
tests/qmltests/Components/tst_PhysicalKeysMapper.qml+24−17
@@ -22,17 +22,16 @@ TestCase {
2222 name: "PhysicalKeysMapper"
2323
2424 property int onPowerKeyLongPressCount: 0
25− property int onScreenshotPressedCount: 0
26− property int onVolumeDownPressedCount: 0
27− property int onVolumeUpPressedCount: 0
25+ property int initialLongPressTimerValue
2826
2927 PhysicalKeysMapper {
3028 id: physicalKeysMapper
3129
3230 onPowerKeyLongPress: onPowerKeyLongPressCount += 1
33− onScreenshotPressed: onScreenshotPressedCount += 1
34− onVolumeDownPressed: onVolumeDownPressedCount += 1
35− onVolumeUpPressed: onVolumeUpPressedCount += 1
31+ }
32+
33+ function initTestCase() {
34+ initialLongPressTimerValue = physicalKeysMapper.powerKeyLongPressTimeMs
3635 }
3736
3837 function cleanup() {
@@ -42,37 +41,45 @@ TestCase {
4241 physicalKeysMapper.onKeyReleased(Qt.Key_VolumeUp);
4342
4443 onPowerKeyLongPressCount = 0;
45− onScreenshotPressedCount = 0;
46− onVolumeDownPressedCount = 0;
47− onVolumeUpPressedCount = 0;
44+
45+ physicalKeysMapper.powerKeyLongPressTimeMs = initialLongPressTimerValue
4846 }
4947
5048 function test_LongPressPowerButton() {
49+ // Delays chosen are lowest reliable values
50+ physicalKeysMapper.powerKeyLongPressTimeMs = 2
5151 physicalKeysMapper.onKeyPressed(Qt.Key_PowerDown);
52− wait(3000);
52+ wait(15);
5353
5454 compare(onPowerKeyLongPressCount, 1);
5555 }
5656
5757 function test_ScreenshotButtons() {
58+ /* Behavior when Power is pressed 1st */
5859 physicalKeysMapper.onKeyPressed(Qt.Key_PowerDown);
5960 physicalKeysMapper.onKeyPressed(Qt.Key_VolumeDown);
61+ compare(physicalKeysMapper.screenshotPressed, true);
62+ compare(physicalKeysMapper.volumeDownPressed, false);
6063
61− compare(onScreenshotPressedCount, 1);
62− compare(onVolumeDownPressedCount, 0);
64+ physicalKeysMapper.onKeyReleased(Qt.Key_PowerDown);
65+ physicalKeysMapper.onKeyReleased(Qt.Key_VolumeDown);
6366
64− wait(3000);
65− compare(onScreenshotPressedCount, 1);
66− compare(onVolumeDownPressedCount, 0);
67+ /* Behavior when VolumeDown is pressed 1st */
68+ physicalKeysMapper.onKeyPressed(Qt.Key_VolumeDown);
69+ compare(physicalKeysMapper.volumeDownPressed, true);
70+ physicalKeysMapper.onKeyPressed(Qt.Key_PowerDown);
71+ compare(physicalKeysMapper.screenshotPressed, true);
6772 }
6873
6974 function test_VolumeDownButton() {
75+ compare(physicalKeysMapper.volumeDownPressed, false);
7076 physicalKeysMapper.onKeyPressed(Qt.Key_VolumeDown);
71− compare(onVolumeDownPressedCount, 1);
77+ compare(physicalKeysMapper.volumeDownPressed, true);
7278 }
7379
7480 function test_VolumeUpButton() {
81+ compare(physicalKeysMapper.volumeUpPressed, false);
7582 physicalKeysMapper.onKeyPressed(Qt.Key_VolumeUp);
76− compare(onVolumeUpPressedCount, 1);
83+ compare(physicalKeysMapper.volumeUpPressed, true);
7784 }
7885}