forked from mono/monodevelop
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathIde.cs
More file actions
251 lines (213 loc) · 8.66 KB
/
Copy pathIde.cs
File metadata and controls
251 lines (213 loc) · 8.66 KB
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Ide.cs
//
// Author:
// Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Threading;
using System.Linq;
using MonoDevelop.Core;
using MonoDevelop.Core.Instrumentation;
using MonoDevelop.Ide.Commands;
using MonoDevelop.Components.AutoTest;
using NUnit.Framework;
using System.Collections.Generic;
namespace UserInterfaceTests
{
public static class Ide
{
static AutoTestClientSession Session {
get { return TestService.Session; }
}
public static void OpenFile (FilePath file)
{
Session.RunAndWaitForTimer (() => Session.GlobalInvoke ("MonoDevelop.Ide.IdeApp.Workbench.OpenDocument", (string)file, true), "Ide.Shell.DocumentOpened");
Assert.AreEqual (file, GetActiveDocumentFilename ());
}
public static void CloseAll (bool exit = true)
{
var isDirty = (bool)Session.GetGlobalValue ("MonoDevelop.Ide.IdeApp.Workbench.DocumentsDirty");
if (isDirty) {
Session.RunAndWaitForTimer (() => {
Session.ExecuteCommand (FileCommands.SaveAll);
}, "Ide.Shell.SaveAll");
}
var workspaceOpen = (bool)Session.GetGlobalValue ("MonoDevelop.Ide.IdeApp.Workspace.IsOpen");
if (workspaceOpen) {
Session.RunAndWaitForTimer (() => {
Session.ExecuteCommand (FileCommands.CloseWorkspace);
}, "Ide.Shell.CloseWorkspace");
}
if (exit)
Session.ExitApp ();
}
public static FilePath GetActiveDocumentFilename ()
{
return Session.GetGlobalValue<FilePath> ("MonoDevelop.Ide.IdeApp.Workbench.ActiveDocument.FileName");
}
public static bool BuildSolution (bool isPass = true, int timeoutInSecs = 360)
{
Session.RunAndWaitForTimer (() => Session.ExecuteCommand (ProjectCommands.BuildSolution), "Ide.Shell.ProjectBuilt", timeoutInSecs * 1000);
return isPass == Workbench.IsBuildSuccessful (timeoutInSecs);
}
public static void WaitUntil (Func<bool> done, int timeout = 20000, int pollStep = 200, Func<string> timeoutMessage = null)
{
try {
PollTimer (timeout, pollStep, done);
} catch (TimeoutException) {
// Replace the exception with another one.
var suffix = timeoutMessage != null ? " Message: " + timeoutMessage () : string.Empty;
throw new TimeoutException ("Timed out waiting for Function: " + done.Method.Name + suffix);
}
}
public static bool ClickButtonAlertDialog (string buttonText)
{
if (Platform.IsMac) {
Ide.WaitUntil (() => Session.Query (c => c.Marked ("Visual Studio").Marked ("AppKit.NSPanel")).Any ());
return Session.ClickElement (c => c.Marked ("AppKit.NSButton").Text (buttonText));
} else {
Ide.WaitUntil (() => Session.Query (c => c.Window ().Marked ("MonoDevelop.Ide.Gui.Dialogs.GtkAlertDialog")).Any ());
return Session.ClickElement (c => c.Window ().Marked ("MonoDevelop.Ide.Gui.Dialogs.GtkAlertDialog").Children ().Button ().Text (buttonText));
}
}
public static void RunAndWaitForTimer (Action action, string counter, int timeout = 20000)
{
var c = Session.GetGlobalValue<TimerCounter> (counter);
var tt = c.TotalTime;
action ();
WaitUntil (() => c.TotalTime > tt, timeout,
timeoutMessage: () => "Counter:" + counter + " T1:" + c.TotalTime + " T2:" + tt + " Timeout:" + timeout);
}
public readonly static Action WaitForPackageUpdateOrSaved = delegate {
try {
WaitForPackageUpdate ();
} catch (TimeoutException e1) {
try {
WaitForSolutionLoaded ();
} catch (TimeoutException e2) {
throw new TimeoutException (string.Format ("{0}\n{1}", e1.Message, e2.Message), e1);
}
}
};
public readonly static Action EmptyAction = delegate { };
static readonly string[] waitForNuGetMessages = {
"Package updates are available.",
"Packages are up to date.",
"No updates found but warnings were reported.",
"Packages successfully added.",
"Packages successfully updated.",
"Packages added with warnings.",
"Packages updated with warnings."};
public readonly static Action WaitForPackageUpdate = delegate {
WaitForStatusMessage (waitForNuGetMessages, timeoutInSecs: 180, pollStepInSecs: 1);
};
public static void WaitForPackageUpdateExtra (List<string> otherMessages)
{
otherMessages.AddRange (waitForNuGetMessages);
WaitForStatusMessage (otherMessages.ToArray (), timeoutInSecs: 180, pollStepInSecs: 1);
}
public readonly static Action WaitForSolutionCheckedOut = delegate {
WaitForStatusMessage (new [] {"Solution checked out", "Solution Loaded."}, timeoutInSecs: 360, pollStepInSecs: 1);
};
public readonly static Action WaitForSolutionLoaded = delegate {
WaitForStatusMessage (new [] {"Project saved.", "Solution loaded."}, timeoutInSecs: 120, pollStepInSecs: 1);
};
public static void WaitForStatusMessage (string[] statusMessage, int timeoutInSecs = 240, int pollStepInSecs = 1)
{
PollStatusMessage (statusMessage, timeoutInSecs, pollStepInSecs);
}
public static void WaitForNoStatusMessage (string[] statusMessage, int timeoutInSecs = 240, int pollStepInSecs = 1)
{
PollStatusMessage (statusMessage, timeoutInSecs, pollStepInSecs, false);
}
static void PollStatusMessage (string[] statusMessage, int timeoutInSecs, int pollStepInSecs, bool waitForMessage = true)
{
Ide.WaitUntil (() => {
string actualStatusMessage = string.Empty;
try {
actualStatusMessage = Workbench.GetStatusMessage ();
return waitForMessage == (statusMessage.Contains (actualStatusMessage, StringComparer.OrdinalIgnoreCase));
} catch (TimeoutException e) {
throw new TimeoutException (
string.Format ("Timed out. Found status message '{0}'\nand expected one of these:\n\t {1}",
actualStatusMessage, string.Join ("\n\t", statusMessage)), e);
}
},
pollStep: pollStepInSecs * 1000,
timeout: timeoutInSecs * 1000,
timeoutMessage: () => "GetStatusMessage=" + Workbench.GetStatusMessage ());
}
static readonly List<string> globalIgnoreStatusMessages = new List<string> {
"Saving...",
"Restoring packages for solution...",
"Restoring packages before update...",
"Restoring packages for project...",
"Updating packages in solution...",
"Updating packages in project..."
};
public static void WaitForIdeIdle (uint totalTimeoutInSecs = 100, uint idlePeriodInSecs = 1, string[] ignoreMessages = null)
{
var ignoreStatusMessages = globalIgnoreStatusMessages.ToList ();
if (ignoreMessages != null)
ignoreStatusMessages.AddRange (ignoreMessages);
var initialStatusMessage = Workbench.GetStatusMessage (waitForNonEmpty: false);
PollTimer ((int)totalTimeoutInSecs * 1000, (int)idlePeriodInSecs * 1000, () => {
var finalStatusMessage = Workbench.GetStatusMessage (waitForNonEmpty: false);
var isIdle = string.Equals (initialStatusMessage, finalStatusMessage) && !ignoreStatusMessages.Contains (finalStatusMessage);
if (!isIdle)
initialStatusMessage = finalStatusMessage;
return isIdle;
});
}
static void PollTimer (int timeout, int interval, Func<bool> checkIsDone)
{
uint retriesLeft = (uint)Math.Ceiling ((double)timeout / interval);
var resetEvent = new ManualResetEvent (false);
var timer = new System.Timers.Timer {
Interval = interval,
AutoReset = true
};
using (timer)
using (resetEvent) {
bool didTimeout = false;
timer.Elapsed += (sender, e) => {
if (retriesLeft == 0) {
didTimeout = true;
resetEvent.Set ();
return;
}
bool done = checkIsDone ();
if (!done) {
retriesLeft--;
} else
resetEvent.Set ();
};
timer.Start ();
resetEvent.WaitOne ();
timer.Stop ();
if (didTimeout)
throw new TimeoutException ("Timeout waiting for IDE to be ready and idle");
}
}
}
}