Bug 1394632: Fix await race condition which could cause timeouts in layout/inspector/tests/chrome/test_parseStyleSheetObservers.html. draft
authorBrad Werth <bwerth@mozilla.com>
Mon, 28 Aug 2017 18:26:34 -0700
changeset 654633 8f250126f455bc7436b51ac779ae991f12463ca6
parent 654351 e2efa420beb1a578c7350ba925c82230da6b1267
child 728609 1629bc7ed9dc711c8bdc09832316951ea8e94d14
push id76622
push userbwerth@mozilla.com
push dateTue, 29 Aug 2017 02:09:01 +0000
bugs1394632
milestone57.0a1
Bug 1394632: Fix await race condition which could cause timeouts in layout/inspector/tests/chrome/test_parseStyleSheetObservers.html. MozReview-Commit-ID: WGdLNznpcW
layout/inspector/tests/chrome/test_parseStyleSheetObservers.html
--- a/layout/inspector/tests/chrome/test_parseStyleSheetObservers.html
+++ b/layout/inspector/tests/chrome/test_parseStyleSheetObservers.html
@@ -60,34 +60,41 @@ function test1Result() {
 // Test 2: Import rules should be deferred until sheet is actually loaded.
 // We'll test this by parsing an import rule followed by style rule, then
 // ensuring that the event for the style rule lands first.
 // This test is constructed with async functions so we can await an event that
 // might take awhile to arrive (but is guaranteed to arrive, per spec).
 let foundImport = false;
 let foundStyle = false;
 let styleFirstAddProcessor = function(event) {
-  info("styleFirstAddProcessor: called");
+  info("styleFirstAddProcessor: called with event "+ event.rule.cssText);
   if (event.rule.type == CSSRule.IMPORT_RULE) {
     foundImport = true;
   } else if (event.rule.type == CSSRule.STYLE_RULE) {
     foundStyle = true;
     is(foundImport, false, "Test 2: The style rule arrived before the import rule.");
   }
-}
+  return foundImport;
+};
 
 async function test2Setup() {
   info("test2Setup: called");
 
-  DOMUtils.parseStyleSheet(sheet, "@import url('imported_no_op.css'); p { color: purple; }");
+  // Create a Promise to watch for two StyleRuleAdded events. The first invocation should
+  // be the style rule, and the second should be the import rule. We use the same processor
+  // for both events, but the processor will only return true (completing the Promise) when
+  // the import rule has been processed.
+  let gotAllStyleRuleAddedEvents = ContentTaskUtils.waitForEvent(document,
+    "StyleRuleAdded", true, styleFirstAddProcessor);
+
+  DOMUtils.parseStyleSheet(sheet, "@import url('imported_no_op.css'); p {color: purple;}");
   is(sheet.cssRules.length, 2, "Test 2: Stylesheet now has 2 rules.");
 
-  // Await and then process the 2 events we expect to arrive.
-  styleFirstAddProcessor(await ContentTaskUtils.waitForEvent(document, "StyleRuleAdded", true));
-  styleFirstAddProcessor(await ContentTaskUtils.waitForEvent(document, "StyleRuleAdded", true));
+  // Await and then process the events we expect to arrive.
+  await gotAllStyleRuleAddedEvents;
 
   is(foundStyle, true, "Test 2: Got the style rule.");
   is(foundImport, true, "Test 2: Got the import rule.");
 
   SimpleTest.finish();
 }
 </script>
 </body>