OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // browser_tests --gtest_filter=ExtensionApiTest.OpenOptionsPage | 5 // browser_tests --gtest_filter=ExtensionApiTest.OpenOptionsPage |
6 | 6 |
7 var assertEq = chrome.test.assertEq; | 7 var assertEq = chrome.test.assertEq; |
| 8 var assertTrue = chrome.test.assertTrue; |
| 9 var listenOnce = chrome.test.listenOnce; |
| 10 var pass = chrome.test.callbackPass; |
8 | 11 |
9 function test() { | 12 var optionsTabUrl = 'chrome://extensions/?options=' + chrome.runtime.id; |
10 chrome.test.listenOnce(chrome.runtime.onMessage, function(m, sender) { | 13 |
11 assertEq('success', m); | 14 // Finds the Tab for an options page, or null if no options page is open. |
12 assertEq(chrome.runtime.id, sender.id); | 15 // Asserts that there is at most 1 options page open. |
13 assertEq(chrome.runtime.getURL('options.html'), sender.url); | 16 // Result is passed to |callback|. |
14 }); | 17 function findOptionsTab(callback) { |
15 chrome.runtime.openOptionsPage(); | 18 chrome.tabs.query({url: optionsTabUrl}, pass(function(tabs) { |
| 19 assertTrue(tabs.length <= 1); |
| 20 callback(tabs.length == 0 ? null : tabs[0]); |
| 21 })); |
16 } | 22 } |
17 | 23 |
18 chrome.test.runTests([test]); | 24 // Tests opening a new options page. |
| 25 function testNewOptionsPage() { |
| 26 findOptionsTab(function(tab) { |
| 27 assertEq(null, tab); |
| 28 listenOnce(chrome.runtime.onMessage, function(m, sender) { |
| 29 assertEq('success', m); |
| 30 assertEq(chrome.runtime.id, sender.id); |
| 31 assertEq(chrome.runtime.getURL('options.html'), sender.url); |
| 32 }); |
| 33 chrome.runtime.openOptionsPage(); |
| 34 }); |
| 35 } |
| 36 |
| 37 // Gets the active tab, or null if no tab is active. Asserts that there is at |
| 38 // most 1 active tab. Result is passed to |callback|. |
| 39 function getActiveTab(callback) { |
| 40 chrome.tabs.query({active: true}, pass(function(tabs) { |
| 41 assertTrue(tabs.length <= 1); |
| 42 callback(tabs.length == 0 ? null : tabs[0]); |
| 43 })); |
| 44 } |
| 45 |
| 46 // Tests refocusing an existing page. |
| 47 function testRefocusExistingOptionsPage() { |
| 48 var testUrl = 'chrome://chrome/'; |
| 49 |
| 50 // There will already be an options page open from the last test. Find it, |
| 51 // focus away from it, then make sure openOptionsPage() refocuses it. |
| 52 findOptionsTab(function(optionsTab) { |
| 53 assertTrue(optionsTab != null); |
| 54 chrome.tabs.create({url: testUrl}, pass(function(tab) { |
| 55 // Make sure the new tab is active. |
| 56 getActiveTab(function(activeTab) { |
| 57 assertEq(testUrl, activeTab.url); |
| 58 // Open options page should refocus it. |
| 59 chrome.runtime.openOptionsPage(); |
| 60 getActiveTab(function(activeTab) { |
| 61 assertEq(optionsTabUrl, activeTab.url); |
| 62 }); |
| 63 }); |
| 64 })); |
| 65 }); |
| 66 } |
| 67 |
| 68 chrome.test.runTests([testNewOptionsPage, testRefocusExistingOptionsPage]); |
OLD | NEW |