Exemple #1
0
 private void DisposeOfData(Application application, Window mainWindow, WindowFixture fixture, object[] dataRow)
 {
     if (application != null)
     {
         application.Kill();
         application.Dispose();
     }
 }
        public void GivenNotOnTopWindow_WhenInvokeSetOnTopCommand_ThenItSetsWindowOnTop()
        {
            MainViewModel mainVmSubject = GivenMainViewModelTestSubject();
            WindowFixture window        = new WindowFixture()
            {
                Topmost = false
            };

            mainVmSubject.SetWindowOnTopCommand.Execute(window);

            Assert.True(window.Topmost);
        }
        public void GivenWindow_WhenResizeCommandExecutes_ThenItResizeWindowProper()
        {
            MainViewModel mainVmSubject = GivenMainViewModelTestSubject();
            WindowFixture window        = new WindowFixture();

            window.ActualHeight = 50;
            window.ActualWidth  = 100;
            double desktopWorkingAreaRight  = 300.0;
            double desktopWorkingAreaBottom = 200.0;

            object[] commandParams = new object[] { $"%,%,{desktopWorkingAreaRight},{desktopWorkingAreaBottom}", window };

            mainVmSubject.ResizeWindowCommand.Execute(commandParams);

            Assert.Equal(desktopWorkingAreaRight - window.ActualWidth, window.Left);
            Assert.Equal(desktopWorkingAreaBottom - window.ActualHeight, window.Top);
        }
Exemple #4
0
        private async Task ExecuteTestMethod(MethodInfo runtimeMethod, RunSummary runSummary, IEnumerable <IApplicationAttribute> applicationAttributes, object[] dataRow)
        {
            foreach (var applicationAttribute in applicationAttributes)
            {
                Application   application = null;
                Window        mainWindow  = null;
                WindowFixture fixture     = null;

                try
                {
                    application = applicationAttribute.ProvideApplication(runtimeMethod);

                    application.WaitWhileBusy();

                    mainWindow = null;

                    if (!string.IsNullOrEmpty(applicationAttribute.Window))
                    {
                        mainWindow = application.GetWindow(applicationAttribute.Window);
                    }
                    else
                    {
                        mainWindow = application.GetWindows().FirstOrDefault();
                    }

                    if (mainWindow == null)
                    {
                        throw new Exception("Could not locate main window " + applicationAttribute.Window);
                    }

                    fixture = new WindowFixture(application, mainWindow);
                    ITypeInfo[] resolvedTypes = null;
                    var         methodToRun   = runtimeMethod;

                    if (methodToRun.IsGenericMethodDefinition)
                    {
                        resolvedTypes = TestCase.TestMethod.Method.ResolveGenericTypes(dataRow);
                        methodToRun   = methodToRun.MakeGenericMethod(resolvedTypes.Select(t => ((IReflectionTypeInfo)t).Type).ToArray());
                    }

                    List <object> parameterList     = new List <object>();
                    var           parameters        = methodToRun.GetParameters().ToArray();
                    object        initializerReturn = null;

                    int dataRowIndex = 0;

                    for (int i = 0; i < parameters.Length; i++)
                    {
                        var parameter  = parameters[i];
                        var attributes = parameter.GetCustomAttributes(true);

                        if (parameter.ParameterType == typeof(Window))
                        {
                            parameterList.Add(mainWindow);
                        }
                        else if (parameter.ParameterType == typeof(IWindowFixture))
                        {
                            parameterList.Add(fixture);
                        }
                        else if (attributes.Any(a => a is GenerateAttribute))
                        {
                            var generateAttribute = (GenerateAttribute)attributes.First(a => a is GenerateAttribute);

                            InitializeCustomAttribute(generateAttribute, runtimeMethod, parameter);

                            var constraintName = generateAttribute.ConstraintName ?? parameter.Name;
                            var min            = generateAttribute.Min;
                            var max            = generateAttribute.Max;

                            var value = fixture.Data.Generate(parameter.ParameterType, constraintName, new { min, max });
                            parameterList.Add(value);
                        }
                        else if (attributes.Any(a => a is LocateAttribute))
                        {
                            var locateAttribute = (LocateAttribute)attributes.First(a => a is LocateAttribute);

                            InitializeCustomAttribute(locateAttribute, runtimeMethod, parameter);

                            var value = locateAttribute.Value;

                            if (value == null)
                            {
                                value = fixture.Data.Generate(new SimpleFixture.DataRequest(null,
                                                                                            fixture.Data,
                                                                                            parameter.ParameterType,
                                                                                            parameter.Name,
                                                                                            false,
                                                                                            null,
                                                                                            null));
                            }

                            parameterList.Add(value);
                        }
                        else if (attributes.Any(a => a is FreezeAttribute))
                        {
                            var freeze = (FreezeAttribute)attributes.FirstOrDefault(a => a is FreezeAttribute);

                            InitializeCustomAttribute(freeze, runtimeMethod, parameter);

                            var value = freeze.Value;

                            if (value == null)
                            {
                                var constraintName = freeze.ConstraintName ?? parameter.Name;
                                var min            = freeze.Min;
                                var max            = freeze.Max;

                                value = fixture.Data.Generate(parameter.ParameterType, constraintName, new { min, max });
                            }

                            parameterList.Add(value);

                            object lastObject         = parameterList.Last();
                            var    closedFreezeMethod =
                                FreezeMethod.MakeGenericMethod(lastObject.GetType());

                            closedFreezeMethod.Invoke(null, new object[] { fixture.Data, value, freeze.For });
                        }
                        else if (initializerReturn != null && parameter.ParameterType == initializerReturn.GetType())
                        {
                            parameterList.Add(initializerReturn);
                            initializerReturn = null;
                        }
                        else if (dataRowIndex < dataRow.Length)
                        {
                            var dataValue = dataRow[dataRowIndex];
                            dataRowIndex++;
                            parameterList.Add(dataValue);
                        }
                        else
                        {
                            var value = fixture.Data.Generate(parameter.ParameterType, parameter.Name);
                            parameterList.Add(value);
                        }
                    }

                    var convertedDataRow  = Reflector.ConvertArguments(parameterList.ToArray(), parameters.Select(p => p.ParameterType).ToArray());
                    var theoryDisplayName =
                        TestCase.TestMethod.Method.GetDisplayNameWithArguments(DisplayName + " " + applicationAttribute.Application,
                                                                               dataRow,
                                                                               resolvedTypes);

                    var             test       = new XunitTest(TestCase, theoryDisplayName);
                    var             skipReason = SkipReason;
                    XunitTestRunner testRunner = CreateTestRunner(test, MessageBus, TestClass, ConstructorArguments, methodToRun, convertedDataRow, skipReason, BeforeAfterAttributes, Aggregator, CancellationTokenSource);

                    runSummary.Aggregate(await testRunner.RunAsync());
                }
                catch (Exception exp)
                {
                    Aggregator.Add(exp);
                }
                finally
                {
                    var timer = new ExecutionTimer();
                    timer.Aggregate(() => DisposeOfData(application, mainWindow, fixture, dataRow));

                    runSummary.Time += timer.Total;
                }
            }
        }