| 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() |
| 50 | 41 : num_items_(0), |
| 51 virtual void SetServiceURLs(const std::vector<GURL>& urls) { | 42 num_icons_changed_(0), |
| 52 num_urls_ = urls.size(); | 43 message_loop_started_(false), |
| 44 pending_async_completed_(false) { |
| 53 } | 45 } |
| 54 | 46 |
| 55 virtual void SetServiceIcon(size_t index, const SkBitmap& icon) {} | 47 virtual void OnModelChanged(WebIntentPickerModel* model) OVERRIDE { |
| 56 | 48 num_items_ = static_cast<int>(model->GetItemCount()); |
| 57 virtual void SetDefaultServiceIcon(size_t index) { | |
| 58 num_default_icons_++; | |
| 59 } | 49 } |
| 60 | 50 |
| 61 virtual void WaitFor(int target_num_urls, int target_num_default_icons) { | 51 virtual void OnFaviconChanged( |
| 62 while (num_urls_ != target_num_urls || | 52 WebIntentPickerModel* model, size_t index) OVERRIDE { |
| 63 num_default_icons_ != target_num_default_icons) { | 53 num_icons_changed_++; |
| 64 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 54 } |
| 65 ui_test_utils::RunAllPendingInMessageLoop(); | 55 |
| 56 virtual void OnInlineDisposition(WebIntentPickerModel* model) OVERRIDE {} |
| 57 virtual void Close() OVERRIDE {} |
| 58 |
| 59 virtual void OnPendingAsyncCompleted() OVERRIDE { |
| 60 pending_async_completed_ = true; |
| 61 |
| 62 if (message_loop_started_) |
| 63 MessageLoop::current()->Quit(); |
| 64 } |
| 65 |
| 66 void WaitForPendingAsync() { |
| 67 if (!pending_async_completed_) { |
| 68 message_loop_started_ = true; |
| 69 ui_test_utils::RunMessageLoop(); |
| 66 } | 70 } |
| 67 } | 71 } |
| 68 | 72 |
| 69 virtual void Close() {} | 73 int num_items_; |
| 70 | 74 int num_icons_changed_; |
| 71 WebContents* SetInlineDisposition(const GURL& url) { return NULL; } | 75 bool message_loop_started_; |
| 72 | 76 bool pending_async_completed_; |
| 73 int num_urls_; | |
| 74 int num_default_icons_; | |
| 75 }; | |
| 76 | |
| 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 }; | 77 }; |
| 102 | 78 |
| 103 class IntentsDispatcherMock : public content::WebIntentsDispatcher { | 79 class IntentsDispatcherMock : public content::WebIntentsDispatcher { |
| 104 public: | 80 public: |
| 105 explicit IntentsDispatcherMock(const webkit_glue::WebIntentData& intent) | 81 explicit IntentsDispatcherMock(const webkit_glue::WebIntentData& intent) |
| 106 : intent_(intent), | 82 : intent_(intent), |
| 107 dispatched_(false) {} | 83 dispatched_(false) {} |
| 108 | 84 |
| 109 virtual const webkit_glue::WebIntentData& GetIntent() { | 85 virtual const webkit_glue::WebIntentData& GetIntent() OVERRIDE { |
| 110 return intent_; | 86 return intent_; |
| 111 } | 87 } |
| 112 | 88 |
| 113 virtual void DispatchIntent(WebContents* tab_contents) { | 89 virtual void DispatchIntent(content::WebContents* web_contents) OVERRIDE { |
| 114 dispatched_ = true; | 90 dispatched_ = true; |
| 115 } | 91 } |
| 116 | 92 |
| 117 virtual void SendReplyMessage(webkit_glue::WebIntentReplyType reply_type, | 93 virtual void SendReplyMessage(webkit_glue::WebIntentReplyType reply_type, |
| 118 const string16& data) { | 94 const string16& data) OVERRIDE { |
| 119 } | 95 } |
| 120 | 96 |
| 121 virtual void RegisterReplyNotification(const base::Closure&) { | 97 virtual void RegisterReplyNotification(const base::Closure&) OVERRIDE { |
| 122 } | 98 } |
| 123 | 99 |
| 124 webkit_glue::WebIntentData intent_; | 100 webkit_glue::WebIntentData intent_; |
| 125 bool dispatched_; | 101 bool dispatched_; |
| 126 }; | 102 }; |
| 127 | 103 |
| 128 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest { | 104 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest { |
| 129 protected: | 105 protected: |
| 130 void AddWebIntentService(const string16& action, | 106 typedef WebIntentPickerModel::Disposition Disposition; |
| 131 const GURL& service_url) { | 107 |
| 108 WebIntentPickerControllerBrowserTest() {} |
| 109 |
| 110 virtual void SetUpOnMainThread() OVERRIDE { |
| 111 web_data_service_ = |
| 112 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| 113 favicon_service_ = |
| 114 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 115 controller_ = browser()-> |
| 116 GetSelectedTabContentsWrapper()->web_intent_picker_controller(); |
| 117 |
| 118 controller_->set_picker(&picker_); |
| 119 controller_->set_model_observer(&picker_); |
| 120 } |
| 121 |
| 122 void AddWebIntentService(const string16& action, const GURL& service_url) { |
| 132 webkit_glue::WebIntentServiceData service; | 123 webkit_glue::WebIntentServiceData service; |
| 133 service.action = action; | 124 service.action = action; |
| 134 service.type = kType; | 125 service.type = kType; |
| 135 service.service_url = service_url; | 126 service.service_url = service_url; |
| 136 web_data_service_->AddWebIntentService(service); | 127 web_data_service_->AddWebIntentService(service); |
| 137 } | 128 } |
| 138 | 129 |
| 139 void OnSendReturnMessage(WebIntentPickerController* controller) { | 130 void OnSendReturnMessage() { |
| 140 controller->OnSendReturnMessage(); | 131 controller_->OnSendReturnMessage(); |
| 141 } | 132 } |
| 142 | 133 |
| 143 void OnServiceChosen(WebIntentPickerController* controller, size_t index) { | 134 void OnServiceChosen(size_t index, Disposition disposition) { |
| 144 controller->OnServiceChosen(index); | 135 controller_->OnServiceChosen(index, disposition); |
| 145 } | 136 } |
| 146 | 137 |
| 147 void SetPickerFactory(WebIntentPickerController* controller, | 138 void OnCancelled() { |
| 148 WebIntentPickerFactory* factory) { | 139 controller_->OnCancelled(); |
| 149 controller->picker_factory_.reset(factory); | |
| 150 } | 140 } |
| 151 | 141 |
| 152 WebIntentPickerMock picker_; | 142 WebIntentPickerMock picker_; |
| 153 | |
| 154 // The picker controller takes ownership. | |
| 155 WebIntentPickerFactoryMock* picker_factory_; | |
| 156 | |
| 157 WebDataService* web_data_service_; | 143 WebDataService* web_data_service_; |
| 158 FaviconService* favicon_service_; | 144 FaviconService* favicon_service_; |
| 145 WebIntentPickerController* controller_; |
| 159 }; | 146 }; |
| 160 | 147 |
| 161 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) { | 148 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) { |
| 162 web_data_service_ = | |
| 163 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); | |
| 164 AddWebIntentService(kAction1, kServiceURL1); | 149 AddWebIntentService(kAction1, kServiceURL1); |
| 165 AddWebIntentService(kAction1, kServiceURL2); | 150 AddWebIntentService(kAction1, kServiceURL2); |
| 166 | 151 |
| 167 favicon_service_ = | 152 controller_->ShowDialog(browser(), kAction1, kType); |
| 168 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); | 153 picker_.WaitForPendingAsync(); |
| 169 | 154 EXPECT_EQ(2, picker_.num_items_); |
| 170 picker_factory_ = new WebIntentPickerFactoryMock(&picker_); | 155 EXPECT_EQ(0, picker_.num_icons_changed_); |
| 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_); | |
| 179 | 156 |
| 180 webkit_glue::WebIntentData intent; | 157 webkit_glue::WebIntentData intent; |
| 181 intent.action = ASCIIToUTF16("a"); | 158 intent.action = ASCIIToUTF16("a"); |
| 182 intent.type = ASCIIToUTF16("b"); | 159 intent.type = ASCIIToUTF16("b"); |
| 183 IntentsDispatcherMock* host = new IntentsDispatcherMock(intent); | 160 IntentsDispatcherMock* host = new IntentsDispatcherMock(intent); |
| 184 controller->SetIntentsDispatcher(host); | 161 controller_->SetIntentsDispatcher(host); |
| 185 | 162 |
| 186 OnServiceChosen(controller, 1); | 163 OnServiceChosen(1, WebIntentPickerModel::DISPOSITION_WINDOW); |
| 187 ASSERT_EQ(2, browser()->tab_count()); | 164 ASSERT_EQ(2, browser()->tab_count()); |
| 188 EXPECT_EQ(GURL(kServiceURL2), | 165 EXPECT_EQ(GURL(kServiceURL2), |
| 189 browser()->GetSelectedWebContents()->GetURL()); | 166 browser()->GetSelectedWebContents()->GetURL()); |
| 190 | 167 |
| 191 EXPECT_TRUE(host->dispatched_); | 168 EXPECT_TRUE(host->dispatched_); |
| 192 | 169 |
| 193 OnSendReturnMessage(controller); | 170 OnSendReturnMessage(); |
| 194 ASSERT_EQ(1, browser()->tab_count()); | 171 ASSERT_EQ(1, browser()->tab_count()); |
| 195 } | 172 } |
| 173 |
| 174 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, OpenCancelOpen) { |
| 175 AddWebIntentService(kAction1, kServiceURL1); |
| 176 AddWebIntentService(kAction1, kServiceURL2); |
| 177 |
| 178 controller_->ShowDialog(browser(), kAction1, kType); |
| 179 picker_.WaitForPendingAsync(); |
| 180 OnCancelled(); |
| 181 |
| 182 controller_->ShowDialog(browser(), kAction1, kType); |
| 183 OnCancelled(); |
| 184 } |
| OLD | NEW |