U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 11 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Bryce Harrington | 12cc405 | 2014-11-19 17:18:33 -0800 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame^] | 30 | #include "input-timestamps-helper.h" |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 31 | #include "shared/timespec-util.h" |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 32 | #include "weston-test-client-helper.h" |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 33 | |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 34 | static const struct timespec t1 = { .tv_sec = 1, .tv_nsec = 1000001 }; |
| 35 | static const struct timespec t2 = { .tv_sec = 2, .tv_nsec = 2000001 }; |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame^] | 36 | static const struct timespec t_other = { .tv_sec = 123, .tv_nsec = 456 }; |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 37 | |
| 38 | static struct client * |
| 39 | create_client_with_keyboard_focus(void) |
| 40 | { |
| 41 | struct client *cl = create_client_and_test_surface(10, 10, 1, 1); |
| 42 | assert(cl); |
| 43 | |
| 44 | weston_test_activate_surface(cl->test->weston_test, |
| 45 | cl->surface->wl_surface); |
| 46 | client_roundtrip(cl); |
| 47 | |
| 48 | return cl; |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | send_key(struct client *client, const struct timespec *time, |
| 53 | uint32_t key, uint32_t state) |
| 54 | { |
| 55 | uint32_t tv_sec_hi, tv_sec_lo, tv_nsec; |
| 56 | |
| 57 | timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec); |
| 58 | weston_test_send_key(client->test->weston_test, tv_sec_hi, tv_sec_lo, |
| 59 | tv_nsec, key, state); |
| 60 | client_roundtrip(client); |
| 61 | } |
| 62 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 63 | TEST(simple_keyboard_test) |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 64 | { |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 65 | struct client *client = create_client_with_keyboard_focus(); |
| 66 | struct keyboard *keyboard = client->input->keyboard; |
| 67 | struct surface *expect_focus = client->surface; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 68 | uint32_t expect_key = 0; |
| 69 | uint32_t expect_state = 0; |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 70 | |
Dawid Gajownik | 74a635b | 2015-08-06 17:12:19 -0300 | [diff] [blame] | 71 | while (1) { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 72 | assert(keyboard->key == expect_key); |
| 73 | assert(keyboard->state == expect_state); |
| 74 | assert(keyboard->focus == expect_focus); |
| 75 | |
| 76 | if (keyboard->state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 77 | expect_state = WL_KEYBOARD_KEY_STATE_RELEASED; |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 78 | send_key(client, &t1, expect_key, expect_state); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 79 | } else if (keyboard->focus) { |
| 80 | expect_focus = NULL; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 81 | weston_test_activate_surface( |
| 82 | client->test->weston_test, NULL); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 83 | client_roundtrip(client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 84 | } else if (expect_key < 10) { |
| 85 | expect_key++; |
| 86 | expect_focus = client->surface; |
| 87 | expect_state = WL_KEYBOARD_KEY_STATE_PRESSED; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 88 | weston_test_activate_surface( |
| 89 | client->test->weston_test, |
| 90 | expect_focus->wl_surface); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 91 | send_key(client, &t1, expect_key, expect_state); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 92 | } else { |
| 93 | break; |
| 94 | } |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 95 | } |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 96 | } |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 97 | |
| 98 | TEST(keyboard_key_event_time) |
| 99 | { |
| 100 | struct client *client = create_client_with_keyboard_focus(); |
| 101 | struct keyboard *keyboard = client->input->keyboard; |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame^] | 102 | struct input_timestamps *input_ts = |
| 103 | input_timestamps_create_for_keyboard(client); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 104 | |
| 105 | send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 106 | assert(keyboard->key_time_msec == timespec_to_msec(&t1)); |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame^] | 107 | assert(timespec_eq(&keyboard->key_time_timespec, &t1)); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 108 | |
| 109 | send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED); |
| 110 | assert(keyboard->key_time_msec == timespec_to_msec(&t2)); |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame^] | 111 | assert(timespec_eq(&keyboard->key_time_timespec, &t2)); |
| 112 | |
| 113 | input_timestamps_destroy(input_ts); |
| 114 | } |
| 115 | |
| 116 | TEST(keyboard_timestamps_stop_after_input_timestamps_object_is_destroyed) |
| 117 | { |
| 118 | struct client *client = create_client_with_keyboard_focus(); |
| 119 | struct keyboard *keyboard = client->input->keyboard; |
| 120 | struct input_timestamps *input_ts = |
| 121 | input_timestamps_create_for_keyboard(client); |
| 122 | |
| 123 | send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 124 | assert(keyboard->key_time_msec == timespec_to_msec(&t1)); |
| 125 | assert(timespec_eq(&keyboard->key_time_timespec, &t1)); |
| 126 | |
| 127 | input_timestamps_destroy(input_ts); |
| 128 | |
| 129 | send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED); |
| 130 | assert(keyboard->key_time_msec == timespec_to_msec(&t2)); |
| 131 | assert(timespec_is_zero(&keyboard->key_time_timespec)); |
| 132 | } |
| 133 | |
| 134 | TEST(keyboard_timestamps_stop_after_client_releases_wl_keyboard) |
| 135 | { |
| 136 | struct client *client = create_client_with_keyboard_focus(); |
| 137 | struct keyboard *keyboard = client->input->keyboard; |
| 138 | struct input_timestamps *input_ts = |
| 139 | input_timestamps_create_for_keyboard(client); |
| 140 | |
| 141 | send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 142 | assert(keyboard->key_time_msec == timespec_to_msec(&t1)); |
| 143 | assert(timespec_eq(&keyboard->key_time_timespec, &t1)); |
| 144 | |
| 145 | wl_keyboard_release(client->input->keyboard->wl_keyboard); |
| 146 | |
| 147 | /* Set input_timestamp to an arbitrary value (different from t1, t2 |
| 148 | * and 0) and check that it is not changed by sending the event. |
| 149 | * This is preferred over just checking for 0, since 0 is used |
| 150 | * internally for resetting the timestamp after handling an input |
| 151 | * event and checking for it here may lead to false negatives. */ |
| 152 | keyboard->input_timestamp = t_other; |
| 153 | send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED); |
| 154 | assert(timespec_eq(&keyboard->input_timestamp, &t_other)); |
| 155 | |
| 156 | input_timestamps_destroy(input_ts); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 157 | } |