Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1913)

Side by Side Diff: chrome/browser/ui/intents/web_intent_picker_controller_unittest.cc

Issue 9148032: [Web Intents] Refactor picker to use WebIntentPickerModel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: groby's fix Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #include "testing/gtest/include/gtest/gtest.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/favicon/favicon_service.h"
13 #include "chrome/browser/intents/web_intents_registry.h"
14 #include "chrome/browser/intents/web_intents_registry_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/intents/web_intent_picker.h"
18 #include "chrome/browser/ui/intents/web_intent_picker_controller.h"
19 #include "chrome/browser/ui/intents/web_intent_picker_factory.h"
20 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
21 #include "chrome/browser/webdata/web_data_service.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/browser/tab_contents/test_tab_contents.h"
24 #include "content/test/test_browser_thread.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "ui/gfx/codec/png_codec.h"
27 #include "webkit/glue/web_intent_service_data.h"
28
29 using content::BrowserThread;
30 using content::WebContents;
31 using testing::_;
32 using testing::AtMost;
33 using testing::DoAll;
34 using testing::Invoke;
35 using testing::Return;
36 using testing::SaveArg;
37
38 namespace {
39
40 const string16 kAction1(ASCIIToUTF16("http://www.example.com/share"));
41 const string16 kAction2(ASCIIToUTF16("http://www.example.com/foobar"));
42 const string16 kType(ASCIIToUTF16("image/png"));
43 const GURL kServiceURL1("http://www.google.com");
44 const GURL kServiceURL2("http://www.chromium.org");
45 const GURL kServiceURL3("http://www.test.com");
46
47 // Fill the given bmp with valid png data.
48 void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
49 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
50 bmp->allocPixels();
51
52 unsigned char* src_data =
53 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0));
54 for (int i = 0; i < w * h; i++) {
55 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
56 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
57 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
58 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
59 }
60 }
61
62 // Fill the given data buffer with valid png data.
63 void FillBitmap(int w, int h, std::vector<unsigned char>* output) {
64 SkBitmap bitmap;
65 FillDataToBitmap(w, h, &bitmap);
66 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output);
67 }
68
69 GURL MakeFaviconURL(const GURL& url) {
70 return GURL(url.spec() + "/favicon.png");
71 }
72
73 MATCHER_P(VectorIsOfSize, n, "") {
74 return arg.size() == static_cast<size_t>(n);
75 }
76
77 } // namespace
78
79 class WebIntentPickerMock : public WebIntentPicker {
80 public:
81 MOCK_METHOD1(SetServiceURLs, void(const std::vector<GURL>& urls));
82 MOCK_METHOD2(SetServiceIcon, void(size_t index, const SkBitmap& icon));
83 MOCK_METHOD1(SetDefaultServiceIcon, void(size_t index));
84 MOCK_METHOD0(Show, void(void));
85 MOCK_METHOD0(Close, void(void));
86
87 WebContents* SetInlineDisposition(const GURL& url) { return NULL; }
88 };
89
90 class WebIntentPickerFactoryMock : public WebIntentPickerFactory {
91 public:
92 MOCK_METHOD3(Create,
93 WebIntentPicker*(Browser* browser,
94 TabContentsWrapper* wrapper,
95 WebIntentPickerDelegate* delegate));
96 MOCK_METHOD1(ClosePicker, void(WebIntentPicker* picker));
97 };
98
99 class TestWebIntentPickerController : public WebIntentPickerController {
100 public:
101 TestWebIntentPickerController(TabContentsWrapper* wrapper,
102 WebIntentPickerFactory* factory)
103 : WebIntentPickerController(wrapper, factory) {
104 }
105
106 MOCK_METHOD1(OnServiceChosen, void(size_t index));
107 MOCK_METHOD0(OnCancelled, void(void));
108 MOCK_METHOD0(OnClosing, void(void));
109
110 // helper functions to forward to the base class.
111 void BaseOnServiceChosen(size_t index) {
112 WebIntentPickerController::OnServiceChosen(index);
113 }
114
115 void BaseOnCancelled() {
116 WebIntentPickerController::OnCancelled();
117 }
118
119 void BaseOnClosing() {
120 WebIntentPickerController::OnClosing();
121 }
122 };
123
124 class WebIntentPickerControllerTest : public TabContentsWrapperTestHarness {
125 public:
126 WebIntentPickerControllerTest()
127 : ui_thread_(BrowserThread::UI, &message_loop_),
128 db_thread_(BrowserThread::DB, &message_loop_),
129 picker_factory_(NULL),
130 delegate_(NULL) {
131 }
132
133 virtual void SetUp() {
134 TabContentsWrapperTestHarness::SetUp();
135
136 profile()->CreateFaviconService();
137 profile()->CreateWebDataService(true);
138 web_data_service_ = profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
139 favicon_service_ = profile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
140 WebIntentsRegistry *registry =
141 WebIntentsRegistryFactory::GetForProfile(profile());
142 // TODO(groby): We should not require a call to Initialize() here.
143 registry->Initialize(web_data_service_, NULL);
144
145 picker_factory_ = new WebIntentPickerFactoryMock();
146 controller_.reset(new TestWebIntentPickerController(contents_wrapper(),
147 picker_factory_));
148 }
149
150 virtual void TearDown() {
151 controller_.reset();
152
153 TabContentsWrapperTestHarness::TearDown();
154 }
155
156 protected:
157 void AddWebIntentService(const string16& action,
158 const GURL& service_url) {
159 webkit_glue::WebIntentServiceData service;
160 service.action = action;
161 service.type = kType;
162 service.service_url = service_url;
163 web_data_service_->AddWebIntentService(service);
164 }
165
166 void AddFaviconForURL(const GURL& url) {
167 std::vector<unsigned char> image_data;
168 FillBitmap(16, 16, &image_data);
169
170 favicon_service_->SetFavicon(url,
171 MakeFaviconURL(url),
172 image_data,
173 history::FAVICON);
174 }
175
176 void SetPickerExpectations(int expected_service_count,
177 int expected_default_favicons) {
178 EXPECT_CALL(*picker_factory_, Create(_, _, _)).
179 WillOnce(DoAll(SaveArg<2>(&delegate_), Return(&picker_)));
180 EXPECT_CALL(picker_,
181 SetServiceURLs(VectorIsOfSize(expected_service_count))).
182 Times(1);
183 EXPECT_CALL(picker_, SetDefaultServiceIcon(_)).
184 Times(expected_default_favicons);
185 }
186
187 void CheckPendingAsync() {
188 if (controller_->pending_async_count() > 0) {
189 MessageLoop::current()->PostTask(
190 FROM_HERE,
191 base::Bind(&WebIntentPickerControllerTest::CheckPendingAsync,
192 base::Unretained(this)));
193 return;
194 }
195
196 MessageLoop::current()->Quit();
197 }
198
199 void WaitForDialogToShow() {
200 CheckPendingAsync();
201 MessageLoop::current()->Run();
202 }
203
204 content::TestBrowserThread ui_thread_;
205 content::TestBrowserThread db_thread_;
206 WebIntentPickerMock picker_;
207
208 // |controller_| takes ownership.
209 WebIntentPickerFactoryMock* picker_factory_;
210
211 scoped_ptr<TestWebIntentPickerController> controller_;
212 WebIntentPickerDelegate* delegate_;
213 WebDataService* web_data_service_;
214 FaviconService* favicon_service_;
215 };
216
217 TEST_F(WebIntentPickerControllerTest, ShowDialogWith3Services) {
218 SetPickerExpectations(3, 3);
219 AddWebIntentService(kAction1, kServiceURL1);
220 AddWebIntentService(kAction1, kServiceURL2);
221 AddWebIntentService(kAction1, kServiceURL3);
222
223 controller_->ShowDialog(NULL, kAction1, kType);
224 WaitForDialogToShow();
225 }
226
227 TEST_F(WebIntentPickerControllerTest, ShowDialogWithNoServices) {
228 SetPickerExpectations(0, 0);
229
230 EXPECT_CALL(picker_, SetServiceIcon(_, _)).Times(0);
231
232 controller_->ShowDialog(NULL, kAction1, kType);
233 WaitForDialogToShow();
234 }
235
236 // TODO(binji) SetServiceIcon isn't called unless I create the HistoryService,
237 // but when I do, the test hangs...
238 TEST_F(WebIntentPickerControllerTest, DISABLED_ShowFavicon) {
239 SetPickerExpectations(3, 2);
240 AddWebIntentService(kAction1, kServiceURL1);
241 AddWebIntentService(kAction1, kServiceURL2);
242 AddWebIntentService(kAction1, kServiceURL3);
243 AddFaviconForURL(kServiceURL1);
244 AddFaviconForURL(kServiceURL3);
245
246 EXPECT_CALL(picker_, SetServiceIcon(0, _)).Times(1);
247 EXPECT_CALL(picker_, SetDefaultServiceIcon(1)).Times(1);
248 EXPECT_CALL(picker_, SetServiceIcon(2, _)).Times(1);
249
250 controller_->ShowDialog(NULL, kAction1, kType);
251 WaitForDialogToShow();
252 }
253
254 TEST_F(WebIntentPickerControllerTest, Cancel) {
255 SetPickerExpectations(2, 2);
256 AddWebIntentService(kAction1, kServiceURL1);
257 AddWebIntentService(kAction1, kServiceURL2);
258
259 EXPECT_CALL(*controller_, OnServiceChosen(0))
260 .Times(0);
261 EXPECT_CALL(*controller_, OnCancelled()).Times(0);
262 EXPECT_CALL(*controller_, OnClosing())
263 .WillOnce(Invoke(controller_.get(),
264 &TestWebIntentPickerController::BaseOnClosing));
265
266 controller_->ShowDialog(NULL, kAction1, kType);
267 WaitForDialogToShow();
268 delegate_->OnClosing();
269 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698