Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / extension_options / embed_self / test.js
blob04672ddb772d23d1985c297dd8a488dd07d30bff
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() {
6 var hasLoaded = false;
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;
12 });
13 return hasLoaded;
16 function assertSenderIsOptionsPage(sender) {
17 chrome.test.assertEq({
18 'id': chrome.runtime.id,
19 'url': chrome.runtime.getURL('options.html')
20 }, sender);
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
28 // object.
29 function createGuestViewDOM() {
30 var extensionoptions = document.createElement('extensionoptions');
31 extensionoptions.addEventListener('load', function() {
32 try {
33 chrome.test.assertTrue(optionsPageLoaded());
34 chrome.test.succeed();
35 } finally {
36 document.body.removeChild(extensionoptions);
38 });
39 extensionoptions.setAttribute('extension', chrome.runtime.id);
40 document.body.appendChild(extensionoptions);
43 function createGuestViewProgrammatic() {
44 var extensionoptions = new ExtensionOptions();
45 extensionoptions.onload = function() {
46 try {
47 chrome.test.assertTrue(optionsPageLoaded());
48 chrome.test.succeed();
49 } finally {
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') {
68 try {
69 var views = chrome.extension.getViews();
70 chrome.test.assertEq(2, views.length);
71 views.forEach(function(view) {
72 chrome.test.assertTrue(view.pass);
73 });
74 chrome.test.assertEq(chrome.runtime.id, sender.id);
75 done();
76 } finally {
77 document.body.removeChild(extensionoptions);
80 });
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);
95 });
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);
111 onSetAndGet = true;
114 if (onStorageChanged && onSetAndGet) {
115 document.body.removeChild(extensionoptions);
116 done();
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') {
133 try {
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);
137 done();
139 } finally {
140 document.body.removeChild(extensionoptions);
145 var extensionoptions = document.createElement('extensionoptions');
146 extensionoptions.setAttribute('extension', chrome.runtime.id);
147 document.body.appendChild(extensionoptions);