public async Task ClickFiresOnCorrectWidgets() { int blueClickCount = 0; int orangeClickCount = 0; int purpleClickCount = 0; lastClicked = null; double waitTime = .5; AutomationTest testToRun = (testRunner) => { testRunner.Wait(2); testRunner.ClickByName("rootClickable", 5); testRunner.Wait(waitTime); Assert.AreEqual(blueClickCount, 1, "Expected 1 click on blue widget"); Assert.AreEqual(orangeClickCount, 0, "Expected 0 clicks on orange widget"); Assert.AreEqual(purpleClickCount, 0, "Expected 1 click on purple widget"); testRunner.ClickByName("orangeClickable", 1); testRunner.Wait(waitTime); Assert.AreEqual(blueClickCount, 1, "Expected 1 click on blue widget"); Assert.AreEqual(orangeClickCount, 1, "Expected 1 clicks on orange widget"); Assert.AreEqual(purpleClickCount, 0, "Expected 0 click on purple widget"); testRunner.ClickByName("rootClickable", 1); testRunner.Wait(waitTime); Assert.AreEqual(blueClickCount, 2, "Expected 1 click on blue widget"); Assert.AreEqual(orangeClickCount, 1, "Expected 0 clicks on orange widget"); Assert.AreEqual(purpleClickCount, 0, "Expected 1 click on purple widget"); testRunner.ClickByName("orangeClickable", 1); testRunner.Wait(waitTime); Assert.AreEqual(blueClickCount, 2, "Expected 1 click on root widget"); Assert.AreEqual(orangeClickCount, 2, "Expected 2 clicks on orange widget"); Assert.AreEqual(purpleClickCount, 0, "Expected 0 click on purple widget"); testRunner.ClickByName("purpleClickable", 1); testRunner.Wait(waitTime); Assert.AreEqual(blueClickCount, 2, "Expected 1 click on blue widget"); Assert.AreEqual(orangeClickCount, 2, "Expected 2 clicks on orange widget"); Assert.AreEqual(purpleClickCount, 1, "Expected 1 click on purple widget"); return(Task.FromResult(0)); }; SystemWindow systemWindow = new SystemWindow(300, 200) { Padding = new BorderDouble(20) }; var rootClickable = new GuiWidget() { Width = 50, HAnchor = HAnchor.ParentLeftRight, VAnchor = VAnchor.ParentBottomTop, Margin = new BorderDouble(50), Name = "rootClickable", BackgroundColor = RGBA_Bytes.Blue }; rootClickable.Click += (sender, e) => { var widget = sender as GuiWidget; blueClickCount += 1; var color = rootClickable.BackgroundColor.AdjustSaturation(0.4); systemWindow.BackgroundColor = color.GetAsRGBA_Bytes(); lastClicked = rootClickable; }; rootClickable.AfterDraw += widget_DrawSelection; var orangeClickable = new GuiWidget() { Width = 35, Height = 25, OriginRelativeParent = new VectorMath.Vector2(10, 10), Name = "orangeClickable", Margin = new BorderDouble(10), BackgroundColor = RGBA_Bytes.Orange }; orangeClickable.Click += (sender, e) => { var widget = sender as GuiWidget; orangeClickCount += 1; var color = widget.BackgroundColor.AdjustSaturation(0.4); systemWindow.BackgroundColor = color.GetAsRGBA_Bytes(); lastClicked = widget; }; orangeClickable.AfterDraw += widget_DrawSelection; rootClickable.AddChild(orangeClickable); var purpleClickable = new GuiWidget() { Width = 35, Height = 25, OriginRelativeParent = new VectorMath.Vector2(0, 10), HAnchor = HAnchor.ParentRight, Name = "purpleClickable", Margin = new BorderDouble(10), BackgroundColor = new RGBA_Bytes(141, 0, 206) }; purpleClickable.Click += (sender, e) => { var widget = sender as GuiWidget; purpleClickCount += 1; var color = widget.BackgroundColor.AdjustSaturation(0.4); systemWindow.BackgroundColor = color.GetAsRGBA_Bytes(); lastClicked = widget; }; purpleClickable.AfterDraw += widget_DrawSelection; rootClickable.AddChild(purpleClickable); systemWindow.AddChild(rootClickable); await AutomationRunner.ShowWindowAndExecuteTests(systemWindow, testToRun, 25); }
public static async Task RunTest( AutomationTest testMethod, string staticDataPathOverride = null, double maxTimeToRun = 60, QueueTemplate queueItemFolderToAdd = QueueTemplate.None, int overrideWidth = -1, int overrideHeight = -1, string defaultTestImages = null) { // Walk back a step in the stack and output the callers name StackTrace st = new StackTrace(false); Debug.WriteLine("\r\n ***** Running automation test: {0} {1} ", st.GetFrames().Skip(1).First().GetMethod().Name, DateTime.Now); if (staticDataPathOverride == null) { // Popping one directory above MatterControl, then back down into MatterControl ensures this works in MCCentral as well and MatterControl staticDataPathOverride = TestContext.CurrentContext.ResolveProjectPath(5, "MatterControl", "StaticData"); } #if DEBUG string outputDirectory = "Debug"; #else string outputDirectory = "Release"; #endif Environment.CurrentDirectory = TestContext.CurrentContext.ResolveProjectPath(5, "MatterControl", "bin", outputDirectory); #if !__ANDROID__ // Set the static data to point to the directory of MatterControl StaticData.Instance = new FileSystemStaticData(staticDataPathOverride); #endif // Popping one directory above MatterControl, then back down into MatterControl ensures this works in MCCentral as well and MatterControl MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(5, "MatterControl")); if (queueItemFolderToAdd != QueueTemplate.None) { string queueTemplateDirectory = queueItemFolderToAdd.ToString(); MatterControlUtilities.AddItemsToQueue(queueTemplateDirectory); } if (defaultTestImages == null) { defaultTestImages = TestContext.CurrentContext.ResolveProjectPath(4, "Tests", "TestData", "TestImages"); } UserSettings.Instance.set(UserSettingsKey.ThumbnailRenderingMode, "orthographic"); //GL.HardwareAvailable = false; MatterControlApplication matterControlWindow = MatterControlApplication.CreateInstance(overrideWidth, overrideHeight); var config = TestAutomationConfig.Load(); // Extract mouse speed from config AutomationRunner.TimeToMoveMouse = config.TimeToMoveMouse; await AutomationRunner.ShowWindowAndExecuteTests(matterControlWindow, testMethod, maxTimeToRun, defaultTestImages, config.AutomationInputType); }
public static Task ShowWindowAndExecuteTests(SystemWindow initialSystemWindow, AutomationTest testMethod, double secondsToTestFailure = 30, string imagesDirectory = "", Action closeWindow = null) { var testRunner = new AutomationRunner(InputMethod, DrawSimulatedMouse, imagesDirectory); var resetEvent = new AutoResetEvent(false); // On load, release the reset event initialSystemWindow.Load += (s, e) => { resetEvent.Set(); }; int testTimeout = (int)(1000 * secondsToTestFailure); var timer = Stopwatch.StartNew(); // Start two tasks, the timeout and the test method. Block in the test method until the first draw Task <Task> task = Task.WhenAny( Task.Delay(testTimeout), Task.Run(() => { // Wait until the first system window draw before running the test method, up to the timeout resetEvent.WaitOne(testTimeout); return(testMethod(testRunner)); })); // Once either the timeout or the test method has completed, reassign the task/result for timeout errors and shutdown the SystemWindow task.ContinueWith((innerTask) => { long elapsedTime = timer.ElapsedMilliseconds; // Invoke the callers close implementation or fall back to CloseOnIdle if (closeWindow != null) { closeWindow(); } else { initialSystemWindow.CloseOnIdle(); } // Create an exception Task for test timeouts if (elapsedTime >= testTimeout) { // Wait for CloseOnIdle to complete testRunner.WaitFor(() => initialSystemWindow.HasBeenClosed); task = new Task <Task>(() => throw new TimeoutException("TestMethod timed out")); task.RunSynchronously(); } }); // Main thread blocks here until released via CloseOnIdle above initialSystemWindow.ShowAsSystemWindow(); // After the system window is closed return the task and any exception to the calling context return(task?.GetAwaiter().GetResult() ?? Task.CompletedTask); }
public async Task OpenAndCloseMenus() { int item1ClickCount = 0; int item2ClickCount = 0; int item3ClickCount = 0; DropDownList testList = new DropDownList("no selection", Color.Blue) { MenuItemsBackgroundColor = Color.White, MenuItemsBackgroundHoverColor = Color.LightGray, Name = "menu1", HoverColor = Color.Green }; AutomationTest testToRun = (testRunner) => { Assert.AreEqual(0, item1ClickCount); Assert.AreEqual(0, item2ClickCount); Assert.AreEqual(0, item3ClickCount); testRunner.ClickByName("menu1"); testRunner.ClickByName("item1"); testRunner.WaitFor(() => !testList.IsOpen, 2); Assert.IsTrue(!testList.IsOpen); Assert.AreEqual(1, item1ClickCount); Assert.AreEqual(0, item2ClickCount); Assert.AreEqual(0, item3ClickCount); testRunner.ClickByName("menu1"); testRunner.ClickByName("item2"); testRunner.WaitFor(() => !testList.IsOpen, 2); Assert.IsTrue(!testList.IsOpen); Assert.AreEqual(1, item1ClickCount); Assert.AreEqual(1, item2ClickCount); Assert.AreEqual(0, item3ClickCount); testRunner.ClickByName("menu1"); testRunner.ClickByName("item3"); testRunner.WaitFor(() => testList.IsOpen, 2); Assert.IsTrue(testList.IsOpen, "It should remain open when clicking on a disabled item."); Assert.AreEqual(1, item1ClickCount); Assert.AreEqual(1, item2ClickCount); Assert.AreEqual(0, item3ClickCount); testRunner.ClickByName("item2"); testRunner.WaitFor(() => !testList.IsOpen, 2); Assert.IsTrue(!testList.IsOpen); Assert.AreEqual(1, item1ClickCount); Assert.AreEqual(2, item2ClickCount); Assert.AreEqual(0, item3ClickCount); testRunner.ClickByName("menu1"); testRunner.ClickByName("OffMenu"); testRunner.WaitFor(() => !testList.IsOpen, 2); Assert.IsTrue(!testList.IsOpen); testRunner.ClickByName("menu1"); testRunner.Delay(.1); Assert.IsTrue(testList.IsOpen); testRunner.ClickByName("item3"); testRunner.Delay(.1); Assert.IsTrue(testList.IsOpen); testRunner.MoveToByName("OffMenu"); Assert.IsTrue(testList.IsOpen); testRunner.ClickByName("OffMenu"); testRunner.WaitFor(() => !testList.IsOpen, 2); Assert.IsFalse(testList.IsOpen, "Menus should close when clicking off menu"); return(Task.CompletedTask); }; var menuTestContainer = new SystemWindow(300, 200) { BackgroundColor = Color.White, Name = "SystemWindow", }; var menuItem1 = testList.AddItem("item1"); menuItem1.Name = "item1"; menuItem1.Selected += (s, e) => item1ClickCount++; var menuItem2 = testList.AddItem("item2"); menuItem2.Name = "item2"; menuItem2.Selected += (s, e) => item2ClickCount++; var menuItem3 = testList.AddItem("item3"); menuItem3.Name = "item3"; menuItem3.Enabled = false; menuItem3.Selected += (s, e) => item3ClickCount++; menuTestContainer.AddChild(testList); menuTestContainer.AddChild(new GuiWidget(20, 20) { OriginRelativeParent = new Vector2(160, 150), BackgroundColor = Color.Cyan, Name = "OffMenu", }); await AutomationRunner.ShowWindowAndExecuteTests(menuTestContainer, testToRun); }
public static Task ShowWindowAndExecuteTests(SystemWindow initialSystemWindow, AutomationTest testMethod, double secondsToTestFailure, string imagesDirectory = "", InputType inputType = InputType.Native) { var testRunner = new AutomationRunner(imagesDirectory, inputType); AutoResetEvent resetEvent = new AutoResetEvent(false); bool firstDraw = true; initialSystemWindow.AfterDraw += (sender, e) => { if (firstDraw) { firstDraw = false; resetEvent.Set(); } }; int testTimeout = (int)(1000 * secondsToTestFailure); var timer = Stopwatch.StartNew(); // Start two tasks, the timeout and the test method. Block in the test method until the first draw Task <Task> task = Task.WhenAny( Task.Delay(testTimeout), Task.Run(() => { // Wait until the first system window draw before running the test method resetEvent.WaitOne(); return(testMethod(testRunner)); })); // Once either the timeout or the test method has completed, reassign the task/result for timeout errors and shutdown the SystemWindow task.ContinueWith((innerTask) => { long elapsedTime = timer.ElapsedMilliseconds; // Create an exception Task for test timeouts if (elapsedTime >= testTimeout) { task = new Task <Task>(() => { throw new TimeoutException("TestMethod timed out"); }); task.RunSynchronously(); } initialSystemWindow.CloseOnIdle(); }); // Main thread blocks here until released via CloseOnIdle above initialSystemWindow.ShowAsSystemWindow(); // After the system window is closed return the task and any exception to the calling context return(task?.Result ?? Task.FromResult(0)); }
public static async Task RunTest( AutomationTest testMethod, string staticDataPathOverride = null, double maxTimeToRun = 60, QueueTemplate queueItemFolderToAdd = QueueTemplate.None, int overrideWidth = -1, int overrideHeight = -1, string defaultTestImages = null) { // Walk back a step in the stack and output the callers name //StackTrace st = new StackTrace(false); //Debug.WriteLine("\r\n ***** Running automation test: {0} {1} ", st.GetFrames().Skip(1).First().GetMethod().Name, DateTime.Now); if (staticDataPathOverride == null) { // Popping one directory above MatterControl, then back down into MatterControl ensures this works in MCCentral as well and MatterControl staticDataPathOverride = TestContext.CurrentContext.ResolveProjectPath(5, "MatterControl", "StaticData"); } #if DEBUG string outputDirectory = "Debug"; #else string outputDirectory = "Release"; #endif Environment.CurrentDirectory = TestContext.CurrentContext.ResolveProjectPath(5, "MatterControl", "bin", outputDirectory); // Override the default SystemWindow type without config.json AggContext.Config.ProviderTypes.SystemWindow = "MatterHackers.Agg.UI.OpenGLSystemWindow, agg_platform_win32"; #if !__ANDROID__ // Set the static data to point to the directory of MatterControl AggContext.StaticData = new FileSystemStaticData(staticDataPathOverride); #endif // Popping one directory above MatterControl, then back down into MatterControl ensures this works in MCCentral as well and MatterControl MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(5, "MatterControl")); if (queueItemFolderToAdd != QueueTemplate.None) { MatterControlUtilities.AddItemsToQueue(queueItemFolderToAdd.ToString()); } if (defaultTestImages == null) { defaultTestImages = TestContext.CurrentContext.ResolveProjectPath(4, "Tests", "TestData", "TestImages"); } UserSettings.Instance.set(UserSettingsKey.ThumbnailRenderingMode, "orthographic"); //GL.HardwareAvailable = false; var config = TestAutomationConfig.Load(); if (config.UseAutomationDialogs) { AggContext.Config.ProviderTypes.DialogProvider = "MatterHackers.Agg.Platform.AutomationDialogProvider, GuiAutomation"; } // Extract mouse speed from config AutomationRunner.TimeToMoveMouse = config.TimeToMoveMouse; AutomationRunner.UpDelaySeconds = config.MouseUpDelay; // Automation runner must do as much as program.cs to spin up platform string platformFeaturesProvider = "MatterHackers.MatterControl.WindowsPlatformsFeatures, MatterControl"; AppContext.Platform = AggContext.CreateInstanceFrom <INativePlatformFeatures>(platformFeaturesProvider); AppContext.Platform.ProcessCommandline(); var(width, height) = RootSystemWindow.GetStartupBounds(); rootSystemWindow = Application.LoadRootWindow( overrideWidth == -1 ? width : overrideWidth, overrideHeight == -1 ? height : overrideHeight); OemSettings.Instance.ShowShopButton = false; await AutomationRunner.ShowWindowAndExecuteTests( rootSystemWindow, testMethod, maxTimeToRun, defaultTestImages, config.UseAutomationMouse?AutomationRunner.InputType.SimulatedDrawMouse : AutomationRunner.InputType.Native, closeWindow : () => { if (ApplicationController.Instance.ActivePrinter.Connection.CommunicationState == CommunicationStates.Printing) { ApplicationController.Instance.ActivePrinter.Connection.Disable(); } rootSystemWindow.Close(); }); }
public static Task RunTest(this SystemWindow systemWindow, AutomationTest automationTest, int timeout) { return(AutomationRunner.ShowWindowAndExecuteTests(systemWindow, automationTest, timeout)); }