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