Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / chrome / test / data / prerender / prerender_events_common.js
blob6d125e1e0a20ef9ee82fe263d2b282db2c47c048
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 receivedPrerenderEvents = {
14 'webkitprerenderstart': [],
15 'webkitprerenderdomcontentloaded': [],
16 'webkitprerenderload': [],
17 'webkitprerenderstop': [],
19 // A list of callbacks to be called on every prerender event. Each callback
20 // returns true if it should never be called again, or false to remain in the
21 // list and be called on future events. These are used to implement
22 // WaitForPrerenderEventCount.
23 var prerenderEventCallbacks = [];
25 function GetPrerenderEventCount(index, type) {
26 return receivedPrerenderEvents[type][index] || 0;
29 function PrerenderEventHandler(index, ev) {
30 // Check for errors.
31 if (ev.type == 'webkitprerenderstart') {
32 // No event may preceed start.
33 if (GetPrerenderEventCount(index, 'webkitprerenderstart') ||
34 GetPrerenderEventCount(index, 'webkitprerenderdomcontentloaded') ||
35 GetPrerenderEventCount(index, 'webkitprerenderload') ||
36 GetPrerenderEventCount(index, 'webkitprerenderstop')) {
37 hadPrerenderEventErrors = true;
39 } else {
40 // There may be multiple load or domcontentloaded events, but they must not
41 // come after start and must come before stop. And there may be at most one
42 // start. Note that stop may be delivered without any load events.
43 if (!GetPrerenderEventCount(index, 'webkitprerenderstart') ||
44 GetPrerenderEventCount(index, 'webkitprerenderstop')) {
45 hadPrerenderEventErrors = true;
49 // Update count.
50 receivedPrerenderEvents[ev.type][index] =
51 (receivedPrerenderEvents[ev.type][index] || 0) + 1;
53 // Run all callbacks. Remove the ones that are done.
54 prerenderEventCallbacks = prerenderEventCallbacks.filter(function(callback) {
55 return !callback();
56 });
59 // Calls |callback| when at least |count| instances of event |type| have been
60 // observed for prerender |index|.
61 function WaitForPrerenderEventCount(index, type, count, callback) {
62 var checkCount = function() {
63 if (GetPrerenderEventCount(index, type) >= count) {
64 callback();
65 return true;
67 return false;
69 if (!checkCount())
70 prerenderEventCallbacks.push(checkCount);
73 function AddEventHandlersToLinkElement(link, index) {
74 link.addEventListener('webkitprerenderstart',
75 PrerenderEventHandler.bind(null, index), false);
76 link.addEventListener('webkitprerenderdomcontentloaded',
77 PrerenderEventHandler.bind(null, index), false);
78 link.addEventListener('webkitprerenderload',
79 PrerenderEventHandler.bind(null, index), false);
80 link.addEventListener('webkitprerenderstop',
81 PrerenderEventHandler.bind(null, index), false);
84 function AddPrerender(url, index) {
85 var link = document.createElement('link');
86 link.id = 'prerenderElement' + index;
87 link.rel = 'prerender';
88 link.href = url;
89 AddEventHandlersToLinkElement(link, index);
90 document.body.appendChild(link);
91 return link;
94 function RemoveLinkElement(index) {
95 var link = document.getElementById('prerenderElement' + index);
96 link.parentElement.removeChild(link);
99 function ExtractGetParameterBadlyAndInsecurely(param, defaultValue) {
100 var re = RegExp('[&?]' + param + '=([^&?#]*)');
101 var result = re.exec(document.location);
102 if (result)
103 return result[1];
104 return defaultValue;
107 function AddAnchor(href, target) {
108 var a = document.createElement('a');
109 a.href = href;
110 if (target)
111 a.target = target;
112 document.body.appendChild(a);
113 return a;
116 function Click(url) {
117 AddAnchor(url).dispatchEvent(new MouseEvent('click', {
118 view: window,
119 bubbles: true,
120 cancelable: true,
121 detail: 1
122 }));
125 function ClickTarget(url) {
126 var eventObject = new MouseEvent('click', {
127 view: window,
128 bubbles: true,
129 cancelable: true,
130 detail: 1
132 AddAnchor(url, '_blank').dispatchEvent(eventObject);
135 function ClickPing(url, pingUrl) {
136 var a = AddAnchor(url);
137 a.ping = pingUrl;
138 a.dispatchEvent(new MouseEvent('click', {
139 view: window,
140 bubbles: true,
141 cancelable: true,
142 detail: 1
143 }));
146 function ShiftClick(url) {
147 AddAnchor(url).dispatchEvent(new MouseEvent('click', {
148 view: window,
149 bubbles: true,
150 cancelable: true,
151 detail: 1,
152 shiftKey: true
153 }));
156 function CtrlClick(url) {
157 AddAnchor(url).dispatchEvent(new MouseEvent('click', {
158 view: window,
159 bubbles: true,
160 cancelable: true,
161 detail: 1,
162 ctrlKey: true
163 }));
166 function CtrlShiftClick(url) {
167 AddAnchor(url).dispatchEvent(new MouseEvent('click', {
168 view: window,
169 bubbles: true,
170 cancelable: true,
171 detail: 1,
172 ctrlKey: true,
173 shiftKey: true
174 }));
177 function MetaClick(url) {
178 AddAnchor(url).dispatchEvent(new MouseEvent('click', {
179 view: window,
180 bubbles: true,
181 cancelable: true,
182 detail: 1,
183 metaKey: true
184 }));
187 function MetaShiftClick(url) {
188 AddAnchor(url).dispatchEvent(new MouseEvent('click', {
189 view: window,
190 bubbles: true,
191 cancelable: true,
192 detail: 1,
193 metaKey: true,
194 shiftKey: true
195 }));
198 function WindowOpen(url) {
199 window.open(url);