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

Side by Side Diff: components/translate/core/browser/translate_prefs_unittest.cc

Issue 2200493002: using ulp to improve TranslateManager GetTargetLanguage() and InitiateTranslation() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change based on 8/3 design review and simplified the use of ULP Created 4 years, 4 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/translate/core/browser/translate_prefs.h" 5 #include "components/translate/core/browser/translate_prefs.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/json/json_reader.h"
13 #include "base/values.h"
12 #include "build/build_config.h" 14 #include "build/build_config.h"
13 #include "components/pref_registry/testing_pref_service_syncable.h" 15 #include "components/pref_registry/testing_pref_service_syncable.h"
14 #include "components/prefs/scoped_user_pref_update.h" 16 #include "components/prefs/scoped_user_pref_update.h"
15 #include "components/translate/core/browser/translate_download_manager.h" 17 #include "components/translate/core/browser/translate_download_manager.h"
16 #include "components/translate/core/browser/translate_prefs.h" 18 #include "components/translate/core/browser/translate_prefs.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 namespace { 21 namespace {
20 22
21 const char kTestLanguage[] = "en"; 23 const char kTestLanguage[] = "en";
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 214
213 update.AddDenialTime(now_); 215 update.AddDenialTime(now_);
214 EXPECT_EQ(update.GetOldestDenialTime(), 216 EXPECT_EQ(update.GetOldestDenialTime(),
215 now_ - base::TimeDelta::FromMinutes(3)); 217 now_ - base::TimeDelta::FromMinutes(3));
216 218
217 update.AddDenialTime(now_); 219 update.AddDenialTime(now_);
218 EXPECT_EQ(update.GetOldestDenialTime(), 220 EXPECT_EQ(update.GetOldestDenialTime(),
219 now_ - base::TimeDelta::FromMinutes(2)); 221 now_ - base::TimeDelta::FromMinutes(2));
220 } 222 }
221 223
224 TEST_F(TranslatePrefTest, ULPPrefs) {
225 // Mock the pref.
226 // Case 1: well formed ULP.
227 const char json1[] =
228 "{\n"
229 " \"reading\": {\n"
230 " \"confidence\": 0.8,\n"
231 " \"preference\": [\n"
232 " {\n"
233 " \"language\": \"en-AU\",\n"
234 " \"probability\": 0.4\n"
235 " }, {\n"
236 " \"language\": \"fr\",\n"
237 " \"probability\": 0.6\n"
238 " }\n"
239 " ]\n"
240 " }\n"
241 "}";
242 int error_code = 0;
243 std::string error_msg;
244 int error_line = 0;
245 int error_column = 0;
246 std::unique_ptr<base::Value> profile(base::JSONReader::ReadAndReturnError(
247 json1, 0, &error_code, &error_msg, &error_line, &error_column));
248 ASSERT_EQ(0, error_code) << error_msg << " at " << error_line << ":"
249 << error_column << std::endl
250 << json1;
251
252 prefs_->SetUserPref(TranslatePrefs::kPrefLanguageProfile, profile.release());
253
254 TranslatePrefs::LanguageAndProbabilityList list;
255 EXPECT_EQ(0.8, translate_prefs_->GetReadingFromUserLanguageProfile(&list));
256 EXPECT_EQ(2UL, list.size());
257 // the order in the ULP is wrong, and our code will sort it and make the
258 // larger
259 // one first.
260 EXPECT_EQ("fr", list[0].first);
261 EXPECT_EQ(0.6, list[0].second);
262 EXPECT_EQ("en", list[1].first); // the "en-AU" should be normalize to "en"
263 EXPECT_EQ(0.4, list[1].second);
264
265 // Case 2: ill-formed ULP.
266 // Test if the ULP lacking some fields the code will gracefully ignore those
267 // items without crash.
268 const char json2[] =
269 "{\n"
270 " \"reading\": {\n"
271 " \"confidence\": 0.3,\n"
272 " \"preference\": [\n"
273 " {\n" // The first one do not have probability. Won't be counted.
274 " \"language\": \"th\"\n"
275 " }, {\n"
276 " \"language\": \"zh-TW\",\n"
277 " \"probability\": 0.4\n"
278 " }, {\n" // The third one has no language nor probability. Won't be
279 // counted.
280 " }, {\n" // The forth one has 'pt-BR' which is not supported by
281 // Translate.
282 // Should be normalize to 'pt'
283 " \"language\": \"pt-BR\",\n"
284 " \"probability\": 0.1\n"
285 " }, {\n" // The fifth one has no language. Won't be counted.
286 " \"probability\": 0.1\n"
287 " }\n"
288 " ]\n"
289 " }\n"
290 "}";
291
292 profile.reset(base::JSONReader::ReadAndReturnError(json2, 0, &error_code,
293 &error_msg, &error_line,
294 &error_column)
295 .release());
296 ASSERT_EQ(0, error_code) << error_msg << " at " << error_line << ":"
297 << error_column << std::endl
298 << json2;
299
300 prefs_->SetUserPref(TranslatePrefs::kPrefLanguageProfile, profile.release());
301
302 list.clear();
303 EXPECT_EQ(0.3, translate_prefs_->GetReadingFromUserLanguageProfile(&list));
304 EXPECT_EQ(2UL, list.size());
305 EXPECT_EQ("zh-TW", list[0].first);
306 EXPECT_EQ(0.4, list[0].second);
307 EXPECT_EQ("pt", list[1].first); // the "pt-BR" should be normalize to "pt"
308 EXPECT_EQ(0.1, list[1].second);
309
310 // Case 3: Language Code normalization and reordering.
311 const char json3[] =
312 "{\n"
313 " \"reading\": {\n"
314 " \"confidence\": 0.8,\n"
315 " \"preference\": [\n"
316 " {\n"
317 " \"language\": \"fr\",\n"
318 " \"probability\": 0.4\n"
319 " }, {\n"
320 " \"language\": \"en-US\",\n"
321 " \"probability\": 0.31\n"
322 " }, {\n"
323 " \"language\": \"en-GB\",\n"
324 " \"probability\": 0.29\n"
325 " }\n"
326 " ]\n"
327 " }\n"
328 "}";
329 profile.reset(base::JSONReader::ReadAndReturnError(json3, 0, &error_code,
330 &error_msg, &error_line,
331 &error_column)
332 .release());
333 ASSERT_EQ(0, error_code) << error_msg << " at " << error_line << ":"
334 << error_column << std::endl
335 << json3;
336
337 prefs_->SetUserPref(TranslatePrefs::kPrefLanguageProfile, profile.release());
338
339 list.clear();
340 EXPECT_EQ(0.8, translate_prefs_->GetReadingFromUserLanguageProfile(&list));
341 EXPECT_EQ(2UL, list.size());
342 EXPECT_EQ("en", list[0].first); // en-US and en-GB will be normalize into en
343 EXPECT_EQ(0.6,
344 list[0].second); // and their probability will add to gether be 0.6
345 EXPECT_EQ("fr", list[1].first); // fr will move down to the 2nd one
346 EXPECT_EQ(0.4, list[1].second);
347 }
348
222 } // namespace translate 349 } // namespace translate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698