0bf163c13e83d7ebab7122f16eae2d203e502e8a
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / automation / tests / tabs / tab_id.js
1 // Copyright 2014 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 createBackgroundTab(url, callback) {
6   chrome.tabs.query({ active: true }, function(tabs) {
7     chrome.test.assertEq(1, tabs.length);
8     var originalActiveTab = tabs[0];
9     createTab(url, function(tab) {
10       chrome.tabs.update(originalActiveTab.id, { active: true }, function() {
11         callback(tab);
12       });
13     })
14   });
15 }
16
17 function assertCorrectTab(rootNode) {
18   var title = rootNode.attributes.docTitle;
19   chrome.test.assertEq('Automation Tests', title);
20   chrome.test.succeed();
21 }
22
23 var allTests = [
24   function testGetTabById() {
25     getUrlFromConfig(function(url) {
26       // Keep the NTP as the active tab so that we know we're requesting the
27       // tab by ID rather than just getting the active tab still.
28       createBackgroundTab(url, function(tab) {
29         chrome.automation.getTree(tab.id, function(rootNode) {
30           if (rootNode.attributes.docLoaded) {
31             assertCorrectTab(rootNode);
32             return;
33           }
34
35           rootNode.addEventListener('loadComplete', function() {
36             assertCorrectTab(rootNode);
37           });
38         })
39       });
40     });
41   }
42 ];
43
44 chrome.test.runTests(allTests);