- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / declarative / redirect_to_data / background.js
1 // Copyright 2013 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.
4
5 // |testTitle| needs to be the same as |kTestTitle| in declarative_apitest.cc.
6 var testTitle = ':TEST:';
7 var redirectDataURI = 'data:text/html;charset=utf-8,<html><head><title>' +
8                       testTitle +
9                       '<%2Ftitle><%2Fhtml>';
10
11 var rule = {
12   conditions: [
13     new chrome.declarativeWebRequest.RequestMatcher({})
14   ],
15   actions: [
16     new chrome.declarativeWebRequest.RedirectRequest({
17       redirectUrl: redirectDataURI
18     })
19   ]
20 };
21
22 function report(details) {
23   if (chrome.extension.lastError) {
24     chrome.test.log(chrome.extension.lastError.message);
25   } else if (details.length < 1) {
26     chrome.test.log('Error loading rules.');
27   }
28 }
29
30 chrome.runtime.onInstalled.addListener(function(details) {
31   chrome.declarativeWebRequest.onRequest.addRules([rule], report);
32 });
33
34 var activeTabId;
35
36 function navigateAndWait(url, callback) {
37   var done =
38       chrome.test.listenForever(chrome.tabs.onUpdated, function(_, info, tab) {
39         if (tab.id == activeTabId && info.status == 'complete') {
40           if (callback)
41             callback(tab);
42           done();
43         }
44       });
45   chrome.tabs.update(activeTabId, {url: url});
46 }
47
48 function checkTitleCallback(tab) {
49   chrome.test.assertEq(testTitle, tab.title);
50 }
51
52 chrome.test.runTests([
53   function setUp() {
54     chrome.windows.getAll(
55       {populate: true},
56       chrome.test.callbackPass(function(windows) {
57         chrome.test.assertEq(1, windows.length);
58         activeTabId = windows[0].tabs[0].id;
59       }))
60   },
61   function checkTitle() {
62     navigateAndWait('http://www.example.com',
63                     chrome.test.callbackPass(checkTitleCallback));
64   }
65 ]);