summaryrefslogtreecommitdiff
path: root/src/tests/efl_mono/Eo.cs
blob: 71bb6deed04c20c5c9e8e49447ebc34cafd0799b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
using System;
using System.Linq;
using System.Collections.Generic;

namespace TestSuite
{

class TestEo
{
    private class Derived : Dummy.TestObject
    {
    }

    public static void return_null_object()
    {
        var testing = new Dummy.TestObject();
        var o1 = testing.ReturnNullObject();
        Test.Assert(o1 == null);
    }

    //
    // Test cases:
    //
    public static void return_same_object()
    {
        var testing = new Dummy.TestObject();
        var o1 = testing.ReturnObject();
        Test.Assert(o1.NativeHandle != IntPtr.Zero);
        Test.Assert(o1.NativeHandle == testing.NativeHandle);
        var o2 = o1.ReturnObject();
        Test.Assert(o2.NativeHandle != IntPtr.Zero);
        Test.Assert(o2.NativeHandle == o1.NativeHandle);
    }
    /* Commented out as adding the event listener seems to prevent it from being GC'd.
    public static void destructor_really_frees()
    {
       bool delEventCalled = false;
       {
           var obj = new Dummy.TestObject();
           obj.DEL += (object sender, EventArgs e) => { delEventCalled = true; };
       }

       System.GC.WaitForPendingFinalizers();
       System.GC.Collect();
       System.GC.WaitForPendingFinalizers();
       System.GC.Collect();
       System.GC.WaitForPendingFinalizers();

       Test.Assert(delEventCalled, "DEL event not called");
    } */

    /* Commented until we figure out a new way to test disposing
    public static void dispose_really_frees()
    {
       bool delEventCalled = false;
       {
           var obj = new Dummy.TestObject();
           Eina.Log.Error($"Created object 0x{obj.NativeHandle.ToInt64():x}");
           obj.DelEvt += (object sender, EventArgs e) => { delEventCalled = true; };
           Eina.Log.Error($"Will dispose object 0x{obj.NativeHandle.ToInt64():x}");
           ((IDisposable)obj).Dispose();
       }

       Test.Assert(delEventCalled, "DEL event not called");
    }
    */

    /* Commented out as adding the event listener seems to prevent it from being GC'd.
    public static void derived_destructor_really_frees()
    {
       bool delEventCalled = false;
       {
           var obj = new Derived();
           obj.DEL += (object sender, EventArgs e) => { delEventCalled = true; };
       }

       System.GC.WaitForPendingFinalizers();
       System.GC.Collect();
       System.GC.WaitForPendingFinalizers();
       System.GC.Collect();
       System.GC.WaitForPendingFinalizers();

       Test.Assert(delEventCalled, "DEL event not called");
    }

    public static void derived_dispose_really_frees()
    {
       bool delEventCalled = false;
       {
           var obj = new Derived();
           obj.DEL += (object sender, EventArgs e) => { delEventCalled = true; };
           ((IDisposable)obj).Dispose();
       }

       Test.Assert(delEventCalled, "DEL event not called");
    }
    */
}


class MyObject : Efl.Object
{
    public MyObject() : base(null) { }
}

class TestEoInherit
{
    public static void instantiate_inherited()
    {
        Efl.Object loop = new MyObject();
        Test.Assert(loop.NativeHandle != System.IntPtr.Zero);
    }

    private static WeakReference CreateCollectableInherited()
    {
        var obj = new MyObject();
        return new WeakReference(obj);
    }

    public static void inherited_collected()
    {
        var wref = CreateCollectableInherited();
        Test.CollectAndIterate(300, 10);

        Test.AssertNull(wref.Target);
    }
}

class TestEoNames
{
    public static void name_getset()
    {
        var obj = new Dummy.TestObject();

        string name = "Dummy";
        obj.SetName(name);
        Test.AssertEquals(name, obj.GetName());
    }
}

class TestEoParent
{
    public static void basic_parent()
    {
        var parent = new Dummy.TestObject(null);
        var child = new Dummy.TestObject(parent);

        Test.AssertEquals(parent, child.GetParent());

        var parent_retrieved = child.GetParent() as Dummy.TestObject;
        Test.AssertEquals(parent, parent_retrieved);
    }

