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
|
// Copyright (c) 2011 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 "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/favicon/favicon_service.h"
#include "chrome/browser/intents/web_intents_registry.h"
#include "chrome/browser/intents/web_intents_registry_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/intents/web_intent_picker.h"
#include "chrome/browser/ui/intents/web_intent_picker_controller.h"
#include "chrome/browser/ui/intents/web_intent_picker_factory.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_intents_dispatcher.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/glue/web_intent_service_data.h"
using content::WebContents;
using testing::_;
using testing::DoAll;
using testing::Return;
using testing::SaveArg;
namespace {
const string16 kAction1(ASCIIToUTF16("http://www.example.com/share"));
const string16 kAction2(ASCIIToUTF16("http://www.example.com/foobar"));
const string16 kType(ASCIIToUTF16("image/png"));
const GURL kServiceURL1("http://www.google.com");
const GURL kServiceURL2("http://www.chromium.org");
MATCHER_P(VectorIsOfSize, n, "") {
return arg.size() == static_cast<size_t>(n);
}
} // namespace
class WebIntentPickerMock : public WebIntentPicker {
public:
WebIntentPickerMock() : num_urls_(0), num_default_icons_(0) {}
virtual void SetServiceURLs(const std::vector<GURL>& urls) {
num_urls_ = urls.size();
}
virtual void SetServiceIcon(size_t index, const SkBitmap& icon) {}
virtual void SetDefaultServiceIcon(size_t index) {
num_default_icons_++;
}
virtual void WaitFor(int target_num_urls, int target_num_default_icons) {
while (num_urls_ != target_num_urls ||
num_default_icons_ != target_num_default_icons) {
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
ui_test_utils::RunAllPendingInMessageLoop();
}
}
virtual void Close() {}
WebContents* SetInlineDisposition(const GURL& url) { return NULL; }
int num_urls_;
int num_default_icons_;
};
class WebIntentPickerFactoryMock : public WebIntentPickerFactory {
public:
explicit WebIntentPickerFactoryMock(WebIntentPickerMock* mock)
: picker_(mock) {}
virtual WebIntentPicker* Create(Browser* browser,
TabContentsWrapper* wrapper,
WebIntentPickerDelegate* delegate) {
return picker_;
}
virtual void ClosePicker(WebIntentPicker* picker) {
if (picker_) {
picker_->Close();
picker_ = NULL;
}
}
void Close() {
picker_ = NULL;
}
WebIntentPicker* picker_;
};
class IntentsDispatcherMock : public content::WebIntentsDispatcher {
public:
explicit IntentsDispatcherMock(const webkit_glue::WebIntentData& intent)
: intent_(intent),
dispatched_(false) {}
virtual const webkit_glue::WebIntentData& GetIntent() {
return intent_;
}
virtual void DispatchIntent(WebContents* tab_contents) {
dispatched_ = true;
}
virtual void SendReplyMessage(webkit_glue::WebIntentReplyType reply_type,
const string16& data) {
}
virtual void RegisterReplyNotification(const base::Closure&) {
}
webkit_glue::WebIntentData intent_;
bool dispatched_;
};
class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest {
protected:
void AddWebIntentService(const string16& action,
const GURL& service_url) {
webkit_glue::WebIntentServiceData service;
service.action = action;
service.type = kType;
service.service_url = service_url;
web_data_service_->AddWebIntentService(service);
}
void OnSendReturnMessage(WebIntentPickerController* controller) {
controller->OnSendReturnMessage();
}
void OnServiceChosen(WebIntentPickerController* controller, size_t index) {
controller->OnServiceChosen(index);
}
void SetPickerFactory(WebIntentPickerController* controller,
WebIntentPickerFactory* factory) {
controller->picker_factory_.reset(factory);
}
WebIntentPickerMock picker_;
// The picker controller takes ownership.
WebIntentPickerFactoryMock* picker_factory_;
WebDataService* web_data_service_;
FaviconService* favicon_service_;
};
IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) {
web_data_service_ =
browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
AddWebIntentService(kAction1, kServiceURL1);
AddWebIntentService(kAction1, kServiceURL2);
favicon_service_ =
browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
picker_factory_ = new WebIntentPickerFactoryMock(&picker_);
WebIntentPickerController* controller = browser()->
GetSelectedTabContentsWrapper()->web_intent_picker_controller();
SetPickerFactory(controller, picker_factory_);
controller->ShowDialog(browser(), kAction1, kType);
picker_.WaitFor(2, 2);
EXPECT_EQ(2, picker_.num_urls_);
EXPECT_EQ(2, picker_.num_default_icons_);
webkit_glue::WebIntentData intent;
intent.action = ASCIIToUTF16("a");
intent.type = ASCIIToUTF16("b");
IntentsDispatcherMock* host = new IntentsDispatcherMock(intent);
controller->SetIntentsDispatcher(host);
OnServiceChosen(controller, 1);
ASSERT_EQ(2, browser()->tab_count());
EXPECT_EQ(GURL(kServiceURL2),
browser()->GetSelectedWebContents()->GetURL());
EXPECT_TRUE(host->dispatched_);
OnSendReturnMessage(controller);
ASSERT_EQ(1, browser()->tab_count());
}
|