re-enable audio_*_loads keyvals.

BUG=chromium:618521
TEST=tested on chromium and chrome browser.

Change-Id: I7689fdec92c00b21393ad6233dcd8401c59d2e6d
Reviewed-on: https://chromium-review.googlesource.com/351311
Commit-Ready: Todd Broch <tbroch@chromium.org>
Tested-by: Ravi Chandra Sadineni <ravisadineni@chromium.org>
Reviewed-by: Todd Broch <tbroch@chromium.org>
diff --git a/client/site_tests/power_LoadTest/extension/ct.js b/client/site_tests/power_LoadTest/extension/ct.js
index 56b5a4c..8e612fd 100755
--- a/client/site_tests/power_LoadTest/extension/ct.js
+++ b/client/site_tests/power_LoadTest/extension/ct.js
@@ -6,41 +6,54 @@
 
 var PLAY_MUSIC_HOSTNAME = 'play.google.com';
 
-chrome.runtime.sendMessage(request, function(response) {
-  if (response && response.should_scroll) {
-    window.focus();
-    lastOffset = window.pageYOffset;
-    var start_interval = Math.max(10000, response.scroll_interval);
-    function smoothScrollDown() {
-      window.scrollBy(0, response.scroll_by);
-      if (window.pageYOffset != lastOffset) {
-        lastOffset = window.pageYOffset;
-        setTimeout(smoothScrollDown, response.scroll_interval);
-      } else if (response.should_scroll_up) {
-        setTimeout(smoothScrollUp, start_interval);
+//Sends message to the test.js(background script). test.js on
+//receiving a message from content script assumes the page has
+//loaded successfully. It further responds with instructions on
+//whether/how to scroll.
+function sendSuccessToBGScript() {
+  chrome.runtime.sendMessage(request, function(response) {
+    if (response && response.should_scroll) {
+      window.focus();
+      lastOffset = window.pageYOffset;
+      var start_interval = Math.max(10000, response.scroll_interval);
+      function smoothScrollDown() {
+        window.scrollBy(0, response.scroll_by);
+        if (window.pageYOffset != lastOffset) {
+          lastOffset = window.pageYOffset;
+          setTimeout(smoothScrollDown, response.scroll_interval);
+        } else if (response.should_scroll_up) {
+          setTimeout(smoothScrollUp, start_interval);
+        }
       }
-    }
-    function smoothScrollUp() {
-      window.scrollBy(0, -1 * response.scroll_by);
-      if (window.pageYOffset != lastOffset) {
-        lastOffset = window.pageYOffset;
-        setTimeout(smoothScrollUp, response.scroll_interval);
-      } else if (response.scroll_loop) {
-        setTimeout(smoothScrollDown, start_interval);
+      function smoothScrollUp() {
+        window.scrollBy(0, -1 * response.scroll_by);
+        if (window.pageYOffset != lastOffset) {
+          lastOffset = window.pageYOffset;
+          setTimeout(smoothScrollUp, response.scroll_interval);
+        } else if (response.scroll_loop) {
+          setTimeout(smoothScrollDown, start_interval);
+        }
       }
+      setTimeout(smoothScrollDown, start_interval);
     }
-    setTimeout(smoothScrollDown, start_interval);
-  }
-});
+  });
+}
 
-function clickPlayButton() {
+function afterLoad() {
+  if (document.location.hostname !== PLAY_MUSIC_HOSTNAME) {
+    sendSuccessToBGScript();
+    return;
+  }
+
   var playButton = document.querySelector('[data-id="play"]');
+
+  //If play music website, if we do not see a play button
+  //that effectively means the music is not loaded. So do not
+  //report success load to test.js.
   if (playButton) {
+    sendSuccessToBGScript();
     playButton.click();
   }
 }
 
-if (document.location.hostname === PLAY_MUSIC_HOSTNAME) {
-  window.addEventListener('load', clickPlayButton);
-}
-
+window.addEventListener('load', afterLoad);
diff --git a/client/site_tests/power_LoadTest/extension/custom_event_listener.js b/client/site_tests/power_LoadTest/extension/custom_event_listener.js
new file mode 100644
index 0000000..537a3be
--- /dev/null
+++ b/client/site_tests/power_LoadTest/extension/custom_event_listener.js
@@ -0,0 +1,6 @@
+Window.prototype.orginalEventListener = Window.prototype.addEventListener;
+Window.prototype.addEventListener = function(type, listener, useCapture) {
+    if (type !== "beforeunload") {
+        orginalEventListener(type, listener, useCapture);
+    }
+}
diff --git a/client/site_tests/power_LoadTest/extension/manifest.json b/client/site_tests/power_LoadTest/extension/manifest.json
index 46bcddb..be3742f 100755
--- a/client/site_tests/power_LoadTest/extension/manifest.json
+++ b/client/site_tests/power_LoadTest/extension/manifest.json
@@ -23,6 +23,15 @@
       "js": ["ct.js"],
       "run_at": "document_end",
       "all_frames": false
+    },
+     {
+      "matches": ["http://*/*", "https://*/*"],
+      "js": ["prevent_unload_popup.js"],
+      "run_at": "document_start",
+      "all_frames": true
     }
+  ],
+  "web_accessible_resources": [
+     "custom_event_listener.js"
   ]
 }
diff --git a/client/site_tests/power_LoadTest/extension/prevent_unload_popup.js b/client/site_tests/power_LoadTest/extension/prevent_unload_popup.js
new file mode 100644
index 0000000..89acb4b
--- /dev/null
+++ b/client/site_tests/power_LoadTest/extension/prevent_unload_popup.js
@@ -0,0 +1,10 @@
+//This content script injected into the page is to suppress
+// popup that prevents closing the tab.
+
+var PLAY_MUSIC_HOSTNAME = 'play.google.com';
+
+if (document.location.hostname === PLAY_MUSIC_HOSTNAME) {
+  var script = document.createElement('script');
+  script.src = chrome.extension.getURL("custom_event_listener.js");
+  document.documentElement.insertBefore(script, document.documentElement.firstChild);
+}
diff --git a/client/site_tests/power_LoadTest/extension/urls.js b/client/site_tests/power_LoadTest/extension/urls.js
index 891d9bd..3908e26 100755
--- a/client/site_tests/power_LoadTest/extension/urls.js
+++ b/client/site_tests/power_LoadTest/extension/urls.js
@@ -60,7 +60,7 @@
   {
     // After 36 minutes, start streaming audio (background tab), total playtime
     // 12 minutes
-    type: 'window',
+    type: 'cycle',
     name: 'audio',
     start: minutes(36),
     duration: minutes(12),
@@ -69,7 +69,8 @@
     focus: false,
     // Google Play Music requires MP3 decoder for playing music.
     // Fall back to BBC if the browser does not have MP3 decoder bundle.
-    tabs: isMP3DecoderPresent() ? [PLAY_MUSIC_URL] : [BBC_AUDIO_URL]
+    urls: isMP3DecoderPresent() ? [PLAY_MUSIC_URL, PLAY_MUSIC_URL] :
+                                  [BBC_AUDIO_URL, BBC_AUDIO_URL]
   },
   {
     // After 48 minutes, play with Google Docs for 6 minutes