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