| 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; | 8 var assertTrue = chrome.test.assertTrue; |
| 9 var listenOnce = chrome.test.listenOnce; | 9 var listenOnce = chrome.test.listenOnce; |
| 10 var pass = chrome.test.callbackPass; | 10 var callbackPass = chrome.test.callbackPass; |
| 11 | 11 |
| 12 var optionsTabUrl = 'chrome://extensions/?options=' + chrome.runtime.id; | 12 var optionsTabUrl = 'chrome://extensions/?options=' + chrome.runtime.id; |
| 13 | 13 |
| 14 // Finds the Tab for an options page, or null if no options page is open. | 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. | 15 // Asserts that there is at most 1 options page open. |
| 16 // Result is passed to |callback|. | 16 // Result is passed to |callback|. |
| 17 function findOptionsTab(callback) { | 17 function findOptionsTab(callback) { |
| 18 chrome.tabs.query({url: optionsTabUrl}, pass(function(tabs) { | 18 chrome.tabs.query({url: optionsTabUrl}, callbackPass(function(tabs) { |
| 19 assertTrue(tabs.length <= 1); | 19 assertTrue(tabs.length <= 1); |
| 20 callback(tabs.length == 0 ? null : tabs[0]); | 20 callback(tabs.length == 0 ? null : tabs[0]); |
| 21 })); | 21 })); |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Tests opening a new options page. | 24 // Tests opening a new options page. |
| 25 function testNewOptionsPage() { | 25 function testNewOptionsPage() { |
| 26 findOptionsTab(function(tab) { | 26 findOptionsTab(function(tab) { |
| 27 assertEq(null, tab); | 27 assertEq(null, tab); |
| 28 listenOnce(chrome.runtime.onMessage, function(m, sender) { | 28 listenOnce(chrome.runtime.onMessage, function(m, sender) { |
| 29 assertEq('success', m); | 29 assertEq('success', m); |
| 30 assertEq(chrome.runtime.id, sender.id); | 30 assertEq(chrome.runtime.id, sender.id); |
| 31 assertEq(chrome.runtime.getURL('options.html'), sender.url); | 31 assertEq(chrome.runtime.getURL('options.html'), sender.url); |
| 32 }); | 32 }); |
| 33 chrome.runtime.openOptionsPage(); | 33 chrome.runtime.openOptionsPage(callbackPass()); |
| 34 }); | 34 }); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Gets the active tab, or null if no tab is active. Asserts that there is at | 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|. | 38 // most 1 active tab. Result is passed to |callback|. |
| 39 function getActiveTab(callback) { | 39 function getActiveTab(callback) { |
| 40 chrome.tabs.query({active: true}, pass(function(tabs) { | 40 chrome.tabs.query({active: true}, callbackPass(function(tabs) { |
| 41 assertTrue(tabs.length <= 1); | 41 assertTrue(tabs.length <= 1); |
| 42 callback(tabs.length == 0 ? null : tabs[0]); | 42 callback(tabs.length == 0 ? null : tabs[0]); |
| 43 })); | 43 })); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Tests refocusing an existing page. | 46 // Tests refocusing an existing page. |
| 47 function testRefocusExistingOptionsPage() { | 47 function testRefocusExistingOptionsPage() { |
| 48 var testUrl = 'chrome://chrome/'; | 48 // Note that using chrome://chrome/ as |testUrl| would be problematic here, |
| 49 // because the options page will happily overwrite the tab pointing to it. |
| 50 var testUrl = 'about:blank'; |
| 49 | 51 |
| 50 // There will already be an options page open from the last test. Find it, | 52 // 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. | 53 // focus away from it, then make sure openOptionsPage() refocuses it. |
| 52 findOptionsTab(function(optionsTab) { | 54 findOptionsTab(function(optionsTab) { |
| 53 assertTrue(optionsTab != null); | 55 assertTrue(optionsTab != null); |
| 54 chrome.tabs.create({url: testUrl}, pass(function(tab) { | 56 chrome.tabs.create({url: testUrl}, callbackPass(function(tab) { |
| 55 // Make sure the new tab is active. | 57 // Make sure the new tab is active. |
| 56 getActiveTab(function(activeTab) { | 58 getActiveTab(function(activeTab) { |
| 57 assertEq(testUrl, activeTab.url); | 59 assertEq(testUrl, activeTab.url); |
| 58 // Open options page should refocus it. | 60 // Open options page should refocus it. |
| 59 chrome.runtime.openOptionsPage(); | 61 chrome.runtime.openOptionsPage(callbackPass(function() { |
| 60 getActiveTab(function(activeTab) { | 62 getActiveTab(function(activeTab) { |
| 61 assertEq(optionsTabUrl, activeTab.url); | 63 assertEq(optionsTabUrl, activeTab.url); |
| 62 }); | 64 }); |
| 65 })); |
| 63 }); | 66 }); |
| 64 })); | 67 })); |
| 65 }); | 68 }); |
| 66 } | 69 } |
| 67 | 70 |
| 68 chrome.test.runTests([testNewOptionsPage, testRefocusExistingOptionsPage]); | 71 chrome.test.runTests([testNewOptionsPage, testRefocusExistingOptionsPage]); |
| OLD | NEW |