/
AndroidRunner.cs
185 lines (153 loc) · 5.99 KB
/
AndroidRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Copyright 2011-2012 Xamarin Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using Android.App;
using Android.Content;
using Android.Widget;
using NUnitLite;
namespace Android.NUnitLite {
public class AndroidRunner : TestListener {
Options options;
private AndroidRunner ()
{
}
public bool AutoStart { get; set; }
public bool TerminateAfterExecution { get; set; }
public Options Options {
get {
if (options == null)
options = new Options ();
return options;
}
set { options = value; }
}
#region writer
public TextWriter Writer { get; set; }
public bool OpenWriter (string message, Context activity)
{
DateTime now = DateTime.Now;
// let the application provide it's own TextWriter to ease automation with AutoStart property
if (Writer == null) {
if (Options.ShowUseNetworkLogger) {
Console.WriteLine ("[{0}] Sending '{1}' results to {2}:{3}", now, message, Options.HostName, Options.HostPort);
try {
Writer = new TcpTextWriter (Options.HostName, Options.HostPort);
}
catch (SocketException) {
string msg = String.Format ("Cannot connect to {0}:{1}. Start network service or disable network option", options.HostName, options.HostPort);
Toast.MakeText (activity, msg, ToastLength.Long).Show ();
return false;
}
} else {
Writer = Console.Out;
}
}
Writer.WriteLine ("[Runner executing:\t{0}]", message);
// FIXME
Writer.WriteLine ("[M4A Version:\t{0}]", "???");
Writer.WriteLine ("[Board:\t\t{0}]", Android.OS.Build.Board);
Writer.WriteLine ("[Bootloader:\t{0}]", Android.OS.Build.Bootloader);
Writer.WriteLine ("[Brand:\t\t{0}]", Android.OS.Build.Brand);
Writer.WriteLine ("[CpuAbi:\t{0} {1}]", Android.OS.Build.CpuAbi, Android.OS.Build.CpuAbi2);
Writer.WriteLine ("[Device:\t{0}]", Android.OS.Build.Device);
Writer.WriteLine ("[Display:\t{0}]", Android.OS.Build.Display);
Writer.WriteLine ("[Fingerprint:\t{0}]", Android.OS.Build.Fingerprint);
Writer.WriteLine ("[Hardware:\t{0}]", Android.OS.Build.Hardware);
Writer.WriteLine ("[Host:\t\t{0}]", Android.OS.Build.Host);
Writer.WriteLine ("[Id:\t\t{0}]", Android.OS.Build.Id);
Writer.WriteLine ("[Manufacturer:\t{0}]", Android.OS.Build.Manufacturer);
Writer.WriteLine ("[Model:\t\t{0}]", Android.OS.Build.Model);
Writer.WriteLine ("[Product:\t{0}]", Android.OS.Build.Product);
Writer.WriteLine ("[Radio:\t\t{0}]", Android.OS.Build.Radio);
Writer.WriteLine ("[Tags:\t\t{0}]", Android.OS.Build.Tags);
Writer.WriteLine ("[Time:\t\t{0}]", Android.OS.Build.Time);
Writer.WriteLine ("[Type:\t\t{0}]", Android.OS.Build.Type);
Writer.WriteLine ("[User:\t\t{0}]", Android.OS.Build.User);
Writer.WriteLine ("[VERSION.Codename:\t{0}]", Android.OS.Build.VERSION.Codename);
Writer.WriteLine ("[VERSION.Incremental:\t{0}]", Android.OS.Build.VERSION.Incremental);
Writer.WriteLine ("[VERSION.Release:\t{0}]", Android.OS.Build.VERSION.Release);
Writer.WriteLine ("[VERSION.Sdk:\t\t{0}]", Android.OS.Build.VERSION.Sdk);
Writer.WriteLine ("[VERSION.SdkInt:\t{0}]", Android.OS.Build.VERSION.SdkInt);
Writer.WriteLine ("[Device Date/Time:\t{0}]", now); // to match earlier C.WL output
// FIXME: add data about how the app was compiled (e.g. ARMvX, LLVM, Linker options)
return true;
}
public void CloseWriter ()
{
Writer.Close ();
Writer = null;
}
#endregion
public void TestStarted (ITest test)
{
if (test is TestSuite) {
Writer.WriteLine ();
time.Push (DateTime.UtcNow);
Writer.WriteLine (test.Name);
}
}
Stack<DateTime> time = new Stack<DateTime> ();
public void TestFinished (TestResult result)
{
AndroidRunner.Results [result.Test.FullName ?? result.Test.Name] = result;
if (result.Test is TestSuite) {
if (!result.IsError && !result.IsFailure && !result.IsSuccess && !result.Executed)
Writer.WriteLine ("\t[INFO] {0}", result.Message);
var diff = DateTime.UtcNow - time.Pop ();
Writer.WriteLine ("{0} : {1} ms", result.Test.Name, diff.TotalMilliseconds);
} else {
if (result.IsSuccess) {
Writer.Write ("\t{0} ", result.Executed ? "[PASS]" : "[IGNORED]");
} else if (result.IsFailure || result.IsError) {
Writer.Write ("\t[FAIL] ");
} else {
Writer.Write ("\t[INFO] ");
}
Writer.Write (result.Test.Name);
string message = result.Message;
if (!String.IsNullOrEmpty (message)) {
Writer.Write (" : {0}", message.Replace ("\r\n", "\\r\\n"));
}
Writer.WriteLine ();
string stacktrace = result.StackTrace;
if (!String.IsNullOrEmpty (result.StackTrace)) {
string[] lines = stacktrace.Split (new char [] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
Writer.WriteLine ("\t\t{0}", line);
}
}
}
static AndroidRunner runner = new AndroidRunner ();
static public AndroidRunner Runner {
get { return runner; }
}
static List<TestSuite> top = new List<TestSuite> ();
static Dictionary<string,TestSuite> suites = new Dictionary<string, TestSuite> ();
static Dictionary<string,TestResult> results = new Dictionary<string, TestResult> ();
static public IList<TestSuite> AssemblyLevel {
get { return top; }
}
static public IDictionary<string,TestSuite> Suites {
get { return suites; }
}
static public IDictionary<string,TestResult> Results {
get { return results; }
}
}
}