summaryrefslogtreecommitdiff
path: root/src/tests/efl_mono/Inheritance.cs
blob: 26f51c591b78b17e1d7c71a41b4dfa14eea8fe25 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

using EinaTestData;
using static EinaTestData.BaseData;

namespace TestSuite
{

class TestInheritance
{
    internal class Inherit1 : Dummy.TestObject
    {
        override public void IntOut (int x, out int y)
        {
            y = 10*x;
        }
    }

    internal class Inherit2 : Dummy.TestObject, Dummy.IInheritIface
    {
        override public void IntOut (int x, out int y)
        {
            Console.WriteLine("IntOut");
            y = 10*x;
        }

        public string StringshareTest (string i)
        {
            Console.WriteLine("StringshareTest");
            return "Hello World";
        }
    }
    
    internal class Inherit3Parent : Dummy.TestObject
    {
        public bool disposed = false;
        public bool childDisposed = false;

        ~Inherit3Parent()
        {
            Console.WriteLine("finalizer called for parent");
        }

        protected override void Dispose(bool disposing)
        {
            Console.WriteLine("Dispose parent");
            base.Dispose(disposing);
        }
    }

    internal class Inherit3Child : Dummy.TestObject
    {
        //Inherit3Parent parent;
        public Inherit3Child(Inherit3Parent parent) : base(parent)
        {
            // WARNING: Uncommenting the line below causes the parent-child cycle to leak.
            // The GC won't be able to collect it.
            // this.parent = parent;
        }

        ~Inherit3Child()
        {
            Console.WriteLine("finalizer called for child");
        }

        protected override void Dispose(bool disposing)
        {
            /* parent.childDisposed = true; */
            Console.WriteLine("Dispose parent");
            base.Dispose(disposing);
        }
    }

    public static void test_inherit_from_regular_class()
    {
        var obj = new Inherit1();
        int i = Dummy.InheritHelper.ReceiveDummyAndCallIntOut(obj);
        Test.AssertEquals (50, i);
    }

    public static void test_inherit_from_iface()
    {
        var obj = new Inherit2();
        int i = Dummy.InheritHelper.ReceiveDummyAndCallIntOut(obj);
        Test.AssertEquals (50, i);
        string s = Dummy.InheritHelper.ReceiveDummyAndCallInStringshare(obj);
        Test.AssertEquals ("Hello World", s);
    }

    private static void CreateAndCheckInheritedObjects(out WeakReference parentWRef, out WeakReference childWRef)
    {
        var parent = new Inherit3Parent();
        var child = new Inherit3Child(parent);

        parentWRef = new WeakReference(parent);
        childWRef = new WeakReference(child);

        Console.WriteLine($"Parent [{parent.ToString()}] has {Efl.Eo.Globals.efl_ref_count(parent.NativeHandle)} refs");
        Console.WriteLine($"Child [{child.ToString()}] has {Efl.Eo.Globals.efl_ref_count(child.NativeHandle)} refs");

        child = null;

        System.GC.Collect(System.GC.MaxGeneration, GCCollectionMode.Forced, true, true);
        System.GC.WaitForPendingFinalizers();
        Efl.App.AppMain.Iterate();

        child = (Inherit3Child) childWRef.Target;

        Test.AssertNotNull(parent);
        Test.AssertNotNull(child);
        Test.AssertEquals(false, parent.disposed);
        Test.AssertEquals(false, parent.childDisposed);

        Console.WriteLine($"Parent [{parent.ToString()}] has {Efl.Eo.Globals.efl_ref_count(parent.NativeHandle)} refs");
        Console.WriteLine($"Child [{child.ToString()}] has {Efl.Eo.Globals.efl_ref_count(child.NativeHandle)} refs");

        parent = null;
        child = null;
    }

    public static void test_inherit_lifetime()
    {
        WeakReference parentWRef;
        WeakReference childWRef;

        CreateAndCheckInheritedObjects(out parentWRef, out childWRef);

        // Two invocations to iterate a the child wasn't being released with a single one
        Test.CollectAndIterate();
        Test.CollectAndIterate();

        var parent = (Dummy.TestObject) parentWRef.Target;
        var child = (Dummy.TestObject) childWRef.Target;

        Test.AssertNull(parent);
        Test.AssertNull(child);
    }
}

}