Chromium Code Reviews| Index: chrome/test/data/extensions/platform_apps/ad_view/ad_network/test_sdk.js |
| diff --git a/chrome/test/data/extensions/platform_apps/ad_view/ad_network/test_sdk.js b/chrome/test/data/extensions/platform_apps/ad_view/ad_network/test_sdk.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..89dec51caef28f9333057e82f0f38ad79ac592b2 |
| --- /dev/null |
| +++ b/chrome/test/data/extensions/platform_apps/ad_view/ad_network/test_sdk.js |
| @@ -0,0 +1,130 @@ |
| +(function( window ) { |
| + "use strict"; |
| + |
| + var document = window.document; |
| + |
| + var ad_urls = [ |
| + 'ads/image315.png', |
| + 'ads/image316.png', |
| + 'ads/image317.png', |
| + 'ads/image318.png', |
| + 'ads/image319.png', |
| + ]; |
| + |
| + // Pick a random ad url |
| + function getAdUrl(index) { |
| + if (typeof index === "undefined") |
| + index = Math.floor(Math.random() * ad_urls.length); |
| + var ad_path = ad_urls[index]; |
| + var prefix_index = location.href.lastIndexOf("/"); |
| + return location.href.substr(0, prefix_index) + "/" + ad_path; |
|
asargent_no_longer_on_chrome
2013/03/11 19:10:14
FYI, we are usually careful about using randomness
rpaquay
2013/03/12 19:56:48
We actually don't need the randomness for tests, s
|
| + } |
| + |
| + // |
| + // Send a message to the embedding app |
| + // |
| + var message_sequence_number = 0; |
| + |
| + function sendAppMessage(source, message, publisher_data, message_data) { |
| + source.postMessage({ |
| + 'sequence_number': message_sequence_number, |
| + 'message': message, |
| + 'publisher_data': publisher_data, |
| + 'data': message_data, |
| + }, "*"); |
| + |
| + message_sequence_number++; |
| + } |
| + |
| + // |
| + // When the img for an ad is loaded into its iframe |
| + // |
| + function onAdLoaded(source, publisher_data) { |
| + // Resize containing iframe to size of image |
| + var iframe = document.getElementById("ad-frame"); |
| + var img = iframe.contentWindow.document.getElementsByTagName("IMG")[0]; |
| + |
| + iframe.style.width = img.width + "px"; |
| + iframe.style.height = img.height + "px"; |
| + |
| + // Size of ad = size of document body + margins |
| + function sint(value) { |
|
asargent_no_longer_on_chrome
2013/03/11 19:10:14
nits: can you use a more meaningful function name
rpaquay
2013/03/12 19:56:48
Done.
|
| + return parseFloat(value.substr(0, value.indexOf("px"))); |
| + } |
| + |
| + var style = document.defaultView.getComputedStyle(document.body, ''); |
| + var elemHeight = sint(style.getPropertyValue('height')); |
| + var elemVerticalMargin = |
| + sint(style.getPropertyValue('margin-top')) + |
| + sint(style.getPropertyValue('margin-bottom')); |
| + |
| + var elemWidth = sint(style.getPropertyValue('width')); |
| + var elemHorizontalMargin = |
| + sint(style.getPropertyValue('margin-left')) + |
| + sint(style.getPropertyValue('margin-right')); |
| + |
| + var adRect = { |
| + height: (elemHeight + elemVerticalMargin) + "px", |
| + width: (elemWidth + elemHorizontalMargin) + "px", |
| + }; |
| + |
| + sendAppMessage(source, "ad-displayed", publisher_data, { |
| + 'ad_size': { |
| + width: adRect.width, |
| + height: adRect.height |
| + }}); |
| + |
| + img.onclick = function() { |
| + sendAppMessage(source, "ad-clicked", publisher_data); |
| + }; |
| + } |
| + |
| + // |
| + // display a random ad in the ad iframe |
| + // |
| + function displayAd(source, publisher_data, ad_index) { |
| + // Remove temporary message since we are about to display an add |
| + var tempDiv = document.getElementById("temp-display"); |
| + if (tempDiv) { |
| + tempDiv.parentNode.removeChild(tempDiv); |
| + } |
| + |
| + // Load image in iframe |
| + var iframe = document.getElementById("ad-frame"); |
| + if (iframe.contentWindow.document.body.firstChild == null) { |
| + var img = iframe.contentWindow.document.createElement("img"); |
| + iframe.contentWindow.document.body.appendChild(img); |
| + } |
| + var img = iframe.contentWindow.document.body.firstChild; |
| + img.onload = function() { onAdLoaded(source, publisher_data); }; |
| + img.src = getAdUrl(ad_index); |
| + iframe.style.visibility = "visible"; |
| + } |
| + |
| + function processAppMessage(source, data) { |
| + if (data.message == "display-ad") { |
| + displayAd(source, data.publisher_data); |
| + } |
| + else if (data.message == "display-first-ad") { |
| + displayAd(source, data.publisher_data, 0/*ad_index*/); |
| + } |
| + else if (data.message == "onloadcommit") { |
| + sendAppMessage(source, "onloadcommit-ack"); |
| + } |
| + } |
| + |
| + function onPostMessage(eventMessage) { |
| + processAppMessage(eventMessage.source, eventMessage.data); |
| + } |
| + |
| + function onDocumentReady() { |
| + document.getElementById("url").textContent = window.location; |
| + } |
| + |
| + // |
| + // Add global event listeners. |
| + // |
| + window.addEventListener("message", onPostMessage, false); |
| + document.addEventListener('DOMContentLoaded', onDocumentReady, false); |
| + |
| +})(window); |