Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / extension_options / embed_self / test.js
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.
4
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;
11     }
12   });
13   return hasLoaded;
14 }
15
16 chrome.test.runTests([
17   // Basic tests that ensure that the <extensionoptions> guest view is created
18   // and loaded, and that the load event is accurate.
19   // createGuestViewDOM tests that it can be created and manipulated like a DOM
20   // element, and createGuestViewProgrammatic tests the same but as a JavaScript
21   // object.
22   function createGuestViewDOM() {
23     var extensionoptions = document.createElement('extensionoptions');
24     extensionoptions.addEventListener('load', function() {
25       try {
26         chrome.test.assertTrue(optionsPageLoaded());
27         chrome.test.succeed();
28       } finally {
29         document.body.removeChild(extensionoptions);
30       }
31     });
32     extensionoptions.setAttribute('extension', chrome.runtime.id);
33     document.body.appendChild(extensionoptions);
34   },
35
36   function createGuestViewProgrammatic() {
37     var extensionoptions = new ExtensionOptions();
38     extensionoptions.onload = function() {
39       try {
40         chrome.test.assertTrue(optionsPageLoaded());
41         chrome.test.succeed();
42       } finally {
43         document.body.removeChild(extensionoptions);
44       }
45     };
46     extensionoptions.extension = chrome.runtime.id;
47     document.body.appendChild(extensionoptions);
48   },
49
50   // Tests if the <extensionoptions> guest view is successfully created and can
51   // communicate with the embedder. This test expects that the guest options
52   // page will add a {'pass': true} property to every Window and fire the
53   // runtime.onMessage event with a short message.
54   function canCommunicateWithGuest() {
55     var done = chrome.test.listenForever(chrome.runtime.onMessage,
56         function(message, sender, sendResponse) {
57       if (message == 'ready') {
58         sendResponse('canCommunicateWithGuest');
59       } else if (message == 'done') {
60         try {
61           var views = chrome.extension.getViews();
62           chrome.test.assertEq(2, views.length);
63           views.forEach(function(view) {
64             chrome.test.assertTrue(view.pass);
65           });
66           chrome.test.assertEq(chrome.runtime.id, sender.id);
67           done();
68         } finally {
69           document.body.removeChild(extensionoptions);
70         }
71       }
72     });
73
74     var extensionoptions = document.createElement('extensionoptions');
75     extensionoptions.setAttribute('extension', chrome.runtime.id);
76     document.body.appendChild(extensionoptions);
77   },
78
79   // Tests if the <extensionoptions> guest view can access the chrome.storage
80   // API, a privileged extension API.
81   function guestCanAccessStorage() {
82     var onStorageChanged = false;
83     var onSetAndGet = false;
84
85     chrome.test.listenOnce(chrome.storage.onChanged, function(change) {
86       chrome.test.assertEq(42, change.test.newValue);
87     });
88
89     // Listens for messages from the options page.
90     var done = chrome.test.listenForever(chrome.runtime.onMessage,
91         function(message, sender, sendResponse) {
92       // Options page is waiting for a command
93       if (message == 'ready') {
94         sendResponse('guestCanAccessStorage');
95       // Messages from the options page containing test data
96       } else if (message.description == 'onStorageChanged') {
97         chrome.test.assertEq(message.expected, message.actual);
98         onStorageChanged = true;
99       } else if (message.description == 'onSetAndGet') {
100         chrome.test.assertEq(message.expected, message.actual);
101         onSetAndGet = true;
102       }
103
104       if (onStorageChanged && onSetAndGet) {
105         document.body.removeChild(extensionoptions);
106         done();
107       }
108     });
109
110     var extensionoptions = document.createElement('extensionoptions');
111     extensionoptions.setAttribute('extension', chrome.runtime.id);
112     document.body.appendChild(extensionoptions);
113   },
114
115   function autosizedGuestIsWithinSizeConstraints() {
116     var done = chrome.test.callbackAdded();
117
118     var extensionoptions = new ExtensionOptions();
119     extensionoptions.extension = chrome.runtime.id;
120     extensionoptions.autosize = 'on';
121     extensionoptions.minheight = 499;
122     extensionoptions.minwidth = 499;
123     extensionoptions.maxheight = 501;
124     extensionoptions.maxwidth = 501;
125
126     extensionoptions.onsizechanged = function(evt) {
127       try {
128         chrome.test.assertTrue(evt.width >= 499);
129         chrome.test.assertTrue(evt.height >= 499);
130         chrome.test.assertTrue(evt.width <= 501);
131         chrome.test.assertTrue(evt.height <= 501);
132         done();
133       } finally {
134         document.body.removeChild(extensionoptions);
135       }
136     };
137
138     document.body.appendChild(extensionoptions);
139   }
140 ]);