efl

Форк
0
/
FunctionPointers.cs 
214 строк · 6.6 Кб
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.Runtime.InteropServices;
18

19
namespace TestSuite
20
{
21

22
class TestFunctionPointers
23
{
24

25
    static bool called = false;
26

27
    static int twice(int a)
28
    {
29
        called = true;
30
        return a * 2;
31
    }
32

33
    static int thrice(int a)
34
    {
35
        called = true;
36
        return a * 3;
37
    }
38

39
    static void setup()
40
    {
41
        called = false;
42
    }
43

44
    public static void set_callback_basic()
45
    {
46
        setup();
47
        var obj = new Dummy.TestObject();
48
        obj.SetCallback(twice);
49

50
        Test.Assert(called == false, "set_callback should not call the callback");
51

52
        int x = obj.CallCallback(42);
53

54
        Test.Assert(called, "call_callback must call a callback");
55
        Test.AssertEquals(42 * 2, x);
56
        obj.Dispose();
57
    }
58

59
    public static void set_callback_with_lambda()
60
    {
61
        setup();
62

63
        var obj = new Dummy.TestObject();
64
        obj.SetCallback(y => {
65
                    called = true;
66
                    return y + 4;
67
                });
68

69
        Test.Assert(called == false, "set_callback should not call the callback");
70

71
        int x = obj.CallCallback(37);
72

73
        Test.Assert(called, "call_callback must call a callback");
74
        Test.AssertEquals(37 + 4, x);
75
        obj.Dispose();
76
    }
77

78
    public static void replace_callback()
79
    {
80
        setup();
81

82
        var obj = new Dummy.TestObject();
83
        obj.SetCallback(twice);
84
        Test.Assert(called == false, "set_callback should not call the callback");
85

86
        int x = obj.CallCallback(42);
87
        Test.Assert(called, "call_callback must call a callback");
88
        Test.AssertEquals(42 * 2, x);
89

90
        bool new_called = false;
91
        obj.SetCallback(y => {
92
                    new_called = true;
93
                    return y * y;
94
                });
95

96
        Test.Assert(new_called == false, "set_callback should not call the callback");
97

98
        x = obj.CallCallback(42);
99
        Test.Assert(new_called, "call_callback must call a callback");
100
        Test.AssertEquals(42 * 42, x);
101
        obj.Dispose();
102
    }
103

104
    class NoOverride : Dummy.TestObject {
105
    }
106
    public static void set_callback_inherited_no_override()
107
    {
108
        setup();
109
        NoOverride obj = new NoOverride();
110
        obj.SetCallback(thrice);
111

112
        Test.Assert(!called, "set_callback in virtual should not call the callback");
113

114
        int x = obj.CallCallback(42);
115

116
        Test.Assert(called, "call_callback must call a callback");
117
        Test.AssertEquals(42 * 3, x);
118
        obj.Dispose();
119
    }
120

121
    class WithOverride : Dummy.TestObject {
122
        public bool set_called = false;
123
        public bool invoke_called = false;
124
        public Dummy.SimpleCb cb = null;
125

126
        public WithOverride() : base() {
127

128
        }
129
        public override void SetCallback(Dummy.SimpleCb cb) {
130
            set_called = true;
131
            this.cb = cb;
132
        }
133
        public override int CallCallback(int a) {
134
            invoke_called = true;
135
            if (cb != null)
136
                return cb(a);
137
            Eina.Log.Error("No callback set upon call_callback invocation");
138
            return -1;
139
        }
140
    }
141
    public static void set_callback_inherited_with_override()
142
    {
143
        setup();
144
        WithOverride obj = new WithOverride();
145
        obj.SetCallback(thrice);
146

147
        Test.Assert(obj.set_called, "set_callback override must have been called");
148
        Test.Assert(!obj.invoke_called, "invoke_callback must not have been called");
149

150
        obj.set_called = false;
151
        int x = obj.CallCallback(42);
152

153
        Test.Assert(!obj.set_called, "set_callback override must not have been called");
154
        Test.Assert(obj.invoke_called, "set_callback in virtual should not call the callback");
155

156
        Test.Assert(called, "call_callback must call a callback");
157
        Test.AssertEquals(42 * 3, x);
158
        obj.Dispose();
159
    }
160

161
    // These are needed due to issues calling methods on obj from the GC thread (where the
162
    // free function is actually called)
163
    [System.Runtime.InteropServices.DllImport("efl_mono_native_test")] static extern bool free_called_get();
164
    [System.Runtime.InteropServices.DllImport("efl_mono_native_test")] static extern bool free_called_set(bool val);
165

166
     public static void set_callback_inherited_called_from_c()
167
     {
168
        setup();
169
        WithOverride obj = new WithOverride();
170
        free_called_set(false);
171
        obj.CallSetCallback();
172

173
        Test.Assert(obj.set_called, "set_callback override must have been called");
174
        Test.Assert(!obj.invoke_called, "invoke_callback must not have been called");
175
        Test.Assert(!free_called_get(), "call_set_callback must not call the free callback");
176

177
        obj.set_called = false;
178
        int x = obj.CallCallback(42);
179

180
        Test.Assert(!obj.set_called, "set_callback override must not have been called");
181
        Test.Assert(obj.invoke_called, "set_callback in virtual should not call the callback");
182
        Test.Assert(!free_called_get(), "call_callback must not call the free callback");
183

184
        Test.AssertEquals(42 * 3, x);
185

186
        setup();
187
        obj.set_called = false;
188
        obj.invoke_called = false;
189
        free_called_set(false);
190

191
        // Should release the handle to the wrapper allocated when calling set_callback from C.
192
        obj.SetCallback(twice);
193

194
        Test.CollectAndIterate(300, 10);
195

196
        Test.Assert(obj.set_called, "set_callback override must have been called");
197
        Test.Assert(!obj.invoke_called, "invoke_callback must not have been called");
198
        Test.Assert(free_called_get(), "free callback must have been called");
199

200
        obj.set_called = false;
201
        free_called_set(false);
202
        x = obj.CallCallback(42);
203

204
        Test.Assert(!obj.set_called, "set_callback override must not have been called");
205
        Test.Assert(obj.invoke_called, "set_callback in virtual should not call the callback");
206
        Test.Assert(!free_called_get(), "must not call old free_callback on new callback");
207

208
        Test.Assert(called, "call_callback must call a callback");
209
        Test.AssertEquals(42 * 2, x);
210
        obj.Dispose();
211
    }
212
}
213

214
}
215

216

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

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

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

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