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

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

Issue 8508019: Add a method to the controller to close the picker/service tab when a return value is passed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change delegate comment to notification sense of OnClosing. Created 9 years, 1 month 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) 2011 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_factory.h"
17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
17 #include "chrome/browser/webdata/web_data_service.h" 18 #include "chrome/browser/webdata/web_data_service.h"
18 #include "chrome/test/base/in_process_browser_test.h" 19 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h" 20 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/browser/tab_contents/tab_contents.h" 21 #include "content/browser/tab_contents/tab_contents.h"
21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 #include "webkit/glue/web_intent_service_data.h" 24 #include "webkit/glue/web_intent_service_data.h"
24 25
25 using testing::_; 26 using testing::_;
26 using testing::DoAll; 27 using testing::DoAll;
27 using testing::Return; 28 using testing::Return;
28 using testing::SaveArg; 29 using testing::SaveArg;
29 30
30 namespace { 31 namespace {
31 32
32 const string16 kAction1(ASCIIToUTF16("http://www.example.com/share")); 33 const string16 kAction1(ASCIIToUTF16("http://www.example.com/share"));
33 const string16 kAction2(ASCIIToUTF16("http://www.example.com/foobar")); 34 const string16 kAction2(ASCIIToUTF16("http://www.example.com/foobar"));
34 const string16 kType(ASCIIToUTF16("image/png")); 35 const string16 kType(ASCIIToUTF16("image/png"));
35 const GURL kServiceURL1("http://www.google.com"); 36 const GURL kServiceURL1("http://www.google.com");
36 const GURL kServiceURL2("http://www.chromium.org"); 37 const GURL kServiceURL2("http://www.chromium.org");
37 38
38 MATCHER_P(VectorIsOfSize, n, "") { 39 MATCHER_P(VectorIsOfSize, n, "") {
39 return arg.size() == static_cast<size_t>(n); 40 return arg.size() == static_cast<size_t>(n);
40 } 41 }
41 42
42 } // namespace 43 } // namespace
43 44
44 class WebIntentPickerMock : public WebIntentPicker { 45 class WebIntentPickerMock : public WebIntentPicker {
45 public: 46 public:
46 MOCK_METHOD1(SetServiceURLs, void(const std::vector<GURL>& urls)); 47 WebIntentPickerMock() : num_urls_(0), num_default_icons_(0) {}
47 MOCK_METHOD2(SetServiceIcon, void(size_t index, const SkBitmap& icon)); 48
48 MOCK_METHOD1(SetDefaultServiceIcon, void(size_t index)); 49 virtual void SetServiceURLs(const std::vector<GURL>& urls) {
49 MOCK_METHOD0(Show, void(void)); 50 num_urls_ = urls.size();
50 MOCK_METHOD0(Close, void(void)); 51 }
52
53 virtual void SetServiceIcon(size_t index, const SkBitmap& icon) {}
54
55 virtual void SetDefaultServiceIcon(size_t index) {
56 num_default_icons_++;
57 }
58
59 virtual void WaitFor(int target_num_urls, int target_num_default_icons) {
60 while (num_urls_ != target_num_urls ||
61 num_default_icons_ != target_num_default_icons) {
62 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
63 ui_test_utils::RunAllPendingInMessageLoop();
64 }
65 }
66
67 virtual void Close() {}
51 68
52 TabContents* SetInlineDisposition(const GURL& url) { return NULL; } 69 TabContents* SetInlineDisposition(const GURL& url) { return NULL; }
70
71 int num_urls_;
72 int num_default_icons_;
53 }; 73 };
54 74
75
55 class WebIntentPickerFactoryMock : public WebIntentPickerFactory { 76 class WebIntentPickerFactoryMock : public WebIntentPickerFactory {
56 public: 77 public:
57 MOCK_METHOD3(Create, 78 explicit WebIntentPickerFactoryMock(WebIntentPickerMock* mock)
58 WebIntentPicker*(Browser* browser, 79 : picker_(mock) {}
59 TabContentsWrapper* wrapper, 80
60 WebIntentPickerDelegate* delegate)); 81 virtual WebIntentPicker* Create(Browser* browser,
61 MOCK_METHOD1(ClosePicker, void(WebIntentPicker* picker)); 82 TabContentsWrapper* wrapper,
83 WebIntentPickerDelegate* delegate) {
84 return picker_;
85 }
86
87 virtual void ClosePicker(WebIntentPicker* picker) {
88 if (picker_) {
89 picker_->Close();
90 picker_ = NULL;
91 }
92 }
93
94 void Close() {
95 picker_ = NULL;
96 }
97
98 WebIntentPicker* picker_;
62 }; 99 };
63 100
64 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest { 101 class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest {
65 protected: 102 protected:
66 void AddWebIntentService(const string16& action, 103 void AddWebIntentService(const string16& action,
67 const GURL& service_url) { 104 const GURL& service_url) {
68 webkit_glue::WebIntentServiceData service; 105 webkit_glue::WebIntentServiceData service;
69 service.action = action; 106 service.action = action;
70 service.type = kType; 107 service.type = kType;
71 service.service_url = service_url; 108 service.service_url = service_url;
72 web_data_service_->AddWebIntentService(service); 109 web_data_service_->AddWebIntentService(service);
73 } 110 }
74 111
75 void SetPickerExpectations(int expected_service_count, 112 void OnSendReturnMessage(WebIntentPickerController* controller) {
76 int expected_default_favicons) { 113 controller->OnSendReturnMessage();
77 EXPECT_CALL(*picker_factory_, Create(_, _, _)).
78 WillOnce(DoAll(SaveArg<2>(&delegate_), Return(&picker_)));
79 EXPECT_CALL(picker_,
80 SetServiceURLs(VectorIsOfSize(expected_service_count))).
81 Times(1);
82 EXPECT_CALL(picker_, SetDefaultServiceIcon(_)).
83 Times(expected_default_favicons);
84 EXPECT_CALL(*picker_factory_, ClosePicker(_));
85 } 114 }
86 115
87 void CheckPendingAsync() { 116 void OnServiceChosen(WebIntentPickerController* controller, size_t index) {
88 if (controller_->pending_async_count() > 0) { 117 controller->OnServiceChosen(index);
89 MessageLoop::current()->PostTask(
90 FROM_HERE,
91 base::Bind(&WebIntentPickerControllerBrowserTest::CheckPendingAsync,
92 base::Unretained(this)));
93 return;
94 }
95
96 MessageLoop::current()->Quit();
97 } 118 }
98 119
99 void WaitForDialogToShow() { 120 void SetPickerFactory(WebIntentPickerController* controller,
100 CheckPendingAsync(); 121 WebIntentPickerFactory* factory) {
101 MessageLoop::current()->Run(); 122 controller->picker_factory_.reset(factory);
102 } 123 }
103 124
104 WebIntentPickerMock picker_; 125 WebIntentPickerMock picker_;
105 126
106 // |controller_| takes ownership. 127 // The picker controller takes ownership.
107 WebIntentPickerFactoryMock* picker_factory_; 128 WebIntentPickerFactoryMock* picker_factory_;
108 129
109 scoped_ptr<WebIntentPickerController> controller_;
110 WebIntentPickerDelegate* delegate_;
111 WebDataService* web_data_service_; 130 WebDataService* web_data_service_;
112 FaviconService* favicon_service_; 131 FaviconService* favicon_service_;
113 }; 132 };
114 133
115 // http://crbug.com/104140 134 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) {
116 IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest,
117 FLAKY_ChooseService) {
118 web_data_service_ = 135 web_data_service_ =
119 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); 136 browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
120 AddWebIntentService(kAction1, kServiceURL1); 137 AddWebIntentService(kAction1, kServiceURL1);
121 AddWebIntentService(kAction1, kServiceURL2); 138 AddWebIntentService(kAction1, kServiceURL2);
122 139
123 favicon_service_ = 140 favicon_service_ =
124 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); 141 browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
125 142
126 picker_factory_ = new WebIntentPickerFactoryMock(); 143 picker_factory_ = new WebIntentPickerFactoryMock(&picker_);
127 controller_.reset(new WebIntentPickerController( 144 WebIntentPickerController* controller = browser()->
128 browser()->GetSelectedTabContentsWrapper(), picker_factory_)); 145 GetSelectedTabContentsWrapper()->web_intent_picker_controller();
146 SetPickerFactory(controller, picker_factory_);
129 147
130 SetPickerExpectations(2, 2); 148 controller->ShowDialog(browser(), kAction1, kType);
149 picker_.WaitFor(2, 2);
150 EXPECT_EQ(2, picker_.num_urls_);
151 EXPECT_EQ(2, picker_.num_default_icons_);
131 152
132 controller_->ShowDialog(NULL, kAction1, kType); 153 webkit_glue::WebIntentData intent;
133 WaitForDialogToShow(); 154 intent.action = ASCIIToUTF16("a");
155 intent.type = ASCIIToUTF16("b");
156 controller->SetIntent(1, intent, 1);
134 157
135 delegate_->OnServiceChosen(1); 158 OnServiceChosen(controller, 1);
136 ASSERT_EQ(2, browser()->tab_count()); 159 ASSERT_EQ(2, browser()->tab_count());
137 EXPECT_EQ(GURL(kServiceURL2), 160 EXPECT_EQ(GURL(kServiceURL2),
138 browser()->GetSelectedTabContents()->GetURL()); 161 browser()->GetSelectedTabContents()->GetURL());
162
163 OnSendReturnMessage(controller);
164 ASSERT_EQ(1, browser()->tab_count());
139 } 165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698