Path: blob/master/tests/core/input/test_input_event.h
10278 views
/**************************************************************************/1/* test_input_event.h */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#pragma once3132#include "core/input/input_event.h"33#include "core/math/rect2.h"34#include "core/os/memory.h"35#include "core/variant/array.h"3637#include "tests/test_macros.h"3839namespace TestInputEvent {40TEST_CASE("[InputEvent] Signal is emitted when device is changed") {41Ref<InputEventKey> input_event;42input_event.instantiate();4344SIGNAL_WATCH(*input_event, CoreStringName(changed));4546Array empty_args = { {} };47input_event->set_device(1);4849SIGNAL_CHECK("changed", empty_args);50CHECK(input_event->get_device() == 1);5152SIGNAL_UNWATCH(*input_event, CoreStringName(changed));53}5455TEST_CASE("[InputEvent] Test accumulate") {56Ref<InputEventMouseMotion> iemm1, iemm2;57Ref<InputEventKey> iek;5859iemm1.instantiate(), iemm2.instantiate();60iek.instantiate();6162iemm1->set_button_mask(MouseButtonMask::LEFT);6364CHECK_FALSE(iemm1->accumulate(iemm2));6566iemm2->set_button_mask(MouseButtonMask::LEFT);6768CHECK(iemm1->accumulate(iemm2));6970CHECK_FALSE(iemm1->accumulate(iek));71CHECK_FALSE(iemm2->accumulate(iek));72}7374TEST_CASE("[InputEvent][SceneTree] Test methods that interact with the InputMap") {75const String mock_action = "mock_action";76Ref<InputEventJoypadMotion> iejm;77iejm.instantiate();7879InputMap::get_singleton()->add_action(mock_action, 0.5);80InputMap::get_singleton()->action_add_event(mock_action, iejm);8182CHECK(iejm->is_action_type());83CHECK(iejm->is_action(mock_action));8485CHECK(iejm->is_action_released(mock_action));86CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.0f));8788iejm->set_axis_value(0.8f);89// Since deadzone is 0.5, action_strength grows linearly from 0.5 to 1.0.90CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.6f));91CHECK(Math::is_equal_approx(iejm->get_action_raw_strength(mock_action), 0.8f));92CHECK(iejm->is_action_pressed(mock_action));9394InputMap::get_singleton()->erase_action(mock_action);95}9697TEST_CASE("[InputEvent] Test xformed_by") {98Ref<InputEventMouseMotion> iemm1;99iemm1.instantiate();100101iemm1->set_position(Vector2(0.0f, 0.0f));102Transform2D transform;103transform = transform.translated(Vector2(2.0f, 3.0f));104105Ref<InputEventMouseMotion> iemm2 = iemm1->xformed_by(transform);106107CHECK(iemm2->get_position().is_equal_approx(Vector2(2.0f, 3.0f)));108}109} // namespace TestInputEvent110111112