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

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

Issue 9148032: [Web Intents] Refactor picker to use WebIntentPickerModel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: vertical layout for views Created 8 years, 11 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
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) {
groby-ooo-7-16 2012/01/25 22:03:13 nit: OVERRIDE, here and elsewhere
binji 2012/01/26 00:27:41 Done.
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(WebIntentPickerModel* model, size_t index) {
62 while (num_urls_ != target_num_urls || 49 num_default_icons_ = GetNumDefaultIcons(model);
63 num_default_icons_ != target_num_default_icons) {
64 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
65 ui_test_utils::RunAllPendingInMessageLoop();
66 }
67 } 50 }
68 51
52 virtual void OnInlineDisposition(WebIntentPickerModel* model) {}
69 virtual void Close() {} 53 virtual void Close() {}
70 54
71 WebContents* SetInlineDisposition(const GURL& url) { return NULL; } 55 int GetNumDefaultIcons(WebIntentPickerModel* model) const {
56 int num_default_icons = 0;
57 for (size_t i = 0; i < model->GetItemCount(); ++i) {
58 const WebIntentPickerModel::Item& item = model->GetItemAt(i);
59 if (item.favicon.get() == NULL)
60 ++num_default_icons;
61 }
72 62
73 int num_urls_; 63 return num_default_icons;
64 }
65
66 int num_items_;
74 int num_default_icons_; 67 int num_default_icons_;
75 }; 68 };
76 69
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 { 70 class IntentsDispatcherMock : public content::WebIntentsDispatcher {
104 public: 71 public:
105 explicit IntentsDispatcherMock(const webkit_glue::WebIntentData& intent) 72 explicit IntentsDispatcherMock(const webkit_glue::WebIntentData& intent)
106 : intent_(intent), 73 : intent_(intent),
107 dispatched_(false) {} 74 dispatched_(false) {}
108 75
109 virtual const webkit_glue::WebIntentData& GetIntent() { 76 virtual const webkit_glue::WebIntentData& GetIntent() {
110 return intent_; 77 return intent_;
111 } 78 }
112 79
113 virtual void DispatchIntent(WebContents* tab_contents) { 80 virtual void DispatchIntent(content::WebContents* web_contents) {
114 dispatched_ = true; 81 dispatched_ = true;
115 } 82 }
116 83
117 virtual void SendReplyMessage(webkit_glue::WebIntentReplyType reply_type, 84 virtual void SendReplyMessage(webkit_glue::WebIntentReplyType reply_type,
118 const string16& data) { 85 const string16& data) {
119 } 86 }
120 87
121 virtual void RegisterReplyNotification(const base::Closure&) { 88 virtual void RegisterReplyNotification(const base::Closure&) {
122 } 89 }
123 90
124 webkit_glue::WebIntentData intent_; 91 webkit_glue::WebIntentData intent_;
125 bool dispatched_; 92 bool dispatched_;
126 }; 93 };
127 94
128 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest { 95 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest {
129 protected: 96 protected:
97 typedef WebIntentPickerModel::Disposition Disposition;
98
99 WebIntentPickerControllerBrowserTest() {}
100
101 virtual void SetUpOnMainThread() OVERRIDE {
102 web_data_service_ =
103 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
104 favicon_service_ =
105 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
106 controller_ = browser()->
107 GetSelectedTabContentsWrapper()->web_intent_picker_controller();
108
109 controller_->set_picker(&picker_);
110 controller_->set_model_observer(&picker_);
111 }
112
130 void AddWebIntentService(const string16& action, 113 void AddWebIntentService(const string16& action,
131 const GURL& service_url) { 114 const GURL& service_url) {
132 webkit_glue::WebIntentServiceData service; 115 webkit_glue::WebIntentServiceData service;
133 service.action = action; 116 service.action = action;
134 service.type = kType; 117 service.type = kType;
135 service.service_url = service_url; 118 service.service_url = service_url;
136 web_data_service_->AddWebIntentService(service); 119 web_data_service_->AddWebIntentService(service);
137 } 120 }
138 121
139 void OnSendReturnMessage(WebIntentPickerController* controller) { 122 void OnSendReturnMessage() {
140 controller->OnSendReturnMessage(); 123 controller_->OnSendReturnMessage();
141 } 124 }
142 125
143 void OnServiceChosen(WebIntentPickerController* controller, size_t index) { 126 void OnServiceChosen(size_t index, Disposition disposition) {
144 controller->OnServiceChosen(index); 127 controller_->OnServiceChosen(index, disposition);
145 } 128 }
146 129
147 void SetPickerFactory(WebIntentPickerController* controller, 130 void OnCancelled() {
148 WebIntentPickerFactory* factory) { 131 controller_->OnCancelled();
149 controller->picker_factory_.reset(factory); 132 }
133
134 void CheckPendingAsync() {
135 if (controller_->pending_async_count() > 0) {
136 MessageLoop::current()->PostTask(
137 FROM_HERE,
138 base::Bind(&WebIntentPickerControllerBrowserTest::CheckPendingAsync,
139 base::Unretained(this)));
140 return;
141 }
142
143 MessageLoop::current()->Quit();
144 }
145
146 void WaitForPendingAsync() {
147 CheckPendingAsync();
148 MessageLoop::current()->Run();
150 } 149 }
151 150
152 WebIntentPickerMock picker_; 151 WebIntentPickerMock picker_;
153
154 // The picker controller takes ownership.
155 WebIntentPickerFactoryMock* picker_factory_;
156
157 WebDataService* web_data_service_; 152 WebDataService* web_data_service_;
158 FaviconService* favicon_service_; 153 FaviconService* favicon_service_;
154 WebIntentPickerController* controller_;
159 }; 155 };
160 156
161 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) { 157 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) {
162 web_data_service_ =
163 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
164 AddWebIntentService(kAction1, kServiceURL1); 158 AddWebIntentService(kAction1, kServiceURL1);
165 AddWebIntentService(kAction1, kServiceURL2); 159 AddWebIntentService(kAction1, kServiceURL2);
166 160
167 favicon_service_ = 161 controller_->ShowDialog(browser(), kAction1, kType);
168 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); 162 WaitForPendingAsync();
169 163 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_); 164 EXPECT_EQ(2, picker_.num_default_icons_);
179 165
180 webkit_glue::WebIntentData intent; 166 webkit_glue::WebIntentData intent;
181 intent.action = ASCIIToUTF16("a"); 167 intent.action = ASCIIToUTF16("a");
182 intent.type = ASCIIToUTF16("b"); 168 intent.type = ASCIIToUTF16("b");
183 IntentsDispatcherMock* host = new IntentsDispatcherMock(intent); 169 IntentsDispatcherMock* host = new IntentsDispatcherMock(intent);
184 controller->SetIntentsDispatcher(host); 170 controller_->SetIntentsDispatcher(host);
185 171
186 OnServiceChosen(controller, 1); 172 OnServiceChosen(1, WebIntentPickerModel::DISPOSITION_WINDOW);
187 ASSERT_EQ(2, browser()->tab_count()); 173 ASSERT_EQ(2, browser()->tab_count());
188 EXPECT_EQ(GURL(kServiceURL2), 174 EXPECT_EQ(GURL(kServiceURL2),
189 browser()->GetSelectedWebContents()->GetURL()); 175 browser()->GetSelectedWebContents()->GetURL());
190 176
191 EXPECT_TRUE(host->dispatched_); 177 EXPECT_TRUE(host->dispatched_);
192 178
193 OnSendReturnMessage(controller); 179 OnSendReturnMessage();
194 ASSERT_EQ(1, browser()->tab_count()); 180 ASSERT_EQ(1, browser()->tab_count());
195 } 181 }
182
183 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, OpenCancelOpen) {
184 AddWebIntentService(kAction1, kServiceURL1);
185 AddWebIntentService(kAction1, kServiceURL2);
186
187 controller_->ShowDialog(browser(), kAction1, kType);
188 WaitForPendingAsync();
189 OnCancelled();
190
191 controller_->ShowDialog(browser(), kAction1, kType);
192 OnCancelled();
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698