efl

Форк
0
/
Events.cs 
368 строк · 10.0 Кб
1
/*
2
 * Copyright 2019 by its authors. See AUTHORS.
3
 *
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
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
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.
15
 */
16
using System;
17
using System.Linq;
18
using System.Collections.Generic;
19

20
namespace TestSuite
21
{
22

23
class TestEoEvents
24
{
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) {
29
        called = true;
30
        Efl.Object obj = sender as Efl.Object;
31
        if (obj != null)
32
        {
33
            obj.Name = "loop_called";
34
            correct_sender = true;
35
        }
36

37
        Eina.Value v = new Eina.Value(Eina.ValueType.Int32);
38
        v.Set(0);
39
        loop.Quit(v);
40
        v.Dispose();
41
    }
42
    protected void another_callback(object sender, EventArgs e) { }
43

44
    public static void idle_event()
45
    {
46
        Efl.Loop loop = Efl.App.AppMain;
47
        loop.Name = "loop";
48
        TestEoEvents listener = new TestEoEvents();
49
        listener.loop = loop;
50
        loop.IdleEvent += listener.callback;
51

52
        Test.Assert(!listener.called);
53
        Test.Assert(!listener.correct_sender);
54
        Test.AssertEquals("loop", loop.Name);
55
        loop.Begin();
56
        Test.Assert(listener.called);
57
        Test.Assert(listener.correct_sender);
58
        Test.AssertEquals("loop_called", loop.Name);
59

60
        loop.IdleEvent -= listener.callback;
61
    }
62

63
    public static void event_with_string_payload()
64
    {
65
        var obj = new Dummy.TestObject();
66
        string received_string = null;
67

68
        obj.EvtWithStringEvent += (object sender, Dummy.TestObjectEvtWithStringEventArgs e) => {
69
            received_string = e.Arg;
70
        };
71

72
        obj.EmitEventWithString("Some args");
73

74
        Test.AssertEquals("Some args", received_string);
75
        obj.Dispose();
76
    }
77

78
    public static void event_with_int_payload()
79
    {
80
        var obj = new Dummy.TestObject();
81
        int received_int= 0;
82

83
        obj.EvtWithIntEvent += (object sender, Dummy.TestObjectEvtWithIntEventArgs e) => {
84
            received_int = e.Arg;
85
        };
86

87
        obj.EmitEventWithInt(-1984);
88

89
        Test.AssertEquals(-1984, received_int);
90
        obj.Dispose();
91
    }
92

93
    public static void event_with_bool_payload()
94
    {
95
        var obj = new Dummy.TestObject();
96
        bool received_bool = false;
97

98
        obj.EvtWithBoolEvent += (object sender, Dummy.TestObjectEvtWithBoolEventArgs e) => {
99
            received_bool = e.Arg;
100
        };
101

102
        obj.EmitEventWithBool(true);
103

104
        Test.AssertEquals(true, received_bool);
105

106
        obj.EmitEventWithBool(false);
107

108
        Test.AssertEquals(false, received_bool);
109
        obj.Dispose();
110
    }
111

112
    public static void event_with_uint_payload()
113
    {
114
        var obj = new Dummy.TestObject();
115
        uint received_uint = 0;
116
        obj.EvtWithUintEvent += (object sender, Dummy.TestObjectEvtWithUintEventArgs e) => {
117
            received_uint = e.Arg;
118
        };
119

120
        obj.EmitEventWithUint(0xbeef);
121

122
        Test.AssertEquals<uint>(0xbeef, received_uint);
123
        obj.Dispose();
124
    }
125

126
    public static void event_with_float_payload()
127
    {
128
        var obj = new Dummy.TestObject();
129
        float received_float = 0;
130
        obj.EvtWithFloatEvent += (object sender, Dummy.TestObjectEvtWithFloatEventArgs e) => {
131
            received_float = e.Arg;
132
        };
133

134
        obj.EmitEventWithFloat(3.14f);
135

136
        Test.AssertAlmostEquals(3.14f, received_float);
137
        obj.Dispose();
138
    }
139

140
    public static void event_with_double_payload()
141
    {
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;
147
        };
148

149
        obj.EmitEventWithDouble(reference);
150

151
        Test.AssertAlmostEquals(reference, received_double);
152
        obj.Dispose();
153
    }
154

155
    public static void event_with_object_payload()
156
    {
157
        var obj = new Dummy.TestObject();
158
        Dummy.TestObject received_obj = null;
159

160
        obj.EvtWithObjEvent += (object sender, Dummy.TestObjectEvtWithObjEventArgs e) => {
161
            received_obj = e.Arg;
162
        };
163

164
        var sent_obj = new Dummy.TestObject();
165

166
        obj.EmitEventWithObj(sent_obj);
167

168
        Test.AssertEquals(sent_obj, received_obj);
169
        sent_obj.Dispose();
170
        obj.Dispose();
171
    }
172

173
    public static void event_with_error_payload()
174
    {
175
        var obj = new Dummy.TestObject();
176
        Eina.Error received_error = 0;
177

178
        obj.EvtWithErrorEvent += (object sender, Dummy.TestObjectEvtWithErrorEventArgs e) => {
179
            received_error = e.Arg;
180
        };
181

182
        Eina.Error sent_error = -2001;
183

184
        obj.EmitEventWithError(sent_error);
185

186
        Test.AssertEquals(sent_error, received_error);
187
        obj.Dispose();
188
    }
189

190
    public static void event_with_struct_payload()
