Chromium Code Reviews| Index: components/translate/core/browser/translate_manager_unittest.cc |
| diff --git a/components/translate/core/browser/translate_manager_unittest.cc b/components/translate/core/browser/translate_manager_unittest.cc |
| index 7c0ba44f42153fd2c68535f0dcdff4062516b4d4..e97a1500770156e7374657f18162bb4a2ed8e527 100644 |
| --- a/components/translate/core/browser/translate_manager_unittest.cc |
| +++ b/components/translate/core/browser/translate_manager_unittest.cc |
| @@ -20,6 +20,10 @@ |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +using testing::_; |
| +using testing::Return; |
| +using testing::SetArgPointee; |
| + |
| namespace translate { |
| namespace { |
| @@ -138,6 +142,9 @@ class TranslateManagerTest : public ::testing::Test { |
| prefs::kEnableTranslate, true, |
| user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| manager_->ResetForTesting(); |
| + |
| + base::FeatureList::ClearInstanceForTesting(); |
| + base::FeatureList::SetInstance(base::WrapUnique(new base::FeatureList())); |
| } |
| user_prefs::TestingPrefServiceSyncable prefs_; |
| @@ -150,6 +157,13 @@ class TranslateManagerTest : public ::testing::Test { |
| ::testing::NiceMock<MockTranslateClient> mock_translate_client_; |
| std::unique_ptr<TranslateManager> translate_manager_; |
| + void TurnOnTranslateByULP() { |
| + base::FeatureList::ClearInstanceForTesting(); |
| + std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| + feature_list->InitializeFromCommandLine( |
| + translate::kTranslateLanguageByULP.name, std::string()); |
| + base::FeatureList::SetInstance(std::move(feature_list)); |
| + } |
| void TearDown() override { manager_->ResetForTesting(); } |
| }; |
| @@ -225,4 +239,122 @@ TEST_F(TranslateManagerTest, DontTranslateOffline) { |
| 1); |
| } |
| +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.
|
| + public: |
| + MockTranslatePrefs() |
| + : TranslatePrefs(nullptr, kAcceptLanguages, kLanguagePreferredLanguages) { |
| + } |
| + |
| + virtual ~MockTranslatePrefs() {} |
| + MOCK_CONST_METHOD1(GetReadingFromUserLanguageProfile, |
| + double(LanguageAndProbabilityList* list)); |
| +}; |
| + |
| +TEST_F(TranslateManagerTest, TestGetTargetLanguageFromULP) { |
| + // Test 6 different cases: |
| + // Case 1. The feature is turn OFF. Expect to return "". |
| + // Case 2-6 The feature is turn ON. |
| + // Case 2. The confidence of the reading list is 0.0. Expect to return "". |
| + // Case 3. The confidence of the reading list is low. Expect to return "". |
| + // Case 4. The confidence of the reading list is high enough but there |
| + // are somehow no items in the list. Expect to return "". |
| + // Case 5. The confidence of the reading list is high enough and |
| + // the first item is "fr" with high probability. Expect to return |
| + // "fr". |
| + // Case 6. The confidence of the reading list is high enough and the first |
| + // item is "pl" with low probability. Expect to return "". |
| + |
| + ::testing::NiceMock<MockTranslatePrefs> mock_translate_prefs; |
| + |
| + // For case 4- no items in the list. |
| + TranslatePrefs::LanguageAndProbabilityList list4; |
| + |
| + // For case 5 - "fr" with high (0.9) probability on the list. |
| + TranslatePrefs::LanguageAndProbabilityList list5; |
| + list5.push_back(std::make_pair("fr", 0.9)); |
| + |
| + // For case 6 - "pl" with low (0.25) probability on the list. |
| + TranslatePrefs::LanguageAndProbabilityList list6; |
| + list6.push_back(std::make_pair("pl", 0.25)); |
| + |
| + // Only be called for case 2-6. |
| + EXPECT_CALL(mock_translate_prefs, |
| + GetReadingFromUserLanguageProfile(_)) |
| + .Times(5) |
| + .WillOnce(Return(0.0)) // For case 2. |
| + .WillOnce(Return(0.2)) // For case 3. |
| + .WillOnce(DoAll(SetArgPointee<0>(list4), Return(0.9))) // For case 4. |
| + .WillOnce(DoAll(SetArgPointee<0>(list5), Return(0.9))) // For case 5. |
| + .WillOnce(DoAll(SetArgPointee<0>(list6), Return(0.9))); // For case 6. |
| + |
| + // Case 1. The feature is turn OFF. Expect to return "". |
| + EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) |
| + .empty()); |
| + |
| + // Case 2-6 The feature is turn ON. |
| + TurnOnTranslateByULP(); |
| + |
| + // Case 2. The confidence of the reading list is 0.0. Expect to return "". |
| + EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) |
| + .empty()); |
| + |
| + // Case 3. The confidence of the reading list is low. Expect to return "". |
| + EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) |
| + .empty()); |
| + |
| + // Case 4. The confidence of the reading list is high enough but there are |
| + // somehow no items in the list. Expect to return "". |
| + EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) |
| + .empty()); |
| + |
| + // Case 5. The confidence of the reading list is high enough and the first |
| + // item is "fr" with high probability. Expect to return "fr". |
| + EXPECT_STREQ("fr", |
| + TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) |
| + .c_str()); |
| + |
| + // Case 6. The confidence of the reading list is high enough and the first |
| + // item is "pl" with low probability. Expect to return "". |
| + EXPECT_TRUE(TranslateManager::GetTargetLanguageFromULP(&mock_translate_prefs) |
| + .empty()); |
| +} |
| + |
| +TEST_F(TranslateManagerTest, TestLanguageInULP) { |
| + // Case Return Feature Reading List |
| + // Confid. Lang. Prob. |
| + // 1 false OFF |
| + // 2 false ON Low |
| + // 3 ture ON High 1st High |
| + // 4 false ON High 2nd Low |
| + |
| + ::testing::NiceMock<MockTranslatePrefs> mock_translate_prefs; |
| + TranslateManager::SetIgnoreMissingKeyForTesting(true); |
| + translate_manager_.reset(new translate::TranslateManager( |
| + &mock_translate_client_, kAcceptLanguages)); |
| + |
| + // For case 3 - "fr" with high (0.7) probability on the reading list. |
| + TranslatePrefs::LanguageAndProbabilityList list3; |
| + list3.push_back(std::make_pair("fr", 0.7)); |
| + |
| + // For case 4 - "fr" with low (0.1) probability on the reading list. |
| + TranslatePrefs::LanguageAndProbabilityList list4; |
| + list4.push_back(std::make_pair("en", 0.9)); |
| + list4.push_back(std::make_pair("fr", 0.1)); |
| + |
| + // Call for case 2-5 |
| + EXPECT_CALL(mock_translate_prefs, |
| + GetReadingFromUserLanguageProfile(_)) |
| + .Times(3) |
| + .WillOnce(Return(0.1)) // For case 2. |
| + .WillOnce(DoAll(SetArgPointee<0>(list3), Return(0.9))) // For case 3. |
| + .WillOnce(DoAll(SetArgPointee<0>(list4), Return(0.9))); // For case 4. |
| + |
| + EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); |
| + // Case 2-4 The feature is turn ON. |
| + TurnOnTranslateByULP(); |
| + EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); |
| + EXPECT_TRUE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); |
| + EXPECT_FALSE(translate_manager_->LanguageInULP(&mock_translate_prefs, "fr")); |
| +} |
| + |
| } // namespace translate |