@@ -18,12 +18,19 @@ import QtQuick 2.4
1818import QtTest 1.0
1919import "../../../qml/Components"
2020import Powerd 0.1
21+import Utils 0.1
2122
22−TestCase {
23− name: "PhysicalKeysMapper"
23+Item {
24+ width: 100
25+ height: 100
2426
2527 property var physicalKeysMapper: loader.item
2628
29+ WindowKeysFilter {
30+ Keys.onPressed: physicalKeysMapper.onKeyPressed(event, currentEventTimestamp);
31+ Keys.onReleased: physicalKeysMapper.onKeyReleased(event, currentEventTimestamp);
32+ }
33+
2734 Loader {
2835 // Using a Loader here to make sure mapper state is coherent
2936 // regardless of unmatched KeyPress and KeyRelease events
@@ -32,6 +39,21 @@ TestCase {
3239 sourceComponent: PhysicalKeysMapper { }
3340 }
3441
42+ Item {
43+ id: inputCatcher
44+ focus: true
45+
46+ property var pressedKeys: []
47+ property var releasedKeys: []
48+
49+ Keys.onPressed: {
50+ inputCatcher.pressedKeys.push(event.key);
51+ }
52+ Keys.onReleased: {
53+ inputCatcher.releasedKeys.push(event.key);
54+ }
55+ }
56+
3557 SignalSpy {
3658 id: powerSpy
3759 target: physicalKeysMapper
@@ -44,65 +66,117 @@ TestCase {
4466 signalName: "screenshotTriggered"
4567 }
4668
47− function init() {
48− Powerd.setStatus(Powerd.On, Powerd.Unknown);
49− loader.active = true;
50− tryCompare(loader.status == Loader.Ready);
51− }
52−
53− function cleanup() {
54− loader.active = false;
55− powerSpy.clear();
56− screenshotSpy.clear();
57− }
58−
59− function test_powerKeyLongPressed_data() {
60− return [
61− { tag: "screenOn", status: Powerd.On },
62− { tag: "screenOff", status: Powerd.Off },
63− ];
64− }
65−
66− function test_powerKeyLongPressed(data) {
67− Powerd.setStatus(data.status, Powerd.Unknown);
68− physicalKeysMapper.powerKeyLongPressTime = 500;
69−
70− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: false }, 100);
71−
72− // After the first key press the screen is always on
73− // and the rest of keypresses are auto repeat
74− Powerd.setStatus(Powerd.On, Powerd.Unknown);
75−
76− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 300);
77− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 400);
78− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 500);
79− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 599);
80−
81− // powerKeyLongPressed should not have been emitted yet.
82− compare(powerSpy.count, 0);
83−
84− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 600);
85−
86− compare(powerSpy.count, 1);
87−
88− // Confirm we only emit once
89− physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 601);
90− compare(powerSpy.count, 1);
91− }
92−
93− function test_screenshotButtons_data() {
94− return [
95− { tag: "UpFirst", first: Qt.Key_VolumeUp, second: Qt.Key_VolumeDown },
96− { tag: "DownFirst", first: Qt.Key_VolumeDown, second: Qt.Key_VolumeUp },
97− ];
98− }
99−
100− function test_screenshotButtons(data) {
101− physicalKeysMapper.onKeyPressed({ key: data.first });
102− physicalKeysMapper.onKeyPressed({ key: data.second });
103− screenshotSpy.wait();
104− physicalKeysMapper.onKeyReleased({ key: data.first });
105− physicalKeysMapper.onKeyReleased({ key: data.second });
106− compare(screenshotSpy.count, 1);
69+ TestCase {
70+ name: "PhysicalKeysMapper"
71+ when: windowShown
72+
73+ function init() {
74+ Powerd.setStatus(Powerd.On, Powerd.Unknown);
75+ loader.active = true;
76+ tryCompare(loader.status == Loader.Ready);
77+ inputCatcher.pressedKeys = [];
78+ inputCatcher.releasedKeys = [];
79+ }
80+
81+ function cleanup() {
82+ loader.active = false;
83+ powerSpy.clear();
84+ screenshotSpy.clear();
85+ }
86+
87+ function test_powerKeyLongPressed_data() {
88+ return [
89+ { tag: "screenOn", status: Powerd.On },
90+ { tag: "screenOff", status: Powerd.Off },
91+ ];
92+ }
93+
94+ function test_powerKeyLongPressed(data) {
95+ Powerd.setStatus(data.status, Powerd.Unknown);
96+ physicalKeysMapper.powerKeyLongPressTime = 500;
97+
98+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: false }, 100);
99+
100+ // After the first key press the screen is always on
101+ // and the rest of keypresses are auto repeat
102+ Powerd.setStatus(Powerd.On, Powerd.Unknown);
103+
104+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 300);
105+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 400);
106+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 500);
107+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 599);
108+
109+ // powerKeyLongPressed should not have been emitted yet.
110+ compare(powerSpy.count, 0);
111+
112+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 600);
113+
114+ compare(powerSpy.count, 1);
115+
116+ // Confirm we only emit once
117+ physicalKeysMapper.onKeyPressed({ key: Qt.Key_PowerDown, isAutoRepeat: true}, 601);
118+ compare(powerSpy.count, 1);
119+ }
120+
121+ function test_screenshotButtons_data() {
122+ return [
123+ { tag: "UpFirst", first: Qt.Key_VolumeUp, second: Qt.Key_VolumeDown },
124+ { tag: "DownFirst", first: Qt.Key_VolumeDown, second: Qt.Key_VolumeUp },
125+ ];
126+ }
127+
128+ function test_screenshotButtons(data) {
129+ physicalKeysMapper.onKeyPressed({ key: data.first });
130+ physicalKeysMapper.onKeyPressed({ key: data.second });
131+ screenshotSpy.wait();
132+ physicalKeysMapper.onKeyReleased({ key: data.first });
133+ physicalKeysMapper.onKeyReleased({ key: data.second });
134+ compare(screenshotSpy.count, 1);
135+ }
136+
137+ function test_altIsDispatchedOnRelease() {
138+ // Press alt, make sure it does *not* end up in inputCatcher (aka, the focused app)
139+ keyPress(Qt.Key_Alt, Qt.NoModifier)
140+ compare(inputCatcher.pressedKeys.length, 0);
141+ // Now release alt. It should cause the previous Alt press to be dispatched along with the release
142+ keyRelease(Qt.Key_Alt, Qt.NoModifier);
143+ compare(inputCatcher.pressedKeys.length, 1);
144+ compare(inputCatcher.pressedKeys[0], Qt.Key_Alt);
145+ compare(inputCatcher.releasedKeys.length, 1);
146+ compare(inputCatcher.releasedKeys[0], Qt.Key_Alt);
147+ }
148+
149+ function test_altIsNotDispatchedOnAltTab() {
150+ // Press alt, make sure it does *not* end up in inputCatcher (aka, the focused app)
151+ inputCatcher.pressedKeys = [];
152+ keyPress(Qt.Key_Alt, Qt.NoModifier)
153+ compare(inputCatcher.pressedKeys.length, 0);
154+ // Now also press tab. As this should trigger the spread, neither of them should end up in the app
155+ keyPress(Qt.Key_Tab, Qt.NoModifier)
156+ compare(inputCatcher.pressedKeys.length, 0);
157+
158+ // Also the release events should not be dispatched to the app
159+ keyRelease(Qt.Key_Tab, Qt.NoModifier);
160+ keyRelease(Qt.Key_Alt, Qt.NoModifier);
161+ compare(inputCatcher.pressedKeys.length, 0);
162+ compare(inputCatcher.releasedKeys.length, 0);
163+ }
164+
165+ function test_altComboIsDispatched() {
166+ inputCatcher.pressedKeys = [];
167+ // Press alt, make sure it does *not* yet end up in inputCatcher (aka, the focused app)
168+ keyPress(Qt.Key_Alt, Qt.NoModifier)
169+ compare(inputCatcher.pressedKeys.length, 0);
170+ // Now press F in order to opening the File menu (Alt+F), now the app should get the full combo
171+ keyPress(Qt.Key_F, Qt.NoModifier)
172+ compare(inputCatcher.pressedKeys.length, 2);
173+ compare(inputCatcher.pressedKeys[0], Qt.Key_Alt);
174+ compare(inputCatcher.pressedKeys[1], Qt.Key_F);
175+
176+ // release them both, both are supposed to end up in the app
177+ keyRelease(Qt.Key_F, Qt.NoModifier);
178+ keyRelease(Qt.Key_Alt, Qt.NoModifier);
179+ compare(inputCatcher.releasedKeys.length, 2);
180+ }
107181 }
108182}