Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NUnitLiteLauncher.Android.cs
Go to the documentation of this file.
1 // ***********************************************************************
2 // Copyright (c) 2009 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 
24 using System;
25 using System.IO;
26 using System.Net.Sockets;
27 using System.Reflection;
28 using System.Text;
29 using System.Threading.Tasks;
30 
31 using Android.App;
32 using Android.NUnitLite.UI;
33 using Android.OS;
34 using Java.IO;
35 
36 using SiliconStudio.Core;
37 using SiliconStudio.Core.Diagnostics;
38 using SiliconStudio.Paradox.Graphics.Regression;
39 
44 
45 namespace NUnitLite.Tests
46 {
47  [Activity(MainLauncher = true)]
48  public class MainActivity : Activity
49  {
50  private const char IpAddressesSplitCharacter = '%';
51 
52  public static Logger Logger = GlobalLogger.GetLogger("NUnitLiteLauncher");
53  private ConsoleLogListener logAction = new ConsoleLogListener();
54 
55  protected TcpClient Connect(string serverAddresses, int serverPort)
56  {
57  // Connect back to server
58  var client = new TcpClient();
59  var possibleIpAddresses = serverAddresses.Split(IpAddressesSplitCharacter);
60  foreach (var possibleIpAddress in possibleIpAddresses)
61  {
62  if (String.IsNullOrEmpty(possibleIpAddress))
63  continue;
64  try
65  {
66  Logger.Debug(@"Trying to connect to the server " + possibleIpAddress + @":" + serverPort + @".");
67  client.Connect(possibleIpAddress, serverPort);
68 
69  Logger.Debug(@"Client connected with ip " + possibleIpAddress + @"... sending data");
70  return client;
71  }
72  catch (Exception ex)
73  {
74  Logger.Error("Error when trying to connect to the server IP {0}.\n{1}", possibleIpAddress, ex);
75  }
76 
77  Logger.Debug(@"Client connected with ip " + possibleIpAddress + @"... sending data");
78 
79  return client;
80  }
81 
82  Logger.Fatal(@"Could not connect to server. Quitting the application.");
83  OnDestroy();
84  Finish();
85 
86  throw new InvalidObjectException("Could not connect to server.");
87  }
88 
89  protected override void OnCreate(Bundle bundle)
90  {
91  GlobalLogger.GlobalMessageLogged += logAction;
92  Logger.ActivateLog(LogMessageType.Debug);
93 
94  base.OnCreate(bundle);
95 
96  // Set the android global context
97  if (PlatformAndroid.Context == null)
98  PlatformAndroid.Context = this;
99 
100  var serverAddresses = Intent.GetStringExtra(TestRunner.ParadoxServerIp);
101  if (serverAddresses == null)
102  {
103  // No explicit intent, switch to UI activity
104  StartActivity(typeof(ParadoxTestSuiteActivity));
105  return;
106  }
107 
108  Task.Run(() => RunTests());
109  }
110 
111  private void RunTests()
112  {
113  var serverAddresses = Intent.GetStringExtra(TestRunner.ParadoxServerIp);
114  var serverPort = Int32.Parse(Intent.GetStringExtra(TestRunner.ParadoxServerPort) ?? "8080");
115  var buildNumber = Int32.Parse(Intent.GetStringExtra(TestRunner.ParadoxBuildNumber) ?? "-1");
116  var branchName = Intent.GetStringExtra(TestRunner.ParadoxBranchName) ?? "";
117 
118  // Remove extra (if activity is recreated)
119  Intent.RemoveExtra(TestRunner.ParadoxServerIp);
120  Intent.RemoveExtra(TestRunner.ParadoxServerPort);
121  Intent.RemoveExtra(TestRunner.ParadoxBuildNumber);
122  Intent.RemoveExtra(TestRunner.ParadoxBranchName);
123 
124 
125  Logger.Info(@"*******************************************************************************************************************************");
126  Logger.Info(@"date: " + DateTime.Now);
127  Logger.Info(@"server addresses: " + serverAddresses);
128  Logger.Info(@"port: " + serverPort);
129  Logger.Info(@"*******************************************************************************************************************************");
130 
131  // Connect to server right away to let it know we're alive
132  var client = Connect(serverAddresses, serverPort);
133 
134  // Update build number (if available)
135  ImageTester.ImageTestResultConnection.BuildNumber = buildNumber;
136  ImageTester.ImageTestResultConnection.BranchName = branchName ?? "";
137 
138  // Connect beforehand to image tester, so that first test timing is not affected by initial connection
139  try
140  {
141  ImageTester.Connect();
142  }
143  catch (Exception e)
144  {
145  Logger.Error("Error connecting to image tester server: {0}", e);
146  }
147 
148  // Start unit test
149  var cachePath = CacheDir.AbsolutePath;
150  var timeNow = DateTime.Now;
151 
152  // Generate result file name
153  var resultFile = Path.Combine(cachePath, string.Format("TestResult-{0:yyyy-MM-dd_hh-mm-ss-tt}.xml", timeNow));
154 
155  Logger.Debug(@"Execute tests");
156 
157  var stringBuilder = new StringBuilder();
158  var stringWriter = new StringWriter(stringBuilder);
159  new TextUI(stringWriter).Execute(new [] { "-format:nunit2", string.Format("-result:{0}", resultFile) });
160 
161  Logger.Debug(@"Execute tests done");
162 
163  // Read result file
164  var result = File.ReadAllText(resultFile);
165 
166  // Delete result file
167  File.Delete(resultFile);
168 
169  // Display some useful info
170  var output = stringBuilder.ToString();
171  Console.WriteLine(output);
172 
173  Logger.Debug(@"Sending results to server");
174 
175  // Send back result
176  var binaryWriter = new BinaryWriter(client.GetStream());
177  binaryWriter.Write(output);
178  binaryWriter.Write(result);
179 
180  Logger.Debug(@"Close connection");
181 
182  ImageTester.Disconnect();
183 
184  client.Close();
185 
186  Finish();
187  }
188 
189  protected override void OnDestroy()
190  {
191  GlobalLogger.GlobalMessageLogged -= logAction;
192 
193  base.OnDestroy();
194  }
195  }
196 
197  [Activity]
199  {
200  protected override void OnCreate(Bundle bundle)
201  {
202  // Set the android global context
203  if (PlatformAndroid.Context == null)
204  PlatformAndroid.Context = this;
205 
206  ImageTester.ImageTestResultConnection.BuildNumber = -1;
207 
208  // Test current assembly
209  Add(Assembly.GetExecutingAssembly());
210 
211  base.OnCreate(bundle);
212  }
213 
214  protected override void OnDestroy()
215  {
216  ImageTester.Disconnect();
217 
218  base.OnDestroy();
219  }
220  }
221 }
Base implementation for ILogger.
Definition: Logger.cs:10
override void OnCreate(Bundle bundle)
System.IO.File File
System.IO.StringWriter StringWriter
System.Console Console
TcpClient Connect(string serverAddresses, int serverPort)
SiliconStudio.Paradox.Graphics.Regression.TextUI TextUI
A LogListener implementation redirecting its output to the default OS console. If console is not supp...