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" |
Pekka Paalanen | 701676d | 2019-11-13 15:45:10 +0200 | [diff] [blame] | 33 | #include "weston-test-fixture-compositor.h" |
| 34 | |
| 35 | static enum test_result_code |
| 36 | fixture_setup(struct weston_test_harness *harness) |
| 37 | { |
| 38 | struct compositor_setup setup; |
| 39 | |
| 40 | compositor_setup_defaults(&setup); |
| 41 | |
| 42 | return weston_test_harness_execute_as_client(harness, &setup); |
| 43 | } |
| 44 | DECLARE_FIXTURE_SETUP(fixture_setup); |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 45 | |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 46 | static const struct timespec t1 = { .tv_sec = 1, .tv_nsec = 1000001 }; |
| 47 | static const struct timespec t2 = { .tv_sec = 2, .tv_nsec = 2000001 }; |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame] | 48 | static const struct timespec t_other = { .tv_sec = 123, .tv_nsec = 456 }; |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 49 | |
| 50 | static struct client * |
| 51 | create_client_with_keyboard_focus(void) |
| 52 | { |
| 53 | struct client *cl = create_client_and_test_surface(10, 10, 1, 1); |
| 54 | assert(cl); |
| 55 | |
| 56 | weston_test_activate_surface(cl->test->weston_test, |
| 57 | cl->surface->wl_surface); |
| 58 | client_roundtrip(cl); |
| 59 | |
| 60 | return cl; |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | send_key(struct client *client, const struct timespec *time, |
| 65 | uint32_t key, uint32_t state) |
| 66 | { |
| 67 | uint32_t tv_sec_hi, tv_sec_lo, tv_nsec; |
| 68 | |
| 69 | timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec); |
| 70 | weston_test_send_key(client->test->weston_test, tv_sec_hi, tv_sec_lo, |
| 71 | tv_nsec, key, state); |
| 72 | client_roundtrip(client); |
| 73 | } |
| 74 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 75 | TEST(simple_keyboard_test) |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 76 | { |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 77 | struct client *client = create_client_with_keyboard_focus(); |
| 78 | struct keyboard *keyboard = client->input->keyboard; |
| 79 | struct surface *expect_focus = client->surface; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 80 | uint32_t expect_key = 0; |
| 81 | uint32_t expect_state = 0; |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 82 | |
Dawid Gajownik | 74a635b | 2015-08-06 17:12:19 -0300 | [diff] [blame] | 83 | while (1) { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 84 | assert(keyboard->key == expect_key); |
| 85 | assert(keyboard->state == expect_state); |
| 86 | assert(keyboard->focus == expect_focus); |
| 87 | |
| 88 | if (keyboard->state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 89 | expect_state = WL_KEYBOARD_KEY_STATE_RELEASED; |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 90 | send_key(client, &t1, expect_key, expect_state); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 91 | } else if (keyboard->focus) { |
| 92 | expect_focus = NULL; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 93 | weston_test_activate_surface( |
| 94 | client->test->weston_test, NULL); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 95 | client_roundtrip(client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 96 | } else if (expect_key < 10) { |
| 97 | expect_key++; |
| 98 | expect_focus = client->surface; |
| 99 | expect_state = WL_KEYBOARD_KEY_STATE_PRESSED; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 100 | weston_test_activate_surface( |
| 101 | client->test->weston_test, |
| 102 | expect_focus->wl_surface); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 103 | send_key(client, &t1, expect_key, expect_state); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 104 | } else { |
| 105 | break; |
| 106 | } |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 107 | } |
Pekka Paalanen | ef8d652 | 2021-06-15 16:31:16 +0300 | [diff] [blame] | 108 | |
| 109 | client_destroy(client); |
U. Artie Eoff | 840d5f9 | 2012-10-01 15:21:16 -0700 | [diff] [blame] | 110 | } |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 111 | |
| 112 | TEST(keyboard_key_event_time) |
| 113 | { |
| 114 | struct client *client = create_client_with_keyboard_focus(); |
| 115 | struct keyboard *keyboard = client->input->keyboard; |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame] | 116 | struct input_timestamps *input_ts = |
| 117 | input_timestamps_create_for_keyboard(client); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 118 | |
| 119 | send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 120 | assert(keyboard->key_time_msec == timespec_to_msec(&t1)); |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame] | 121 | assert(timespec_eq(&keyboard->key_time_timespec, &t1)); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 122 | |
| 123 | send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED); |
| 124 | assert(keyboard->key_time_msec == timespec_to_msec(&t2)); |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame] | 125 | assert(timespec_eq(&keyboard->key_time_timespec, &t2)); |
| 126 | |
| 127 | input_timestamps_destroy(input_ts); |
Pekka Paalanen | ef8d652 | 2021-06-15 16:31:16 +0300 | [diff] [blame] | 128 | |
| 129 | client_destroy(client); |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | TEST(keyboard_timestamps_stop_after_input_timestamps_object_is_destroyed) |
| 133 | { |
| 134 | struct client *client = create_client_with_keyboard_focus(); |
| 135 | struct keyboard *keyboard = client->input->keyboard; |
| 136 | struct input_timestamps *input_ts = |
| 137 | input_timestamps_create_for_keyboard(client); |
| 138 | |
| 139 | send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 140 | assert(keyboard->key_time_msec == timespec_to_msec(&t1)); |
| 141 | assert(timespec_eq(&keyboard->key_time_timespec, &t1)); |
| 142 | |
| 143 | input_timestamps_destroy(input_ts); |
| 144 | |
| 145 | send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED); |
| 146 | assert(keyboard->key_time_msec == timespec_to_msec(&t2)); |
| 147 | assert(timespec_is_zero(&keyboard->key_time_timespec)); |
Pekka Paalanen | ef8d652 | 2021-06-15 16:31:16 +0300 | [diff] [blame] | 148 | |
| 149 | client_destroy(client); |
Alexandros Frantzis | 2b44248 | 2018-02-20 14:05:50 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | TEST(keyboard_timestamps_stop_after_client_releases_wl_keyboard) |
| 153 | { |
| 154 | struct client *client = create_client_with_keyboard_focus(); |
| 155 | struct keyboard *keyboard = client->input->keyboard; |
| 156 | struct input_timestamps *input_ts = |
| 157 | input_timestamps_create_for_keyboard(client); |
| 158 | |
| 159 | send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 160 | assert(keyboard->key_time_msec == timespec_to_msec(&t1)); |
| 161 | assert(timespec_eq(&keyboard->key_time_timespec, &t1)); |
| 162 | |
| 163 | wl_keyboard_release(client->input->keyboard->wl_keyboard); |
| 164 | |
| 165 | /* Set input_timestamp to an arbitrary value (different from t1, t2 |
| 166 | * and 0) and check that it is not changed by sending the event. |
| 167 | * This is preferred over just checking for 0, since 0 is used |
| 168 | * internally for resetting the timestamp after handling an input |
| 169 | * event and checking for it here may lead to false negatives. */ |
| 170 | keyboard->input_timestamp = t_other; |
| 171 | send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED); |
| 172 | assert(timespec_eq(&keyboard->input_timestamp, &t_other)); |
| 173 | |
| 174 | input_timestamps_destroy(input_ts); |
Pekka Paalanen | ef8d652 | 2021-06-15 16:31:16 +0300 | [diff] [blame] | 175 | |
| 176 | free(client->input->keyboard); |
| 177 | client->input->keyboard = NULL; |
| 178 | client_destroy(client); |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 179 | } |