Path: blob/master/tests/core/input/test_shortcut.cpp
23450 views
/**************************************************************************/1/* test_shortcut.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_shortcut)3334#include "core/input/input_event.h"35#include "core/input/shortcut.h"36#include "core/object/ref_counted.h"37#include "core/os/keyboard.h"3839namespace TestShortcut {4041TEST_CASE("[Shortcut] Empty shortcut should have no valid events and text equal to None") {42Shortcut s;4344CHECK(s.get_as_text() == "None");45CHECK(s.has_valid_event() == false);46}4748TEST_CASE("[Shortcut] Setting and getting an event should result in the same event as the input") {49Ref<InputEventKey> k1;50Ref<InputEventKey> k2;51k1.instantiate();52k2.instantiate();5354k1->set_keycode(Key::ENTER);55k2->set_keycode(Key::BACKSPACE);5657// Cast to InputEvent so the internal code recognizes the objects.58Ref<InputEvent> e1 = k1;59Ref<InputEvent> e2 = k2;60Array input_array = { e1, e2 };6162Shortcut s;63s.set_events(input_array);6465// Get result, read it out, check whether it equals the input.66Array result_array = s.get_events();67Ref<InputEventKey> result1 = result_array.front();68Ref<InputEventKey> result2 = result_array.back();6970CHECK(result1->get_keycode() == k1->get_keycode());71CHECK(result2->get_keycode() == k2->get_keycode());72}7374TEST_CASE("[Shortcut] 'set_events_list' should result in the same events as the input") {75Ref<InputEventKey> k1;76Ref<InputEventKey> k2;77k1.instantiate();78k2.instantiate();7980k1->set_keycode(Key::ENTER);81k2->set_keycode(Key::BACKSPACE);8283// Cast to InputEvent so the set_events_list() method recognizes the objects.84Ref<InputEvent> e1 = k1;85Ref<InputEvent> e2 = k2;8687List<Ref<InputEvent>> list;88list.push_back(e1);89list.push_back(e2);9091Shortcut s;92s.set_events_list(&list);9394// Get result, read it out, check whether it equals the input.95Array result_array = s.get_events();96Ref<InputEventKey> result1 = result_array.front();97Ref<InputEventKey> result2 = result_array.back();9899CHECK(result1->get_keycode() == k1->get_keycode());100CHECK(result2->get_keycode() == k2->get_keycode());101}102103TEST_CASE("[Shortcut] 'matches_event' should correctly match the same event") {104Ref<InputEventKey> original; // The one we compare with.105Ref<InputEventKey> similar_but_not_equal; // Same keycode, different event.106Ref<InputEventKey> different; // Different event, different keycode.107Ref<InputEventKey> copy; // Copy of original event.108109original.instantiate();110similar_but_not_equal.instantiate();111different.instantiate();112copy.instantiate();113114original->set_keycode(Key::ENTER);115similar_but_not_equal->set_keycode(Key::ENTER);116similar_but_not_equal->set_keycode(Key::ESCAPE);117copy = original;118119// Only the copy is really the same, so only that one should match.120// The rest should not match.121122Ref<InputEvent> e_original = original;123124Ref<InputEvent> e_similar_but_not_equal = similar_but_not_equal;125Ref<InputEvent> e_different = different;126Ref<InputEvent> e_copy = copy;127128Array a = { e_original };129Shortcut s;130s.set_events(a);131132CHECK(s.matches_event(e_similar_but_not_equal) == false);133CHECK(s.matches_event(e_different) == false);134135CHECK(s.matches_event(e_copy) == true);136}137138TEST_CASE("[Shortcut] 'get_as_text' text representation should be correct") {139Ref<InputEventKey> same;140// k2 will not go into the shortcut but only be used to compare.141Ref<InputEventKey> different;142143same.instantiate();144different.instantiate();145146same->set_keycode(Key::ENTER);147different->set_keycode(Key::ESCAPE);148149Ref<InputEvent> key_event1 = same;150Array a = { key_event1 };151Shortcut s;152s.set_events(a);153154CHECK(s.get_as_text() == same->as_text());155CHECK(s.get_as_text() != different->as_text());156}157158TEST_CASE("[Shortcut] Event validity should be correctly checked.") {159Ref<InputEventKey> valid;160// k2 will not go into the shortcut but only be used to compare.161Ref<InputEventKey> invalid = nullptr;162163valid.instantiate();164valid->set_keycode(Key::ENTER);165166Ref<InputEvent> valid_event = valid;167Ref<InputEvent> invalid_event = invalid;168169Array a = { invalid_event, valid_event };170171Shortcut s;172s.set_events(a);173174CHECK(s.has_valid_event() == true);175176Array b = { invalid_event };177178Shortcut shortcut_with_invalid_event;179shortcut_with_invalid_event.set_events(b);180181CHECK(shortcut_with_invalid_event.has_valid_event() == false);182}183184TEST_CASE("[Shortcut] Equal arrays should be recognized as such.") {185Ref<InputEventKey> k1;186// k2 will not go into the shortcut but only be used to compare.187Ref<InputEventKey> k2;188189k1.instantiate();190k2.instantiate();191192k1->set_keycode(Key::ENTER);193k2->set_keycode(Key::ESCAPE);194195Ref<InputEvent> key_event1 = k1;196Ref<InputEvent> key_event2 = k2;197198Array same;199same.append(key_event1);200201Array same_as_same;202same_as_same.append(key_event1);203204Array different1 = { key_event2 };205Array different2 = { key_event1, key_event2 };206Array different3;207208Shortcut s;209210CHECK(s.is_event_array_equal(same, same_as_same) == true);211CHECK(s.is_event_array_equal(same, different1) == false);212CHECK(s.is_event_array_equal(same, different2) == false);213CHECK(s.is_event_array_equal(same, different3) == false);214}215216} // namespace TestShortcut217218219