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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.progress.util;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.ex.ApplicationManagerEx;
import com.intellij.openapi.progress.*;
import com.intellij.openapi.progress.impl.ProgressManagerImpl;
import com.intellij.openapi.wm.ex.ProgressIndicatorEx;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.Alarm;
import com.intellij.util.containers.DoubleArrayList;
import com.intellij.util.containers.Stack;
import com.intellij.util.ui.UIUtil;
import gnu.trove.TLongArrayList;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author yole
*/
public class ProgressIndicatorTest extends LightPlatformTestCase {
public void testCheckCanceledHasNoStackFrame() {
ProgressIndicatorBase pib = new ProgressIndicatorBase();
pib.cancel();
boolean hadException = false;
try {
pib.checkCanceled();
}
catch(ProcessCanceledException ex) {
hadException = true;
assertTrue("Should have no stackframe", ex.getStackTrace().length == 0);
}
assertTrue("Please restore ProgressIndicatorBase.checkCanceled() check!", hadException);
}
public void testProgressManagerCheckCanceledWorksRightAfterIndicatorBeenCanceled() {
for (int i=0; i<1000;i++) {
final ProgressIndicatorBase indicator = new ProgressIndicatorBase();
ProgressManager.getInstance().runProcess(new Runnable() {
@Override
public void run() {
ProgressManager.checkCanceled();
try {
indicator.cancel();
ProgressManager.checkCanceled();
fail("checkCanceled() must have caught just canceled indicator");
}
catch (ProcessCanceledException ignored) {
}
}
}, indicator);
}
}
private volatile long prevTime;
private volatile long now;
public void testCheckCanceledGranularity() throws InterruptedException {
prevTime = now = 0;
final long warmupEnd = System.currentTimeMillis() + 1000;
final TLongArrayList times = new TLongArrayList();
final long end = warmupEnd + 1000;
ApplicationManagerEx.getApplicationEx().runProcessWithProgressSynchronously(new Runnable() {
@Override
public void run() {
final Alarm alarm = new Alarm(Alarm.ThreadToUse.OWN_THREAD, getTestRootDisposable());
ProgressIndicatorEx indicator = (ProgressIndicatorEx)ProgressIndicatorProvider.getGlobalProgressIndicator();
prevTime = System.currentTimeMillis();
assert indicator != null;
indicator.addStateDelegate(new ProgressIndicatorStub() {
@Override
public void checkCanceled() throws ProcessCanceledException {
now = System.currentTimeMillis();
if (now > warmupEnd) {
int delta = (int)(now - prevTime);
times.add(delta);
}
prevTime = now;
}
});
while (System.currentTimeMillis() < end) {
ProgressManager.checkCanceled();
}
alarm.cancelAllRequests();
}
}, "", false, getProject(), null, "");
long averageDelay = PlatformTestUtil.averageAmongMedians(times.toNativeArray(), 5);
System.out.println("averageDelay = " + averageDelay);
assertTrue(averageDelay < ProgressManagerImpl.CHECK_CANCELED_DELAY_MILLIS*3);
}
public void testProgressIndicatorUtils() throws Throwable {
final AtomicBoolean run = new AtomicBoolean(true);
final AtomicBoolean insideReadAction = new AtomicBoolean();
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
final ProgressIndicatorBase indicator = new ProgressIndicatorBase();
ProgressIndicatorUtils.scheduleWithWriteActionPriority(indicator, new ReadTask() {
@Override
public void computeInReadAction(@NotNull ProgressIndicator indicator) {
insideReadAction.set(true);
while (run.get()) {
ProgressManager.checkCanceled();
}
}
@Override
public void onCanceled(@NotNull ProgressIndicator indicator) {
try {
assertTrue(run.get()); // cancel should happen early
run.set(false);
}
catch (Throwable e) {
exception.set(e);
}
}
});
UIUtil.dispatchAllInvocationEvents();
while (!insideReadAction.get()) {
;
}
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
try {
assertTrue(indicator.isCanceled());
}
catch (Throwable e) {
exception.set(e);
}
}
});
assertFalse(run.get());
if (exception.get() != null) throw exception.get();
}
private static class ProgressIndicatorStub implements ProgressIndicatorEx {
@Override
public void addStateDelegate(@NotNull ProgressIndicatorEx delegate) {
}
@Override
public boolean isModalityEntered() {
return false;
}
@Override
public void finish(@NotNull TaskInfo task) {
}
@Override
public boolean isFinished(@NotNull TaskInfo task) {
return false;
}
@Override
public boolean wasStarted() {
return false;
}
@Override
public void processFinish() {
}
@Override
public void initStateFrom(@NotNull ProgressIndicator indicator) {
}
@NotNull
@Override
public Stack<String> getTextStack() {
return null;
}
@NotNull
@Override
public DoubleArrayList getFractionStack() {
return null;
}
@NotNull
@Override
public Stack<String> getText2Stack() {
return null;
}
@Override
public int getNonCancelableCount() {
return 0;
}
@Override
public void start() {
}
@Override
public void stop() {
}
@Override
public boolean isRunning() {
return false;
}
@Override
public void cancel() {
}
@Override
public boolean isCanceled() {
return false;
}
@Override
public void setText(String text) {
}
@Override
public String getText() {
return null;
}
@Override
public void setText2(String text) {
}
@Override
public String getText2() {
return null;
}
@Override
public double getFraction() {
return 0;
}
@Override
public void setFraction(double fraction) {
}
@Override
public void pushState() {
}
@Override
public void popState() {
}
@Override
public void startNonCancelableSection() {
}
@Override
public void finishNonCancelableSection() {
}
@Override
public boolean isModal() {
return false;
}
@NotNull
@Override
public ModalityState getModalityState() {
return null;
}
@Override
public void setModalityProgress(ProgressIndicator modalityProgress) {
}
@Override
public boolean isIndeterminate() {
return false;
}
@Override
public void setIndeterminate(boolean indeterminate) {
}
@Override
public void checkCanceled() throws ProcessCanceledException {
}
@Override
public boolean isPopupWasShown() {
return false;
}
@Override
public boolean isShowing() {
return false;
}
}
}
|