Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: packages/analyzer/test/src/task/html_work_manager_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library test.src.task.html_work_manager_test;
6
7 import 'package:analyzer/src/context/cache.dart';
8 import 'package:analyzer/src/generated/engine.dart'
9 show
10 AnalysisEngine,
11 AnalysisErrorInfo,
12 AnalysisErrorInfoImpl,
13 CacheState,
14 ChangeNoticeImpl,
15 InternalAnalysisContext;
16 import 'package:analyzer/src/generated/error.dart'
17 show AnalysisError, HtmlErrorCode;
18 import 'package:analyzer/src/generated/java_engine.dart' show CaughtException;
19 import 'package:analyzer/src/generated/source.dart';
20 import 'package:analyzer/src/task/html.dart';
21 import 'package:analyzer/src/task/html_work_manager.dart';
22 import 'package:analyzer/task/dart.dart';
23 import 'package:analyzer/task/general.dart';
24 import 'package:analyzer/task/html.dart';
25 import 'package:analyzer/task/model.dart';
26 import 'package:typed_mock/typed_mock.dart';
27 import 'package:unittest/unittest.dart';
28
29 import '../../generated/test_support.dart';
30 import '../../reflective_tests.dart';
31 import '../../utils.dart';
32
33 main() {
34 initializeTestEnvironment();
35 runReflectiveTests(HtmlWorkManagerTest);
36 }
37
38 @reflectiveTest
39 class HtmlWorkManagerTest {
40 InternalAnalysisContext context = new _InternalAnalysisContextMock();
41 AnalysisCache cache;
42 HtmlWorkManager manager;
43
44 CaughtException caughtException = new CaughtException(null, null);
45
46 Source source1 = new TestSource('1.html');
47 Source source2 = new TestSource('2.html');
48 Source source3 = new TestSource('3.html');
49 Source source4 = new TestSource('4.html');
50 CacheEntry entry1;
51 CacheEntry entry2;
52 CacheEntry entry3;
53 CacheEntry entry4;
54
55 void expect_sourceQueue(List<Source> sources) {
56 expect(manager.sourceQueue, unorderedEquals(sources));
57 }
58
59 void setUp() {
60 cache = context.analysisCache;
61 manager = new HtmlWorkManager(context);
62 entry1 = context.getCacheEntry(source1);
63 entry2 = context.getCacheEntry(source2);
64 entry3 = context.getCacheEntry(source3);
65 entry4 = context.getCacheEntry(source4);
66 }
67
68 void test_applyChange_add() {
69 // add source1
70 manager.applyChange([source1], [], []);
71 expect_sourceQueue([source1]);
72 // add source2
73 manager.applyChange([source2], [], []);
74 expect_sourceQueue([source1, source2]);
75 }
76
77 void test_applyChange_add_duplicate() {
78 // add source1
79 manager.applyChange([source1], [], []);
80 expect_sourceQueue([source1]);
81 // add source1 again
82 manager.applyChange([source1], [], []);
83 expect_sourceQueue([source1]);
84 }
85
86 void test_applyChange_change() {
87 // change source1
88 manager.applyChange([], [source1], []);
89 expect_sourceQueue([source1]);
90 }
91
92 void test_applyChange_change_afterAdd() {
93 manager.applyChange([source1, source2], [], []);
94 // change source1
95 manager.applyChange([], [source1], []);
96 expect_sourceQueue([source1, source2]);
97 }
98
99 void test_applyChange_remove() {
100 manager.applyChange([source1, source2], [], []);
101 // remove source1
102 manager.applyChange([], [], [source1]);
103 expect_sourceQueue([source2]);
104 // remove source2
105 manager.applyChange([], [], [source2]);
106 expect_sourceQueue([]);
107 // remove source3
108 manager.applyChange([], [], [source3]);
109 expect_sourceQueue([]);
110 }
111
112 void test_applyPriorityTargets() {
113 when(context.shouldErrorsBeAnalyzed(source2, null)).thenReturn(true);
114 when(context.shouldErrorsBeAnalyzed(source3, null)).thenReturn(true);
115 manager.priorityResultQueue.add(new TargetedResult(source1, HTML_ERRORS));
116 manager.priorityResultQueue.add(new TargetedResult(source2, HTML_ERRORS));
117 // -source1 +source3
118 manager.applyPriorityTargets([source2, source3]);
119 expect(
120 manager.priorityResultQueue,
121 unorderedEquals([
122 new TargetedResult(source2, HTML_ERRORS),
123 new TargetedResult(source3, HTML_ERRORS)
124 ]));
125 // get next request
126 TargetedResult request = manager.getNextResult();
127 expect(request.target, source2);
128 expect(request.result, HTML_ERRORS);
129 }
130
131 void test_getErrors_fullList() {
132 AnalysisError error1 =
133 new AnalysisError(source1, 1, 0, HtmlErrorCode.PARSE_ERROR, ['']);
134 AnalysisError error2 =
135 new AnalysisError(source1, 2, 0, HtmlErrorCode.PARSE_ERROR, ['']);
136 entry1.setValue(HTML_DOCUMENT_ERRORS, <AnalysisError>[error1], []);
137
138 DartScript script = new DartScript(source1, []);
139 entry1.setValue(DART_SCRIPTS, [script], []);
140 CacheEntry scriptEntry = context.getCacheEntry(script);
141 scriptEntry.setValue(DART_ERRORS, [error2], []);
142
143 List<AnalysisError> errors = manager.getErrors(source1);
144 expect(errors, unorderedEquals([error1, error2]));
145 }
146
147 void test_getErrors_partialList() {
148 AnalysisError error1 =
149 new AnalysisError(source1, 1, 0, HtmlErrorCode.PARSE_ERROR, ['']);
150 AnalysisError error2 =
151 new AnalysisError(source1, 2, 0, HtmlErrorCode.PARSE_ERROR, ['']);
152 entry1.setValue(HTML_DOCUMENT_ERRORS, <AnalysisError>[error1, error2], []);
153
154 List<AnalysisError> errors = manager.getErrors(source1);
155 expect(errors, unorderedEquals([error1, error2]));
156 }
157
158 void test_getNextResult_hasNormal_firstIsError() {
159 entry1.setErrorState(caughtException, [HTML_ERRORS]);
160 manager.sourceQueue.addAll([source1, source2]);
161 TargetedResult request = manager.getNextResult();
162 expect(request.target, source2);
163 expect(request.result, HTML_ERRORS);
164 // source1 is out, source2 is waiting
165 expect_sourceQueue([source2]);
166 }
167
168 void test_getNextResult_hasNormal_firstIsInvalid() {
169 entry1.setState(HTML_ERRORS, CacheState.INVALID);
170 manager.sourceQueue.addAll([source1, source2]);
171 TargetedResult request = manager.getNextResult();
172 expect(request.target, source1);
173 expect(request.result, HTML_ERRORS);
174 // no changes until computed
175 expect_sourceQueue([source1, source2]);
176 }
177
178 void test_getNextResult_hasNormal_firstIsValid() {
179 entry1.setValue(HTML_ERRORS, [], []);
180 manager.sourceQueue.addAll([source1, source2]);
181 TargetedResult request = manager.getNextResult();
182 expect(request.target, source2);
183 expect(request.result, HTML_ERRORS);
184 // source1 is out, source2 is waiting
185 expect_sourceQueue([source2]);
186 }
187
188 void test_getNextResult_hasNormalAndPriority() {
189 entry1.setState(HTML_ERRORS, CacheState.INVALID);
190 manager.sourceQueue.addAll([source1, source2]);
191 manager.addPriorityResult(source3, HTML_ERRORS);
192
193 TargetedResult request = manager.getNextResult();
194 expect(request.target, source3);
195 expect(request.result, HTML_ERRORS);
196 // no changes until computed
197 expect_sourceQueue([source1, source2]);
198 }
199
200 void test_getNextResult_hasPriority() {
201 manager.addPriorityResult(source1, HTML_ERRORS);
202 manager.addPriorityResult(source2, HTML_ERRORS);
203 expect(
204 manager.priorityResultQueue,
205 unorderedEquals([
206 new TargetedResult(source1, HTML_ERRORS),
207 new TargetedResult(source2, HTML_ERRORS)
208 ]));
209
210 TargetedResult request = manager.getNextResult();
211 expect(request.target, source1);
212 expect(request.result, HTML_ERRORS);
213 // no changes until computed
214 expect(
215 manager.priorityResultQueue,
216 unorderedEquals([
217 new TargetedResult(source1, HTML_ERRORS),
218 new TargetedResult(source2, HTML_ERRORS)
219 ]));
220 }
221
222 void test_getNextResult_nothingToDo() {
223 TargetedResult request = manager.getNextResult();
224 expect(request, isNull);
225 }
226
227 void test_getNextResultPriority_hasPriority() {
228 manager.addPriorityResult(source1, SOURCE_KIND);
229 expect(manager.getNextResultPriority(), WorkOrderPriority.PRIORITY);
230 }
231
232 void test_getNextResultPriority_hasSource() {
233 manager.sourceQueue.addAll([source1]);
234 expect(manager.getNextResultPriority(), WorkOrderPriority.NORMAL);
235 }
236
237 void test_getNextResultPriority_nothingToDo() {
238 expect(manager.getNextResultPriority(), WorkOrderPriority.NONE);
239 }
240
241 void test_onAnalysisOptionsChanged() {
242 when(context.exists(anyObject)).thenReturn(true);
243 // set cache values
244 entry1.setValue(DART_SCRIPTS, [], []);
245 entry1.setValue(HTML_DOCUMENT, null, []);
246 entry1.setValue(HTML_DOCUMENT_ERRORS, [], []);
247 entry1.setValue(HTML_ERRORS, [], []);
248 entry1.setValue(REFERENCED_LIBRARIES, [], []);
249 // notify
250 manager.onAnalysisOptionsChanged();
251 // Only resolution-based values are invalidated.
252 expect(entry1.getState(DART_SCRIPTS), CacheState.VALID);
253 expect(entry1.getState(HTML_DOCUMENT), CacheState.VALID);
254 expect(entry1.getState(HTML_DOCUMENT_ERRORS), CacheState.VALID);
255 expect(entry1.getState(HTML_ERRORS), CacheState.INVALID);
256 expect(entry1.getState(REFERENCED_LIBRARIES), CacheState.VALID);
257 }
258
259 void test_onResultInvalidated_scheduleInvalidatedLibraries() {
260 // set HTML_ERRORS for source1 and source3
261 entry1.setValue(HTML_ERRORS, [], []);
262 entry3.setValue(HTML_ERRORS, [], []);
263 // invalidate HTML_ERRORS for source1, schedule it
264 entry1.setState(HTML_ERRORS, CacheState.INVALID);
265 expect_sourceQueue([source1]);
266 // invalidate HTML_ERRORS for source3, schedule it
267 entry3.setState(HTML_ERRORS, CacheState.INVALID);
268 expect_sourceQueue([source1, source3]);
269 }
270
271 void test_onSourceFactoryChanged() {
272 when(context.exists(anyObject)).thenReturn(true);
273 // set cache values
274 entry1.setValue(DART_SCRIPTS, [], []);
275 entry1.setValue(HTML_DOCUMENT, null, []);
276 entry1.setValue(HTML_DOCUMENT_ERRORS, [], []);
277 entry1.setValue(HTML_ERRORS, [], []);
278 entry1.setValue(REFERENCED_LIBRARIES, [], []);
279 // notify
280 manager.onSourceFactoryChanged();
281 // Only resolution-based values are invalidated.
282 expect(entry1.getState(DART_SCRIPTS), CacheState.VALID);
283 expect(entry1.getState(HTML_DOCUMENT), CacheState.VALID);
284 expect(entry1.getState(HTML_DOCUMENT_ERRORS), CacheState.VALID);
285 expect(entry1.getState(HTML_ERRORS), CacheState.INVALID);
286 expect(entry1.getState(REFERENCED_LIBRARIES), CacheState.INVALID);
287 }
288
289 void test_resultsComputed_errors() {
290 AnalysisError error1 =
291 new AnalysisError(source1, 1, 0, HtmlErrorCode.PARSE_ERROR, ['']);
292 AnalysisError error2 =
293 new AnalysisError(source1, 2, 0, HtmlErrorCode.PARSE_ERROR, ['']);
294 LineInfo lineInfo = new LineInfo([0]);
295 entry1.setValue(LINE_INFO, lineInfo, []);
296 entry1.setValue(HTML_ERRORS, <AnalysisError>[error1, error2], []);
297 // RESOLVED_UNIT is ready, set errors
298 manager.resultsComputed(source1, {HTML_ERRORS: null});
299 // all of the errors are included
300 ChangeNoticeImpl notice = context.getNotice(source1);
301 expect(notice.errors, unorderedEquals([error1, error2]));
302 expect(notice.lineInfo, lineInfo);
303 }
304 }
305
306 class _InternalAnalysisContextMock extends TypedMock
307 implements InternalAnalysisContext {
308 @override
309 CachePartition privateAnalysisCachePartition;
310
311 @override
312 AnalysisCache analysisCache;
313
314 Map<Source, ChangeNoticeImpl> _pendingNotices = <Source, ChangeNoticeImpl>{};
315
316 _InternalAnalysisContextMock() {
317 privateAnalysisCachePartition = new UniversalCachePartition(this);
318 analysisCache = new AnalysisCache([privateAnalysisCachePartition]);
319 }
320
321 @override
322 CacheEntry getCacheEntry(AnalysisTarget target) {
323 CacheEntry entry = analysisCache.get(target);
324 if (entry == null) {
325 entry = new CacheEntry(target);
326 analysisCache.put(entry);
327 }
328 return entry;
329 }
330
331 @override
332 AnalysisErrorInfo getErrors(Source source) {
333 String name = source.shortName;
334 List<AnalysisError> errors = AnalysisError.NO_ERRORS;
335 if (AnalysisEngine.isDartFileName(name) || source is DartScript) {
336 errors = getCacheEntry(source).getValue(DART_ERRORS);
337 } else if (AnalysisEngine.isHtmlFileName(name)) {
338 errors = getCacheEntry(source).getValue(HTML_ERRORS);
339 }
340 return new AnalysisErrorInfoImpl(
341 errors, getCacheEntry(source).getValue(LINE_INFO));
342 }
343
344 @override
345 ChangeNoticeImpl getNotice(Source source) {
346 return _pendingNotices.putIfAbsent(
347 source, () => new ChangeNoticeImpl(source));
348 }
349
350 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
351 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/src/task/html_test.dart ('k') | packages/analyzer/test/src/task/incremental_element_builder_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698