~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Analysis/Profiler/Frontend/AddIn/Src/ProfilerTestRunner.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Diagnostics;
 
7
using System.Linq;
 
8
using ICSharpCode.Core;
 
9
using ICSharpCode.Profiler.AddIn;
 
10
using ICSharpCode.Profiler.Controller.Data;
 
11
using ICSharpCode.SharpDevelop;
 
12
using ICSharpCode.SharpDevelop.Dom;
 
13
using ICSharpCode.SharpDevelop.Gui;
 
14
using ICSharpCode.UnitTesting;
 
15
 
 
16
namespace ICSharpCode.Profiler.AddIn
 
17
{
 
18
        public class ProfilerTestRunner : TestRunnerBase
 
19
        {
 
20
                ProfilerRunner runner;
 
21
                UnitTestingOptions options = new UnitTestingOptions();
 
22
                TestResultsMonitor testResultsMonitor;
 
23
                
 
24
                public ProfilerTestRunner()
 
25
                {
 
26
                }
 
27
                
 
28
                public override void Start(SelectedTests selectedTests)
 
29
                {
 
30
                        ProcessStartInfo startInfo = GetProcessStartInfo(selectedTests);
 
31
                        Start(startInfo, selectedTests);
 
32
                }
 
33
                
 
34
                protected override ProcessStartInfo GetProcessStartInfo(SelectedTests selectedTests)
 
35
                {
 
36
                        NUnitConsoleApplication app = new NUnitConsoleApplication(selectedTests, options);
 
37
                        testResultsMonitor = new TestResultsMonitor();
 
38
                        app.Results = testResultsMonitor.FileName;
 
39
                        return app.GetProcessStartInfo();
 
40
                }
 
41
                
 
42
                void Start(ProcessStartInfo startInfo, SelectedTests selectedTests)
 
43
                {
 
44
                        LogCommandLine(startInfo);
 
45
                        
 
46
                        string path = selectedTests.Project.GetSessionFileName();
 
47
                        
 
48
                        LoggingService.Info("starting profiler...");
 
49
                        
 
50
                        runner = new ProfilerRunner(startInfo, true, new UnitTestWriter(new ProfilingDataSQLiteWriter(path), GetUnitTestNames(selectedTests).ToArray()));
 
51
                        
 
52
                        runner.RunFinished += delegate {
 
53
                                WorkbenchSingleton.SafeThreadCall(() => FileService.OpenFile(path));
 
54
                                AfterFinish(selectedTests, path);
 
55
                        };
 
56
                        
 
57
                        testResultsMonitor.TestFinished += OnTestFinished;
 
58
                        testResultsMonitor.Start();
 
59
                        runner.Run();
 
60
                }
 
61
                                
 
62
                IEnumerable<string> GetUnitTestNames(SelectedTests selectedTests)
 
63
                {
 
64
                        IProjectContent content = ParserService.GetProjectContent(selectedTests.Project);
 
65
                        
 
66
                        if (selectedTests.Class == null) {
 
67
                                var testClasses = content.Classes
 
68
                                        .Where(c => c.Attributes.Any(a => a.AttributeType.FullyQualifiedName == "NUnit.Framework.TestFixtureAttribute"));
 
69
                                return testClasses
 
70
                                        .SelectMany(c2 => c2.Methods)
 
71
                                        .Where(m => m.Attributes.Any(a2 => a2.AttributeType.FullyQualifiedName == "NUnit.Framework.TestAttribute"))
 
72
                                        .Select(m2 => m2.FullyQualifiedName);
 
73
                        }
 
74
                        
 
75
                        if (selectedTests.Method == null) {
 
76
                                return content.Classes
 
77
                                        .Where(c => c.FullyQualifiedName == selectedTests.Class.DotNetName).First().Methods
 
78
                                        .Where(m => m.Attributes.Any(a2 => a2.AttributeType.FullyQualifiedName == "NUnit.Framework.TestAttribute"))
 
79
                                        .Select(m2 => m2.FullyQualifiedName);
 
80
                        }
 
81
                        
 
82
                        return new[] { selectedTests.Class.DotNetName + "." + selectedTests.Method.Name };
 
83
                }
 
84
                
 
85
                void AfterFinish(SelectedTests selectedTests, string path)
 
86
                {
 
87
                        selectedTests.Project.AddSessionToProject(path);
 
88
                        OnAllTestsFinished(this, new EventArgs());
 
89
                        LoggingService.Info("shutting profiler down...");
 
90
                }
 
91
                
 
92
                public override void Stop()
 
93
                {
 
94
                        if (this.runner != null && this.runner.Profiler.IsRunning) {
 
95
                                LoggingService.Info("stopping profiler...");
 
96
                                runner.Stop();
 
97
                        }
 
98
                        
 
99
                        if (testResultsMonitor != null) {
 
100
                                testResultsMonitor.Stop();
 
101
                                testResultsMonitor.Read();
 
102
                                testResultsMonitor = null;
 
103
                        }
 
104
                }
 
105
                
 
106
                public override void Dispose()
 
107
                {
 
108
                        Stop();
 
109
                }
 
110
        }
 
111
}