    public static void parent_inherited_class()
    {
        Dummy.Numberwrapper parent = new Dummy.Numberwrapper(null);
        var child = new Dummy.TestObject(parent);

        Test.AssertEquals(parent, child.GetParent());

        Dummy.Numberwrapper parent_retrieved = child.GetParent() as Dummy.Numberwrapper;
        Test.AssertEquals(parent, parent_retrieved);
    }

    private class Derived : Dummy.TestObject
    {
        public Derived(Dummy.TestObject parent = null) : base (parent)
        {
        }
    }

    public static void basic_parent_managed_inherit()
    {
        var parent = new Derived(null);
        var child = new Derived(parent);

        Test.AssertEquals(parent, child.GetParent());

        var parent_from_cast = child.GetParent() as Derived;
        Test.AssertEquals(parent, parent_from_cast);
    }
}

class TestKlassMethods
{
    public static void basic_class_method()
    {
        int reference = 0xbeef;
        Dummy.TestObject.SetKlassProp(reference);
        Test.AssertEquals(reference, Dummy.TestObject.GetKlassProp());
    }

    public static void inherited_class_method()
    {
        int reference = 0xdead;
        Dummy.Child.SetKlassProp(reference);
        Test.AssertEquals(reference, Dummy.Child.GetKlassProp());
    }
}

class TestTypedefs
{
    public static void basic_typedef_test()
    {
        var obj = new Dummy.TestObject();
        Dummy.MyInt input = 1900;
        Dummy.MyInt receiver;

        int ret = obj.BypassTypedef(input, out receiver);

        Test.AssertEquals((Dummy.MyInt)ret, input);
        Test.AssertEquals(receiver, input);

    }
}

class TestVariables
{
    public static void test_constant_variables()
    {
        Test.AssertEquals(Dummy.Constants.ConstvarBool, true);
        Test.AssertEquals(Dummy.Constants.ConstvarInt, -32766);
        Test.AssertEquals(Dummy.Constants.ConstvarUInt, 65533U);
        Test.AssertEquals(Dummy.Constants.ConstvarLong, -2147483644L);
        Test.AssertEquals(Dummy.Constants.ConstvarULong, 4294967288UL);
        Test.AssertEquals(Dummy.Constants.ConstvarLLong, -9223372036854775800);
        Test.AssertEquals(Dummy.Constants.ConstvarULLong, 18446744073709551615);
        Test.AssertEquals(Dummy.Constants.ConstvarFloat, 16777211.0f);
        Test.AssertEquals(Dummy.Constants.ConstvarDouble, 9007199254740988.0);
        Test.AssertEquals(Dummy.Constants.ConstvarChar, '!');
        Test.AssertEquals(Dummy.Constants.ConstvarString, "test_str");
    }
}

class TestEoAccessors
{
    public static void basic_eo_accessors()
    {
        var obj = new Dummy.TestObject();
        Eina.List<int> lst = new Eina.List<int>();
        lst.Append(4);
        lst.Append(3);
        lst.Append(2);
        lst.Append(5);
        Eina.Accessor<int> acc = obj.CloneAccessor(lst.GetAccessor());

        var zipped = acc.Zip(lst, (first, second) => new Tuple<int, int>(first, second));

        foreach(Tuple<int, int> pair in zipped)
        {
            Test.AssertEquals(pair.Item1, pair.Item2);
        }
    }
}

class TestEoFinalize
{
    public sealed class Inherit : Efl.Object
    {
        public bool finalizeCalled = false;
        public override Efl.Object FinalizeAdd()
        {
            finalizeCalled = true;
            return this;
        }
    }


