OLD | NEW |
1 // Copyright (c) 2012 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/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/json/json_file_value_serializer.h" | 7 #include "base/json/json_file_value_serializer.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 scoped_refptr<Extension> extension = LoadExtension(name, &error); | 72 scoped_refptr<Extension> extension = LoadExtension(name, &error); |
73 EXPECT_TRUE(extension) << name; | 73 EXPECT_TRUE(extension) << name; |
74 EXPECT_EQ("", error) << name; | 74 EXPECT_EQ("", error) << name; |
75 return extension; | 75 return extension; |
76 } | 76 } |
77 | 77 |
78 } // namespace | 78 } // namespace |
79 | 79 |
80 class WebIntentsRegistryTest : public testing::Test { | 80 class WebIntentsRegistryTest : public testing::Test { |
81 public: | 81 public: |
82 WebIntentsRegistryTest() | 82 WebIntentsRegistryTest() |
83 : ui_thread_(BrowserThread::UI, &message_loop_), | 83 : ui_thread_(BrowserThread::UI, &message_loop_), |
84 db_thread_(BrowserThread::DB) {} | 84 db_thread_(BrowserThread::DB) {} |
85 | 85 |
86 protected: | 86 protected: |
87 virtual void SetUp() { | 87 virtual void SetUp() { |
88 CommandLine::ForCurrentProcess()->AppendSwitch("--enable-web-intents"); | |
89 | |
90 db_thread_.Start(); | 88 db_thread_.Start(); |
91 wds_ = new WebDataService(); | 89 wds_ = new WebDataService(); |
92 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
93 wds_->Init(temp_dir_.path()); | 91 wds_->Init(temp_dir_.path()); |
94 registry_.Initialize(wds_, &extension_service_); | 92 registry_.Initialize(wds_, &extension_service_); |
95 EXPECT_CALL(extension_service_, extensions()). | 93 EXPECT_CALL(extension_service_, extensions()). |
96 WillRepeatedly(testing::Return(&extensions_)); | 94 WillRepeatedly(testing::Return(&extensions_)); |
97 EXPECT_CALL(extension_service_, GetExtensionById(testing::_, testing::_)). | 95 EXPECT_CALL(extension_service_, GetExtensionById(testing::_, testing::_)). |
98 WillRepeatedly( | 96 WillRepeatedly( |
99 testing::Invoke(this, &WebIntentsRegistryTest::GetExtensionById)); | 97 testing::Invoke(this, &WebIntentsRegistryTest::GetExtensionById)); |
100 } | 98 } |
101 | 99 |
102 virtual void TearDown() { | 100 virtual void TearDown() { |
103 // Clear all references to wds to force it destruction. | 101 // Clear all references to wds to force it destruction. |
104 wds_->ShutdownOnUIThread(); | 102 wds_->ShutdownOnUIThread(); |
105 wds_ = NULL; | 103 wds_ = NULL; |
106 | 104 |
107 // Schedule another task on the DB thread to notify us that it's safe to | 105 // Schedule another task on the DB thread to notify us that it's safe to |
108 // carry on with the test. | 106 // carry on with the test. |
109 base::WaitableEvent done(false, false); | 107 base::WaitableEvent done(false, false); |
110 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 108 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
111 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | 109 base::Bind(&base::WaitableEvent::Signal, |
| 110 base::Unretained(&done))); |
112 done.Wait(); | 111 done.Wait(); |
113 db_thread_.Stop(); | 112 db_thread_.Stop(); |
114 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 113 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
115 MessageLoop::current()->Run(); | 114 MessageLoop::current()->Run(); |
116 } | 115 } |
117 | 116 |
118 const Extension* GetExtensionById(const std::string& extension_id, | 117 const Extension* GetExtensionById(const std::string& extension_id, |
119 testing::Unused) { | 118 testing::Unused) { |
120 for (ExtensionSet::const_iterator iter = extensions_.begin(); | 119 for (ExtensionSet::const_iterator iter = extensions_.begin(); |
121 iter != extensions_.end(); ++iter) { | 120 iter != extensions_.end(); ++iter) { |
(...skipping 10 matching lines...) Expand all Loading... |
132 scoped_refptr<WebDataService> wds_; | 131 scoped_refptr<WebDataService> wds_; |
133 MockExtensionService extension_service_; | 132 MockExtensionService extension_service_; |
134 ExtensionSet extensions_; | 133 ExtensionSet extensions_; |
135 WebIntentsRegistry registry_; | 134 WebIntentsRegistry registry_; |
136 ScopedTempDir temp_dir_; | 135 ScopedTempDir temp_dir_; |
137 }; | 136 }; |
138 | 137 |
139 // Base consumer for WebIntentsRegistry results. | 138 // Base consumer for WebIntentsRegistry results. |
140 class TestConsumer { | 139 class TestConsumer { |
141 public: | 140 public: |
142 // Wait for the UI message loop to terminate - happens when OnIntesQueryDone | 141 // Wait for the UI message loop to terminate - happens when OnIntesQueryDone |
143 // is invoked. | 142 // is invoked. |
144 void WaitForData() { | 143 void WaitForData() { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
146 MessageLoop::current()->Run(); | 145 MessageLoop::current()->Run(); |
147 } | 146 } |
148 }; | 147 }; |
149 | 148 |
150 // Consumer of service lists. Stores result data and | 149 // Consumer of service lists. Stores result data and |
151 // terminates UI thread when callback is invoked. | 150 // terminates UI thread when callback is invoked. |
152 class ServiceListConsumer : public TestConsumer { | 151 class ServiceListConsumer : public TestConsumer { |
153 public: | 152 public: |
154 void Accept( | 153 void Accept( |
155 const std::vector<webkit_glue::WebIntentServiceData>& services) { | 154 const std::vector<webkit_glue::WebIntentServiceData>& services) { |
156 services_ = services; | 155 services_ = services; |
157 | 156 |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
159 MessageLoop::current()->Quit(); | 158 MessageLoop::current()->Quit(); |
160 } | 159 } |
161 | 160 |
162 bool ResultsContain(const webkit_glue::WebIntentServiceData& service) { | 161 bool ResultsContain(const webkit_glue::WebIntentServiceData& service) { |
163 for (size_t i = 0; i < services_.size(); ++i) { | 162 for (size_t i = 0; i < services_.size(); ++i) { |
164 if (services_[i] == service) | 163 if (services_[i] == service) |
165 return true; | 164 return true; |
166 } | 165 } |
167 return false; | 166 return false; |
168 } | 167 } |
169 | 168 |
170 // Result data from callback. | 169 // Result data from callback. |
171 std::vector<webkit_glue::WebIntentServiceData> services_; | 170 std::vector<webkit_glue::WebIntentServiceData> services_; |
172 }; | 171 }; |
173 | 172 |
174 // Consume or defaultservice lists. Stores result data and | 173 // Consume or defaultservice lists. Stores result data and |
175 // terminates UI thread when callback is invoked. | 174 // terminates UI thread when callback is invoked. |
176 class DefaultServiceListConsumer : public TestConsumer { | 175 class DefaultServiceListConsumer : public TestConsumer { |
177 public: | 176 public: |
178 void Accept(const std::vector<DefaultWebIntentService>& services) { | 177 void Accept(const std::vector<DefaultWebIntentService>& services) { |
179 services_ = services; | 178 services_ = services; |
180 | 179 |
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
182 MessageLoop::current()->Quit(); | 181 MessageLoop::current()->Quit(); |
183 } | 182 } |
184 | 183 |
185 bool ResultsContain(const DefaultWebIntentService& service) { | 184 bool ResultsContain(const DefaultWebIntentService& service) { |
186 for (size_t i = 0; i < services_.size(); ++i) { | 185 for (size_t i = 0; i < services_.size(); ++i) { |
187 if (services_[i] == service) | 186 if (services_[i] == service) |
188 return true; | 187 return true; |
189 } | 188 } |
190 return false; | 189 return false; |
191 } | 190 } |
192 | 191 |
193 // Result data from callback. | 192 // Result data from callback. |
194 std::vector<DefaultWebIntentService> services_; | 193 std::vector<DefaultWebIntentService> services_; |
195 }; | 194 }; |
196 | 195 |
197 // Consumer of a default service. Stores result data and | 196 // Consumer of a default service. Stores result data and |
198 // terminates UI thread when callback is invoked. | 197 // terminates UI thread when callback is invoked. |
199 class DefaultServiceConsumer : public TestConsumer { | 198 class DefaultServiceConsumer : public TestConsumer { |
200 public: | 199 public: |
201 void Accept( | 200 void Accept( |
202 const DefaultWebIntentService& default_service) { | 201 const DefaultWebIntentService& default_service) { |
203 service_ = default_service; | 202 service_ = default_service; |
204 | 203 |
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
206 MessageLoop::current()->Quit(); | 205 MessageLoop::current()->Quit(); |
207 } | 206 } |
208 | 207 |
209 // Result default data from callback. | 208 // Result default data from callback. |
210 DefaultWebIntentService service_; | 209 DefaultWebIntentService service_; |
211 }; | 210 }; |
212 | 211 |
213 TEST_F(WebIntentsRegistryTest, BasicTests) { | 212 TEST_F(WebIntentsRegistryTest, BasicTests) { |
214 webkit_glue::WebIntentServiceData service; | 213 webkit_glue::WebIntentServiceData service; |
215 service.service_url = GURL("http://google.com"); | 214 service.service_url = GURL("http://google.com"); |
216 service.action = ASCIIToUTF16("share"); | 215 service.action = ASCIIToUTF16("share"); |
217 service.type = ASCIIToUTF16("image/*"); | 216 service.type = ASCIIToUTF16("image/*"); |
218 service.title = ASCIIToUTF16("Google's Sharing Service"); | 217 service.title = ASCIIToUTF16("Google's Sharing Service"); |
219 | 218 |
220 registry_.RegisterIntentService(service); | 219 registry_.RegisterIntentService(service); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 service.action = ASCIIToUTF16("search"); | 283 service.action = ASCIIToUTF16("search"); |
285 registry_.RegisterIntentService(service); | 284 registry_.RegisterIntentService(service); |
286 | 285 |
287 ServiceListConsumer consumer; | 286 ServiceListConsumer consumer; |
288 registry_.GetAllIntentServices(base::Bind(&ServiceListConsumer::Accept, | 287 registry_.GetAllIntentServices(base::Bind(&ServiceListConsumer::Accept, |
289 base::Unretained(&consumer))); | 288 base::Unretained(&consumer))); |
290 consumer.WaitForData(); | 289 consumer.WaitForData(); |
291 ASSERT_EQ(2U, consumer.services_.size()); | 290 ASSERT_EQ(2U, consumer.services_.size()); |
292 | 291 |
293 if (consumer.services_[0].action != ASCIIToUTF16("share")) | 292 if (consumer.services_[0].action != ASCIIToUTF16("share")) |
294 std::swap(consumer.services_[0],consumer.services_[1]); | 293 std::swap(consumer.services_[0], consumer.services_[1]); |
295 | 294 |
296 service.action = ASCIIToUTF16("share"); | 295 service.action = ASCIIToUTF16("share"); |
297 EXPECT_EQ(service, consumer.services_[0]); | 296 EXPECT_EQ(service, consumer.services_[0]); |
298 | 297 |
299 service.action = ASCIIToUTF16("search"); | 298 service.action = ASCIIToUTF16("search"); |
300 EXPECT_EQ(service, consumer.services_[1]); | 299 EXPECT_EQ(service, consumer.services_[1]); |
301 } | 300 } |
302 | 301 |
303 TEST_F(WebIntentsRegistryTest, GetExtensionIntents) { | 302 TEST_F(WebIntentsRegistryTest, GetExtensionIntents) { |
304 extensions_.Insert(LoadAndExpectSuccess("intent_valid.json")); | 303 extensions_.Insert(LoadAndExpectSuccess("intent_valid.json")); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 ServiceListConsumer consumer; | 727 ServiceListConsumer consumer; |
729 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"), | 728 registry_.GetIntentServices(ASCIIToUTF16("http://webintents.org/share"), |
730 ASCIIToUTF16("image/*"), | 729 ASCIIToUTF16("image/*"), |
731 base::Bind(&ServiceListConsumer::Accept, | 730 base::Bind(&ServiceListConsumer::Accept, |
732 base::Unretained(&consumer))); | 731 base::Unretained(&consumer))); |
733 | 732 |
734 consumer.WaitForData(); | 733 consumer.WaitForData(); |
735 ASSERT_EQ(1U, consumer.services_.size()); | 734 ASSERT_EQ(1U, consumer.services_.size()); |
736 EXPECT_EQ(ASCIIToUTF16("image/png,image/jpg"), consumer.services_[0].type); | 735 EXPECT_EQ(ASCIIToUTF16("image/png,image/jpg"), consumer.services_[0].type); |
737 } | 736 } |
OLD | NEW |