Chromium Code Reviews| Index: chrome/test/data/extensions/api_test/webnavigation/framework.js |
| diff --git a/chrome/test/data/extensions/api_test/webnavigation/framework.js b/chrome/test/data/extensions/api_test/webnavigation/framework.js |
| index b45db890d02588737189ddd203062f06664e8c0f..cf5a58cae0f15ceda6c2c3f0ca45a9684bb7274c 100644 |
| --- a/chrome/test/data/extensions/api_test/webnavigation/framework.js |
| +++ b/chrome/test/data/extensions/api_test/webnavigation/framework.js |
| @@ -2,7 +2,9 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +var deepEq = chrome.test.checkDeepEq; |
| var expectedEventData; |
| +var expectedEventOrder; |
| var capturedEventData; |
| var nextFrameId; |
| var frameIds; |
| @@ -10,9 +12,18 @@ var nextTabId; |
| var tabIds; |
| var initialized = false; |
| -function expect(data) { |
| +// data: array of expected events, each one is a dictionary: |
| +// { label: "<unique identifier>", |
| +// event: "<webnavigation event type>", |
| +// details: { <expected details of the event> } |
| +// } |
| +// order: an array of sequences, e.g. [ ["a", "b", "c"], ["d", "e"] ] means that |
| +// event with label "a" needs to occur before event with label "b". The |
| +// relative order of "a" and "d" does not matter. |
| +function expect(data, order) { |
| expectedEventData = data; |
| capturedEventData = []; |
| + expectedEventOrder = order; |
| nextFrameId = 1; |
| frameIds = {}; |
| nextTabId = 0; |
| @@ -24,8 +35,35 @@ function checkExpectations() { |
| if (capturedEventData.length < expectedEventData.length) { |
| return; |
| } |
| - chrome.test.assertEq(JSON.stringify(expectedEventData), |
| - JSON.stringify(capturedEventData)); |
| + if (capturedEventData.length > expectedEventData.length) { |
| + chrome.test.fail("Recorded too many events. " + |
| + JSON.stringify(capturedEventData)); |
| + } |
| + // We have ensured that capturedEventData contains exactly the same elements |
| + // as expectedEventData. Now we need to verify the ordering. |
| + // Step 1: build positions such that |
| + // position[<event-label>]=<position of this event in capturedEventData> |
| + var curPos = 0; |
| + var positions = {}; |
| + capturedEventData.forEach(function (event) { |
| + chrome.test.assertTrue(event.hasOwnProperty("label")); |
| + positions[event.label] = curPos; |
| + curPos++; |
| + }); |
| + // Step 2: check that elements arrived in correct order |
| + expectedEventOrder.forEach(function (order) { |
| + var previousLabel = undefined; |
| + order.forEach(function (label) { |
| + if (previousLabel === undefined) { |
| + previousLabel = label; |
| + return; |
| + } |
| + chrome.test.assertTrue(positions[previousLabel] < positions[label], |
| + "Event " + previousLabel + " is supposed to arrive before " + |
| + label + "."); |
| + previousLabel = label; |
| + }); |
| + }); |
| chrome.test.succeed(); |
| } |
| @@ -62,7 +100,24 @@ function captureEvent(name, details) { |
| } |
| details.sourceTabId = tabIds[details.sourceTabId]; |
| } |
| - capturedEventData.push([name, details]); |
| + |
| + // find |details| in expectedEventData |
| + var found = false; |
| + var label = undefined; |
| + expectedEventData.forEach(function (exp) { |
| + if (deepEq(exp.event, name) && deepEq(exp.details, details)) { |
| + if (!found) { |
| + found = true; |
| + label = exp.label; |
| + exp.event = undefined; |
| + } |
| + } |
| + }); |
| + if (!found) { |
| + chrome.test.fail("Received unexpected event '" + name + "':" + |
| + JSON.stringify(details)); |
| + } |
| + capturedEventData.push({label: label, event: name, details: details}); |
| checkExpectations(); |
| } |
| @@ -99,3 +154,26 @@ function initListeners() { |
| captureEvent("onErrorOccurred", details); |
| }); |
| } |
| + |
| +// Returns the usual order of navigation events. |
| +function navigationOrder(prefix) { |
|
Matt Perry
2011/09/09 19:28:15
great idea!
|
| + return [ prefix + "onBeforeNavigate", |
| + prefix + "onCommitted", |
| + prefix + "onDOMContentLoaded", |
| + prefix + "onCompleted" ]; |
| +} |
| + |
| +// Returns the constraints expressing that a frame is an iframe of another |
| +// frame. |
| +function isIFrameOf(iframe, main_frame) { |
| + return [ main_frame + "onCommitted", |
| + iframe + "onBeforeNavigate", |
| + main_frame + "onDOMContentLoaded", |
| + iframe + "onCompleted", |
| + main_frame + "onCompleted" ]; |
| +} |
| + |
| +// Returns the constraint expressing that a frame was loaded by another. |
| +function isLoadedBy(target, source) { |
| + return [ source + "onDOMContentLoaded", target + "onBeforeNavigate"]; |
| +} |