|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +use crate::framework::itest; |
| 8 | +use godot::obj::WithBaseField; |
| 9 | +use godot::prelude::*; |
| 10 | +use godot::task::{SignalFuture, TaskHandle}; |
| 11 | + |
| 12 | +const ACCEPTED_NAME: &str = "touched"; |
| 13 | + |
| 14 | +#[derive(GodotClass)] |
| 15 | +#[class(init,base=Node2D)] |
| 16 | +struct DeferredTestNode { |
| 17 | + base: Base<Node2D>, |
| 18 | +} |
| 19 | + |
| 20 | +#[godot_api] |
| 21 | +impl DeferredTestNode { |
| 22 | + #[signal] |
| 23 | + fn test_completed(name: StringName); |
| 24 | + |
| 25 | + #[func] |
| 26 | + fn accept(&mut self) { |
| 27 | + self.base_mut().set_name(ACCEPTED_NAME); |
| 28 | + } |
| 29 | + |
| 30 | + fn create_assertion_task(&mut self) -> TaskHandle { |
| 31 | + assert_ne!( |
| 32 | + self.base().get_name().to_string(), |
| 33 | + ACCEPTED_NAME, |
| 34 | + "accept evaluated synchronously" |
| 35 | + ); |
| 36 | + |
| 37 | + let run_test: SignalFuture<(StringName,)> = self.signals().test_completed().to_future(); |
| 38 | + |
| 39 | + godot::task::spawn(async move { |
| 40 | + let (name,) = run_test.await; |
| 41 | + |
| 42 | + assert_eq!(name.to_string(), ACCEPTED_NAME); |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[godot_api] |
| 48 | +impl INode2D for DeferredTestNode { |
| 49 | + fn process(&mut self, _delta: f64) { |
| 50 | + let name = self.base().get_name(); |
| 51 | + self.signals().test_completed().emit(&name); |
| 52 | + self.base_mut().queue_free(); |
| 53 | + } |
| 54 | + |
| 55 | + fn ready(&mut self) { |
| 56 | + self.base_mut().set_name("verify"); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[itest(async)] |
| 61 | +fn call_deferred_untyped(ctx: &crate::framework::TestContext) -> TaskHandle { |
| 62 | + let mut test_node = DeferredTestNode::new_alloc(); |
| 63 | + ctx.scene_tree.clone().add_child(&test_node); |
| 64 | + |
| 65 | + // Called through Godot and therefore requires #[func] on the method. |
| 66 | + test_node.call_deferred("accept", &[]); |
| 67 | + |
| 68 | + let mut gd_mut = test_node.bind_mut(); |
| 69 | + gd_mut.create_assertion_task() |
| 70 | +} |
0 commit comments