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 "chrome/browser/intents/cws_intents_registry.h" | 5 #include "chrome/browser/intents/cws_intents_registry.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/common/extensions/extension_l10n_util.h" | |
12 #include "net/url_request/test_url_fetcher_factory.h" | 11 #include "net/url_request/test_url_fetcher_factory.h" |
13 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 | 14 |
16 // Create a CWSIntentsRegistry proxy for testing purposes. | |
17 // Needs to be non-anonymous so it can be friended. | |
18 class CWSIntentsRegistryForTest { | |
19 public: | |
20 CWSIntentsRegistryForTest() : registry_(NULL) {} | |
21 ~CWSIntentsRegistryForTest() { delete registry_; } | |
22 | |
23 void reset(net::URLRequestContextGetter* context) { | |
24 delete registry_; | |
25 registry_ = new CWSIntentsRegistry(context); | |
26 } | |
27 | |
28 CWSIntentsRegistry* operator->() { return registry_; } | |
29 | |
30 private: | |
31 CWSIntentsRegistry* registry_; | |
32 }; | |
33 | |
34 namespace { | 15 namespace { |
35 | 16 |
36 const char kCWSResponseInvalid[] = | 17 const char kCWSResponseInvalid[] = |
37 "{\"error\":{" | 18 "{\"error\":{" |
38 "\"errors\":[{" | 19 "\"errors\":[{" |
39 "\"domain\":\"global\"," | 20 "\"domain\":\"global\"," |
40 "\"reason\":\"invalid\"," | 21 "\"reason\":\"invalid\"," |
41 "\"message\":\"Invalid mimetype:foo\"}]," | 22 "\"message\":\"Invalid mimetype:foo\"}]," |
42 "\"code\":400," | 23 "\"code\":400," |
43 "\"message\":\"Invalid mimetype:foo\"}}\""; | 24 "\"message\":\"Invalid mimetype:foo\"}}\""; |
(...skipping 22 matching lines...) Expand all Loading... |
66 "\" : \\\"//services/edit\\\",\\n \\\"title\\\" : " | 47 "\" : \\\"//services/edit\\\",\\n \\\"title\\\" : " |
67 "\\\"Sample Editing Intent\\\",\\n \\\"disposition\\\" : \\\"inline\\" | 48 "\\\"Sample Editing Intent\\\",\\n \\\"disposition\\\" : \\\"inline\\" |
68 "\"\\n },\\n \\\"http://webintents.org/share\\\" : " | 49 "\"\\n },\\n \\\"http://webintents.org/share\\\" : " |
69 "{\\n \\\"type\\\" : [\\\"text/plain\\\", \\\"image/jpg\\\"]," | 50 "{\\n \\\"type\\\" : [\\\"text/plain\\\", \\\"image/jpg\\\"]," |
70 "\\n \\\"path\\\" : \\\"//services/share\\\",\\n \\\"title\\\" : " | 51 "\\n \\\"path\\\" : \\\"//services/share\\\",\\n \\\"title\\\" : " |
71 "\\\"Sample sharing Intent\\\",\\n \\\"disposition\\\" : " | 52 "\\\"Sample sharing Intent\\\",\\n \\\"disposition\\\" : " |
72 "\\\"inline\\\"\\n }\\n }\\n}\\n\"," | 53 "\\\"inline\\\"\\n }\\n }\\n}\\n\"," |
73 " \"family_safe\":true," | 54 " \"family_safe\":true," |
74 " \"icon_url\":\"http://qa-lighthouse.sandbox.google.com/image/" | 55 " \"icon_url\":\"http://qa-lighthouse.sandbox.google.com/image/" |
75 "QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ\"}]}"; | 56 "QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ\"}]}"; |
76 | 57 const char kValidIconURL[]= |
77 const char kCWSResponseValidL10n[] = | |
78 "{\"kind\":\"chromewebstore#itemList\"," | |
79 " \"total_items\":1," | |
80 " \"start_index\":0," | |
81 " \"items\":[ " | |
82 " {\"kind\":\"chromewebstore#item\"," | |
83 " \"id\":\"nhkckhebbbncbkefhcpcgepcgfaclehe\"," | |
84 " \"type\":\"APPLICATION\"," | |
85 " \"num_ratings\":0," | |
86 " \"average_rating\":0.0," | |
87 " \"manifest\":\"{\\n\\\"update_url\\\":\\" | |
88 "\"http://0.tbhome_staging.dserver.download-qa.td.borg.google.com/" | |
89 "service/update2/crx\\\",\\n \\\"name\\\": \\\"__MSG_name__\\\"" | |
90 ",\\n \\\"description\\\": \\\"Do stuff\\\",\\n \\\"version\\\": " | |
91 "\\\"1.2.19\\\",\\n \\\"app\\\": {\\n \\\"urls\\\": [ \\n ]," | |
92 "\\n \\\"launch\\\": {\\n \\\"web_url\\\": \\" | |
93 "\"http://siddharthasaha.net/\\\"\\n }\\n },\\n \\\"icons\\\": " | |
94 "{\\n \\\"128\\\": \\\"icon128.png\\\"\\n },\\n \\\"permissions\\\":" | |
95 " [\\n \\\"unlimitedStorage\\\",\\n \\\"notifications\\\"\\n ],\\n" | |
96 " \\\"intents\\\": {\\n \\\"http://webintents.org/edit\\\" : {\\n " | |
97 "\\\"type\\\" : [\\\"image/png\\\", \\\"image/jpg\\\"],\\n \\\"path\\" | |
98 "\" : \\\"//services/edit\\\",\\n \\\"title\\\" : " | |
99 "\\\"Sample Editing Intent\\\",\\n \\\"disposition\\\" : \\\"inline\\" | |
100 "\"\\n },\\n \\\"http://webintents.org/share\\\" : " | |
101 "{\\n \\\"type\\\" : [\\\"text/plain\\\", \\\"image/jpg\\\"]," | |
102 "\\n \\\"path\\\" : \\\"//services/share\\\",\\n \\\"title\\\" : " | |
103 "\\\"Sample sharing Intent\\\",\\n \\\"disposition\\\" : " | |
104 "\\\"inline\\\"\\n }\\n }\\n}\\n\"," | |
105 " \"family_safe\":true," | |
106 " \"icon_url\":\"http://qa-lighthouse.sandbox.google.com/image/" | |
107 "QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ\"," | |
108 "\"locale_data\": ["; | |
109 | |
110 const char kCWSResponseValidL10nPostfix[] = | |
111 "]}]}"; | |
112 | |
113 const char kLocaleDataEn[] = | |
114 "{" | |
115 " \"locale_string\": \"en\"," | |
116 " \"title\": \"Localized EN\"" | |
117 "}"; | |
118 | |
119 const char kLocaleDataDe[] = | |
120 "{" | |
121 " \"locale_string\": \"de\"," | |
122 " \"title\": \"Localized DE\"" | |
123 "}"; | |
124 | |
125 const char kLocaleDataAll[] = | |
126 "{" | |
127 " \"locale_string\": \"all\"," | |
128 " \"title\": \"Localized ALL\"" | |
129 "}"; | |
130 | |
131 const char kValidIconURL[] = | |
132 "http://qa-lighthouse.sandbox.google.com/image/" | 58 "http://qa-lighthouse.sandbox.google.com/image/" |
133 "QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ"; | 59 "QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ"; |
134 | 60 const char kValidManifest[]= |
135 const char kValidManifest[] = | |
136 "{\n\"update_url\":\"http://0.tbhome_staging.dserver.download-qa.td.borg." | 61 "{\n\"update_url\":\"http://0.tbhome_staging.dserver.download-qa.td.borg." |
137 "google.com/service/update2/crx\",\n \"name\": \"Sidd's Intent App\",\n" | 62 "google.com/service/update2/crx\",\n \"name\": \"Sidd's Intent App\",\n" |
138 " \"description\": \"Do stuff\",\n \"version\": \"1.2.19\",\n \"app\":" | 63 " \"description\": \"Do stuff\",\n \"version\": \"1.2.19\",\n \"app\":" |
139 " {\n \"urls\": [ \n ],\n \"launch\": {\n \"web_url\":" | 64 " {\n \"urls\": [ \n ],\n \"launch\": {\n \"web_url\":" |
140 " \"http://siddharthasaha.net/\"\n }\n },\n \"icons\": {\n " | 65 " \"http://siddharthasaha.net/\"\n }\n },\n \"icons\": {\n " |
141 "\"128\": \"icon128.png\"\n },\n \"permissions\": [\n " | 66 "\"128\": \"icon128.png\"\n },\n \"permissions\": [\n " |
142 "\"unlimitedStorage\",\n \"notifications\"\n ],\n \"intents\": " | 67 "\"unlimitedStorage\",\n \"notifications\"\n ],\n \"intents\": " |
143 "{\n \"http://webintents.org/edit\" : {\n \"type\" : [" | 68 "{\n \"http://webintents.org/edit\" : {\n \"type\" : [" |
144 "\"image/png\", \"image/jpg\"],\n \"path\" : \"//services/edit\",\n" | 69 "\"image/png\", \"image/jpg\"],\n \"path\" : \"//services/edit\",\n" |
145 " \"title\" : \"Sample Editing Intent\",\n \"disposition\" : " | 70 " \"title\" : \"Sample Editing Intent\",\n \"disposition\" : " |
146 "\"inline\"\n },\n \"http://webintents.org/share\" : {\n " | 71 "\"inline\"\n },\n \"http://webintents.org/share\" : {\n " |
147 "\"type\" : [\"text/plain\", \"image/jpg\"],\n \"path\" : " | 72 "\"type\" : [\"text/plain\", \"image/jpg\"],\n \"path\" : " |
148 "\"//services/share\",\n \"title\" : \"Sample sharing Intent\",\n" | 73 "\"//services/share\",\n \"title\" : \"Sample sharing Intent\",\n" |
149 " \"disposition\" : \"inline\"\n }\n }\n}\n"; | 74 " \"disposition\" : \"inline\"\n }\n }\n}\n"; |
150 | 75 |
151 const char kEditAction[] = "http://webintents.org/edit"; | |
152 const char kImagePngType[] = "image/png"; | |
153 | |
154 class ScopedLocale { | |
155 public: | |
156 ScopedLocale() : locale_(extension_l10n_util::CurrentLocaleOrDefault()) {} | |
157 ~ScopedLocale() { extension_l10n_util::SetProcessLocale(locale_); } | |
158 std::string locale_; | |
159 }; | |
160 | |
161 class CWSIntentsRegistryTest : public testing::Test { | 76 class CWSIntentsRegistryTest : public testing::Test { |
162 public: | 77 public: |
163 virtual void SetUp() { | 78 CWSIntentsRegistryTest() : test_factory_(NULL) { |
164 scoped_refptr<TestURLRequestContextGetter> context_getter_( | |
165 new TestURLRequestContextGetter(ui_loop_.message_loop_proxy())); | |
166 registry_.reset(context_getter_); | |
167 } | 79 } |
168 | 80 |
169 virtual void TearDown() { | 81 virtual void TearDown() { |
170 // Pump messages posted by the main thread. | 82 // Pump messages posted by the main thread. |
171 ui_loop_.RunAllPending(); | 83 ui_loop_.RunAllPending(); |
172 } | 84 } |
173 | 85 |
174 CWSIntentsRegistry::IntentExtensionList WaitForResults() { | 86 CWSIntentsRegistry::IntentExtensionList WaitForResults() { |
175 ui_loop_.RunAllPending(); | 87 ui_loop_.RunAllPending(); |
176 return extensions_; | 88 return extensions_; |
177 } | 89 } |
178 | 90 |
179 void Callback(const CWSIntentsRegistry::IntentExtensionList& extensions) { | 91 void Callback(const CWSIntentsRegistry::IntentExtensionList& extensions) { |
180 extensions_ = extensions; | 92 extensions_ = extensions; |
181 } | 93 } |
182 | 94 |
183 void RunRequest(const std::string& action, const std::string& mime, | 95 void SetFakeResponse(const std::string& action, const std::string& mime, |
184 const std::string& response) { | 96 const std::string& response) { |
185 extensions_.clear(); | 97 test_factory_.SetFakeResponse( |
186 | |
187 net::FakeURLFetcherFactory test_factory; | |
188 test_factory.SetFakeResponse( | |
189 CWSIntentsRegistry::BuildQueryURL( | 98 CWSIntentsRegistry::BuildQueryURL( |
190 ASCIIToUTF16(action),ASCIIToUTF16(mime)).spec(), | 99 ASCIIToUTF16(action),ASCIIToUTF16(mime)).spec(), |
191 response, true); | 100 response, true); |
192 | |
193 registry_->GetIntentServices(ASCIIToUTF16(action), | |
194 ASCIIToUTF16(mime), | |
195 base::Bind(&CWSIntentsRegistryTest::Callback, | |
196 base::Unretained(this))); | |
197 WaitForResults(); | |
198 } | 101 } |
199 | 102 |
| 103 CWSIntentsRegistry::IntentExtensionList extensions_; |
| 104 net::FakeURLFetcherFactory test_factory_; |
200 | 105 |
201 protected: | 106 protected: |
202 CWSIntentsRegistryForTest registry_; | |
203 CWSIntentsRegistry::IntentExtensionList extensions_; | |
204 MessageLoop ui_loop_; | 107 MessageLoop ui_loop_; |
205 }; | 108 }; |
206 | 109 |
207 } // namespace | 110 } // namespace |
208 | 111 |
209 TEST_F(CWSIntentsRegistryTest, ValidQuery) { | 112 TEST_F(CWSIntentsRegistryTest, ValidQuery) { |
210 RunRequest(kEditAction, kImagePngType,kCWSResponseValid); | 113 const scoped_refptr<TestURLRequestContextGetter> context_getter( |
| 114 new TestURLRequestContextGetter(ui_loop_.message_loop_proxy())); |
| 115 SetFakeResponse("http://webintents.org/edit", "*/png", kCWSResponseValid); |
| 116 |
| 117 CWSIntentsRegistry registry(context_getter); |
| 118 registry.GetIntentServices(ASCIIToUTF16("http://webintents.org/edit"), |
| 119 ASCIIToUTF16("*/png"), |
| 120 base::Bind(&CWSIntentsRegistryTest::Callback, |
| 121 base::Unretained(this))); |
| 122 |
| 123 WaitForResults(); |
211 ASSERT_EQ(1UL, extensions_.size()); | 124 ASSERT_EQ(1UL, extensions_.size()); |
212 | 125 |
213 EXPECT_EQ(0, extensions_[0].num_ratings); | 126 EXPECT_EQ(0, extensions_[0].num_ratings); |
214 EXPECT_EQ(0.0, extensions_[0].average_rating); | 127 EXPECT_EQ(0.0, extensions_[0].average_rating); |
215 EXPECT_EQ(std::string(kValidManifest), UTF16ToUTF8(extensions_[0].manifest)); | 128 EXPECT_EQ(std::string(kValidManifest), UTF16ToUTF8(extensions_[0].manifest)); |
216 EXPECT_EQ(std::string("nhkckhebbbncbkefhcpcgepcgfaclehe"), | 129 EXPECT_EQ(std::string("nhkckhebbbncbkefhcpcgepcgfaclehe"), |
217 UTF16ToUTF8(extensions_[0].id) ); | 130 UTF16ToUTF8(extensions_[0].id) ); |
218 EXPECT_EQ(std::string("Sidd's Intent App"), | 131 EXPECT_EQ(std::string("Sidd's Intent App"), |
219 UTF16ToUTF8(extensions_[0].name)); | 132 UTF16ToUTF8(extensions_[0].name)); |
220 EXPECT_EQ(std::string(kValidIconURL), extensions_[0].icon_url.spec()); | 133 EXPECT_EQ(std::string(kValidIconURL), extensions_[0].icon_url.spec()); |
221 } | 134 } |
222 | 135 |
223 TEST_F(CWSIntentsRegistryTest, InvalidQuery) { | 136 TEST_F(CWSIntentsRegistryTest, InvalidQuery) { |
224 RunRequest("foo", "foo",kCWSResponseInvalid); | 137 const scoped_refptr<TestURLRequestContextGetter> context_getter( |
| 138 new TestURLRequestContextGetter(ui_loop_.message_loop_proxy())); |
| 139 SetFakeResponse("foo", "foo", kCWSResponseInvalid); |
| 140 |
| 141 CWSIntentsRegistry registry(context_getter); |
| 142 registry.GetIntentServices(ASCIIToUTF16("foo"), |
| 143 ASCIIToUTF16("foo"), |
| 144 base::Bind(&CWSIntentsRegistryTest::Callback, |
| 145 base::Unretained(this))); |
| 146 |
| 147 WaitForResults(); |
225 EXPECT_EQ(0UL, extensions_.size()); | 148 EXPECT_EQ(0UL, extensions_.size()); |
226 } | 149 } |
227 | 150 |
228 TEST_F(CWSIntentsRegistryTest, BuildQueryURL) { | 151 TEST_F(CWSIntentsRegistryTest, BuildQueryURL) { |
229 const std::string kExpectedURL = "https://www.googleapis.com" | 152 const std::string kExpectedURL = "https://www.googleapis.com" |
230 "/chromewebstore/v1.1b/items/intent" | 153 "/chromewebstore/v1.1b/items/intent" |
231 "?intent=action&mime_types=mime%2Ftype&start_index=0&num_results=15"; | 154 "?intent=action&mime_types=mime%2Ftype&start_index=0&num_results=15"; |
232 GURL url = CWSIntentsRegistry::BuildQueryURL(ASCIIToUTF16("action"), | 155 GURL url = CWSIntentsRegistry::BuildQueryURL(ASCIIToUTF16("action"), |
233 ASCIIToUTF16("mime/type")); | 156 ASCIIToUTF16("mime/type")); |
234 | 157 |
235 EXPECT_EQ(kExpectedURL, url.spec().substr(0, kExpectedURL.size())); | 158 EXPECT_EQ(kExpectedURL, url.spec().substr(0, kExpectedURL.size())); |
236 } | 159 } |
237 | |
238 // Test for match to the application locale - i.e. if running in "en", | |
239 // registry will use locale_data for "en" key, with or without "all" | |
240 // locale_data present. | |
241 TEST_F(CWSIntentsRegistryTest, LocalizeMatchingLocale) { | |
242 ScopedLocale restoreLocaleOnExit; | |
243 | |
244 std::string response = kCWSResponseValidL10n; | |
245 response += kLocaleDataEn + std::string(","); | |
246 response += kLocaleDataAll + std::string(","); | |
247 response += kLocaleDataDe; | |
248 response += kCWSResponseValidL10nPostfix; | |
249 | |
250 // Picks the proper locale_data based on application locale. | |
251 extension_l10n_util::SetProcessLocale("en"); | |
252 RunRequest(kEditAction, kImagePngType, response); | |
253 ASSERT_EQ(1UL, extensions_.size()); | |
254 EXPECT_EQ(std::string("Localized EN"), | |
255 UTF16ToUTF8(extensions_[0].name)); | |
256 | |
257 extension_l10n_util::SetProcessLocale("de"); | |
258 RunRequest(kEditAction, kImagePngType, response); | |
259 ASSERT_EQ(1UL, extensions_.size()); | |
260 EXPECT_EQ(std::string("Localized DE"), | |
261 UTF16ToUTF8(extensions_[0].name)); | |
262 | |
263 // Falls back to locale_data for "All" if unknown application locale. | |
264 extension_l10n_util::SetProcessLocale("fr"); | |
265 RunRequest(kEditAction, kImagePngType, std::string(kCWSResponseValidL10n) + | |
266 kLocaleDataAll + kCWSResponseValidL10nPostfix); | |
267 ASSERT_EQ(1UL, extensions_.size()); | |
268 EXPECT_EQ(std::string("Localized ALL"), | |
269 UTF16ToUTF8(extensions_[0].name)); | |
270 | |
271 // Keeps original content if unknown application locale and no "all" | |
272 // localization data exists. | |
273 response = kCWSResponseValidL10n; | |
274 response += kLocaleDataEn; | |
275 response += kCWSResponseValidL10nPostfix; | |
276 extension_l10n_util::SetProcessLocale("de"); | |
277 RunRequest(kEditAction, kImagePngType, response); | |
278 ASSERT_EQ(1UL, extensions_.size()); | |
279 EXPECT_EQ(std::string("__MSG_name__"), | |
280 UTF16ToUTF8(extensions_[0].name)); | |
281 } | |
OLD | NEW |