2
* Copyright 2019 by its authors. See AUTHORS.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
using System.Collections.Generic;
25
public bool called = false;
26
public bool correct_sender = false;
27
public Efl.Loop loop { get; set; }
28
protected void callback(object sender, EventArgs e) {
30
Efl.Object obj = sender as Efl.Object;
33
obj.Name = "loop_called";
34
correct_sender = true;
37
Eina.Value v = new Eina.Value(Eina.ValueType.Int32);
42
protected void another_callback(object sender, EventArgs e) { }
44
public static void idle_event()
46
Efl.Loop loop = Efl.App.AppMain;
48
TestEoEvents listener = new TestEoEvents();
50
loop.IdleEvent += listener.callback;
52
Test.Assert(!listener.called);
53
Test.Assert(!listener.correct_sender);
54
Test.AssertEquals("loop", loop.Name);
56
Test.Assert(listener.called);
57
Test.Assert(listener.correct_sender);
58
Test.AssertEquals("loop_called", loop.Name);
60
loop.IdleEvent -= listener.callback;
63
public static void event_with_string_payload()
65
var obj = new Dummy.TestObject();
66
string received_string = null;
68
obj.EvtWithStringEvent += (object sender, Dummy.TestObjectEvtWithStringEventArgs e) => {
69
received_string = e.Arg;
72
obj.EmitEventWithString("Some args");
74
Test.AssertEquals("Some args", received_string);
78
public static void event_with_int_payload()
80
var obj = new Dummy.TestObject();
83
obj.EvtWithIntEvent += (object sender, Dummy.TestObjectEvtWithIntEventArgs e) => {
87
obj.EmitEventWithInt(-1984);
89
Test.AssertEquals(-1984, received_int);
93
public static void event_with_bool_payload()
95
var obj = new Dummy.TestObject();
96
bool received_bool = false;
98
obj.EvtWithBoolEvent += (object sender, Dummy.TestObjectEvtWithBoolEventArgs e) => {
99
received_bool = e.Arg;
102
obj.EmitEventWithBool(true);
104
Test.AssertEquals(true, received_bool);
106
obj.EmitEventWithBool(false);
108
Test.AssertEquals(false, received_bool);
112
public static void event_with_uint_payload()
114
var obj = new Dummy.TestObject();
115
uint received_uint = 0;
116
obj.EvtWithUintEvent += (object sender, Dummy.TestObjectEvtWithUintEventArgs e) => {
117
received_uint = e.Arg;
120
obj.EmitEventWithUint(0xbeef);
122
Test.AssertEquals<uint>(0xbeef, received_uint);
126
public static void event_with_float_payload()
128
var obj = new Dummy.TestObject();
129
float received_float = 0;
130
obj.EvtWithFloatEvent += (object sender, Dummy.TestObjectEvtWithFloatEventArgs e) => {
131
received_float = e.Arg;
134
obj.EmitEventWithFloat(3.14f);
136
Test.AssertAlmostEquals(3.14f, received_float);
140
public static void event_with_double_payload()
142
var obj = new Dummy.TestObject();
143
double received_double = 0;
144
double reference = float.MaxValue + 42;
145
obj.EvtWithDoubleEvent += (object sender, Dummy.TestObjectEvtWithDoubleEventArgs e) => {
146
received_double = e.Arg;
149
obj.EmitEventWithDouble(reference);
151
Test.AssertAlmostEquals(reference, received_double);
155
public static void event_with_object_payload()
157
var obj = new Dummy.TestObject();
158
Dummy.TestObject received_obj = null;
160
obj.EvtWithObjEvent += (object sender, Dummy.TestObjectEvtWithObjEventArgs e) => {
161
received_obj = e.Arg;
164
var sent_obj = new Dummy.TestObject();
166
obj.EmitEventWithObj(sent_obj);
168
Test.AssertEquals(sent_obj, received_obj);
173
public static void event_with_error_payload()
175
var obj = new Dummy.TestObject();
176
Eina.Error received_error = 0;
178
obj.EvtWithErrorEvent += (object sender, Dummy.TestObjectEvtWithErrorEventArgs e) => {
179
received_error = e.Arg;
182
Eina.Error sent_error = -2001;
184
obj.EmitEventWithError(sent_error);
186
Test.AssertEquals(sent_error, received_error);
190
public static void event_with_struct_payload()
192
var obj = new Dummy.TestObject();
193
Dummy.StructSimple received_struct = default(Dummy.StructSimple);
195
obj.EvtWithStructEvent += (object sender, Dummy.TestObjectEvtWithStructEventArgs e) => {
196
received_struct = e.Arg;
199
Dummy.StructSimple sent_struct = new Dummy.StructSimple(fstring: "Struct Event");
201
obj.EmitEventWithStruct(sent_struct);
203
Test.AssertEquals(sent_struct.Fstring, received_struct.Fstring);
208
public static void event_with_struct_complex_payload()
210
var obj = new Dummy.TestObject();
211
Dummy.StructComplex received_struct = default(Dummy.StructComplex);
213
obj.EvtWithStructComplexEvent += (object sender, Dummy.TestObjectEvtWithStructComplexEventArgs e) => {
214
received_struct = e.Arg;
217
Dummy.StructComplex sent_struct = StructHelpers.structComplexWithValues();
219
obj.EmitEventWithStructComplex(sent_struct);
221
Test.AssertEquals(sent_struct.Fobj, received_struct.Fobj);
226
public static void event_with_array_payload()
228
var obj = new Dummy.TestObject();
229
List<string> received = null;
230
Eina.Array<string> sent = new Eina.Array<string>();
236
obj.EvtWithArrayEvent += (object sender, Dummy.TestObjectEvtWithArrayEventArgs e) => {
237
received = e.Arg as List<string>;
240
obj.EmitEventWithArray(sent);
242
Test.AssertEquals(sent.Count, received.Count);
243
var pairs = sent.Zip(received, (string sentItem, string receivedItem) => new { Sent = sentItem, Received = receivedItem } );
244
foreach (var pair in pairs)
246
Test.AssertEquals(pair.Sent, pair.Received);
253
class TestEventAddRemove
255
public static void test_add_remove_event()
257
var obj = new Dummy.TestObject();
260
EventHandler<Dummy.TestObjectEvtWithIntEventArgs> evtCb = (object sender, Dummy.TestObjectEvtWithIntEventArgs e) => {
264
obj.EvtWithIntEvent += evtCb;
265
obj.EmitEventWithInt(42);
269
obj.EvtWithIntEvent -= evtCb;
270
obj.EmitEventWithInt(42);
271
Test.Assert(!called);
276
class TestInterfaceEvents
278
public static void test_nonconflicting_events()
280
var obj = new Dummy.TestObject();
283
EventHandler cb = (object sender, EventArgs e) => {
287
obj.NonconflictedEvent += cb;
288
obj.EmitNonconflicted();
296
// For events named line focus_geometry,changed
297
public static void test_event_naming()
299
var obj = new Dummy.TestObject();
300
var test_called = false;
302
EventHandler cb = (object sender, EventArgs e) => {
306
obj.EvtWithUnderEvent += cb;
308
obj.EmitEventWithUnder();
310
Test.Assert(test_called);
315
class TestEventWithDeadWrappers
318
private static WeakReference AttachToManager(Dummy.EventManager manager,
319
EventHandler<Dummy.TestObjectEvtWithIntEventArgs> cb)
321
var obj = new Dummy.TestObject();
322
manager.SetEmitter(obj);
324
obj.EvtWithIntEvent += cb;
325
return new WeakReference(obj);
328
public static void test_event_from_c_owned_wrapper()
330
// Set upon object instantiation
331
WeakReference wref = null;
333
// Checks in the callback called
334
bool callbackCalled = false;
337
// attach to evt with int
338
EventHandler<Dummy.TestObjectEvtWithIntEventArgs> cb = (object sender, Dummy.TestObjectEvtWithIntEventArgs args) => {
339
callbackCalled = true;
341
Test.Assert(Object.ReferenceEquals(sender, wref.Target));
344
Dummy.EventManager manager = new Dummy.EventManager();
345
wref = AttachToManager(manager, cb);
347
Test.CollectAndIterate();
349
manager.EmitWithInt(42);
351
Test.CollectAndIterate();
353
Test.Assert(callbackCalled, "Callback must have been called.");
354
Test.AssertEquals(42, received, "Wrong value received.");
359
// Make sure the released wrapper is collected and release the Eo object
360
Test.CollectAndIterate();
362
Test.AssertNull(wref.Target);