MainPage.xaml.cs 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
16 17 18 19 20 21
using belle_sip_tester.DataModel;
using belle_sip_tester_runtime_component;
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml.Documents;
using Windows.Storage;
22 23 24

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

25
namespace belle_sip_tester
26 27 28 29
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
30
    public sealed partial class MainPage : Page, OutputTraceListener
31 32 33
    {
        public MainPage()
        {
34
            this.InitializeComponent();           
35 36
        }

37
        protected override void OnNavigatedTo(NavigationEventArgs e)
38
        {
39
            base.OnNavigatedTo(e);
40
            BelleSipTester.Instance.setWritableDirectory(ApplicationData.Current.LocalFolder);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
            _suites = UnitTestDataSource.GetSuites(BelleSipTester.Instance);
            TryAutoLaunch();
        }

        public IEnumerable<UnitTestSuite> Suites
        {
            get { return _suites; }
        }

        private IEnumerable<UnitTestSuite> _suites;

        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            bool allSelected = Suites.All(x => x.Selected);
            foreach (UnitTestSuite suite in Suites)
            {
                suite.Selected = !allSelected;
            }
        }

        private void RunSelected_Click(object sender, RoutedEventArgs e)
        {
            int nbCases = 0;
            foreach (UnitTestSuite suite in Suites)
            {
                foreach (UnitTestCase c in suite.Cases)
                {
                    if (c.Selected) nbCases++;
                }
            }
            if (nbCases == 0) return;

            PrepareRun(nbCases);

            var tup = new Tuple<IEnumerable<UnitTestSuite>, bool?>(Suites, Verbose.IsChecked);
            var t = Task.Factory.StartNew(async (object parameters) =>
            {
                var p = parameters as Tuple<IEnumerable<UnitTestSuite>, bool?>;
                IEnumerable<UnitTestSuite> suites = p.Item1;
                bool verbose = p.Item2 != null ? (bool)p.Item2 : false;
                foreach (UnitTestSuite suite in suites)
                {
                    foreach (UnitTestCase c in suite.Cases)
                    {
                        if (c.Selected)
                        {
                            await RunUnitTestCase(c, verbose);
                        }
                    }
                }
            }, tup);
        }

        private void RunSingle_Click(object sender, RoutedEventArgs e)
        {
            PrepareRun(1);

            var tup = new Tuple<UnitTestCase, bool?>(DisplayedTestCase, Verbose.IsChecked);
            var t = Task.Factory.StartNew(async (object parameters) =>
            {
                var p = parameters as Tuple<UnitTestCase, bool?>;
                UnitTestCase c = p.Item1;
                bool verbose = p.Item2 != null ? (bool)p.Item2 : false;
                await RunUnitTestCase(c, verbose);
            }, tup);
        }

        private void PrepareRun(int nbCases)
        {
            CommandBar.IsEnabled = false;
            ProgressIndicator.IsEnabled = true;
            ProgressIndicator.Minimum = 0;
            ProgressIndicator.Maximum = nbCases;
            ProgressIndicator.Value = 0;
            BelleSipTester.Instance.setOutputTraceListener(this);
        }

        private async Task RunUnitTestCase(UnitTestCase c, bool verbose)
        {
            UnitTestCaseState newState = UnitTestCaseState.NotRun;
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                RunningTestCase = c;
            });
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                c.Traces.Clear();
            });
            c.Dispatcher = Dispatcher;
            if (BelleSipTester.Instance.run(c.Suite.Name, c.Name, verbose))
131
            {
132
                newState = UnitTestCaseState.Failure;
133 134 135
            }
            else
            {
136
                newState = UnitTestCaseState.Success;
137
            }
138 139 140 141 142 143 144 145 146
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                c.State = newState;
                ProgressIndicator.Value += 1;
                if (ProgressIndicator.Value == ProgressIndicator.Maximum)
                {
                    UnprepareRun();
                }
            });
147 148
        }

149
        private void UnprepareRun()
150
        {
151 152 153 154
            BelleSipTester.Instance.setOutputTraceListener(null);
            RunningTestCase = null;
            ProgressIndicator.IsEnabled = false;
            CommandBar.IsEnabled = true;
155 156
        }

157
        private void UnitTestCase_Click(object sender, ItemClickEventArgs e)
158
        {
159 160 161 162
            DisplayedTestCase = (e.ClickedItem as UnitTestCase);
            TestResultPage.DataContext = DisplayedTestCase;
            TestResultState.Visibility = Visibility.Visible;
            TestResultRun.Visibility = Visibility.Visible;
163 164
        }

165
        public async void outputTrace(String lev, String msg)
166
        {
167
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
168
            {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                if (RunningTestCase != null)
                {
                    RunningTestCase.Traces.Add(new OutputTrace(lev, msg));
                }
            });
        }

        private async void TryAutoLaunch()
        {
            try
            {
                await ApplicationData.Current.LocalFolder.GetFileAsync("autolaunch");
                CommandBar.IsEnabled = false;
                ProgressIndicator.IsIndeterminate = true;
                ProgressIndicator.IsEnabled = true;
                BelleSipTester.Instance.runAllToXml();
                if (BelleSipTester.Instance.AsyncAction != null)
                {
                    BelleSipTester.Instance.AsyncAction.Completed += (asyncInfo, asyncStatus) => {
                        App.Current.Exit();
                    };
                }
191
            }
192
            catch (Exception) { }
193
        }
194 195 196

        private UnitTestCase RunningTestCase;
        private UnitTestCase DisplayedTestCase;
197 198
    }
}