OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 // API test for chrome.experimental.sidebar. | |
6 // browser_tests.exe --gtest_filter=SidebarApiTest.Sidebar | |
7 | |
8 const assertEq = chrome.test.assertEq; | |
9 const assertTrue = chrome.test.assertTrue; | |
10 const pass = chrome.test.callbackPass; | |
11 | |
12 if (!chrome.sidebar) { | |
13 chrome.sidebar = chrome.experimental.sidebar; | |
14 } | |
15 | |
16 /** | |
17 * A helper function to show sidebar. Verifies that sidebar was hidden before | |
18 * and is shown after the call. | |
19 * @param {id} tab id to expand sidebar for. | |
20 * @param {function} callback Closure. | |
21 */ | |
22 function showSidebar(id, callback) { | |
23 chrome.sidebar.getState({tabId: id}, function(state) { | |
24 assertEq('hidden', state); | |
25 chrome.sidebar.show({tabId: id}); | |
26 chrome.sidebar.getState({tabId: id}, function(state) { | |
27 assertEq('shown', state); | |
28 callback(); | |
29 }); | |
30 }); | |
31 } | |
32 | |
33 /** | |
34 * A helper function to expand sidebar. Verifies that sidebar was shown | |
35 * before and is expanded after the call (provided the specified tab | |
36 * is currently selected). | |
37 * @param {id} tab id to expand sidebar for. | |
38 * @param {function} callback Closure. | |
39 */ | |
40 function expandSidebar(id, callback) { | |
41 chrome.sidebar.getState({tabId: id}, function(state) { | |
42 assertEq('shown', state); | |
43 chrome.sidebar.expand({tabId: id}); | |
44 chrome.sidebar.getState({tabId: id}, function(state) { | |
45 if (undefined == id) { | |
46 assertEq('active', state); | |
47 callback(); | |
48 } else { | |
49 chrome.tabs.get(id, function(tab) { | |
50 if (tab.selected) { | |
51 assertEq('active', state); | |
52 } else { | |
53 assertEq('shown', state); | |
54 } | |
55 callback(); | |
56 }); | |
57 } | |
58 }); | |
59 }); | |
60 } | |
61 | |
62 /** | |
63 * A helper function to collapse sidebar. Verifies that sidebar was active | |
64 * before (provided the specified tab is currently selected) and is not active | |
65 * after the call. | |
66 * @param {id} tab id to collapse sidebar for. | |
67 * @param {function} callback Closure. | |
68 */ | |
69 function collapseSidebar(id, callback) { | |
70 chrome.sidebar.getState({tabId: id}, function(state) { | |
71 if (undefined == id) { | |
72 assertEq('active', state); | |
73 } else { | |
74 chrome.tabs.get(id, function(tab) { | |
75 if (tab.selected) { | |
76 assertEq('active', state); | |
77 } else { | |
78 assertEq('shown', state); | |
79 } | |
80 }); | |
81 } | |
82 chrome.sidebar.collapse({tabId: id}); | |
83 chrome.sidebar.getState({tabId: id}, function(state) { | |
84 assertEq('shown', state); | |
85 callback(); | |
86 }); | |
87 }); | |
88 } | |
89 | |
90 /** | |
91 * A helper function to hide sidebar. Verifies that sidebar was not hidden | |
92 * before and is hidden after the call. | |
93 * @param {id} tab id to hide sidebar for. | |
94 * @param {function} callback Closure. | |
95 */ | |
96 function hideSidebar(id, callback) { | |
97 chrome.sidebar.getState({tabId: id}, function(state) { | |
98 assertTrue('hidden' != state); | |
99 chrome.sidebar.hide({tabId: id}); | |
100 chrome.sidebar.getState({tabId: id}, function(state) { | |
101 assertEq('hidden', state); | |
102 callback(); | |
103 }); | |
104 }); | |
105 } | |
106 | |
107 /** | |
108 * A helper function to show sidebar for the current tab. | |
109 * @param {function} callback Closure. | |
110 */ | |
111 function showSidebarForCurrentTab(callback) { | |
112 showSidebar(undefined, callback); | |
113 } | |
114 | |
115 /** | |
116 * A helper function to expand sidebar for the current tab. | |
117 * @param {function} callback Closure. | |
118 */ | |
119 function expandSidebarForCurrentTab(callback) { | |
120 expandSidebar(undefined, callback); | |
121 } | |
122 | |
123 /** | |
124 * A helper function to collapse sidebar for the current tab. | |
125 * @param {function} callback Closure. | |
126 */ | |
127 function collapseSidebarForCurrentTab(callback) { | |
128 collapseSidebar(undefined, callback); | |
129 } | |
130 | |
131 /** | |
132 * A helper function to hide sidebar for the current tab. | |
133 * @param {function} callback Closure. | |
134 */ | |
135 function hideSidebarForCurrentTab(callback) { | |
136 hideSidebar(undefined, callback); | |
137 } | |
138 | |
139 var tests = [ | |
140 function showHideSidebar() { | |
141 showSidebarForCurrentTab(function() { | |
142 expandSidebarForCurrentTab(function() { | |
143 collapseSidebarForCurrentTab(function() { | |
144 hideSidebarForCurrentTab(function() { | |
145 showSidebarForCurrentTab(function() { | |
146 hideSidebarForCurrentTab(pass()); | |
147 }); | |
148 }); | |
149 }); | |
150 }); | |
151 }); | |
152 }, | |
153 | |
154 function switchingTabs() { | |
155 showSidebarForCurrentTab(function() { | |
156 expandSidebarForCurrentTab(function() { | |
157 chrome.tabs.getSelected(null, function(tabWithSidebar) { | |
158 chrome.tabs.create({}, function(tab) { | |
159 // Make sure sidebar is not visible on this new tab. | |
160 chrome.sidebar.getState({}, function(state) { | |
161 assertEq('hidden', state); | |
162 // Switch back to the tab with the sidebar. | |
163 chrome.tabs.update(tabWithSidebar.id, {selected: true}, | |
164 function(theSameTab) { | |
165 // It makes sure sidebar is visible before hiding it. | |
166 hideSidebarForCurrentTab(pass()); | |
167 }); | |
168 }); | |
169 }); | |
170 }); | |
171 }); | |
172 }); | |
173 }, | |
174 | |
175 function sidebarOnInactiveTab() { | |
176 // 'switchingTabs' test created two tabs. | |
177 chrome.tabs.getAllInWindow(null, function(tabs) { | |
178 assertEq(2, tabs.length); | |
179 showSidebar(tabs[1].id, function() { | |
180 expandSidebar(tabs[1].id, function() { | |
181 // Make sure sidebar is not visible on the current tab. | |
182 chrome.sidebar.getState({}, function(state) { | |
183 assertEq('hidden', state); | |
184 // Switch to the tab with the sidebar. | |
185 chrome.tabs.update(tabs[1].id, {selected: true}, | |
186 function() { | |
187 // Make sure sidebar is visible on the current tab. | |
188 chrome.sidebar.getState({}, function(state) { | |
189 assertEq('active', state); | |
190 | |
191 // Switch to the tab with no sidebar. | |
192 chrome.tabs.update(tabs[0].id, {selected: true}, | |
193 function() { | |
194 collapseSidebar(tabs[1].id, function() { | |
195 hideSidebar(tabs[1].id, pass()); | |
196 }); | |
197 }); | |
198 }); | |
199 }); | |
200 }); | |
201 }); | |
202 }); | |
203 }); | |
204 }, | |
205 | |
206 function navigateSidebar() { | |
207 showSidebarForCurrentTab(function() { | |
208 expandSidebarForCurrentTab(function() { | |
209 chrome.sidebar.navigate({path: 'simple_page.html'}); | |
210 hideSidebarForCurrentTab(pass()); | |
211 }); | |
212 }); | |
213 }, | |
214 | |
215 function crashTest() { | |
216 // Chrome should not crash on this request. | |
217 chrome.sidebar.getState(undefined, function(state) { | |
218 assertEq('hidden', state); | |
219 chrome.sidebar.getState(null, function(state) { | |
220 assertEq('hidden', state); | |
221 // Also check that it really does return state of the current tab. | |
222 showSidebarForCurrentTab(function() { | |
223 chrome.sidebar.getState(undefined, function(state) { | |
224 assertEq('shown', state); | |
225 chrome.sidebar.getState(null, function(state) { | |
226 assertEq('shown', state); | |
227 hideSidebarForCurrentTab(pass()); | |
228 }); | |
229 }); | |
230 }); | |
231 }); | |
232 }); | |
233 }, | |
234 | |
235 function testSetFunctions() { | |
236 showSidebarForCurrentTab(function() { | |
237 // TODO(alekseys): test unicode strings. | |
238 chrome.sidebar.setBadgeText({text: 'Some random text'}); | |
239 chrome.sidebar.setIcon({path: 'icon.png'}); | |
240 chrome.sidebar.setTitle({title: 'Some random title'}); | |
241 hideSidebarForCurrentTab(pass()); | |
242 }); | |
243 } | |
244 ]; | |
245 | |
246 chrome.test.runTests(tests); | |
OLD | NEW |