Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / test / ElmSharp.Wearable.Test / TestRunner.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.IO;
19 using System.Linq;
20 using System.Reflection;
21 using System.Collections.Generic;
22
23 using Tizen.Applications;
24
25 namespace ElmSharp.Test
26 {
27     public class TestRunner : CoreUIApplication
28     {
29         internal Window _firstPageWindow;
30         private static bool s_terminated;
31
32         public static string ResourceDir { get; private set; }
33
34         public TestRunner()
35         {
36             s_terminated = false;
37         }
38
39         protected override void OnCreate()
40         {
41             ResourceDir = DirectoryInfo.Resource;
42
43             var testCases = GetTestCases();
44             CreateFirstPage(testCases);
45             base.OnCreate();
46         }
47
48         protected override void OnTerminate()
49         {
50             s_terminated = true;
51             base.OnTerminate();
52         }
53
54         public void RunStandalone(string[] args)
55         {
56             ResourceDir = Path.Combine(Path.GetDirectoryName(typeof(TestRunner).GetTypeInfo().Assembly.Location), "res");
57
58             EcoreSynchronizationContext.Initialize();
59
60             var testCases = GetTestCases();
61             TestCaseBase theTest = null;
62
63             if (args.Count() > 0)
64             {
65                 theTest = testCases.Where((testCase) => testCase.TestName == args[0] || testCase.GetType().ToString() == args[0]).FirstOrDefault();
66             }
67
68             if (theTest != null)
69             {
70                 StartTC(theTest);
71                 EcoreMainloop.Begin();
72             }
73             else
74             {
75                 CreateFirstPage(testCases);
76                 EcoreMainloop.Begin();
77             }
78
79             Elementary.Shutdown();
80         }
81
82         private IEnumerable<TestCaseBase> GetTestCases()
83         {
84             Assembly asm = typeof(TestRunner).GetTypeInfo().Assembly;
85             Type testCaseType = typeof(TestCaseBase);
86
87             var tests = from test in asm.GetTypes()
88                         where testCaseType.IsAssignableFrom(test) && !test.GetTypeInfo().IsInterface && !test.GetTypeInfo().IsAbstract
89                         select Activator.CreateInstance(test) as TestCaseBase;
90
91             return from test in tests
92                    orderby test.TestName
93                    select test;
94         }
95
96         internal static void UIExit()
97         {
98             EcoreMainloop.Quit();
99         }
100
101         private Window CreateWindow(bool isSecond = false)
102         {
103             Window window = new Window("ElmSharp UI Tests")
104             {
105                 AvailableRotations = DisplayRotation.Degree_0 | DisplayRotation.Degree_180 | DisplayRotation.Degree_270 | DisplayRotation.Degree_90
106             };
107             window.Show();
108             if (isSecond)
109             {
110                 window.BackButtonPressed += (s, e) =>
111                 {
112                     window.Hide();
113                     window.Unrealize();
114                     GC.Collect();
115                     GC.WaitForPendingFinalizers();
116                 };
117             }
118             else
119             {
120                 window.BackButtonPressed += (s, e) =>
121                 {
122                     UIExit();
123                 };
124             }
125             return window;
126         }
127
128         private void CreateFirstPage(IEnumerable<TestCaseBase> testCases)
129         {
130             _firstPageWindow = CreateWindow();
131             Console.WriteLine("Screen DPI : {0}", _firstPageWindow.ScreenDpi.X);
132             Conformant conformant = new Conformant(_firstPageWindow);
133             conformant.Show();
134             Box box = new Box(_firstPageWindow)
135             {
136                 AlignmentX = -1,
137                 AlignmentY = -1,
138                 WeightX = 1,
139                 WeightY = 1,
140             };
141             box.Show();
142             var bg = new Background(_firstPageWindow);
143             bg.Color = Color.White;
144             bg.SetContent(box);
145             conformant.SetContent(bg);
146
147             GenList list = new GenList(_firstPageWindow)
148             {
149                 Homogeneous = true,
150                 AlignmentX = -1,
151                 AlignmentY = -1,
152                 WeightX = 1,
153                 WeightY = 1
154             };
155
156             GenItemClass defaultClass = new GenItemClass("default")
157             {
158                 GetTextHandler = (data, part) =>
159                 {
160                     return string.Format("{0}",(string)data);
161                 }
162             };
163
164             list.Append(defaultClass, "");
165             foreach (var tc in testCases)
166             {
167                 list.Append(defaultClass, tc.TestName);
168             }
169             list.Append(defaultClass, "");
170
171             list.ItemSelected += (s, e) =>
172             {
173                 foreach (var tc in testCases)
174                 {
175                     if (tc.TestName == (string)(e.Item.Data))
176                     {
177                         StartTCFromList(tc);
178                         break;
179                     }
180                 }
181             };
182             list.Show();
183
184             box.PackEnd(list);
185         }
186
187         private void StartTC(TestCaseBase tc)
188         {
189             Window window = CreateWindow();
190             tc.Run(window);
191         }
192
193         private void StartTCFromList(TestCaseBase tc)
194         {
195             Window window = CreateWindow(true);
196             tc.Run(window);
197         }
198
199         static void Main(string[] args)
200         {
201             Elementary.Initialize();
202             Elementary.ThemeOverlay();
203
204             Console.WriteLine("ELM_PROFILE : {0}", Elementary.GetProfile());
205             Console.WriteLine("ELM_SCALE : {0}", Elementary.GetScale());
206
207             TestRunner testRunner = new TestRunner();
208             testRunner.Run(args);
209
210             // if running with appfw is failed, below line will be executed.
211             if (!s_terminated)
212             {
213                 testRunner.RunStandalone(args);
214             }
215         }
216     }
217 }