| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/favicon/favicon_service.h" | 9 #include "chrome/browser/favicon/favicon_service.h" |
| 10 #include "chrome/browser/intents/web_intents_registry.h" | 10 #include "chrome/browser/intents/web_intents_registry.h" |
| 11 #include "chrome/browser/intents/web_intents_registry_factory.h" | 11 #include "chrome/browser/intents/web_intents_registry_factory.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/intents/web_intent_picker.h" | 14 #include "chrome/browser/ui/intents/web_intent_picker.h" |
| 15 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" | 15 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" |
| 16 #include "chrome/browser/ui/intents/web_intent_picker_factory.h" | 16 #include "chrome/browser/ui/intents/web_intent_picker_model.h" |
| 17 #include "chrome/browser/ui/intents/web_intent_picker_model_observer.h" |
| 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 18 #include "chrome/browser/webdata/web_data_service.h" | 19 #include "chrome/browser/webdata/web_data_service.h" |
| 19 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/browser/web_intents_dispatcher.h" | 23 #include "content/public/browser/web_intents_dispatcher.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "webkit/glue/web_intent_service_data.h" | 25 #include "webkit/glue/web_intent_service_data.h" |
| 26 | 26 |
| 27 using content::WebContents; | |
| 28 using testing::_; | |
| 29 using testing::DoAll; | |
| 30 using testing::Return; | |
| 31 using testing::SaveArg; | |
| 32 | |
| 33 namespace { | 27 namespace { |
| 34 | 28 |
| 35 const string16 kAction1(ASCIIToUTF16("http://www.example.com/share")); | 29 const string16 kAction1(ASCIIToUTF16("http://www.example.com/share")); |
| 36 const string16 kAction2(ASCIIToUTF16("http://www.example.com/foobar")); | 30 const string16 kAction2(ASCIIToUTF16("http://www.example.com/foobar")); |
| 37 const string16 kType(ASCIIToUTF16("image/png")); | 31 const string16 kType(ASCIIToUTF16("image/png")); |
| 38 const GURL kServiceURL1("http://www.google.com"); | 32 const GURL kServiceURL1("http://www.google.com"); |
| 39 const GURL kServiceURL2("http://www.chromium.org"); | 33 const GURL kServiceURL2("http://www.chromium.org"); |
| 40 | 34 |
| 41 MATCHER_P(VectorIsOfSize, n, "") { | |
| 42 return arg.size() == static_cast<size_t>(n); | |
| 43 } | |
| 44 | |
| 45 } // namespace | 35 } // namespace |
| 46 | 36 |
| 47 class WebIntentPickerMock : public WebIntentPicker { | 37 class WebIntentPickerMock : public WebIntentPicker, |
| 38 public WebIntentPickerModelObserver { |
| 48 public: | 39 public: |
| 49 WebIntentPickerMock() : num_urls_(0), num_default_icons_(0) {} | 40 WebIntentPickerMock() : num_items_(0), num_default_icons_(0) { |
| 50 | |
| 51 virtual void SetServiceURLs(const std::vector<GURL>& urls) { | |
| 52 num_urls_ = urls.size(); | |
| 53 } | 41 } |
| 54 | 42 |
| 55 virtual void SetServiceIcon(size_t index, const SkBitmap& icon) {} | 43 virtual void OnModelChanged(WebIntentPickerModel* model) OVERRIDE { |
| 56 | 44 num_items_ = static_cast<int>(model->GetItemCount()); |
| 57 virtual void SetDefaultServiceIcon(size_t index) { | 45 num_default_icons_ = GetNumDefaultIcons(model); |
| 58 num_default_icons_++; | |
| 59 } | 46 } |
| 60 | 47 |
| 61 virtual void WaitFor(int target_num_urls, int target_num_default_icons) { | 48 virtual void OnFaviconChanged( |
| 62 while (num_urls_ != target_num_urls || | 49 WebIntentPickerModel* model, size_t index) OVERRIDE { |
| 63 num_default_icons_ != target_num_default_icons) { | 50 num_default_icons_ = GetNumDefaultIcons(model); |
| 64 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 65 ui_test_utils::RunAllPendingInMessageLoop(); | |
| 66 } | |
| 67 } | 51 } |
| 68 | 52 |
| 69 virtual void Close() {} | 53 virtual void OnInlineDisposition(WebIntentPickerModel* model) OVERRIDE {} |
| 54 virtual void Close() OVERRIDE {} |
| 70 | 55 |
| 71 WebContents* SetInlineDisposition(const GURL& url) { return NULL; } | 56 int GetNumDefaultIcons(WebIntentPickerModel* model) const { |
| 57 int num_default_icons = 0; |
| 58 for (size_t i = 0; i < model->GetItemCount(); ++i) { |
| 59 const WebIntentPickerModel::Item& item = model->GetItemAt(i); |
| 60 if (item.favicon.get() == NULL) |
| 61 ++num_default_icons; |
| 62 } |
| 72 | 63 |
| 73 int num_urls_; | 64 return num_default_icons; |
| 65 } |
| 66 |
| 67 int num_items_; |
| 74 int num_default_icons_; | 68 int num_default_icons_; |
| 75 }; | 69 }; |
| 76 | 70 |
| 77 | |
| 78 class WebIntentPickerFactoryMock : public WebIntentPickerFactory { | |
| 79 public: | |
| 80 explicit WebIntentPickerFactoryMock(WebIntentPickerMock* mock) | |
| 81 : picker_(mock) {} | |
| 82 | |
| 83 virtual WebIntentPicker* Create(Browser* browser, | |
| 84 TabContentsWrapper* wrapper, | |
| 85 WebIntentPickerDelegate* delegate) { | |
| 86 return picker_; | |
| 87 } | |
| 88 | |
| 89 virtual void ClosePicker(WebIntentPicker* picker) { | |
| 90 if (picker_) { | |
| 91 picker_->Close(); | |
| 92 picker_ = NULL; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void Close() { | |
| 97 picker_ = NULL; | |
| 98 } | |
| 99 | |
| 100 WebIntentPicker* picker_; | |
| 101 }; | |
| 102 | |
| 103 class IntentsDispatcherMock : public content::WebIntentsDispatcher { | 71 class IntentsDispatcherMock : public content::WebIntentsDispatcher { |
| 104 public: | 72 public: |
| 105 explicit IntentsDispatcherMock(const webkit_glue::WebIntentData& intent) | 73 explicit IntentsDispatcherMock(const webkit_glue::WebIntentData& intent) |
| 106 : intent_(intent), | 74 : intent_(intent), |
| 107 dispatched_(false) {} | 75 dispatched_(false) {} |
| 108 | 76 |
| 109 virtual const webkit_glue::WebIntentData& GetIntent() { | 77 virtual const webkit_glue::WebIntentData& GetIntent() OVERRIDE { |
| 110 return intent_; | 78 return intent_; |
| 111 } | 79 } |
| 112 | 80 |
| 113 virtual void DispatchIntent(WebContents* tab_contents) { | 81 virtual void DispatchIntent(content::WebContents* web_contents) OVERRIDE { |
| 114 dispatched_ = true; | 82 dispatched_ = true; |
| 115 } | 83 } |
| 116 | 84 |
| 117 virtual void SendReplyMessage(webkit_glue::WebIntentReplyType reply_type, | 85 virtual void SendReplyMessage(webkit_glue::WebIntentReplyType reply_type, |
| 118 const string16& data) { | 86 const string16& data) OVERRIDE { |
| 119 } | 87 } |
| 120 | 88 |
| 121 virtual void RegisterReplyNotification(const base::Closure&) { | 89 virtual void RegisterReplyNotification(const base::Closure&) OVERRIDE { |
| 122 } | 90 } |
| 123 | 91 |
| 124 webkit_glue::WebIntentData intent_; | 92 webkit_glue::WebIntentData intent_; |
| 125 bool dispatched_; | 93 bool dispatched_; |
| 126 }; | 94 }; |
| 127 | 95 |
| 128 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest { | 96 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest { |
| 129 protected: | 97 protected: |
| 98 typedef WebIntentPickerModel::Disposition Disposition; |
| 99 |
| 100 WebIntentPickerControllerBrowserTest() {} |
| 101 |
| 102 virtual void SetUpOnMainThread() OVERRIDE { |
| 103 web_data_service_ = |
| 104 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| 105 favicon_service_ = |
| 106 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 107 controller_ = browser()-> |
| 108 GetSelectedTabContentsWrapper()->web_intent_picker_controller(); |
| 109 |
| 110 controller_->set_picker(&picker_); |
| 111 controller_->set_model_observer(&picker_); |
| 112 } |
| 113 |
| 130 void AddWebIntentService(const string16& action, | 114 void AddWebIntentService(const string16& action, |
| 131 const GURL& service_url) { | 115 const GURL& service_url) { |
| 132 webkit_glue::WebIntentServiceData service; | 116 webkit_glue::WebIntentServiceData service; |
| 133 service.action = action; | 117 service.action = action; |
| 134 service.type = kType; | 118 service.type = kType; |
| 135 service.service_url = service_url; | 119 service.service_url = service_url; |
| 136 web_data_service_->AddWebIntentService(service); | 120 web_data_service_->AddWebIntentService(service); |
| 137 } | 121 } |
| 138 | 122 |
| 139 void OnSendReturnMessage(WebIntentPickerController* controller) { | 123 void OnSendReturnMessage() { |
| 140 controller->OnSendReturnMessage(); | 124 controller_->OnSendReturnMessage(); |
| 141 } | 125 } |
| 142 | 126 |
| 143 void OnServiceChosen(WebIntentPickerController* controller, size_t index) { | 127 void OnServiceChosen(size_t index, Disposition disposition) { |
| 144 controller->OnServiceChosen(index); | 128 controller_->OnServiceChosen(index, disposition); |
| 145 } | 129 } |
| 146 | 130 |
| 147 void SetPickerFactory(WebIntentPickerController* controller, | 131 void OnCancelled() { |
| 148 WebIntentPickerFactory* factory) { | 132 controller_->OnCancelled(); |
| 149 controller->picker_factory_.reset(factory); | 133 } |
| 134 |
| 135 void CheckPendingAsync() { |
| 136 if (controller_->pending_async_count() > 0) { |
| 137 MessageLoop::current()->PostTask( |
| 138 FROM_HERE, |
| 139 base::Bind(&WebIntentPickerControllerBrowserTest::CheckPendingAsync, |
| 140 base::Unretained(this))); |
| 141 return; |
| 142 } |
| 143 |
| 144 MessageLoop::current()->Quit(); |
| 145 } |
| 146 |
| 147 void WaitForPendingAsync() { |
| 148 CheckPendingAsync(); |
| 149 MessageLoop::current()->Run(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 WebIntentPickerMock picker_; | 152 WebIntentPickerMock picker_; |
| 153 | |
| 154 // The picker controller takes ownership. | |
| 155 WebIntentPickerFactoryMock* picker_factory_; | |
| 156 | |
| 157 WebDataService* web_data_service_; | 153 WebDataService* web_data_service_; |
| 158 FaviconService* favicon_service_; | 154 FaviconService* favicon_service_; |
| 155 WebIntentPickerController* controller_; |
| 159 }; | 156 }; |
| 160 | 157 |
| 161 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) { | 158 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) { |
| 162 web_data_service_ = | |
| 163 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); | |
| 164 AddWebIntentService(kAction1, kServiceURL1); | 159 AddWebIntentService(kAction1, kServiceURL1); |
| 165 AddWebIntentService(kAction1, kServiceURL2); | 160 AddWebIntentService(kAction1, kServiceURL2); |
| 166 | 161 |
| 167 favicon_service_ = | 162 controller_->ShowDialog(browser(), kAction1, kType); |
| 168 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); | 163 WaitForPendingAsync(); |
| 169 | 164 EXPECT_EQ(2, picker_.num_items_); |
| 170 picker_factory_ = new WebIntentPickerFactoryMock(&picker_); | |
| 171 WebIntentPickerController* controller = browser()-> | |
| 172 GetSelectedTabContentsWrapper()->web_intent_picker_controller(); | |
| 173 SetPickerFactory(controller, picker_factory_); | |
| 174 | |
| 175 controller->ShowDialog(browser(), kAction1, kType); | |
| 176 picker_.WaitFor(2, 2); | |
| 177 EXPECT_EQ(2, picker_.num_urls_); | |
| 178 EXPECT_EQ(2, picker_.num_default_icons_); | 165 EXPECT_EQ(2, picker_.num_default_icons_); |
| 179 | 166 |
| 180 webkit_glue::WebIntentData intent; | 167 webkit_glue::WebIntentData intent; |
| 181 intent.action = ASCIIToUTF16("a"); | 168 intent.action = ASCIIToUTF16("a"); |
| 182 intent.type = ASCIIToUTF16("b"); | 169 intent.type = ASCIIToUTF16("b"); |
| 183 IntentsDispatcherMock* host = new IntentsDispatcherMock(intent); | 170 IntentsDispatcherMock* host = new IntentsDispatcherMock(intent); |
| 184 controller->SetIntentsDispatcher(host); | 171 controller_->SetIntentsDispatcher(host); |
| 185 | 172 |
| 186 OnServiceChosen(controller, 1); | 173 OnServiceChosen(1, WebIntentPickerModel::DISPOSITION_WINDOW); |
| 187 ASSERT_EQ(2, browser()->tab_count()); | 174 ASSERT_EQ(2, browser()->tab_count()); |
| 188 EXPECT_EQ(GURL(kServiceURL2), | 175 EXPECT_EQ(GURL(kServiceURL2), |
| 189 browser()->GetSelectedWebContents()->GetURL()); | 176 browser()->GetSelectedWebContents()->GetURL()); |
| 190 | 177 |
| 191 EXPECT_TRUE(host->dispatched_); | 178 EXPECT_TRUE(host->dispatched_); |
| 192 | 179 |
| 193 OnSendReturnMessage(controller); | 180 OnSendReturnMessage(); |
| 194 ASSERT_EQ(1, browser()->tab_count()); | 181 ASSERT_EQ(1, browser()->tab_count()); |
| 195 } | 182 } |
| 183 |
| 184 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, OpenCancelOpen) { |
| 185 AddWebIntentService(kAction1, kServiceURL1); |
| 186 AddWebIntentService(kAction1, kServiceURL2); |
| 187 |
| 188 controller_->ShowDialog(browser(), kAction1, kType); |
| 189 WaitForPendingAsync(); |
| 190 OnCancelled(); |
| 191 |
| 192 controller_->ShowDialog(browser(), kAction1, kType); |
| 193 OnCancelled(); |
| 194 } |
| OLD | NEW |