191
    {
192
        var obj = new Dummy.TestObject();
193
        Dummy.StructSimple received_struct = default(Dummy.StructSimple);
194

195
        obj.EvtWithStructEvent += (object sender, Dummy.TestObjectEvtWithStructEventArgs e) => {
196
            received_struct = e.Arg;
197
        };
198

199
        Dummy.StructSimple sent_struct = new Dummy.StructSimple(fstring: "Struct Event");
200

201
        obj.EmitEventWithStruct(sent_struct);
202

203
        Test.AssertEquals(sent_struct.Fstring, received_struct.Fstring);
204
        obj.Dispose();
205
    }
206

207
#if EFL_BETA
208
    public static void event_with_struct_complex_payload()
209
    {
210
        var obj = new Dummy.TestObject();
211
        Dummy.StructComplex received_struct = default(Dummy.StructComplex);
212

213
        obj.EvtWithStructComplexEvent += (object sender, Dummy.TestObjectEvtWithStructComplexEventArgs e) => {
214
            received_struct = e.Arg;
215
        };
216

217
        Dummy.StructComplex sent_struct = StructHelpers.structComplexWithValues();
218

219
        obj.EmitEventWithStructComplex(sent_struct);
220

221
        Test.AssertEquals(sent_struct.Fobj, received_struct.Fobj);
222
        obj.Dispose();
223
    }
224
#endif
225

226
    public static void event_with_array_payload()
227
    {
228
        var obj = new Dummy.TestObject();
229
        List<string> received = null;
230
        Eina.Array<string> sent = new Eina.Array<string>();
231

232
        sent.Append("Abc");
233
        sent.Append("Def");
234
        sent.Append("Ghi");
235

236
        obj.EvtWithArrayEvent += (object sender, Dummy.TestObjectEvtWithArrayEventArgs e) => {
237
            received = e.Arg as List<string>;
238
        };
239

240
        obj.EmitEventWithArray(sent);
241

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)
245
        {
246
            Test.AssertEquals(pair.Sent, pair.Received);
247
        }
248
        sent.Dispose();
249
        obj.Dispose();
250
    }
251
}
252

253
class TestEventAddRemove
254
{
255
    public static void test_add_remove_event()
256
    {
257
        var obj = new Dummy.TestObject();
258
        bool called = true;
259

260
        EventHandler<Dummy.TestObjectEvtWithIntEventArgs> evtCb = (object sender, Dummy.TestObjectEvtWithIntEventArgs e) => {
261
            called = true;
262
        };
263

264
        obj.EvtWithIntEvent += evtCb;
265
        obj.EmitEventWithInt(42);
266
        Test.Assert(called);
267

268
        called = false;
269
        obj.EvtWithIntEvent -= evtCb;
270
        obj.EmitEventWithInt(42);
271
        Test.Assert(!called);
272
        obj.Dispose();
273
    }
274
}
275

276
class TestInterfaceEvents
277
{
278
    public static void test_nonconflicting_events()
279
    {
280
        var obj = new Dummy.TestObject();
281
        var called = false;
282

283
        EventHandler cb = (object sender, EventArgs e) => {
284
            called = true;
285
        };
286

287
        obj.NonconflictedEvent += cb;
288
        obj.EmitNonconflicted();
289
        Test.Assert(called);
290
        obj.Dispose();
291
    }
292
}
293

294
class TestEventNaming
295
{
296
    // For events named line focus_geometry,changed
297
    public static void test_event_naming()
298
    {
299
        var obj = new Dummy.TestObject();
300
        var test_called = false;
301

302
        EventHandler cb = (object sender, EventArgs e) => {
303
            test_called = true;
304
        };
305

306
        obj.EvtWithUnderEvent += cb;
307

308
        obj.EmitEventWithUnder();
309

310
        Test.Assert(test_called);
311
        obj.Dispose();
312
    }
313
}
314

315
class TestEventWithDeadWrappers
316
{
317

318
    private static WeakReference AttachToManager(Dummy.EventManager manager,
319
                                          EventHandler<Dummy.TestObjectEvtWithIntEventArgs> cb)
320
    {
321
        var obj = new Dummy.TestObject();
322
        manager.SetEmitter(obj);
323

324
        obj.EvtWithIntEvent += cb;
325
        return new WeakReference(obj);
326
    }
327

328
    public static void test_event_from_c_owned_wrapper()
329
    {
330
        // Set upon object instantiation
331
        WeakReference wref = null;
332

333
        // Checks in the callback called
334
        bool callbackCalled = false;
335
        int received = -1;
336

337
        // attach to evt with int
338
        EventHandler<Dummy.TestObjectEvtWithIntEventArgs> cb = (object sender, Dummy.TestObjectEvtWithIntEventArgs args) => {
339
            callbackCalled = true;
340
            received = args.Arg;
341
            Test.Assert(Object.ReferenceEquals(sender, wref.Target));
342
        };
343

344
        Dummy.EventManager manager = new Dummy.EventManager();
345
        wref = AttachToManager(manager, cb);
346

347
        Test.CollectAndIterate();
348

349
        manager.EmitWithInt(42);
350

351
        Test.CollectAndIterate();
352

353
        Test.Assert(callbackCalled, "Callback must have been called.");
354
        Test.AssertEquals(42, received, "Wrong value received.");
355

356
        // Cleanup checks
357
        manager.Release();
358

359
        // Make sure the released wrapper is collected and release the Eo object
360
        Test.CollectAndIterate();
361

362
        Test.AssertNull(wref.Target);
363
        manager.Dispose();
364
    }
365

366
}
367

368
}
369

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.