Path: blob/master/tests/core/input/test_input_event.cpp
23450 views
/**************************************************************************/1/* test_input_event.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_input_event)3334#include "core/input/input_event.h"35#include "core/input/input_map.h"36#include "core/math/rect2.h"37#include "core/variant/array.h"38#include "tests/signal_watcher.h"3940namespace TestInputEvent {4142TEST_CASE("[InputEvent] Signal is emitted when device is changed") {43Ref<InputEventKey> input_event;44input_event.instantiate();4546SIGNAL_WATCH(*input_event, CoreStringName(changed));4748Array empty_args = { {} };49input_event->set_device(1);5051SIGNAL_CHECK("changed", empty_args);52CHECK(input_event->get_device() == 1);5354SIGNAL_UNWATCH(*input_event, CoreStringName(changed));55}5657TEST_CASE("[InputEvent] Test accumulate") {58Ref<InputEventMouseMotion> iemm1, iemm2;59Ref<InputEventKey> iek;6061iemm1.instantiate(), iemm2.instantiate();62iek.instantiate();6364iemm1->set_button_mask(MouseButtonMask::LEFT);6566CHECK_FALSE(iemm1->accumulate(iemm2));6768iemm2->set_button_mask(MouseButtonMask::LEFT);6970CHECK(iemm1->accumulate(iemm2));7172CHECK_FALSE(iemm1->accumulate(iek));73CHECK_FALSE(iemm2->accumulate(iek));74}7576TEST_CASE("[InputEvent][SceneTree] Test methods that interact with the InputMap") {77const String mock_action = "mock_action";78Ref<InputEventJoypadMotion> iejm;79iejm.instantiate();8081InputMap::get_singleton()->add_action(mock_action, 0.5);82InputMap::get_singleton()->action_add_event(mock_action, iejm);8384CHECK(iejm->is_action_type());85CHECK(iejm->is_action(mock_action));8687CHECK(iejm->is_action_released(mock_action));88CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.0f));8990iejm->set_axis_value(0.8f);91// Since deadzone is 0.5, action_strength grows linearly from 0.5 to 1.0.92CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.6f));93CHECK(Math::is_equal_approx(iejm->get_action_raw_strength(mock_action), 0.8f));94CHECK(iejm->is_action_pressed(mock_action));9596InputMap::get_singleton()->erase_action(mock_action);97}9899TEST_CASE("[InputEvent] Test xformed_by") {100Ref<InputEventMouseMotion> iemm1;101iemm1.instantiate();102103iemm1->set_position(Vector2(0.0f, 0.0f));104Transform2D transform;105transform = transform.translated(Vector2(2.0f, 3.0f));106107Ref<InputEventMouseMotion> iemm2 = iemm1->xformed_by(transform);108109CHECK(iemm2->get_position().is_equal_approx(Vector2(2.0f, 3.0f)));110}111112} // namespace TestInputEvent113114115