Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GraphicsTestBase.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 using System;
4 using System.Threading.Tasks;
5 
6 using NUnit.Framework;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Paradox.Games;
9 
10 namespace SiliconStudio.Paradox.Graphics.Regression
11 {
12  [TestFixture]
13  public abstract class GraphicsTestBase : TestGameBase
14  {
15  #region Public properties
16 
17  public static bool ForceInteractiveMode;
18 
19  public FrameGameSystem FrameGameSystem { get; private set; }
20 
21  protected TestContext CurrentTestContext { get; set; }
22 
23  #endregion
24 
25  #region Public members
26 
27  /// <summary>
28  /// The current version of the test
29  /// </summary>
30  public int CurrentVersion;
31 
32  /// <summary>
33  /// The current version extra parameter (concatenated to CurrentVersionNumber).
34  /// </summary>
35  public string CurrentVersionExtra;
36 
37  public int FrameIndex;
38 
39  private bool screenshotAutomationEnabled;
40 
41  #endregion
42 
43  #region Constructors
44 
45  protected GraphicsTestBase()
46  {
47  CurrentVersion = 0;
48 
49  FrameGameSystem = new FrameGameSystem(Services);
50  GameSystems.Add(FrameGameSystem);
51 
52 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
53  // get build number
54  int buildNumber;
55  if (ImageTester.ImageTestResultConnection.BuildNumber <= 0 && Int32.TryParse(Environment.GetEnvironmentVariable("PARADOX_BUILD_NUMBER"), out buildNumber))
56  ImageTester.ImageTestResultConnection.BuildNumber = buildNumber;
57 
58  // get branch name
59  if (String.IsNullOrEmpty(ImageTester.ImageTestResultConnection.BranchName))
60  ImageTester.ImageTestResultConnection.BranchName = Environment.GetEnvironmentVariable("PARADOX_BRANCH_NAME") ?? "";
61 #endif
62  }
63 
64  #endregion
65 
66  #region public methods
67 
68  /// <summary>
69  /// Save the image locally or on the server.
70  /// </summary>
71  /// <param name="textureToSave">The texture to save.</param>
72  public void SaveImage(Texture textureToSave)
73  {
74  if (textureToSave == null)
75  return;
76 
77  TestGameLogger.Info(@"Saving non null image");
78  var testName = CurrentTestContext != null ? CurrentTestContext.Test.FullName : null;
79  TestGameLogger.Info(@"saving remotely.");
80  using (var image = textureToSave.GetDataAsImage())
81  {
82  try
83  {
84  SendImage(image, testName);
85  }
86  catch (Exception)
87  {
88  TestGameLogger.Error(@"An error occurred when trying to send the data to the server.");
89  throw;
90  }
91  }
92  }
93 
94  /// <summary>
95  /// Save the image locally or on the server.
96  /// </summary>
97  public void SaveBackBuffer()
98  {
99  TestGameLogger.Info(@"Saving the backbuffer");
100  SaveImage(GraphicsDevice.BackBuffer.Texture);
101  }
102 
103  /// <summary>
104  /// Gets or sets the value indicating if the screen shots automation should be enabled or not.
105  /// </summary>
106  public bool ScreenShotAutomationEnabled
107  {
108  get
109  {
110  return screenshotAutomationEnabled;
111  }
112  set
113  {
114  FrameGameSystem.Visible = value;
115  FrameGameSystem.Enabled = value;
116  screenshotAutomationEnabled = value;
117  }
118  }
119 
120  #endregion
121 
122  #region Protected methods
123 
124  protected override async Task LoadContent()
125  {
126  await base.LoadContent();
127 
128  Window.Position = Int2.Zero; // avoid possible side effects due to position of the window in the screen.
129 
130 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
131  // Register 3D card name
132  ImageTester.ImageTestResultConnection.DeviceName += "_" + GraphicsDevice.Adapter.Description;
133 #endif
134 
135  Script.Add(RegisterTestsInternal);
136  }
137 
138  private Task RegisterTestsInternal()
139  {
140  RegisterTests();
141 
142  return Task.FromResult(true);
143  }
144 
145  /// <summary>
146  /// Loop through all the tests and save the images.
147  /// </summary>
148  /// <param name="gameTime">the game time.</param>
149  protected override void Draw(GameTime gameTime)
150  {
151  base.Draw(gameTime);
152 
153  if (!ScreenShotAutomationEnabled)
154  return;
155 
157  Exit();
158  else if (FrameGameSystem.TakeSnapshot)
159  SaveBackBuffer();
160  }
161 
162  /// <summary>
163  /// Method to register the tests.
164  /// </summary>
165  protected virtual void RegisterTests()
166  {
167  }
168 
169  protected static void RunGameTest(GraphicsTestBase game)
170  {
171  game.CurrentTestContext = TestContext.CurrentContext;
172 
173  game.ScreenShotAutomationEnabled = !ForceInteractiveMode;
174 
175  GameTester.RunGameTest(game);
176 
178  Assert.IsTrue(ImageTester.RequestImageComparisonStatus(game.CurrentTestContext.Test.FullName), "The image comparison returned false.");
179  }
180 
181  #endregion
182 
183  #region Private methods
184 
185  /// <summary>
186  /// Send the data of the test to the server.
187  /// </summary>
188  /// <param name="image">The image to send.</param>
189  /// <param name="testName">The name of the test.</param>
190  public void SendImage(Image image, string testName)
191  {
192  var currentVersion = CurrentVersion.ToString();
193  if (CurrentVersionExtra != null)
194  currentVersion += CurrentVersionExtra;
195 
196  // TODO: Allow textual frame names (and use FrameIndex if not properly set)
197  var frameIndex = FrameIndex++;
198 
199  ImageTester.SendImage(new TestResultImage { CurrentVersion = currentVersion, Frame = frameIndex.ToString(), Image = image, TestName = testName });
200  }
201 
202  #endregion
203 
204  #region Helper structures and classes
205 
206  /// <summary>
207  /// A structure to store information about the connected test devices.
208  /// </summary>
209  public struct ConnectedDevice
210  {
211  public string Serial;
212  public string Name;
214 
215  public override string ToString()
216  {
217  return Name + " " + Serial + " " + PlatformPermutator.GetPlatformName(Platform);
218  }
219  }
220 
221  #endregion
222  }
223 }
void SendImage(Image image, string testName)
Send the data of the test to the server.
static ImageTestResultConnection ImageTestResultConnection
Definition: ImageTester.cs:11
override async Task LoadContent()
Loads the content.
Provides method to instantiate an image 1D/2D/3D supporting TextureArray and mipmaps on the CPU or to...
Definition: Image.cs:88
RenderTarget BackBuffer
Gets the back buffer sets by the current Presenter setup on this device.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
bool ScreenShotAutomationEnabled
Gets or sets the value indicating if the screen shots automation should be enabled or not...
virtual void RegisterTests()
Method to register the tests.
Current timing used for variable-step (real time) or fixed-step (game time) games.
Definition: GameTime.cs:31
void SaveBackBuffer()
Save the image locally or on the server.
bool TakeSnapshot
Flag stating that a screenshot should be taken.
bool AllTestsCompleted
Flag stating that all the tests have been rendered.
override void Draw(GameTime gameTime)
Loop through all the tests and save the images.
readonly Texture Texture
The underlying texture.
Definition: RenderTarget.cs:17
string CurrentVersionExtra
The current version extra parameter (concatenated to CurrentVersionNumber).
void SaveImage(Texture textureToSave)
Save the image locally or on the server.
Base class for texture resources.
Definition: Texture.cs:38
A structure to store information about the connected test devices.