- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / browsertest / last_error / bg.js
1 // Copyright (c) 2011 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 function fail() {
6   window.domAutomationController.send(false);
7   throw "Failed!";
8 }
9
10 function succeed() {
11   window.domAutomationController.send(true);
12 }
13
14 function testLastError() {
15   // Make sure lastError is not yet set
16   if (chrome.tabs.lastError)
17     fail();
18
19   var maxTabId = 0;
20
21   // Find the highest tab id
22   chrome.windows.getAll({populate:true}, function(windows) {
23     // Make sure lastError is still not set. (this call have should succeeded).
24     if (chrome.tabs.lastError)
25       fail();
26
27     for (var i = 0; i < windows.length; i++) {
28       var win = windows[i];
29       for (var j = 0; j < win.tabs.length; j++) {
30         var tab = win.tabs[j];
31         if (tab.id > maxTabId)
32           maxTabId = tab.id;
33       }
34     }
35
36     // Now ask for the next highest tabId.
37     chrome.tabs.get(maxTabId + 1, function(tab) {
38       // Make sure lastError *is* set and tab is not.
39       if (!chrome.runtime.lastError ||
40           !chrome.runtime.lastError.message ||
41           tab)
42         fail();
43
44       window.setTimeout(finish, 10);
45     });
46   });
47 }
48
49 function finish() {
50   // Now make sure lastError is unset outside the callback context.
51   if (chrome.tabs.lastError)
52     fail();
53
54   succeed();
55 }
56
57 document.documentElement.addEventListener("click", function() {
58   testLastError();
59 }, true);