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
|
// 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 "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/location_bar.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/extensions/extension_action.h"
#include "chrome/test/ui_test_utils.h"
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageAction) {
StartHTTPServer();
ASSERT_TRUE(RunExtensionTest("page_action")) << message_;
ExtensionsService* service = browser()->profile()->GetExtensionsService();
ASSERT_EQ(1u, service->extensions()->size());
Extension* extension = service->extensions()->at(0);
ASSERT_TRUE(extension);
{
// Tell the extension to update the page action state.
ResultCatcher catcher;
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("update.html")));
ASSERT_TRUE(catcher.GetNextResult());
}
// Test that we received the changes.
int tab_id =
browser()->GetSelectedTabContents()->controller().session_id().id();
ExtensionAction* action = extension->page_action();
ASSERT_TRUE(action);
EXPECT_EQ("Modified", action->GetTitle(tab_id));
{
// Simulate the page action being clicked.
ResultCatcher catcher;
int tab_id =
ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents());
ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
browser()->profile(), extension->id(), "", tab_id, "", 0);
EXPECT_TRUE(catcher.GetNextResult());
}
{
// Tell the extension to update the page action state again.
ResultCatcher catcher;
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("update2.html")));
ASSERT_TRUE(catcher.GetNextResult());
}
// Test that we received the changes.
tab_id = browser()->GetSelectedTabContents()->controller().session_id().id();
EXPECT_FALSE(action->GetIcon(tab_id).isNull());
}
// Tests old-style pageActions API that is deprecated but we don't want to
// break.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, OldPageActions) {
ASSERT_TRUE(RunExtensionTest("old_page_actions")) << message_;
ExtensionsService* service = browser()->profile()->GetExtensionsService();
ASSERT_EQ(1u, service->extensions()->size());
Extension* extension = service->extensions()->at(0);
ASSERT_TRUE(extension);
// Have the extension enable the page action.
{
ResultCatcher catcher;
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("page.html")));
ASSERT_TRUE(catcher.GetNextResult());
}
// Simulate the page action being clicked.
{
ResultCatcher catcher;
int tab_id =
ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents());
ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
browser()->profile(), extension->id(), "action", tab_id, "", 1);
EXPECT_TRUE(catcher.GetNextResult());
}
}
class PageActionPopupTest : public ExtensionApiTest {
public:
bool RunExtensionTest(const char* extension_name) {
last_action_ = NULL;
last_visibility_ = false;
waiting_ = false;
return ExtensionApiTest::RunExtensionTest(extension_name);
};
void Observe(NotificationType type, const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED: {
last_action_ = Source<ExtensionAction>(source).ptr();
TabContents* contents = Details<TabContents>(details).ptr();
int tab_id = ExtensionTabUtil::GetTabId(contents);
last_visibility_ = last_action_->GetIsVisible(tab_id);
if (waiting_)
MessageLoopForUI::current()->Quit();
break;
}
default:
ExtensionBrowserTest::Observe(type, source, details);
break;
}
}
void WaitForPopupVisibilityChange() {
// Wait for EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED to come in.
if (!last_action_) {
waiting_ = true;
ui_test_utils::RunMessageLoop();
waiting_ = false;
}
}
protected:
bool waiting_;
ExtensionAction* last_action_;
bool last_visibility_;
};
#if !defined(OS_WIN)
// The following test fails on Mac and Linux because some of page action
// implementation is missing in LocationBarView
// (see http://crbug.com/29898, http://crbug.com/30326)
#define MAYBE_Show DISABLED_Show
#else
#define MAYBE_Show Show
#endif
// Tests popups in page actions.
IN_PROC_BROWSER_TEST_F(PageActionPopupTest, MAYBE_Show) {
NotificationRegistrar registrar;
registrar.Add(this,
NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED,
NotificationService::AllSources());
ASSERT_TRUE(RunExtensionTest("page_action_popup")) << message_;
ExtensionsService* service = browser()->profile()->GetExtensionsService();
ASSERT_EQ(1u, service->extensions()->size());
Extension* extension = service->extensions()->at(0);
ASSERT_TRUE(extension);
// Wait for The page action to actually become visible.
if (!last_visibility_)
last_action_ = NULL;
WaitForPopupVisibilityChange();
ASSERT_TRUE(last_visibility_);
LocationBarTesting* location_bar =
browser()->window()->GetLocationBar()->GetLocationBarForTesting();
ASSERT_EQ(1, location_bar->PageActionVisibleCount());
ExtensionAction* action = location_bar->GetVisiblePageAction(0);
ASSERT_TRUE(action == last_action_);
{
ResultCatcher catcher;
location_bar->TestPageActionPressed(0);
ASSERT_TRUE(catcher.GetNextResult());
}
}
|