[NUI] Change for AddFrameRenderedCallback to maintain the reference of user callback...
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / AddFrameRenderedCallbackTest.cs
1 
2 using global::System;
3 using Tizen.NUI;
4 using Tizen.NUI.BaseComponents;
5 using NUnit.Framework;
6 using System.Threading.Tasks;
7
8 namespace Tizen.NUI.Samples
9 {
10     using tlog = Tizen.Log;
11     public class AddFrameRenderedCallbackTest : IExample
12     {
13         const string tag = "NUITEST";
14
15         public void Activate()
16         {
17             test();
18         }
19         public void Deactivate()
20         {
21         }
22
23         Window win;
24         Window.FrameCallbackType cb;
25         TextLabel tl;
26         View view;
27         void test()
28         {
29             tlog.Fatal(tag, $"test start");
30             win = NUIApplication.GetDefaultWindow();
31             win.TouchEvent += WinTouchEvent;
32             view = new View();
33             view.Size = new Size(100, 100);
34             view.BackgroundColor = Color.Blue;
35             view.Focusable = true;
36             view.KeyEvent += View_KeyEvent;
37             win.Add(view);
38
39             cb = testCallback;
40             win.AddFrameRenderedCallback(cb, 0);
41             win.AddFramePresentedCallback(cb, 0);
42
43             Timer timer = new Timer(5000);
44             timer.Tick += testOnTick;
45             timer.Start();
46
47             tl = new TextLabel("frameId");
48             tl.Size = new Size(500, 200);
49             tl.Position = new Position(10, 200);
50             tl.BackgroundColor = Color.White;
51             win.Add(tl);
52
53             FocusManager.Instance.SetCurrentFocusView(view);
54         }
55
56         private void WinTouchEvent(object sender, Window.TouchEventArgs e)
57         {
58             win.AddFrameRenderedCallback(cb, cnt++);
59             Console.WriteLine($"testOnTick() AddFrameRenderedCallback() send id={cnt}");
60             win.AddFramePresentedCallback(cb, cnt++);
61             Console.WriteLine($"testOnTick() AddFramePresentedCallback() send id={cnt}");
62         }
63
64         int cnt;
65         bool testOnTick(object o, Timer.TickEventArgs e)
66         {
67             win.AddFrameRenderedCallback(cb, cnt++);
68             Console.WriteLine($"testOnTick() AddFrameRenderedCallback() send id={cnt}");
69             win.AddFramePresentedCallback(cb, cnt++);
70             Console.WriteLine($"testOnTick() AddFramePresentedCallback() send id={cnt}");
71             return true;
72         }
73
74         void testCallback(int id)
75         {
76             tlog.Fatal(tag, $"testCallback() id={id}");
77             tl.Text = $"frameId={id}";
78         }
79
80         private bool View_KeyEvent(object source, View.KeyEventArgs e)
81         {
82             if (e.Key.State == Key.StateType.Down)
83             {
84                 if (e.Key.KeyPressedName == "1")
85                 {
86                     cb = testCallback;
87                     win.AddFrameRenderedCallback(cb, cnt++);
88                     tlog.Fatal(tag, $"1) testOnTick() AddFrameRenderedCallback() send id={cnt}");
89                 }
90                 else if (e.Key.KeyPressedName == "2")
91                 {
92                     cb = testCallback;
93                     win.AddFramePresentedCallback(cb, cnt++);
94                     tlog.Fatal(tag, $"2) testOnTick() AddFramePresentedCallback() send id={cnt}");
95                 }
96                 else if (e.Key.KeyPressedName == "3")
97                 {
98                     cb = testCallback;
99                     win.AddFramePresentedCallback(cb, cnt++);
100                     cb = null;
101                     tlog.Fatal(tag, $"3) testOnTick() AddFramePresentedCallback() send id={cnt}");
102                 }
103                 else if (e.Key.KeyPressedName == "4")
104                 {
105                     win.AddFrameRenderedCallback((int id) =>
106                     {
107                         tlog.Fatal(tag, $"testCallback() id={id}");
108                         tl.Text = $"frameId={id}";
109                     }, cnt++);
110                     tlog.Fatal(tag, $"4) testOnTick() AddFrameRenderedCallback() send id={cnt}");
111                 }
112                 else if (e.Key.KeyPressedName == "Return")
113                 {
114                     Random rand = new Random();
115                     view.BackgroundColor = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 1);
116                 }
117             }
118             return true;
119         }
120
121
122         [Test]
123         public async Task Test_AddFrameRenderedCallback()
124         {
125             var isCalled = false;
126             Tizen.NUI.Window.FrameCallbackType callback = (int id) =>
127             {
128                 isCalled = true;
129             };
130
131             win.AddFrameRenderedCallback(callback, 0);
132
133             await Task.Delay(500);
134
135             Assert.IsTrue(isCalled, "isCalled should be true");
136         }
137         [Test]
138         public async Task Test_AddFramePresentedCallback()
139         {
140             var isCalled = false;
141             Tizen.NUI.Window.FrameCallbackType callback = (int id) =>
142             {
143                 isCalled = true;
144             };
145
146             win.AddFramePresentedCallback(callback, 0);
147
148             await Task.Delay(500);
149
150             Assert.IsTrue(isCalled, "isCalled should be true");
151         }
152
153         [Test]
154         public void Test_AddFrameRenderedCallbackNegative()
155         {
156             try
157             {
158                 win.AddFrameRenderedCallback(null, 0);
159             }
160             catch (Exception ex)
161             {
162                 if (ex is ArgumentNullException)
163                 {
164                     Assert.Pass();
165                 }
166                 else
167                 {
168                     Assert.Fail("ArgumentNullException should occur");
169                 }
170             }
171         }
172
173         [Test]
174         public void Test_AddFramePresentedCallbackNegative()
175         {
176             try
177             {
178                 win.AddFramePresentedCallback(null, 0);
179             }
180             catch (Exception ex)
181             {
182                 if (ex is ArgumentNullException)
183                 {
184                     Assert.Pass();
185                 }
186                 else
187                 {
188                     Assert.Fail("ArgumentNullException should occur");
189                 }
190             }
191         }
192         [Test]
193         public async Task Test_AddFrameRenderedCallbackId()
194         {
195             var checkId = -1;
196             Tizen.NUI.Window.FrameCallbackType callback = (int id) =>
197             {
198                 checkId = id;
199             };
200
201             var testId = 9;
202             win.AddFrameRenderedCallback(callback, testId);
203
204             await Task.Delay(500);
205
206             Assert.AreEqual(testId, checkId, "should be same");
207         }
208
209         [Test]
210         public async Task Test_AddFramePresentedCallbackId()
211         {
212             var checkId = -1;
213             Tizen.NUI.Window.FrameCallbackType callback = (int id) =>
214             {
215                 checkId = id;
216             };
217
218             var testId = 7;
219             win.AddFramePresentedCallback(callback, testId);
220
221             await Task.Delay(500);
222
223             Assert.AreEqual(testId, checkId, "should be same");
224         }
225     }
226 }