    public static void finalize_call()
    {
        Inherit inherit = new Inherit();
        Test.Assert(inherit.finalizeCalled);
    }
}

class TestEoMultipleChildClasses
{

    public sealed class FirstChild : Efl.Object
    {
        public int receivedValue = 0;
        public override Efl.Object FinalizeAdd()
        {
            receivedValue = 1;
            return this;
        }
    }

    public sealed class SecondChild : Efl.Object
    {
        public int receivedValue = 0;
        public override Efl.Object FinalizeAdd()
        {
            receivedValue = 2;
            return this;
        }
    }

    public static void test_multiple_child_classes()
    {
        FirstChild obj = new FirstChild();
        Test.AssertEquals(1, obj.receivedValue);
        SecondChild obj2 = new SecondChild();
        Test.AssertEquals(2, obj2.receivedValue);

        obj = new FirstChild();
        Test.AssertEquals(1, obj.receivedValue);
    }
}

class TestCsharpProperties
{
    public static void test_csharp_properties()
    {
        var obj = new Dummy.TestObject();
        var name = "My Name";
        obj.Name = name;

        Test.AssertEquals(name, obj.Name);
    }

    public static void test_getter_only()
    {
        var obj = new Dummy.TestObject();
        Test.Assert(!obj.Invalidating);
    }

    public static void test_setter_only()
    {
        var obj = new Dummy.TestObject();
        int val = -1984;

        obj.SetterOnly = val;
        Test.AssertEquals(val, obj.GetSetterOnly());
    }

    public static void test_class_property()
    {
        int val = -42;
        Dummy.TestObject.KlassProp = val;
        Test.AssertEquals(val, Dummy.TestObject.KlassProp);
    }

    public static void test_iface_property()
    {
        int val = -33;
        Dummy.ITestIface iface = new Dummy.TestObject();
        iface.IfaceProp = val;
        Test.AssertEquals(val, iface.IfaceProp);
    }

    public static void test_csharp_multi_valued_prop()
    {
        var obj = new Dummy.TestObject();
        obj.MultiValuedProp = (1, 2);
        var ret = obj.MultiValuedProp;
        Test.AssertEquals(ret, (1, 2));
    }
}

class TestEoGrandChildrenFinalize
{
    public sealed class Child : Dummy.TestObject
    {
        public int receivedValue = 0;
        public override Efl.Object FinalizeAdd()
        {
            receivedValue = 42;
            return this;
        }
    }

    public static void test_grand_children_finalize()
    {
        Child obj = new Child();
        Test.AssertEquals(42, obj.receivedValue);
    }

    public sealed class GrandChild : Dummy.Child
    {

#if EFL_BETA
        public GrandChild() : base(null, "", 0.0, 0) { }
#else
        public GrandChild() : base(null, "", 0.0) { }
#endif

        public int receivedValue = 0;
        public override Efl.Object FinalizeAdd()
        {
            receivedValue = -42;
            return this;
        }
    }

    public static void test_grand_grand_children_finalize()
    {
        GrandChild obj = new GrandChild();
        Test.AssertEquals(-42, obj.receivedValue);
    }
}

class TestConstructors
{
    public static void test_simple_constructor()
    {
        int iface_prop = 42;
        string a = "LFE";
        double b = 3.14;
#if EFL_BETA
        int beta = 1337;
#endif

#if EFL_BETA
        var obj = new Dummy.Child(null, a, b, beta, iface_prop, 0);
#else
        var obj = new Dummy.Child(null, a, b, iface_prop);
#endif
        Test.AssertEquals(iface_prop, obj.IfaceProp);

#if EFL_BETA
        obj = new Dummy.Child(parent: null, ifaceProp : iface_prop, doubleParamsA : a, doubleParamsB : b,
                              obligatoryBetaCtor : beta,
                              optionalBetaCtor : -beta);
#else
        obj = new Dummy.Child(parent: null, ifaceProp : iface_prop, doubleParamsA : a, doubleParamsB : b);
#endif
        Test.AssertEquals(iface_prop, obj.IfaceProp);

#if EFL_BETA
        Test.Assert(obj.ObligatoryBetaCtorWasCalled);
        Test.Assert(obj.OptionalBetaCtorWasCalled);
#endif
    }

