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

Side by Side Diff: components/translate/core/browser/translate_manager_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: add unit tests 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_manager.h" 5 #include "components/translate/core/browser/translate_manager.h"
6 6
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/test/histogram_tester.h" 8 #include "base/test/histogram_tester.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "components/infobars/core/infobar.h" 10 #include "components/infobars/core/infobar.h"
11 #include "components/pref_registry/pref_registry_syncable.h" 11 #include "components/pref_registry/pref_registry_syncable.h"
12 #include "components/pref_registry/testing_pref_service_syncable.h" 12 #include "components/pref_registry/testing_pref_service_syncable.h"
13 #include "components/translate/core/browser/mock_translate_driver.h" 13 #include "components/translate/core/browser/mock_translate_driver.h"
14 #include "components/translate/core/browser/translate_browser_metrics.h" 14 #include "components/translate/core/browser/translate_browser_metrics.h"
15 #include "components/translate/core/browser/translate_client.h" 15 #include "components/translate/core/browser/translate_client.h"
16 #include "components/translate/core/browser/translate_download_manager.h" 16 #include "components/translate/core/browser/translate_download_manager.h"
17 #include "components/translate/core/browser/translate_prefs.h" 17 #include "components/translate/core/browser/translate_prefs.h"
18 #include "components/translate/core/common/translate_pref_names.h" 18 #include "components/translate/core/common/translate_pref_names.h"
19 #include "net/base/network_change_notifier.h" 19 #include "net/base/network_change_notifier.h"
20 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using testing::_;
24 using testing::Return;
25 using testing::SetArgPointee;
26
23 namespace translate { 27 namespace translate {
24 28
25 namespace { 29 namespace {
26 30
27 #if defined(OS_CHROMEOS) 31 #if defined(OS_CHROMEOS)
28 const char kLanguagePreferredLanguages[] = 32 const char kLanguagePreferredLanguages[] =
29 "settings.language.preferred_languages"; 33 "settings.language.preferred_languages";
30 #else 34 #else
31 const char* kLanguagePreferredLanguages = nullptr; 35 const char* kLanguagePreferredLanguages = nullptr;
32 #endif 36 #endif
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 #if defined(OS_CHROMEOS) 135 #if defined(OS_CHROMEOS)
132 prefs_.registry()->RegisterStringPref(kLanguagePreferredLanguages, 136 prefs_.registry()->RegisterStringPref(kLanguagePreferredLanguages,
133 std::string()); 137 std::string());
134 #endif 138 #endif
135 TranslatePrefs::RegisterProfilePrefs(prefs_.registry()); 139 TranslatePrefs::RegisterProfilePrefs(prefs_.registry());
136 // TODO(groby): Figure out RegisterProfilePrefs() should register this. 140 // TODO(groby): Figure out RegisterProfilePrefs() should register this.
137 prefs_.registry()->RegisterBooleanPref( 141 prefs_.registry()->RegisterBooleanPref(
138 prefs::kEnableTranslate, true, 142 prefs::kEnableTranslate, true,
139 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 143 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
140 manager_->ResetForTesting(); 144 manager_->ResetForTesting();
145
146 base::FeatureList::ClearInstanceForTesting();
147 base::FeatureList::SetInstance(base::WrapUnique(new base::FeatureList()));
141 } 148 }
142 user_prefs::TestingPrefServiceSyncable prefs_; 149 user_prefs::TestingPrefServiceSyncable prefs_;
143 150
144 // TODO(groby): request TranslatePrefs from |mock_translate_client_| instead. 151 // TODO(groby): request TranslatePrefs from |mock_translate_client_| instead.
145 TranslatePrefs translate_prefs_; 152 TranslatePrefs translate_prefs_;
146 TranslateDownloadManager* manager_; 153 TranslateDownloadManager* manager_;
147 154
148 TestNetworkChangeNotifier network_notifier_; 155 TestNetworkChangeNotifier network_notifier_;
149 translate::testing::MockTranslateDriver driver_; 156 translate::testing::MockTranslateDriver driver_;
150 ::testing::NiceMock<MockTranslateClient> mock_translate_client_; 157 ::testing::NiceMock<MockTranslateClient> mock_translate_client_;
151 std::unique_ptr<TranslateManager> translate_manager_; 158 std::unique_ptr<TranslateManager> translate_manager_;
152 159
160 void TurnOnTranslateByULP() {
161 base::FeatureList::ClearInstanceForTesting();
162 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
163 feature_list->InitializeFromCommandLine(
164 translate::kTranslateLanguageByULP.name, std::string());
165 base::FeatureList::SetInstance(std::move(feature_list));
166 }
153 void TearDown() override { manager_->ResetForTesting(); } 167 void TearDown() override { manager_->ResetForTesting(); }
154 }; 168 };
155 169
156 } // namespace 170 } // namespace
157 171
158 // Target language comes from application locale if the locale's language 172 // Target language comes from application locale if the locale's language
159 // is supported. 173 // is supported.
160 TEST_F(TranslateManagerTest, GetTargetLanguageDefaultsToAppLocale) { 174 TEST_F(TranslateManagerTest, GetTargetLanguageDefaultsToAppLocale) {
161 // Ensure the locale is set to a supported language. 175 // Ensure the locale is set to a supported language.
162 ASSERT_TRUE(TranslateDownloadManager::IsSupportedLanguage("en")); 176 ASSERT_TRUE(TranslateDownloadManager::IsSupportedLanguage("en"));
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 232
219 // In the online case, InitiateTranslation will proceed past early out tests. 233 // In the online case, InitiateTranslation will proceed past early out tests.
220 network_notifier_.SimulateOnline(); 234 network_notifier_.SimulateOnline();
221 translate_manager_->InitiateTranslation("de"); 235 translate_manager_->InitiateTranslation("de");
222 histogram_tester.ExpectUniqueSample( 236 histogram_tester.ExpectUniqueSample(
223 kMetricName, 237 kMetricName,
224 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS, 238 translate::TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS,
225 1); 239 1);
226 } 240 }
227 241
242 class MockTranslatePrefs : public TranslatePrefs {
groby-ooo-7-16 2016/08/02 00:38:21 Please don't mock translate prefs. This seems to j
ftang 2016/08/03 02:01:18 I have to mock the TranslatePrefs so I can test th
243 public:
244 MockTranslatePrefs()
245 : TranslatePrefs(nullptr, kAcceptLanguages, kLanguagePreferredLanguages) {
246 }
247
248 virtual ~MockTranslatePrefs() {}
249 MOCK_CONST_METHOD1(GetReadingFromUserLanguageProfile,
250 double(LanguageProbabilityList* list));
251 MOCK_CONST_METHOD1(GetWritingFromUserLanguageProfile,
252 double(LanguageProbabilityList* list));
253 };
254
255 TEST_F(TranslateManagerTest, TestGetTargetLanguageFromULP) {
256 // Test 6 different cases:
257 // Case 1. The feature is turn OFF. Expect to return "".
258 // Case 2-6 The feature is turn ON.
259 // Case 2. The confidence of the reading list is 0.0. Expect to return "".
260 // Case 3. The confidence of the reading list is low. Expect to return "".
261 // Case 4. The confidence of the reading list is high enough but there
262 // are somehow no items in the list. Expect to return "".
263 // Case 5. The confidence of the reading list is high enough and
264 // the first item is "fr" with high probability. Expect to return
265 // "fr".
266 // Case 6. The confidence of the reading list is high enough and the first
267 // item is "pl" with low probability. Expect to return "".
268
269 ::testing::NiceMock<MockTranslatePrefs> mock_translate_prefs;
270
271 // For case 4- no items in the list.
272 TranslatePrefs::LanguageProbabilityList list4;
273
274 // For case 5 - "fr" with high (0.9) probability on the list.
275 TranslatePrefs::LanguageProbabilityList list5;
276 list5.push_back(std::make_pair("fr", 0.9));
277
278 // For case 6 - "pl" with low (0.25) probability on the list.
279 TranslatePrefs::LanguageProbabilityList list6;
280 list6.push_back(std::make_pair("pl", 0.25));
281
282 // Only be called for case 2-6.
283 EXPECT_CALL(mock_translate_prefs,
284 GetReadingFromUserLanguageProfile(_))
285 .Times(5)
286 .WillOnce(Return(0.0)) // For case 2.
287 .WillOnce(Return(0.2)) // For case 3.
288 .WillOnce(DoAll(SetArgPointee<0>(list4), Return(0.9))) // For case 4.
289 .WillOnce(DoAll(SetArgPointee<0>(list5), Return(0.9))) // For case 5.
290 .WillOnce(DoAll(SetArgPointee<0>(list6), Return(0.9))); // For case 6.
291
292 // Case 1. The feature is turn OFF. Expect to return "".
293 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs)
294 .empty());
295
296 // Case 2-6 The feature is turn ON.
297 TurnOnTranslateByULP();
298
299 // Case 2. The confidence of the reading list is 0.0. Expect to return "".
300 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs)
301 .empty());
302
303 // Case 3. The confidence of the reading list is low. Expect to return "".
304 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs)
305 .empty());
306
307 // Case 4. The confidence of the reading list is high enough but there are
308 // somehow no items in the list. Expect to return "".
309 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs)
310 .empty());
311
312 // Case 5. The confidence of the reading list is high enough and the first
313 // item is "fr" with high probability. Expect to return "fr".
314 EXPECT_STREQ("fr",
315 TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs)
316 .c_str());
317
318 // Case 6. The confidence of the reading list is high enough and the first
319 // item is "pl" with low probability. Expect to return "".
320 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs)
321 .empty());
322 }
323
324 TEST_F(TranslateManagerTest, TestLanguageInULP) {
325 // Case Return Feature Writing List Reading List
326 // Confid. Lang. Prob. Confid. Lang. Prob.
327 // 1 false OFF
328 // 2 false ON Low Low
329 // 3 ture ON High 1st High
330 // 4 false ON High 2nd Low High 2nd Low
331 // 5 false ON High 2nd Low High 2nd High
332
333 ::testing::NiceMock<MockTranslatePrefs> mock_translate_prefs;
334 TranslateManager::SetIgnoreMissingKeyForTesting(true);
335 translate_manager_.reset(new translate::TranslateManager(
336 &mock_translate_client_, kAcceptLanguages));
337
338 // For case 3 - "fr" with high (0.7) probability on the writing list.
339 TranslatePrefs::LanguageProbabilityList wlist3;
340 wlist3.push_back(std::make_pair("fr", 0.7));
341
342 // For case 4 & 5 - "fr" with low (0.1) probability on the writing list.
343 TranslatePrefs::LanguageProbabilityList wlist4_5;
344 wlist4_5.push_back(std::make_pair("en", 0.9));
345 wlist4_5.push_back(std::make_pair("fr", 0.1));
346
347 // For case 4 - "fr" with low (0.1) probability on the reading list.
348 TranslatePrefs::LanguageProbabilityList rlist4;
349 rlist4.push_back(std::make_pair("en", 0.9));
350 rlist4.push_back(std::make_pair("fr", 0.1));
351
352 // For case 5 - "fr" with high (0.49) probability on the reading list.
353 TranslatePrefs::LanguageProbabilityList rlist5;
354 rlist5.push_back(std::make_pair("en", 0.51));
355 rlist5.push_back(std::make_pair("fr", 0.49));
356
357 // Call for case 2-5
358 EXPECT_CALL(mock_translate_prefs,
359 GetWritingFromUserLanguageProfile(_))
360 .Times(4)
361 .WillOnce(Return(0.1)) // For case 2.
362 .WillOnce(DoAll(SetArgPointee<0>(wlist3), Return(0.9))) // For case 3.
363 .WillOnce(DoAll(SetArgPointee<0>(wlist4_5), Return(0.9))) // For case 4.
364 .WillOnce(DoAll(SetArgPointee<0>(wlist4_5), Return(0.9))); // For case 5.
365
366 // Call for case 2, 4, 5
367 EXPECT_CALL(mock_translate_prefs,
368 GetReadingFromUserLanguageProfile(_))
369 .Times(3)
370 .WillOnce(Return(0.1)) // For case 2.
371 .WillOnce(DoAll(SetArgPointee<0>(rlist4), Return(0.9))) // For case 4.
372 .WillOnce(DoAll(SetArgPointee<0>(rlist5), Return(0.9))); // For case 5.
373
374 EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr"));
375 // Case 2-5 The feature is turn ON.
376 TurnOnTranslateByULP();
377 EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr"));
378 EXPECT_TRUE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr"));
379 EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr"));
380 EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr"));
381 }
382
228 } // namespace translate 383 } // namespace translate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698