Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AndroidRunner.cs
Go to the documentation of this file.
1 //
2 // Copyright 2011-2012 Xamarin Inc.
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.Collections.Generic;
19 using System.IO;
20 using System.Net.Sockets;
21 using System.Reflection;
22 using System.Threading.Tasks;
23 using Android.App;
24 using Android.Content;
25 using Android.Widget;
26 using NUnit.Framework.Api;
27 using NUnit.Framework.Internal;
28 using NUnitLite;
29 using NUnit.Framework.Api;
30 using NUnit.Framework.Internal;
32 using NUnit.Framework.Internal.WorkItems;
33 
34 namespace Android.NUnitLite {
35 
36  public class AndroidRunner : ITestListener {
37 
38  private AndroidRunner ()
39  {
40  }
41 
42  public bool AutoStart { get; set; }
43 
44  public bool TerminateAfterExecution { get; set; }
45 
46  #region writer
47 
48  public TextWriter Writer { get; set; }
49 
50  public bool OpenWriter (string message, Context activity)
51  {
52  DateTime now = DateTime.Now;
53  // let the application provide it's own TextWriter to ease automation with AutoStart property
54  if (Writer == null) {
55  Writer = Console.Out;
56  }
57 
58  Writer.WriteLine ("[Runner executing:\t{0}]", message);
59  // FIXME
60  Writer.WriteLine ("[M4A Version:\t{0}]", "???");
61 
62  Writer.WriteLine ("[Board:\t\t{0}]", Android.OS.Build.Board);
63  Writer.WriteLine ("[Bootloader:\t{0}]", Android.OS.Build.Bootloader);
64  Writer.WriteLine ("[Brand:\t\t{0}]", Android.OS.Build.Brand);
65  Writer.WriteLine ("[CpuAbi:\t{0} {1}]", Android.OS.Build.CpuAbi, Android.OS.Build.CpuAbi2);
66  Writer.WriteLine ("[Device:\t{0}]", Android.OS.Build.Device);
67  Writer.WriteLine ("[Display:\t{0}]", Android.OS.Build.Display);
68  Writer.WriteLine ("[Fingerprint:\t{0}]", Android.OS.Build.Fingerprint);
69  Writer.WriteLine ("[Hardware:\t{0}]", Android.OS.Build.Hardware);
70  Writer.WriteLine ("[Host:\t\t{0}]", Android.OS.Build.Host);
71  Writer.WriteLine ("[Id:\t\t{0}]", Android.OS.Build.Id);
72  Writer.WriteLine ("[Manufacturer:\t{0}]", Android.OS.Build.Manufacturer);
73  Writer.WriteLine ("[Model:\t\t{0}]", Android.OS.Build.Model);
74  Writer.WriteLine ("[Product:\t{0}]", Android.OS.Build.Product);
75  Writer.WriteLine ("[Radio:\t\t{0}]", Android.OS.Build.Radio);
76  Writer.WriteLine ("[Tags:\t\t{0}]", Android.OS.Build.Tags);
77  Writer.WriteLine ("[Time:\t\t{0}]", Android.OS.Build.Time);
78  Writer.WriteLine ("[Type:\t\t{0}]", Android.OS.Build.Type);
79  Writer.WriteLine ("[User:\t\t{0}]", Android.OS.Build.User);
80  Writer.WriteLine ("[VERSION.Codename:\t{0}]", Android.OS.Build.VERSION.Codename);
81  Writer.WriteLine ("[VERSION.Incremental:\t{0}]", Android.OS.Build.VERSION.Incremental);
82  Writer.WriteLine ("[VERSION.Release:\t{0}]", Android.OS.Build.VERSION.Release);
83  Writer.WriteLine ("[VERSION.Sdk:\t\t{0}]", Android.OS.Build.VERSION.Sdk);
84  Writer.WriteLine ("[VERSION.SdkInt:\t{0}]", Android.OS.Build.VERSION.SdkInt);
85  Writer.WriteLine ("[Device Date/Time:\t{0}]", now); // to match earlier C.WL output
86 
87  // FIXME: add data about how the app was compiled (e.g. ARMvX, LLVM, Linker options)
88 
89  return true;
90  }
91 
92  public void CloseWriter ()
93  {
94  Writer.Close ();
95  Writer = null;
96  }
97 
98  #endregion
99 
100  public void TestStarted (ITest test)
101  {
102  if (test is TestSuite) {
103  Writer.WriteLine ();
104  Writer.WriteLine (test.Name);
105  }
106  }
107 
108  public void TestFinished(ITestResult result)
109  {
110  AndroidRunner.Results[result.Test.FullName ?? result.Test.Name] = result;
111 
112  if (result.Test is TestSuite)
113  {
114  if (!result.IsFailure() && !result.IsSuccess() && !result.IsInconclusive() && !result.IsIgnored())
115  Writer.WriteLine("\t[INFO] {0}", result.Message);
116 
117  string name = result.Test.Name;
118  if (!String.IsNullOrEmpty(name))
119  Writer.WriteLine("{0} : {1} ms", name, result.Duration.TotalMilliseconds);
120  }
121  else
122  {
123  if (result.IsSuccess())
124  {
125  Writer.Write("\t[PASS] ");
126  }
127  else if (result.IsIgnored())
128  {
129  Writer.Write("\t[IGNORED] ");
130  }
131  else if (result.IsFailure())
132  {
133  Writer.Write("\t[FAIL] ");
134  }
135  else if (result.IsInconclusive())
136  {
137  Writer.Write("\t[INCONCLUSIVE] ");
138  }
139  else
140  {
141  Writer.Write("\t[INFO] ");
142  }
143  Writer.Write(result.Test.Name);
144 
145  string message = result.Message;
146  if (!String.IsNullOrEmpty(message))
147  {
148  Writer.Write(" : {0}", message.Replace("\r\n", "\\r\\n"));
149  }
150  Writer.WriteLine();
151 
152  string stacktrace = result.StackTrace;
153  if (!String.IsNullOrEmpty(result.StackTrace))
154  {
155  string[] lines = stacktrace.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
156  foreach (string line in lines)
157  Writer.WriteLine("\t\t{0}", line);
158  }
159  }
160  }
161 
162  public void TestOutput(TestOutput testOutput)
163  {
164  }
165 
166  Stack<DateTime> time = new Stack<DateTime> ();
167 
168  public void TestFinished (TestResult result)
169  {
170 
171  }
172 
173  static AndroidRunner runner = new AndroidRunner ();
174 
175  static public AndroidRunner Runner {
176  get { return runner; }
177  }
178 
179  static List<TestSuite> top = new List<TestSuite> ();
180  static Dictionary<string,TestSuite> suites = new Dictionary<string, TestSuite> ();
181  static Dictionary<string,ITestResult> results = new Dictionary<string, ITestResult> ();
182 
183  static public IList<TestSuite> AssemblyLevel {
184  get { return top; }
185  }
186 
187  static public IDictionary<string,TestSuite> Suites {
188  get { return suites; }
189  }
190 
191  static public IDictionary<string,ITestResult> Results {
192  get { return results; }
193  }
194 
195  public Task<TestResult> Run(NUnit.Framework.Internal.Test test)
196  {
197  return Task.Run(() =>
198  {
199  TestExecutionContext current = TestExecutionContext.CurrentContext;
200  current.WorkDirectory = Environment.CurrentDirectory;
201  //current.Listener = this; // Internal on Android
202  current.GetType().GetField("listener", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(current, this);
203  current.TestObject = test is TestSuite ? null : Reflect.Construct((test as TestMethod).Method.ReflectedType, null);
204  WorkItem wi = test.CreateWorkItem(TestFilter.Empty);
205 
206  wi.Execute(current);
207  return wi.Result;
208  });
209  }
210  }
211 }
void TestFinished(ITestResult result)
void TestOutput(TestOutput testOutput)
bool OpenWriter(string message, Context activity)
Task< TestResult > Run(NUnit.Framework.Internal.Test test)
void TestFinished(TestResult result)