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.
5 function optionsPageLoaded() {
7 chrome
.extension
.getViews().forEach(function(view
) {
8 if (view
.document
.location
.pathname
== '/options.html') {
9 chrome
.test
.assertEq(false, hasLoaded
);
10 hasLoaded
= view
.loaded
;
16 function assertSenderIsOptionsPage(sender
) {
17 chrome
.test
.assertEq({
18 'id': chrome
.runtime
.id
,
19 'url': chrome
.runtime
.getURL('options.html')
23 chrome
.test
.runTests([
24 // Basic tests that ensure that the <extensionoptions> guest view is created
25 // and loaded, and that the load event is accurate.
26 // createGuestViewDOM tests that it can be created and manipulated like a DOM
27 // element, and createGuestViewProgrammatic tests the same but as a JavaScript
29 function createGuestViewDOM() {
30 var extensionoptions
= document
.createElement('extensionoptions');
31 extensionoptions
.addEventListener('load', function() {
33 chrome
.test
.assertTrue(optionsPageLoaded());
34 chrome
.test
.succeed();
36 document
.body
.removeChild(extensionoptions
);
39 extensionoptions
.setAttribute('extension', chrome
.runtime
.id
);
40 document
.body
.appendChild(extensionoptions
);
43 function createGuestViewProgrammatic() {
44 var extensionoptions
= new ExtensionOptions();
45 extensionoptions
.onload = function() {
47 chrome
.test
.assertTrue(optionsPageLoaded());
48 chrome
.test
.succeed();
50 document
.body
.removeChild(extensionoptions
);
53 extensionoptions
.extension
= chrome
.runtime
.id
;
54 document
.body
.appendChild(extensionoptions
);
57 // Tests if the <extensionoptions> guest view is successfully created and can
58 // communicate with the embedder. This test expects that the guest options
59 // page will add a {'pass': true} property to every Window and fire the
60 // runtime.onMessage event with a short message.
61 function canCommunicateWithGuest() {
62 var done
= chrome
.test
.listenForever(chrome
.runtime
.onMessage
,
63 function(message
, sender
, sendResponse
) {
64 assertSenderIsOptionsPage(sender
);
65 if (message
== 'ready') {
66 sendResponse('canCommunicateWithGuest');
67 } else if (message
== 'done') {
69 var views
= chrome
.extension
.getViews();
70 chrome
.test
.assertEq(2, views
.length
);
71 views
.forEach(function(view
) {
72 chrome
.test
.assertTrue(view
.pass
);
74 chrome
.test
.assertEq(chrome
.runtime
.id
, sender
.id
);
77 document
.body
.removeChild(extensionoptions
);
82 var extensionoptions
= document
.createElement('extensionoptions');
83 extensionoptions
.setAttribute('extension', chrome
.runtime
.id
);
84 document
.body
.appendChild(extensionoptions
);
87 // Tests if the <extensionoptions> guest view can access the chrome.storage
88 // API, a privileged extension API.
89 function guestCanAccessStorage() {
90 var onStorageChanged
= false;
91 var onSetAndGet
= false;
93 chrome
.test
.listenOnce(chrome
.storage
.onChanged
, function(change
) {
94 chrome
.test
.assertEq(42, change
.test
.newValue
);
97 // Listens for messages from the options page.
98 var done
= chrome
.test
.listenForever(chrome
.runtime
.onMessage
,
99 function(message
, sender
, sendResponse
) {
100 assertSenderIsOptionsPage(sender
);
102 // Options page is waiting for a command
103 if (message
== 'ready') {
104 sendResponse('guestCanAccessStorage');
105 // Messages from the options page containing test data
106 } else if (message
.description
== 'onStorageChanged') {
107 chrome
.test
.assertEq(message
.expected
, message
.actual
);
108 onStorageChanged
= true;
109 } else if (message
.description
== 'onSetAndGet') {
110 chrome
.test
.assertEq(message
.expected
, message
.actual
);
114 if (onStorageChanged
&& onSetAndGet
) {
115 document
.body
.removeChild(extensionoptions
);
120 var extensionoptions
= document
.createElement('extensionoptions');
121 extensionoptions
.setAttribute('extension', chrome
.runtime
.id
);
122 document
.body
.appendChild(extensionoptions
);
125 function externalLinksOpenInNewTab() {
126 var done
= chrome
.test
.listenForever(chrome
.runtime
.onMessage
,
127 function(message
, sender
, sendResponse
) {
128 assertSenderIsOptionsPage(sender
);
130 if (message
== 'ready') {
131 sendResponse('externalLinksOpenInNewTab');
132 } else if (message
== 'done') {
134 chrome
.tabs
.query({url
: 'http://www.chromium.org/'}, function(tabs
) {
135 chrome
.test
.assertEq(1, tabs
.length
);
136 chrome
.test
.assertEq('http://www.chromium.org/', tabs
[0].url
);
140 document
.body
.removeChild(extensionoptions
);
145 var extensionoptions
= document
.createElement('extensionoptions');
146 extensionoptions
.setAttribute('extension', chrome
.runtime
.id
);
147 document
.body
.appendChild(extensionoptions
);