Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_ranker.h" | 5 #include "components/translate/core/browser/translate_ranker.h" |
| 6 | 6 |
| 7 #include <initializer_list> | 7 #include <initializer_list> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/test/scoped_feature_list.h" | 12 #include "base/test/scoped_feature_list.h" |
| 13 #include "components/metrics/proto/translate_event.pb.h" | |
| 13 #include "components/pref_registry/testing_pref_service_syncable.h" | 14 #include "components/pref_registry/testing_pref_service_syncable.h" |
| 14 #include "components/prefs/scoped_user_pref_update.h" | 15 #include "components/prefs/scoped_user_pref_update.h" |
| 15 #include "components/translate/core/browser/proto/translate_ranker_model.pb.h" | 16 #include "components/translate/core/browser/proto/translate_ranker_model.pb.h" |
| 16 #include "components/translate/core/browser/translate_download_manager.h" | 17 #include "components/translate/core/browser/translate_download_manager.h" |
| 17 #include "components/translate/core/browser/translate_prefs.h" | 18 #include "components/translate/core/browser/translate_prefs.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace translate { | 21 namespace translate { |
| 21 | 22 |
| 22 class TranslateRankerTest : public ::testing::Test { | 23 class TranslateRankerTest : public ::testing::Test { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 auto& locale_weight = *details->mutable_locale_weight(); | 74 auto& locale_weight = *details->mutable_locale_weight(); |
| 74 locale_weight["en-us"] = 0.10f; | 75 locale_weight["en-us"] = 0.10f; |
| 75 locale_weight["en-ca"] = 0.11f; | 76 locale_weight["en-ca"] = 0.11f; |
| 76 locale_weight["zh-cn"] = 0.12f; // Normalized to lowercase. | 77 locale_weight["zh-cn"] = 0.12f; // Normalized to lowercase. |
| 77 | 78 |
| 78 return TranslateRanker::CreateForTesting(model.SerializeAsString()); | 79 return TranslateRanker::CreateForTesting(model.SerializeAsString()); |
| 79 } | 80 } |
| 80 | 81 |
| 81 static double Sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); } | 82 static double Sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); } |
| 82 | 83 |
| 84 static metrics::TranslateEventProto CreateTranslateEvent( | |
| 85 const std::string& src_lang, | |
| 86 const std::string& dst_lang, | |
| 87 int accept_count, | |
| 88 int decline_count, | |
| 89 int ignore_count) { | |
| 90 metrics::TranslateEventProto translate_event; | |
| 91 translate_event.set_source_language(src_lang); | |
| 92 translate_event.set_target_language(dst_lang); | |
| 93 translate_event.set_accept_count(accept_count); | |
| 94 translate_event.set_decline_count(decline_count); | |
| 95 translate_event.set_ignore_count(ignore_count); | |
| 96 return translate_event; | |
| 97 } | |
| 98 | |
| 83 static const char* const kPreferredLanguagePrefs; | 99 static const char* const kPreferredLanguagePrefs; |
| 84 | 100 |
| 85 std::unique_ptr<user_prefs::TestingPrefServiceSyncable> prefs_; | 101 std::unique_ptr<user_prefs::TestingPrefServiceSyncable> prefs_; |
| 86 std::unique_ptr<translate::TranslatePrefs> translate_prefs_; | 102 std::unique_ptr<translate::TranslatePrefs> translate_prefs_; |
| 87 | 103 |
| 88 private: | 104 private: |
| 89 // Manages the enabling/disabling of features within the scope of a test. | 105 // Manages the enabling/disabling of features within the scope of a test. |
| 90 base::test::ScopedFeatureList scoped_feature_list_; | 106 base::test::ScopedFeatureList scoped_feature_list_; |
| 91 | 107 |
| 92 // Cache and reset the application locale for each test. | 108 // Cache and reset the application locale for each test. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 TEST_F(TranslateRankerTest, ShouldOfferTranslation) { | 165 TEST_F(TranslateRankerTest, ShouldOfferTranslation) { |
| 150 InitFeatures({kTranslateRankerQuery, kTranslateRankerEnforcement}, {}); | 166 InitFeatures({kTranslateRankerQuery, kTranslateRankerEnforcement}, {}); |
| 151 // With a bias of -0.5 en->fr is not over the threshold. | 167 // With a bias of -0.5 en->fr is not over the threshold. |
| 152 EXPECT_FALSE(GetRankerForTest(-0.5f)->ShouldOfferTranslation( | 168 EXPECT_FALSE(GetRankerForTest(-0.5f)->ShouldOfferTranslation( |
| 153 *translate_prefs_, "en", "fr")); | 169 *translate_prefs_, "en", "fr")); |
| 154 // With a bias of 0.25 en-fr is over the threshold. | 170 // With a bias of 0.25 en-fr is over the threshold. |
| 155 EXPECT_TRUE(GetRankerForTest(0.25f)->ShouldOfferTranslation(*translate_prefs_, | 171 EXPECT_TRUE(GetRankerForTest(0.25f)->ShouldOfferTranslation(*translate_prefs_, |
| 156 "en", "fr")); | 172 "en", "fr")); |
| 157 } | 173 } |
| 158 | 174 |
| 175 TEST_F(TranslateRankerTest, RecordAndFlushEvents) { | |
| 176 InitFeatures({kTranslateRankerQuery, kTranslateRankerEnforcement}, {}); | |
| 177 std::unique_ptr<translate::TranslateRanker> ranker = GetRankerForTest(0.0f); | |
| 178 std::vector<metrics::TranslateEventProto> flushed_events; | |
| 179 | |
| 180 // Check that flushing an empty cache will return an empty vector. | |
| 181 ranker->FlushTranslateEvents(&flushed_events); | |
| 182 EXPECT_EQ(0U, flushed_events.size()); | |
| 183 | |
| 184 auto event_1 = CreateTranslateEvent("fr", "en", 1, 0, 3); | |
| 185 auto event_2 = CreateTranslateEvent("jp", "en", 2, 0, 3); | |
| 186 auto event_3 = CreateTranslateEvent("es", "de", 4, 5, 6); | |
| 187 ranker->RecordTranslateEvent(event_1); | |
| 188 ranker->RecordTranslateEvent(event_2); | |
| 189 ranker->RecordTranslateEvent(event_3); | |
| 190 | |
| 191 // Capture the data and verify that it is as expected. | |
| 192 ranker->FlushTranslateEvents(&flushed_events); | |
| 193 EXPECT_EQ(3U, flushed_events.size()); | |
| 194 ASSERT_EQ("fr", flushed_events[0].source_language()); | |
| 195 ASSERT_EQ("jp", flushed_events[1].source_language()); | |
| 196 ASSERT_EQ("es", flushed_events[2].source_language()); | |
| 197 | |
| 198 // Check that the cache has been cleared. | |
| 199 ranker->FlushTranslateEvents(&flushed_events); | |
| 200 EXPECT_EQ(0U, flushed_events.size()); | |
| 201 } | |
| 202 | |
|
Alexei Svitkine (slow)
2016/10/12 15:21:58
Nit: Remove extra empty line.
hamelphi
2016/10/12 16:17:21
Done.
| |
| 203 | |
| 159 } // namespace translate | 204 } // namespace translate |
| OLD | NEW |