[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / SubWindowTest.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
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  */
17
18 using Tizen.NUI.BaseComponents;
19
20 namespace Tizen.NUI.Samples
21 {
22     using log = Tizen.Log;
23     public class SubWindowTest : IExample
24     {
25         private const string tag = "NUITEST";
26         private const string KEY_BACK = "XF86Back";
27         private const string KEY_ESCAPE = "Escape";
28         private const string KEY_NUM_1 = "1";
29         private const string KEY_NUM_2 = "2";
30         private const string KEY_NUM_3 = "3";
31         private const string KEY_NUM_4 = "4";
32         private const string KEY_NUM_5 = "5";
33         private const string KEY_PARENT_ABOVE = "6";
34         private const string KEY_PARENT_BELOW = "7";
35         private Window mainWin;
36         private Window subWinFirst;
37         private Window subWinSecond;
38         private Window subWinThird;
39         private Timer disposeTimer;
40         private bool belowParent;
41         private const int oneSecond = 1000;
42         private const int testSize = 100;
43         private const int testPosition = 100;
44         private const float testPointSize = 12.0f;
45
46         public void Activate()
47         {
48             Initialize();
49         }
50
51         public void Deactivate()
52         {
53         }
54
55         private void Initialize()
56         {
57             mainWin = NUIApplication.GetDefaultWindow();
58             mainWin.KeyEvent += OnKeyEvent;
59             mainWin.BackgroundColor = Color.Cyan;
60             mainWin.WindowSize = new Size2D(5 * testSize, 5 * testSize);
61             mainWin.TouchEvent += WinTouchEvent;
62             belowParent = false;
63
64             TextLabel testText = new TextLabel("Hello Tizen NUI World")
65             {
66                 HorizontalAlignment = HorizontalAlignment.Center,
67                 VerticalAlignment = VerticalAlignment.Center,
68                 TextColor = Color.Blue,
69                 PointSize = testPointSize,
70                 HeightResizePolicy = ResizePolicyType.FillToParent,
71                 WidthResizePolicy = ResizePolicyType.FillToParent
72             };
73             mainWin.Add(testText);
74
75             Animation testRotationAnim = new Animation(2 * oneSecond);
76             testRotationAnim.AnimateTo(testText, "Orientation", new Rotation(new Radian(new Degree(180.0f)), PositionAxis.X), 0, oneSecond);
77             testRotationAnim.AnimateTo(testText, "Orientation", new Rotation(new Radian(new Degree(0.0f)), PositionAxis.X), oneSecond, 2 * oneSecond);
78             testRotationAnim.Looping = true;
79             testRotationAnim.Play();
80
81             log.Debug(tag, "animation play!");
82         }
83
84         private void CreateSubWinThird()
85         {
86             if (subWinThird)
87             {
88                 log.Debug(tag, $"subWinThird is already created");
89                 return;
90             }
91             subWinThird = new Window("subWinThird", new Rectangle(0, 0, 3 * testSize, 3 * testSize), false)
92             {
93                 BackgroundColor = Color.Blue
94             };
95             View childDummyView = new View()
96             {
97                 Size = new Size(testSize, testSize),
98                 Position = new Position(testPosition, testPosition),
99                 BackgroundColor = Color.Yellow,
100             };
101             subWinThird.Add(childDummyView);
102             subWinThird.KeyEvent += SubWinThirdKeyEvent;
103         }
104
105         private void SetParentAbove()
106         {
107             CreateSubWinThird();
108             subWinThird.SetParent(mainWin, false);
109         }
110
111         private void SetParentBelow()
112         {
113             CreateSubWinThird();
114             subWinThird.SetParent(mainWin, true);
115         }
116
117         private void SubWinThirdKeyEvent(object sender, Window.KeyEventArgs e)
118         {
119             if (e.Key.State == Key.StateType.Down)
120             {
121                 log.Debug(tag, $"key down! key={e.Key.KeyPressedName}");
122
123                 switch (e.Key.KeyPressedName)
124                 {
125                     case KEY_PARENT_ABOVE:
126                         SetParentAbove();
127                         break;
128                     case KEY_PARENT_BELOW:
129                         SetParentBelow();
130                         break;
131                 }
132             }
133         }
134
135         private void WinTouchEvent(object sender, Window.TouchEventArgs e)
136         {
137             if (e.Touch.GetState(0) == PointStateType.Up)
138             {
139                 if (belowParent == false)
140                 {
141                     SetParentBelow();
142                     belowParent = true;
143                 }
144                 else
145                 {
146                     SetParentAbove();
147                     belowParent = false;
148                 }
149             }
150         }
151
152         private void OnKeyEvent(object sender, Window.KeyEventArgs e)
153         {
154             if (e.Key.State == Key.StateType.Down)
155             {
156                 log.Debug(tag, $"key down! key={e.Key.KeyPressedName}");
157
158                 switch (e.Key.KeyPressedName)
159                 {
160                     case KEY_BACK:
161                     case KEY_ESCAPE:
162                         break;
163
164                     case KEY_NUM_1:
165                         TestMakeSubWindowAndAddSomeDummyObject();
166                         break;
167
168                     case KEY_NUM_2:
169                         TestDisposeSubWindowFirstWhichWasCreatedInPrivousTestCase();
170                         break;
171
172                     case KEY_NUM_3:
173                         TestCreateSubWindowSecondWhichDoesNotHaveAnySettingAndDisposeItAfterThreeSecondsDelay();
174                         break;
175
176                     case KEY_NUM_4:
177                         TestCreateSubWindowSecondWhichHasSomeSettingAndDisposeItAfterThreeSecondsDelay();
178                         break;
179
180                     case KEY_NUM_5:
181                         TestCreateSubWindowSecondWhichHasSomeSettingAndAddSomeDummyObjectAndDisposeItAfterThreeSecondsDelay();
182                         break;
183
184                     case KEY_PARENT_ABOVE:
185                         SetParentAbove();
186                         break;
187
188                     case KEY_PARENT_BELOW:
189                         SetParentBelow();
190                         break;
191
192                     default:
193                         log.Debug(tag, $"no test!");
194                         break;
195                 }
196             }
197         }
198
199         //TDD
200         private void TestMakeSubWindowAndAddSomeDummyObject()
201         {
202             subWinFirst = new Window("subWinFirst", new Rectangle(5 * testPosition, 5 * testPosition, 3 * testSize, 3 * testSize), false)
203             {
204                 BackgroundColor = Color.Blue
205             };
206             View childView = new View()
207             {
208                 Size = new Size(testSize, testSize),
209                 Position = new Position(testPosition, testPosition),
210                 BackgroundColor = Color.Yellow,
211             };
212             subWinFirst.Add(childView);
213             subWinFirst.KeyEvent += SubWinFirstKeyEvent;
214         }
215
216         private void TestDisposeSubWindowFirstWhichWasCreatedInPrivousTestCase()
217         {
218             subWinFirst?.Dispose();
219         }
220
221         private void TestCreateSubWindowSecondWhichDoesNotHaveAnySettingAndDisposeItAfterThreeSecondsDelay()
222         {
223             subWinSecond = null;
224             subWinSecond = new Window();
225             disposeTimer = new Timer(3 * oneSecond);
226             disposeTimer.Tick += OnDisposeTimerTick;
227             disposeTimer.Start();
228         }
229
230         private void TestCreateSubWindowSecondWhichHasSomeSettingAndDisposeItAfterThreeSecondsDelay()
231         {
232             subWinSecond = null;
233             subWinSecond = new Window("subWinSecond", new Rectangle(testPosition, testPosition, testSize, testSize), false)
234             {
235                 BackgroundColor = Color.Red
236             };
237             disposeTimer = new Timer(3 * oneSecond);
238             disposeTimer.Tick += OnDisposeTimerTick;
239             disposeTimer.Start();
240         }
241
242         private void TestCreateSubWindowSecondWhichHasSomeSettingAndAddSomeDummyObjectAndDisposeItAfterThreeSecondsDelay()
243         {
244             subWinSecond = null;
245             subWinSecond = new Window("subWinSecond", new Rectangle(5 * testPosition, 5 * testPosition, 3 * testSize, 3 * testSize), false)
246             {
247                 BackgroundColor = Color.Red
248             };
249             View testView = new View()
250             {
251                 Size = new Size(testSize, testSize),
252                 Position = new Position(testPosition, testPosition),
253                 BackgroundColor = Color.Yellow,
254             };
255             subWinSecond.Add(testView);
256
257             disposeTimer = new Timer(3 * oneSecond);
258             disposeTimer.Tick += OnDisposeTimerTick;
259             disposeTimer.Start();
260         }
261
262         private bool OnDisposeTimerTick(object source, Timer.TickEventArgs e)
263         {
264             log.Debug(tag, $"after 3000ms, subWinSecond dispose!");
265             subWinSecond?.Dispose();
266             return false;
267         }
268
269         private void SubWinFirstKeyEvent(object sender, Window.KeyEventArgs e)
270         {
271             if (e.Key.State == Key.StateType.Down)
272             {
273                 log.Debug(tag, $"subWinFirst key down! key={e.Key.KeyPressedName}");
274
275                 switch (e.Key.KeyPressedName)
276                 {
277                     case KEY_BACK:
278                     case KEY_ESCAPE:
279                         break;
280                     case KEY_NUM_1:
281                         mainWin.Raise();
282                         break;
283                     default:
284                         log.Debug(tag, $"default!");
285                         break;
286                 }
287             }
288         }
289     }
290 }