Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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/04 02:33:42
Please don't mock this - use actual TranslatePrefs
ftang
2016/08/05 04:45:27
Done.
| |
| 243 public: | |
| 244 MockTranslatePrefs() | |
| 245 : TranslatePrefs(nullptr, kAcceptLanguages, kLanguagePreferredLanguages) { | |
| 246 } | |
| 247 | |
| 248 virtual ~MockTranslatePrefs() {} | |
| 249 MOCK_CONST_METHOD1(GetReadingFromUserLanguageProfile, | |
| 250 double(LanguageAndProbabilityList* list)); | |
| 251 }; | |
| 252 | |
| 253 TEST_F(TranslateManagerTest, TestGetTargetLanguageFromULP) { | |
| 254 // Test 6 different cases: | |
| 255 // Case 1. The feature is turn OFF. Expect to return "". | |
| 256 // Case 2-6 The feature is turn ON. | |
| 257 // Case 2. The confidence of the reading list is 0.0. Expect to return "". | |
| 258 // Case 3. The confidence of the reading list is low. Expect to return "". | |
| 259 // Case 4. The confidence of the reading list is high enough but there | |
| 260 // are somehow no items in the list. Expect to return "". | |
| 261 // Case 5. The confidence of the reading list is high enough and | |
| 262 // the first item is "fr" with high probability. Expect to return | |
| 263 // "fr". | |
| 264 // Case 6. The confidence of the reading list is high enough and the first | |
| 265 // item is "pl" with low probability. Expect to return "". | |
| 266 | |
| 267 ::testing::NiceMock<MockTranslatePrefs> mock_translate_prefs; | |
| 268 | |
| 269 // For case 4- no items in the list. | |
| 270 TranslatePrefs::LanguageAndProbabilityList list4; | |
| 271 | |
| 272 // For case 5 - "fr" with high (0.9) probability on the list. | |
| 273 TranslatePrefs::LanguageAndProbabilityList list5; | |
| 274 list5.push_back(std::make_pair("fr", 0.9)); | |
| 275 | |
| 276 // For case 6 - "pl" with low (0.25) probability on the list. | |
| 277 TranslatePrefs::LanguageAndProbabilityList list6; | |
| 278 list6.push_back(std::make_pair("pl", 0.25)); | |
| 279 | |
| 280 // Only be called for case 2-6. | |
| 281 EXPECT_CALL(mock_translate_prefs, | |
| 282 GetReadingFromUserLanguageProfile(_)) | |
| 283 .Times(5) | |
| 284 .WillOnce(Return(0.0)) // For case 2. | |
| 285 .WillOnce(Return(0.2)) // For case 3. | |
| 286 .WillOnce(DoAll(SetArgPointee<0>(list4), Return(0.9))) // For case 4. | |
| 287 .WillOnce(DoAll(SetArgPointee<0>(list5), Return(0.9))) // For case 5. | |
| 288 .WillOnce(DoAll(SetArgPointee<0>(list6), Return(0.9))); // For case 6. | |
| 289 | |
| 290 // Case 1. The feature is turn OFF. Expect to return "". | |
| 291 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) | |
| 292 .empty()); | |
| 293 | |
| 294 // Case 2-6 The feature is turn ON. | |
| 295 TurnOnTranslateByULP(); | |
| 296 | |
| 297 // Case 2. The confidence of the reading list is 0.0. Expect to return "". | |
| 298 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) | |
| 299 .empty()); | |
| 300 | |
| 301 // Case 3. The confidence of the reading list is low. Expect to return "". | |
| 302 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) | |
| 303 .empty()); | |
| 304 | |
| 305 // Case 4. The confidence of the reading list is high enough but there are | |
| 306 // somehow no items in the list. Expect to return "". | |
| 307 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) | |
| 308 .empty()); | |
| 309 | |
| 310 // Case 5. The confidence of the reading list is high enough and the first | |
| 311 // item is "fr" with high probability. Expect to return "fr". | |
| 312 EXPECT_STREQ("fr", | |
| 313 TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) | |
| 314 .c_str()); | |
| 315 | |
| 316 // Case 6. The confidence of the reading list is high enough and the first | |
| 317 // item is "pl" with low probability. Expect to return "". | |
| 318 EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) | |
| 319 .empty()); | |
| 320 } | |
| 321 | |
| 322 TEST_F(TranslateManagerTest, TestLanguageInULP) { | |
| 323 // Case Return Feature Reading List | |
| 324 // Confid. Lang. Prob. | |
| 325 // 1 false OFF | |
| 326 // 2 false ON Low | |
| 327 // 3 ture ON High 1st High | |
| 328 // 4 false ON High 2nd Low | |
| 329 | |
| 330 ::testing::NiceMock<MockTranslatePrefs> mock_translate_prefs; | |
| 331 TranslateManager::SetIgnoreMissingKeyForTesting(true); | |
| 332 translate_manager_.reset(new translate::TranslateManager( | |
| 333 &mock_translate_client_, kAcceptLanguages)); | |
| 334 | |
| 335 // For case 3 - "fr" with high (0.7) probability on the reading list. | |
| 336 TranslatePrefs::LanguageAndProbabilityList list3; | |
| 337 list3.push_back(std::make_pair("fr", 0.7)); | |
| 338 | |
| 339 // For case 4 - "fr" with low (0.1) probability on the reading list. | |
| 340 TranslatePrefs::LanguageAndProbabilityList list4; | |
| 341 list4.push_back(std::make_pair("en", 0.9)); | |
| 342 list4.push_back(std::make_pair("fr", 0.1)); | |
| 343 | |
| 344 // Call for case 2-5 | |
| 345 EXPECT_CALL(mock_translate_prefs, | |
| 346 GetReadingFromUserLanguageProfile(_)) | |
| 347 .Times(3) | |
| 348 .WillOnce(Return(0.1)) // For case 2. | |
| 349 .WillOnce(DoAll(SetArgPointee<0>(list3), Return(0.9))) // For case 3. | |
| 350 .WillOnce(DoAll(SetArgPointee<0>(list4), Return(0.9))); // For case 4. | |
| 351 | |
| 352 EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); | |
| 353 // Case 2-4 The feature is turn ON. | |
| 354 TurnOnTranslateByULP(); | |
| 355 EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); | |
| 356 EXPECT_TRUE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); | |
| 357 EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); | |
| 358 } | |
| 359 | |
| 228 } // namespace translate | 360 } // namespace translate |
| OLD | NEW |