1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Common js for prerender loaders; defines the helper functions that put
6 // event handlers on prerenders and track the events for browser tests.
8 // TODO(gavinp): Put more common loader logic in here.
10 // Currently only errors with the ordering of Prerender events are caught.
11 var hadPrerenderEventErrors
= false;
13 var receivedPrerenderStartEvents
= [];
14 var receivedPrerenderLoadEvents
= [];
15 var receivedPrerenderStopEvents
= [];
17 function PrerenderStartHandler(index
) {
18 if (receivedPrerenderStartEvents
[index
] ||
19 receivedPrerenderLoadEvents
[index
] ||
20 receivedPrerenderStopEvents
[index
]) {
21 hadPrerenderEventErrors
= true;
24 receivedPrerenderStartEvents
[index
] = true;
27 function PrerenderLoadHandler(index
) {
28 if (!receivedPrerenderStartEvents
[index
] ||
29 receivedPrerenderStopEvents
[index
]) {
30 hadPrerenderEventErrors
= true;
33 if (!receivedPrerenderLoadEvents
[index
])
34 receivedPrerenderLoadEvents
[index
] = 0;
35 receivedPrerenderLoadEvents
[index
]++;
38 function PrerenderStopHandler(index
) {
39 if (!receivedPrerenderStartEvents
[index
] ||
40 receivedPrerenderStopEvents
[index
]) {
41 hadPrerenderEventErrors
= true;
44 receivedPrerenderStopEvents
[index
] = true;
47 function AddEventHandlersToLinkElement(link
, index
) {
48 link
.addEventListener('webkitprerenderstart',
49 PrerenderStartHandler
.bind(null, index
), false);
50 link
.addEventListener('webkitprerenderload',
51 PrerenderLoadHandler
.bind(null, index
), false);
52 link
.addEventListener('webkitprerenderstop',
53 PrerenderStopHandler
.bind(null, index
), false);
56 function AddPrerender(url
, index
) {
57 var link
= document
.createElement('link');
58 link
.rel
= 'prerender';
60 AddEventHandlersToLinkElement(link
, index
);
61 document
.body
.appendChild(link
);
66 document
.getElementById('toClick').dispatchEvent(new MouseEvent('click', {
74 function ClickTarget() {
75 var eventObject
= new MouseEvent('click', {
81 document
.getElementById('toClickTarget').dispatchEvent(eventObject
);
84 function ShiftClick() {
85 document
.getElementById('toClick').dispatchEvent(new MouseEvent('click', {
94 function CtrlClick() {
95 document
.getElementById('toClick').dispatchEvent(new MouseEvent('click', {
104 function CtrlShiftClick() {
105 document
.getElementById('toClick').dispatchEvent(new MouseEvent('click', {
115 function MetaClick() {
116 document
.getElementById('toClick').dispatchEvent(new MouseEvent('click', {
125 function MetaShiftClick() {
126 document
.getElementById('toClick').dispatchEvent(new MouseEvent('click', {
136 function WindowOpen() {
137 window
.open(document
.getElementById('toClick').href
);