[NUI] Clean up DisconnectFromSignals(). and Add Exception when Animation is used...
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / DisposeWithoutEventUnsubscribedTest.cs
1
2 using global::System;
3 using Tizen.NUI.BaseComponents;
4 using System.Collections.Generic;
5
6 namespace Tizen.NUI.Samples
7 {
8     using tlog = Tizen.Log;
9     public class DisposeWithoutEventUnsubscribedTestAndAnimationDisposeTest : IExample
10     {
11         private const string tag = "NUITEST";
12         private const int NUMBER_OF_VIEW = 1000;
13         private const int INTERVAL_MS = 1000;
14         private const int MIN_POSITION = 100;
15         private const int MAX_POSITION = 900;
16         private const bool TURN_ON_GC = true;
17
18         private Window win;
19         private View rootView;
20         private int cnt;
21         private List<View> list = new List<View>();
22         private Random rand = new Random();
23         private bool toggle, toggleRemoveMany;
24         private string res;
25         private Timer timer;
26
27         public void Activate()
28         {
29             DisposeWithoutUnsubscribedEventTest();
30             AnimationDisposeTest();
31         }
32         public void Deactivate()
33         {
34             DisposeManyObject();
35             timer.Stop();
36             timer.Dispose();
37             rootView.Unparent();
38             rootView.Dispose();
39         }
40
41         private void AnimationDisposeTest()
42         {
43             var ani = new Animation(1000);
44             ani.AnimateTo(rootView, "size", new Size(500, 500, 0));
45             ani.Dispose();
46             try
47             {
48                 ani.Play();
49                 tlog.Fatal(tag, "this should not be shown! test NG!");
50             }
51             catch (Exception e)
52             {
53                 if (e is ObjectDisposedException)
54                 {
55                     tlog.Fatal(tag, "Animation is used after disposed! test OK!");
56                 }
57                 else
58                 {
59                     tlog.Fatal(tag, $"unwanted exception came! test NG! exception:{e} msg:{e.Message}");
60                 }
61             }
62
63             try
64             {
65                 ani.Clear();
66                 tlog.Fatal(tag, "this should not be shown! test NG!");
67             }
68             catch (Exception e)
69             {
70                 if (e is ObjectDisposedException)
71                 {
72                     tlog.Fatal(tag, "Animation is used after disposed! test OK!");
73                 }
74                 else
75                 {
76                     tlog.Fatal(tag, $"unwanted exception came! test NG! exception:{e} msg:{e.Message}");
77                 }
78             }
79         }
80
81         private void DisposeWithoutUnsubscribedEventTest()
82         {
83             win = NUIApplication.GetDefaultWindow();
84
85             res = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
86
87             rootView = new View()
88             {
89                 Size = new Size(100, 100),
90                 BackgroundColor = Color.Blue,
91             };
92             rootView.Relayout += View_Relayout;
93             win.Add(rootView);
94
95             timer = new Timer(INTERVAL_MS);
96             timer.Tick += Timer_Tick;
97             timer.Start();
98         }
99
100         private void AddManyObject()
101         {
102             tlog.Fatal(tag, $"AddManyObject()");
103
104             for (int i = 0; i < NUMBER_OF_VIEW; i++)
105             {
106                 var child = new ImageView()
107                 {
108                     ResourceUrl = res + "images/Dali/DaliDemo/application-icon-1.png",
109                     Size = new Size(60, 60),
110                     Position = new Position(rand.Next(MIN_POSITION, MAX_POSITION), rand.Next(MIN_POSITION, MAX_POSITION)),
111                     BackgroundColor = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 1),
112                 };
113
114                 rootView.Add(child);
115                 child.Relayout += Child_Relayout;
116                 child.RemovedFromWindow += Child_RemovedFromWindow;
117                 child.AddedToWindow += Child_AddedToWindow;
118                 child.WheelEvent += Child_WheelEvent;
119                 child.HoverEvent += Child_HoverEvent;
120                 child.InterceptTouchEvent += Child_InterceptTouchEvent;
121                 child.TouchEvent += Child_TouchEvent;
122                 child.ResourcesLoaded += Child_ResourcesLoaded;
123                 child.KeyEvent += Child_KeyEvent;
124                 child.FocusLost += Child_FocusLost;
125                 child.FocusGained += Child_FocusGained;
126             }
127         }
128
129         private void Child_FocusGained(object sender, EventArgs e)
130         {
131             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
132             {
133                 tlog.Fatal(tag, $"Child_FocusGained() called!");
134             }
135         }
136
137         private void Child_FocusLost(object sender, EventArgs e)
138         {
139             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
140             {
141                 tlog.Fatal(tag, $"Child_FocusLost() called!");
142             }
143         }
144
145         private bool Child_KeyEvent(object source, View.KeyEventArgs e)
146         {
147             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
148             {
149                 tlog.Fatal(tag, $"Child_KeyEvent() called!");
150             }
151             return true;
152         }
153
154         private void Child_ResourcesLoaded(object sender, EventArgs e)
155         {
156             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
157             {
158                 tlog.Fatal(tag, $"Child_ResourcesLoaded() called!");
159             }
160         }
161
162         private bool Child_TouchEvent(object source, View.TouchEventArgs e)
163         {
164             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
165             {
166                 tlog.Fatal(tag, $"Child_TouchEvent() called!");
167             }
168             return true;
169         }
170
171         private bool Child_InterceptTouchEvent(object source, View.TouchEventArgs e)
172         {
173             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
174             {
175                 tlog.Fatal(tag, $"Child_InterceptTouchEvent() called!");
176             }
177             return true;
178         }
179
180         private bool Child_HoverEvent(object source, View.HoverEventArgs e)
181         {
182             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
183             {
184                 tlog.Fatal(tag, $"Child_HoverEvent() called!");
185             }
186             return true;
187         }
188
189         private bool Child_WheelEvent(object source, View.WheelEventArgs e)
190         {
191             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
192             {
193                 tlog.Fatal(tag, $"Child_WheelEvent() called!");
194             }
195             return true;
196         }
197
198         private void Child_AddedToWindow(object sender, EventArgs e)
199         {
200             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
201             {
202                 tlog.Fatal(tag, $"Child_AddedToWindow() called!");
203             }
204         }
205
206         private void Child_RemovedFromWindow(object sender, EventArgs e)
207         {
208             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
209             {
210                 tlog.Fatal(tag, $"Child_RemovedFromWindow() called!");
211             }
212         }
213
214         private void Child_Relayout(object sender, EventArgs e)
215         {
216             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
217             {
218                 tlog.Fatal(tag, $"Child_Relayout() called!");
219             }
220         }
221
222         private void View_Relayout(object sender, EventArgs e)
223         {
224             if (++cnt % (NUMBER_OF_VIEW * 3) == 1)
225             {
226                 tlog.Fatal(tag, $"View_Relayout() called! cnt:{cnt}");
227             }
228         }
229
230         private void RemoveManyObject()
231         {
232             int childCnt = (int)rootView.ChildCount;
233             tlog.Fatal(tag, $"RemoveManyObject() child count:{childCnt}");
234
235             for (int i = childCnt - 1; i >= 0; i--)
236             {
237                 var child = rootView.GetChildAt((uint)i);
238                 rootView.Remove(child);
239             }
240         }
241         private void DisposeManyObject()
242         {
243             int childCnt = (int)rootView.ChildCount;
244             tlog.Fatal(tag, $"DisposeManyObject() child count:{childCnt}");
245
246             for (int i = childCnt - 1; i >= 0; i--)
247             {
248                 var child = rootView.GetChildAt((uint)i);
249                 rootView.Remove(child);
250                 child.Size += new Size(100, 100);
251                 child.Size -= new Size(100, 100);
252                 child.Size += new Size(100, 100);
253                 child.Size -= new Size(100, 100);
254                 child.Dispose();
255             }
256         }
257
258         private bool Timer_Tick(object source, Timer.TickEventArgs e)
259         {
260             toggle = !toggle;
261             if (toggle)
262             {
263                 AddManyObject();
264             }
265             else
266             {
267                 toggleRemoveMany = !toggleRemoveMany;
268                 if (toggleRemoveMany)
269                 {
270                     RemoveManyObject();
271                 }
272                 else
273                 {
274                     DisposeManyObject();
275                 }
276             }
277
278             if (TURN_ON_GC)
279             {
280                 FullGC();
281             }
282             return true;
283         }
284
285         private void FullGC()
286         {
287             global::System.GC.Collect();
288             global::System.GC.WaitForPendingFinalizers();
289             global::System.GC.Collect();
290         }
291
292     }
293 }