    public static void test_optional_constructor()
    {
        string a = "LFE";
        double b = 3.14;
#if EFL_BETA
        int beta = 2241;
        var obj = new Dummy.Child(null, a, b, obligatoryBetaCtor : beta);
        Test.Assert(!obj.OptionalBetaCtorWasCalled);
#else
        var obj = new Dummy.Child(null, a, b);
#endif
        Test.Assert(!obj.GetIfaceWasSet());
    }
}

class TestInterfaceConcrete
{
    // For T7619
    public static void test_iface_concrete_methods()
    {
        var obj = new Dummy.TestObject();
        Dummy.ITestIface iface = obj.ReturnIface();

        iface.IfaceProp = 1970;
        Test.AssertEquals(iface.IfaceProp, 1970);
    }
}

class TestProvider
{
    public static void test_find_provider()
    {
        // Tests only the direction C# -> C
        var obj = new Dummy.TestObject();
        Dummy.Numberwrapper provider = obj.FindProvider(typeof(Dummy.Numberwrapper)) as Dummy.Numberwrapper;
        Test.AssertEquals(provider.GetType(), typeof(Dummy.Numberwrapper));
        Test.AssertEquals(provider.GetNumber(), 1999);
    }

    private class ProviderHolder : Dummy.TestObject
    {
        private Dummy.TestObject provider;
        public string ProviderName
        {
            get
            {
                return "MyProvider";
            }
        }

        public ProviderHolder() : base(null)
        {
            this.provider = new Dummy.TestObject(this);
            this.provider.Name = this.ProviderName;
            this.provider.IfaceProp = 1997;
        }

        public override Efl.Object FindProvider(System.Type type)
        {
            Console.WriteLine("Called FindProvider");
            if (type == typeof(Dummy.ITestIface))
            {
                return this.provider;
            }
            else
            {
                return null;
            }
        }
    }

    public static void test_find_provider_iface()
    {
        var obj = new ProviderHolder();

        var provider = obj.CallFindProvider(typeof(Efl.Object));
        Test.AssertNull(provider, msg : "Unkonw provider must be null");

        provider = obj.CallFindProviderForIface();
        Test.AssertNotNull(provider, msg : "Provider of ITestIFace must not be null");
        Test.AssertEquals(provider.Name, obj.ProviderName, "Provider name does not match expected");

    }
}

class TestObjectDeletion
{
    public static void test_object_deletion()
    {
        var obj = new Dummy.PartHolder();
        var part = obj.OnePart;

        Test.AssertNotNull(part);

        part.Del();

        Test.AssertNull(obj.OnePart);
    }
}

class TestProtectedInterfaceMembers
{

    private class MyObject : Dummy.TestObject
    {
        public MyObject(Efl.Object parent = null) : base(parent)
        {
        }

        public override int MethodProtected(int x)
        {
            return x * x;
        }
    }

    public static void test_protected_interface_in_generated_class()
    {
        var obj = new Dummy.TestObject();
        Test.AssertEquals(obj.MethodProtected(42), -42);
    }

    public static void test_protected_interface_in_generated_class_called_from_c()
    {
        var obj = new Dummy.TestObject();
        Test.AssertEquals(obj.CallMethodProtected(42), -42);
    }

    public static void test_protected_interface_in_inherited_class()
    {
        var obj = new MyObject();
        Test.AssertEquals(obj.MethodProtected(42), 42 * 42);
    }

    public static void test_protected_interface_in_inherited_class_called_from_c()
    {
        var obj = new MyObject();
        Test.AssertEquals(obj.CallMethodProtected(42), 42 * 42);
    }
}

}