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
3 // found in the LICENSE file.
5 // browser_tests --gtest_filter=ExtensionApiTest.OpenOptionsPage
7 var assertEq
= chrome
.test
.assertEq
;
8 var assertTrue
= chrome
.test
.assertTrue
;
9 var listenOnce
= chrome
.test
.listenOnce
;
10 var pass
= chrome
.test
.callbackPass
;
12 var optionsTabUrl
= 'chrome://extensions/?options=' + chrome
.runtime
.id
;
14 // Finds the Tab for an options page, or null if no options page is open.
15 // Asserts that there is at most 1 options page open.
16 // Result is passed to |callback|.
17 function findOptionsTab(callback
) {
18 chrome
.tabs
.query({url
: optionsTabUrl
}, pass(function(tabs
) {
19 assertTrue(tabs
.length
<= 1);
20 callback(tabs
.length
== 0 ? null : tabs
[0]);
24 // Tests opening a new options page.
25 function testNewOptionsPage() {
26 findOptionsTab(function(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
);
33 chrome
.runtime
.openOptionsPage();
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]);
46 // Tests refocusing an existing page.
47 function testRefocusExistingOptionsPage() {
48 var testUrl
= 'chrome://chrome/';
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
);
68 chrome
.test
.runTests([testNewOptionsPage
, testRefocusExistingOptionsPage
]);