Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImageTester.NotRuntime.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 #if !SILICONSTUDIO_PLATFORM_WINDOWS_STORE && !SILICONSTUDIO_PLATFORM_WINDOWS_PHONE
5 
6 using System;
7 using System.IO;
8 using System.Net.Sockets;
9 
10 namespace SiliconStudio.Paradox.Graphics.Regression
11 {
12  public static partial class ImageTester
13  {
14  private static TcpClient ImageComparisonServer;
15 
16  public static bool Connect()
17  {
18  if (ImageComparisonServer != null)
19  return true;
20 
21  try
22  {
23  ImageComparisonServer = new TcpClient();
24  ImageComparisonServer.Connect(ParadoxImageServerHost, ParadoxImageServerPort);
25 
26  // Send initial parameters
27  var networkStream = ImageComparisonServer.GetStream();
28  var binaryWriter = new BinaryWriter(networkStream);
29  ImageTestResultConnection.Write(binaryWriter);
30 
31  return true;
32  }
33  catch (Exception)
34  {
35  ImageComparisonServer = null;
36 
37  return false;
38  }
39  }
40 
41  public static void Disconnect()
42  {
43  if (ImageComparisonServer != null)
44  {
45  try
46  {
47  // Properly sends a message notifying we want to close the connection
48  var networkStream = ImageComparisonServer.GetStream();
49  var binaryWriter = new BinaryWriter(networkStream);
50  binaryWriter.Write((int)ImageServerMessageType.ConnectionFinished);
51 
52  ImageComparisonServer.Close();
53  }
54  catch (Exception)
55  {
56  }
57  ImageComparisonServer = null;
58  }
59  }
60 
61  public static bool RequestImageComparisonStatus(string testName = null)
62  {
63  if (!Connect())
64  throw new InvalidOperationException("Could not connect to image comparer server");
65 
66  try
67  {
68  if (testName == null && NUnit.Framework.TestContext.CurrentContext == null)
69  {
70  testName = NUnit.Framework.TestContext.CurrentContext.Test.FullName;
71  }
72 
73  var networkStream = ImageComparisonServer.GetStream();
74  var binaryWriter = new BinaryWriter(networkStream);
75  var binaryReader = new BinaryReader(networkStream);
76 
77  // Header
78  binaryWriter.Write((int)ImageServerMessageType.RequestImageComparisonStatus);
79  binaryWriter.Write(testName);
80 
81  return binaryReader.ReadBoolean();
82  }
83  catch (Exception)
84  {
85  throw;
86  }
87  }
88 
89  /// <summary>
90  /// Send the data of the test to the server.
91  /// </summary>
92  /// <param name="testResultImage">The image to send.</param>
93  public static bool SendImage(TestResultImage testResultImage)
94  {
95  if (!Connect())
96  throw new InvalidOperationException("Could not connect to image comparer server");
97 
98  try
99  {
100  if (testResultImage.TestName == null && NUnit.Framework.TestContext.CurrentContext != null)
101  {
102  testResultImage.TestName = NUnit.Framework.TestContext.CurrentContext.Test.FullName;
103  }
104 
105 
106  var networkStream = ImageComparisonServer.GetStream();
107  var binaryWriter = new BinaryWriter(networkStream);
108  var binaryReader = new BinaryReader(networkStream);
109 
110  // Header
111  binaryWriter.Write((int)ImageServerMessageType.SendImage);
112 
113  TestGameBase.TestGameLogger.Info(@"Sending image information...");
114  testResultImage.Write(binaryWriter);
115 
116  return binaryReader.ReadBoolean();
117  }
118  catch (Exception)
119  {
120  throw;
121  }
122  }
123  }
124 }
125 
126 #endif
static bool SendImage(TestResultImage testResultImage)
Send the data of the test to the server.
static bool RequestImageComparisonStatus(string testName=null)