Merge "Add ThemeOverlay() API in Elementary." into devel/dotnet
[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                     }
110                 };
111             }
112             else
113             {
114                 window.KeyGrab(EvasKeyEventArgs.PlatformBackButtonName, false);
115                 window.KeyUp += (s, e) =>
116                 {
117                     if (e.KeyName == EvasKeyEventArgs.PlatformBackButtonName)
118                     {
119                         UIExit();
120                     }
121                 };
122             }
123             return window;
124         }
125
126         private void CreateFirstPage(IEnumerable<TestCaseBase> testCases)
127         {
128             _firstPageWindow = CreateWindow();
129             Conformant conformant = new Conformant(_firstPageWindow);
130             conformant.Show();
131             Box box = new Box(_firstPageWindow)
132             {
133                 AlignmentX = -1,
134                 AlignmentY = -1,
135                 WeightX = 1,
136                 WeightY = 1,
137             };
138             box.Show();
139             conformant.SetContent(box);
140
141             GenList list = new GenList(_firstPageWindow)
142             {
143                 Homogeneous = true,
144                 AlignmentX = -1,
145                 AlignmentY = -1,
146                 WeightX = 1,
147                 WeightY = 1
148             };
149
150             GenItemClass defaultClass = new GenItemClass("default")
151             {
152                 GetTextHandler = (data, part) =>
153                 {
154                     return string.Format("{0}",(string)data);
155                 }
156             };
157
158             foreach (var tc in testCases)
159             {
160                 list.Append(defaultClass, tc.TestName);
161             }
162
163             list.ItemSelected += (s, e) =>
164             {
165                 foreach (var tc in testCases)
166                 {
167                     if (tc.TestName == (string)(e.Item.Data))
168                     {
169                         StartTCFromList(tc);
170                         break;
171                     }
172                 }
173             };
174             list.Show();
175
176             box.PackEnd(list);
177         }
178
179         private void StartTC(TestCaseBase tc)
180         {
181             Window window = CreateWindow();
182             tc.Run(window);
183         }
184
185         private void StartTCFromList(TestCaseBase tc)
186         {
187             Window window = CreateWindow(true);
188             tc.Run(window);
189         }
190
191         static void Main(string[] args)
192         {
193             TestRunner testRunner = new TestRunner();
194             testRunner.Run(args);
195
196             // if running with appfw is failed, below line will be executed.
197             testRunner.RunStandalone(args);
198         }
199     }
200 }