AppDelegate.cs 2.71 KB
Newer Older
gaoqiong's avatar
gaoqiong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
using System;
using System.Reflection;
using System.Threading.Tasks;
using Foundation;
using Microsoft.ML.OnnxRuntime.Tests.Devices;
using UIKit;
using Xunit.Runner;
using Xunit.Runners;
using Xunit.Sdk;

namespace Microsoft.ML.OnnxRuntime.Tests.iOS
{
    [Register("AppDelegate")]
    public partial class AppDelegate : RunnerAppDelegate
    {
#if __NATIVE_DEPENDENCIES_EXIST__
        OnnxRuntimeResultChannel _resultChannel = new OnnxRuntimeResultChannel();
#endif

        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            Xamarin.Calabash.Start();

            AddExecutionAssembly(typeof(ExtensibilityPointFactory).Assembly);

#if __NATIVE_DEPENDENCIES_EXIST__
            AddTestAssembly(Assembly.GetExecutingAssembly());
            ResultChannel = _resultChannel;
#endif

            return base.FinishedLaunching(app, options);
        }

        [Export("getTestResults")]
        public NSString GetTestResults()
        {
            NSString results = null;

            try
            {
#if __NATIVE_DEPENDENCIES_EXIST__
                var serializedResults = _resultChannel.GetResults();
                if (serializedResults != null) results = new NSString(serializedResults);
#endif
            }
            catch (Exception ex)
            {
                CoreFoundation.OSLog.Default.Log(CoreFoundation.OSLogLevel.Error, ex.Message);
            }

            return results;
        }
    }

#if __NATIVE_DEPENDENCIES_EXIST__
    public class OnnxRuntimeResultChannel : ITestListener, IResultChannel
    {
        TestResultProcessor _resultProcessor;

        public string GetResults()
            => _resultProcessor.GetSerializedResults();

        public Task CloseChannel()
            => Task.CompletedTask;

        public Task<bool> OpenChannel(string message = null)
        {
            _resultProcessor = new TestResultProcessor();
            return Task.FromResult(true);
        }

        public void RecordResult(TestResultViewModel result)
            => _resultProcessor?.RecordResult(result.TestResultMessage, result.TestCase.TestCase, GetTestOutcomeFromTestState(result.TestCase.Result));

        TestOutcome GetTestOutcomeFromTestState(TestState state)
        {
            switch (state)
            {
                case TestState.Failed:
                    return TestOutcome.Failed;
                case TestState.NotRun:
                    return TestOutcome.NotRun;
                case TestState.Passed:
                    return TestOutcome.Passed;
                case TestState.Skipped:
                    return TestOutcome.Skipped;
                default:
                    throw new System.NotImplementedException();
            }
        }
    }
#endif
}