Add ScreenDpi API in Window class
[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         private Window _firstPageWindow;
30
31         public static string ResourceDir { get; private set; }
32
33         public TestRunner()
34         {
35         }
36
37         protected override void OnCreate()
38         {
39             ResourceDir = DirectoryInfo.Resource;
40
41             var testCases = GetTestCases();
42             CreateFirstPage(testCases);
43             base.OnCreate();
44         }
45
46         public void RunStandalone(string[] args)
47         {
48             ResourceDir = Path.Combine(Path.GetDirectoryName(typeof(TestRunner).GetTypeInfo().Assembly.Location), "res");
49
50             Elementary.Initialize();
51             Elementary.ThemeOverlay();
52
53             EcoreSynchronizationContext.Initialize();
54
55             var testCases = GetTestCases();
56             TestCaseBase theTest = null;
57
58             if (args.Count() > 0)
59             {
60                 theTest = testCases.Where((testCase) => testCase.TestName == args[0] || testCase.GetType().ToString() == args[0]).FirstOrDefault();
61             }
62
63             if (theTest != null)
64             {
65                 StartTC(theTest);
66                 EcoreMainloop.Begin();
67             }
68             else
69             {
70                 CreateFirstPage(testCases);
71                 EcoreMainloop.Begin();
72             }
73
74             Elementary.Shutdown();
75         }
76
77         private IEnumerable<TestCaseBase> GetTestCases()
78         {
79             Assembly asm = typeof(TestRunner).GetTypeInfo().Assembly;
80             Type testCaseType = typeof(TestCaseBase);
81
82             var tests = from test in asm.GetTypes()
83                         where testCaseType.IsAssignableFrom(test) && !test.GetTypeInfo().IsInterface && !test.GetTypeInfo().IsAbstract
84                         select Activator.CreateInstance(test) as TestCaseBase;
85
86             return from test in tests
87                    orderby test.TestName
88                    select test;
89         }
90
91         internal static void UIExit()
92         {
93             EcoreMainloop.Quit();
94         }
95
96         private Window CreateWindow(bool isSecond = false)
97         {
98             Window window = new Window("ElmSharp UI Tests");
99             window.Show();
100             if (isSecond)
101             {
102                 window.KeyGrab(EvasKeyEventArgs.PlatformBackButtonName, true);
103                 window.KeyUp += (s, e) =>
104                 {
105                     if (e.KeyName == EvasKeyEventArgs.PlatformBackButtonName)
106                     {
107                         window.Hide();
108                         window.Unrealize();
109                         GC.Collect();
110                         GC.WaitForPendingFinalizers();
111                     }
112                 };
113             }
114             else
115             {
116                 window.KeyGrab(EvasKeyEventArgs.PlatformBackButtonName, false);
117                 window.KeyUp += (s, e) =>
118                 {
119                     if (e.KeyName == EvasKeyEventArgs.PlatformBackButtonName)
120                     {
121                         UIExit();
122                     }
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             conformant.SetContent(box);
143
144             GenList list = new GenList(_firstPageWindow)
145             {
146                 Homogeneous = true,
147                 AlignmentX = -1,
148                 AlignmentY = -1,
149                 WeightX = 1,
150                 WeightY = 1
151             };
152
153             GenItemClass defaultClass = new GenItemClass("default")
154             {
155                 GetTextHandler = (data, part) =>
156                 {
157                     return string.Format("{0}",(string)data);
158                 }
159             };
160
161             foreach (var tc in testCases)
162             {
163                 list.Append(defaultClass, tc.TestName);
164             }
165
166             list.ItemSelected += (s, e) =>
167             {
168                 foreach (var tc in testCases)
169                 {
170                     if (tc.TestName == (string)(e.Item.Data))
171                     {
172                         StartTCFromList(tc);
173                         break;
174                     }
175                 }
176             };
177             list.Show();
178
179             box.PackEnd(list);
180         }
181
182         private void StartTC(TestCaseBase tc)
183         {
184             Window window = CreateWindow();
185             tc.Run(window);
186         }
187
188         private void StartTCFromList(TestCaseBase tc)
189         {
190             Window window = CreateWindow(true);
191             tc.Run(window);
192         }
193
194         static void Main(string[] args)
195         {
196             TestRunner testRunner = new TestRunner();
197             testRunner.Run(args);
198
199             // if running with appfw is failed, below line will be executed.
200             testRunner.RunStandalone(args);
201         }
202     }
203 }