Home | History | Annotate | Download | only in extension
      1 // Copyright (c) 2014 The Chromium OS 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.
      4 
      5 var cycle_tabs = {};
      6 var cycles = {};
      7 var time_ratio = 3600 * 1000 / test_time_ms; // default test time is 1 hour
      8 var preexisting_windows = [];
      9 
     10 function setupTest() {
     11   chrome.windows.getAll(null, function(windows) {
     12     preexisting_windows = windows;
     13     for (var i = 0; i < tasks.length; i++) {
     14       setTimeout(launch_task, tasks[i].start / time_ratio, tasks[i]);
     15     }
     16     var end = 3600 * 1000 / time_ratio
     17     setTimeout(send_status, end);
     18   });
     19 }
     20 
     21 function testListener(request, sender, sendResponse) {
     22   if (sender.tab.id in cycle_tabs) {
     23     cycle = cycle_tabs[sender.tab.id];
     24     cycle.successful_loads++;
     25     if (request.action == "should_scroll" && cycle.focus) {
     26       sendResponse({"should_scroll": should_scroll,
     27                     "should_scroll_up": should_scroll_up,
     28                     "scroll_loop": scroll_loop,
     29                     "scroll_interval": scroll_interval_ms,
     30                     "scroll_by": scroll_by_pixels});
     31     }
     32     delete cycle_tabs[sender.tab.id];
     33   }
     34 }
     35 
     36 function close_preexisting_windows() {
     37   for (var i = 0; i < preexisting_windows.length; i++) {
     38     chrome.windows.remove(preexisting_windows[i].id);
     39   }
     40   preexisting_windows.length = 0;
     41 }
     42 
     43 function cycle_navigate(cycle) {
     44   cycle_tabs[cycle.id] = cycle;
     45   var url = cycle.urls[cycle.idx];
     46   chrome.tabs.update(cycle.id, {'url': url, 'selected': true});
     47   cycle.idx = (cycle.idx + 1) % cycle.urls.length;
     48   if (cycle.timeout < cycle.delay / time_ratio && cycle.timeout > 0) {
     49     cycle.timer = setTimeout(cycle_check_timeout, cycle.timeout, cycle);
     50   } else {
     51     cycle.timer = setTimeout(cycle_navigate, cycle.delay / time_ratio, cycle);
     52   }
     53 }
     54 
     55 function cycle_check_timeout(cycle) {
     56   if (cycle.id in cycle_tabs) {
     57     cycle.failed_loads++;
     58     cycle_navigate(cycle);
     59   } else {
     60     cycle.timer = setTimeout(cycle_navigate,
     61                              cycle.delay / time_ratio - cycle.timeout,
     62                              cycle);
     63   }
     64 }
     65 
     66 function launch_task(task) {
     67   if (task.type == 'window' && task.tabs) {
     68     chrome.windows.create({'url': 'about:blank'}, function (win) {
     69       close_preexisting_windows();
     70       chrome.tabs.getSelected(win.id, function(tab) {
     71         chrome.tabs.update(tab.id, {'url': task.tabs[0], 'selected': true});
     72         for (var i = 1; i < task.tabs.length; i++) {
     73           chrome.tabs.create({'windowId': win.id, url: task.tabs[i]});
     74         }
     75         setTimeout(chrome.windows.remove, task.duration / time_ratio, win.id);
     76       });
     77     });
     78   } else if (task.type == 'cycle' && task.urls) {
     79     chrome.windows.create({'url': 'about:blank'}, function (win) {
     80       close_preexisting_windows();
     81       chrome.tabs.getSelected(win.id, function(tab) {
     82         var cycle = {
     83            'timeout': task.timeout,
     84            'name': task.name,
     85            'delay': task.delay,
     86            'urls': task.urls,
     87            'id': tab.id,
     88            'idx': 0,
     89            'timer': null,
     90            'focus': !!task.focus,
     91            'successful_loads': 0,
     92            'failed_loads': 0
     93         };
     94         cycles[task.name] = cycle;
     95         cycle_navigate(cycle);
     96         setTimeout(function(cycle, win_id) {
     97           clearTimeout(cycle.timer);
     98           chrome.windows.remove(win_id);
     99         }, task.duration / time_ratio, cycle, win.id);
    100       });
    101     });
    102   }
    103 }
    104 
    105 function send_status() {
    106   var post = ["status=good"];
    107 
    108   for (var name in cycles) {
    109     var cycle = cycles[name];
    110     post.push(name + "_successful_loads=" + cycle.successful_loads);
    111     post.push(name + "_failed_loads=" + cycle.failed_loads);
    112   }
    113 
    114   chrome.runtime.onMessage.removeListener(testListener);
    115 
    116   var log_url = 'http://localhost:8001/status';
    117   var req = new XMLHttpRequest();
    118   req.open('POST', log_url, true);
    119   req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    120   req.send(post.join("&"));
    121   console.log(post.join("&"));
    122 }
    123 
    124 function startTest() {
    125   time_ratio = 3600 * 1000 / test_time_ms; // default test time is 1 hour
    126   chrome.runtime.onMessage.addListener(testListener);
    127   setTimeout(setupTest, 1000);
    128 }
    129 
    130 function initialize() {
    131   // Called when the user clicks on the browser action.
    132   chrome.browserAction.onClicked.addListener(function(tab) {
    133     // Start the test with default settings.
    134     chrome.runtime.onMessage.addListener(testListener);
    135     setTimeout(setupTest, 1000);
    136   });
    137 }
    138 
    139 window.addEventListener("load", initialize);
    140