1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#if defined(TOOLKIT_GTK)
#include <gtk/gtk.h>
#endif
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extension_tabs_module.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/extensions/extension_action.h"
#include "chrome/test/ui_test_utils.h"
#if defined(OS_WIN) || defined(TOOLKIT_VIEWS)
#include "chrome/browser/views/browser_actions_container.h"
#include "chrome/browser/views/extensions/extension_popup.h"
#include "chrome/browser/views/toolbar_view.h"
#elif defined(OS_LINUX)
#include "chrome/browser/gtk/view_id_util.h"
#endif
class BrowserActionTest : public ExtensionApiTest {
public:
BrowserActionTest() { }
virtual ~BrowserActionTest() { }
int NumberOfBrowserActions() {
int rv = -1;
#if defined(OS_WIN) || defined(TOOLKIT_VIEWS)
BrowserActionsContainer* browser_actions =
browser()->window()->GetBrowserWindowTesting()->GetToolbarView()->
browser_actions();
if (browser_actions)
rv = browser_actions->num_browser_actions();
#elif defined(OS_LINUX)
GtkWidget* toolbar = ViewIDUtil::GetWidget(
GTK_WIDGET(browser()->window()->GetNativeHandle()),
VIEW_ID_BROWSER_ACTION_TOOLBAR);
if (toolbar) {
GList* children = gtk_container_get_children(GTK_CONTAINER(toolbar));
rv = g_list_length(children);
g_list_free(children);
}
#endif
EXPECT_NE(-1, rv);
return rv;
}
bool IsIconNull(int index) {
#if defined(OS_WIN) || defined(TOOLKIT_VIEWS)
BrowserActionsContainer* browser_actions =
browser()->window()->GetBrowserWindowTesting()->GetToolbarView()->
browser_actions();
// We can't ASSERT_TRUE in non-void functions.
if (browser_actions) {
return browser_actions->GetBrowserActionViewAt(index)->button()->icon().
empty();
} else {
EXPECT_TRUE(false);
}
#elif defined(OS_LINUX)
GtkWidget* button = GetButton(index);
if (button)
return gtk_button_get_image(GTK_BUTTON(button)) == NULL;
else
EXPECT_TRUE(false);
#endif
return false;
}
void ExecuteBrowserAction(int index) {
#if defined(OS_WIN) || defined(TOOLKIT_VIEWS)
BrowserActionsContainer* browser_actions =
browser()->window()->GetBrowserWindowTesting()->GetToolbarView()->
browser_actions();
ASSERT_TRUE(browser_actions);
browser_actions->TestExecuteBrowserAction(index);
#elif defined(OS_LINUX) || defined(TOOLKIT_VIEWS)
GtkWidget* button = GetButton(index);
ASSERT_TRUE(button);
gtk_button_clicked(GTK_BUTTON(button));
#endif
}
std::string GetTooltip(int index) {
#if defined(OS_WIN) || defined(TOOLKIT_VIEWS)
BrowserActionsContainer* browser_actions =
browser()->window()->GetBrowserWindowTesting()->GetToolbarView()->
browser_actions();
if (browser_actions) {
std::wstring text;
EXPECT_TRUE(browser_actions->GetBrowserActionViewAt(0)->button()->
GetTooltipText(0, 0, &text));
return WideToUTF8(text);
}
#elif defined(OS_LINUX)
GtkWidget* button = GetButton(index);
if (button) {
gchar* text = gtk_widget_get_tooltip_text(button);
std::string rv = std::string(text);
g_free(text);
return rv;
}
#endif
EXPECT_TRUE(false);
return std::string();
}
private:
#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
GtkWidget* GetButton(int index) {
GtkWidget* rv = NULL;
GtkWidget* toolbar = ViewIDUtil::GetWidget(
GTK_WIDGET(browser()->window()->GetNativeHandle()),
VIEW_ID_BROWSER_ACTION_TOOLBAR);
if (toolbar) {
GList* children = gtk_container_get_children(GTK_CONTAINER(toolbar));
rv = static_cast<GtkWidget*>(g_list_nth(children, index)->data);
g_list_free(children);
}
return rv;
}
#endif
};
#if defined(OS_MACOSX)
// http://crbug.com/29709 port to Mac
#define MAYBE_Basic DISABLED_Basic
#define MAYBE_DynamicBrowserAction DISABLED_DynamicBrowserAction
#define MAYBE_TabSpecificBrowserActionState DISABLED_TabSpecificBrowserActionState
#else
#define MAYBE_Basic Basic
#define MAYBE_DynamicBrowserAction DynamicBrowserAction
#define MAYBE_TabSpecificBrowserActionState TabSpecificBrowserActionState
#endif
IN_PROC_BROWSER_TEST_F(BrowserActionTest, MAYBE_Basic) {
StartHTTPServer();
ASSERT_TRUE(RunExtensionTest("browser_action")) << message_;
// Test that there is a browser action in the toolbar.
ASSERT_EQ(1, NumberOfBrowserActions());
// Tell the extension to update the browser action state.
ResultCatcher catcher;
ExtensionsService* service = browser()->profile()->GetExtensionsService();
Extension* extension = service->extensions()->at(0);
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("update.html")));
ASSERT_TRUE(catcher.GetNextResult());
// Test that we received the changes.
ExtensionAction* action = extension->browser_action();
ASSERT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId));
ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId));
ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
// Simulate the browser action being clicked.
ui_test_utils::NavigateToURL(browser(),
GURL("http://localhost:1337/files/extensions/test_file.txt"));
ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
browser()->profile(), action->extension_id(), browser());
// Verify the command worked.
TabContents* tab = browser()->GetSelectedTabContents();
bool result = false;
ui_test_utils::ExecuteJavaScriptAndExtractBool(
tab->render_view_host(), L"",
L"setInterval(function(){"
L" if(document.body.bgColor == 'red'){"
L" window.domAutomationController.send(true)}}, 100)",
&result);
ASSERT_TRUE(result);
}
IN_PROC_BROWSER_TEST_F(BrowserActionTest, MAYBE_DynamicBrowserAction) {
ASSERT_TRUE(RunExtensionTest("browser_action_no_icon")) << message_;
// Test that there is a browser action in the toolbar and that it has no icon.
ASSERT_EQ(1, NumberOfBrowserActions());
EXPECT_TRUE(IsIconNull(0));
// Tell the extension to update the icon using setIcon({imageData:...}).
ResultCatcher catcher;
ExtensionsService* service = browser()->profile()->GetExtensionsService();
Extension* extension = service->extensions()->at(0);
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("update.html")));
ASSERT_TRUE(catcher.GetNextResult());
// Test that we received the changes.
EXPECT_FALSE(IsIconNull(0));
// Tell the extension to update using setIcon({path:...});
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("update2.html")));
ASSERT_TRUE(catcher.GetNextResult());
// Test that we received the changes.
EXPECT_FALSE(IsIconNull(0));
// TODO(aa): Would be nice here to actually compare that the pixels change.
}
IN_PROC_BROWSER_TEST_F(BrowserActionTest, MAYBE_TabSpecificBrowserActionState) {
ASSERT_TRUE(RunExtensionTest("browser_action_tab_specific_state")) <<
message_;
// Test that there is a browser action in the toolbar and that it has an icon.
ASSERT_EQ(1, NumberOfBrowserActions());
EXPECT_FALSE(IsIconNull(0));
// Execute the action, its title should change.
ResultCatcher catcher;
ExecuteBrowserAction(0);
ASSERT_TRUE(catcher.GetNextResult());
EXPECT_EQ("Showing icon 2", GetTooltip(0));
// Open a new tab, the title should go back.
browser()->NewTab();
EXPECT_EQ("hi!", GetTooltip(0));
// Go back to first tab, changed title should reappear.
browser()->SelectTabContentsAt(0, true);
EXPECT_EQ("Showing icon 2", GetTooltip(0));
// Reload that tab, default title should come back.
ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
EXPECT_EQ("hi!", GetTooltip(0));
}
// TODO(estade): http://crbug.com/29710 port to Mac & Linux
#if defined(OS_WIN)
IN_PROC_BROWSER_TEST_F(BrowserActionTest, BrowserActionPopup) {
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("popup")));
ResultCatcher catcher;
// This value is in api_test/popup/popup.html.
const int growFactor = 500;
ASSERT_GT(ExtensionPopup::kMinHeight + growFactor * 2,
ExtensionPopup::kMaxHeight);
ASSERT_GT(ExtensionPopup::kMinWidth + growFactor * 2,
ExtensionPopup::kMaxWidth);
// Our initial expected size.
int width = ExtensionPopup::kMinWidth;
int height = ExtensionPopup::kMinHeight;
BrowserActionsContainer* browser_actions =
browser()->window()->GetBrowserWindowTesting()->GetToolbarView()->
browser_actions();
ASSERT_TRUE(browser_actions);
// Simulate a click on the browser action and verify the size of the resulting
// popup. The first one tries to be 0x0, so it should be the min values.
ExecuteBrowserAction(0);
EXPECT_TRUE(browser_actions->TestGetPopup() != NULL);
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
gfx::Rect bounds = browser_actions->TestGetPopup()->view()->bounds();
EXPECT_EQ(width, bounds.width());
EXPECT_EQ(height, bounds.height());
browser_actions->HidePopup();
EXPECT_TRUE(browser_actions->TestGetPopup() == NULL);
// Do it again, and verify the new bigger size (the popup grows each time it's
// opened).
width = growFactor;
height = growFactor;
browser_actions->TestExecuteBrowserAction(0);
EXPECT_TRUE(browser_actions->TestGetPopup() != NULL);
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
bounds = browser_actions->TestGetPopup()->view()->bounds();
EXPECT_EQ(width, bounds.width());
EXPECT_EQ(height, bounds.height());
browser_actions->HidePopup();
EXPECT_TRUE(browser_actions->TestGetPopup() == NULL);
// One more time, but this time it should be constrained by the max values.
width = ExtensionPopup::kMaxWidth;
height = ExtensionPopup::kMaxHeight;
browser_actions->TestExecuteBrowserAction(0);
EXPECT_TRUE(browser_actions->TestGetPopup() != NULL);
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
bounds = browser_actions->TestGetPopup()->view()->bounds();
EXPECT_EQ(width, bounds.width());
EXPECT_EQ(height, bounds.height());
browser_actions->HidePopup();
EXPECT_TRUE(browser_actions->TestGetPopup() == NULL);
}
#endif // defined(OS_WIN)
|