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