OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/translate/core/browser/ranker_model_loader.h" | |
6 | |
7 #include <deque> | |
8 #include <initializer_list> | |
9 #include <memory> | |
10 #include <vector> | |
11 | |
12 #include "base/files/file_util.h" | |
13 #include "base/files/scoped_temp_dir.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/run_loop.h" | |
17 #include "base/strings/stringprintf.h" | |
18 #include "base/task_scheduler/post_task.h" | |
19 #include "base/test/scoped_feature_list.h" | |
20 #include "base/test/scoped_task_scheduler.h" | |
21 #include "base/test/test_simple_task_runner.h" | |
22 #include "base/threading/thread_task_runner_handle.h" | |
23 #include "components/metrics/proto/translate_event.pb.h" | |
24 #include "components/prefs/scoped_user_pref_update.h" | |
25 #include "components/sync_preferences/testing_pref_service_syncable.h" | |
26 #include "components/translate/core/browser/proto/ranker_model.pb.h" | |
27 #include "components/translate/core/browser/proto/translate_ranker_model.pb.h" | |
28 #include "components/translate/core/browser/translate_download_manager.h" | |
29 #include "components/translate/core/browser/translate_prefs.h" | |
30 #include "net/url_request/test_url_fetcher_factory.h" | |
31 #include "net/url_request/url_request_test_util.h" | |
32 #include "testing/gtest/include/gtest/gtest.h" | |
33 | |
34 namespace { | |
35 | |
36 using ::chrome_intelligence::RankerModel; | |
37 using ::translate::RankerModelLoader; | |
38 using ::translate::RankerModelStatus; | |
39 using ::translate::TranslateDownloadManager; | |
40 | |
41 const char kInvalidModelData[] = "not a valid model"; | |
42 const int kInvalidModelSize = sizeof(kInvalidModelData) - 1; | |
43 | |
44 class RankerModelLoaderTest : public ::testing::Test { | |
groby-ooo-7-16
2017/02/14 20:56:48
This is a fairly huge fixture. I like those better
Roger McFarlane (Chromium)
2017/02/21 22:16:51
Done.
| |
45 protected: | |
46 RankerModelLoaderTest() : url_fetcher_factory_(nullptr) {} | |
47 | |
48 void SetUp() override { | |
49 // Setup the translate download manager. | |
50 locale_ = TranslateDownloadManager::GetInstance()->application_locale(); | |
51 request_context_getter_ = new net::TestURLRequestContextGetter( | |
52 base::CreateSingleThreadTaskRunnerWithTraits(base::TaskTraits())); | |
53 TranslateDownloadManager::GetInstance()->set_application_locale("en-US"); | |
groby-ooo-7-16
2017/02/14 20:56:48
Would you mind not using en-US? I have the sneakin
Roger McFarlane (Chromium)
2017/02/21 22:16:51
Done.
| |
54 TranslateDownloadManager::GetInstance()->set_request_context( | |
55 request_context_getter_.get()); | |
56 | |
57 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | |
58 const auto& temp_dir = scoped_temp_dir_.GetPath(); | |
groby-ooo-7-16
2017/02/14 20:56:48
temp_dir_path?
Roger McFarlane (Chromium)
2017/02/21 22:16:51
Done.
| |
59 | |
60 // Setup the model file paths. | |
61 local_model_path_ = temp_dir.AppendASCII("local_model.bin"); | |
62 expired_model_path_ = temp_dir.AppendASCII("expired_model.bin"); | |
63 invalid_model_path_ = temp_dir.AppendASCII("invalid_model.bin"); | |
64 | |
65 // Setup the model URLs. | |
66 remote_model_url_ = GURL("https://some.url.net/good.model.bin"); | |
67 invalid_model_url_ = GURL("https://some.url.net/bad.model.bin"); | |
68 failed_model_url_ = GURL("https://some.url.net/fail"); | |
69 | |
70 // Initialize the model data. | |
71 ASSERT_NO_FATAL_FAILURE(InitRemoteModels()); | |
72 ASSERT_NO_FATAL_FAILURE(InitLocalModels()); | |
73 } | |
74 | |
75 void TearDown() override { | |
76 base::RunLoop().RunUntilIdle(); | |
77 TranslateDownloadManager::GetInstance()->set_application_locale(locale_); | |
78 TranslateDownloadManager::GetInstance()->set_request_context(nullptr); | |
79 } | |
80 | |
81 static bool IsEqual(const RankerModel& m1, const RankerModel& m2) { | |
82 return m1.SerializeAsString() == m2.SerializeAsString(); | |
83 } | |
84 | |
85 static bool IsEquivalent(const RankerModel& m1, const RankerModel& m2) { | |
86 RankerModel copy_m1 = m1; | |
87 copy_m1.mutable_metadata()->Clear(); | |
88 RankerModel copy_m2 = m2; | |
89 copy_m2.mutable_metadata()->Clear(); | |
90 return IsEqual(copy_m1, copy_m2); | |
91 } | |
92 | |
93 bool DoLoaderTest(const base::FilePath& model_path, const GURL& model_url) { | |
94 auto loader = base::MakeUnique<RankerModelLoader>( | |
95 base::Bind(&RankerModelLoaderTest::Validate, base::Unretained(this)), | |
96 base::Bind(&RankerModelLoaderTest::OnModelAvailable, | |
97 base::Unretained(this)), | |
98 model_path, model_url, "RankerModelLoaderTest"); | |
99 | |
100 loader->Start(); | |
101 base::RunLoop().RunUntilIdle(); | |
102 | |
103 return true; | |
104 } | |
105 | |
106 void InitRemoteModels() { | |
107 InitModel(remote_model_url_, base::Time(), base::TimeDelta(), | |
108 &remote_model_); | |
109 url_fetcher_factory_.SetFakeResponse( | |
110 remote_model_url_, remote_model_.SerializeAsString(), net::HTTP_OK, | |
111 net::URLRequestStatus::SUCCESS); | |
112 url_fetcher_factory_.SetFakeResponse(invalid_model_url_, kInvalidModelData, | |
113 net::HTTP_OK, | |
114 net::URLRequestStatus::SUCCESS); | |
115 url_fetcher_factory_.SetFakeResponse(failed_model_url_, "", | |
116 net::HTTP_INTERNAL_SERVER_ERROR, | |
117 net::URLRequestStatus::FAILED); | |
118 } | |
119 | |
120 void InitLocalModels() { | |
121 InitModel(remote_model_url_, base::Time::Now(), | |
122 base::TimeDelta::FromDays(30), &local_model_); | |
123 InitModel(remote_model_url_, | |
124 base::Time::Now() - base::TimeDelta::FromDays(60), | |
125 base::TimeDelta::FromDays(30), &expired_model_); | |
126 SaveModel(local_model_, local_model_path_); | |
127 SaveModel(expired_model_, expired_model_path_); | |
128 ASSERT_EQ(base::WriteFile(invalid_model_path_, kInvalidModelData, | |
129 kInvalidModelSize), | |
130 kInvalidModelSize); | |
131 } | |
132 | |
133 void InitModel(const GURL& model_url, | |
134 const base::Time& last_modified, | |
135 const base::TimeDelta& cache_duration, | |
136 RankerModel* model) { | |
137 model->Clear(); | |
138 | |
139 auto* metadata = model->mutable_metadata(); | |
140 if (!model_url.is_empty()) | |
141 metadata->set_source(model_url.spec()); | |
142 if (!last_modified.is_null()) { | |
143 auto last_modified_sec = (last_modified - base::Time()).InSeconds(); | |
144 metadata->set_last_modified_sec(last_modified_sec); | |
145 } | |
146 if (!cache_duration.is_zero()) | |
147 metadata->set_cache_duration_sec(cache_duration.InSeconds()); | |
148 | |
149 auto* translate = model->mutable_translate(); | |
150 auto* logit = translate->mutable_logistic_regression_model(); | |
151 translate->set_version(1); | |
152 logit->set_bias(0.1f); | |
153 logit->set_accept_ratio_weight(0.2f); | |
154 logit->set_decline_ratio_weight(0.3f); | |
155 logit->set_ignore_ratio_weight(0.4f); | |
156 } | |
157 | |
158 void SaveModel(const RankerModel& model, const base::FilePath& model_path) { | |
159 std::string model_str = model.SerializeAsString(); | |
160 ASSERT_EQ(base::WriteFile(model_path, model_str.data(), model_str.size()), | |
161 static_cast<int>(model_str.size())); | |
162 } | |
163 | |
164 RankerModelStatus Validate(const RankerModel& model) { | |
165 validated_models_.push_back(model); | |
166 RankerModelStatus response = RankerModelStatus::OK; | |
167 if (!validate_response_.empty()) { | |
168 response = validate_response_.front(); | |
169 validate_response_.pop_front(); | |
170 } | |
171 return response; | |
172 } | |
173 | |
174 void OnModelAvailable(std::unique_ptr<RankerModel> model) { | |
175 available_models_.push_back(*model); | |
groby-ooo-7-16
2017/02/14 20:56:48
Wait - isn't that model deleted when you leave OnM
Roger McFarlane (Chromium)
2017/02/21 22:16:51
This was storing a copy of the model.
I've added
| |
176 } | |
177 | |
178 // Override the default URL fetcher to return custom responses for tests. | |
179 net::FakeURLFetcherFactory url_fetcher_factory_; | |
180 | |
181 // Sets up the task scheduling/task-runner environment for each test. | |
182 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
183 | |
184 // Temporary directory for model files. | |
185 base::ScopedTempDir scoped_temp_dir_; | |
186 | |
187 // Cache and reset the application locale for each test. | |
188 std::string locale_; | |
189 | |
190 // Used to initialize the translate download manager. | |
191 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
192 | |
193 // A queue of responses to return from Validate(). If empty, validate will | |
194 // return 'OK'. | |
195 std::deque<RankerModelStatus> validate_response_; | |
196 | |
197 // A cached to remember the model validation calls. | |
198 std::vector<RankerModel> validated_models_; | |
199 | |
200 // A cache to remember the OnModelAvailable calls. | |
201 std::vector<RankerModel> available_models_; | |
202 | |
203 // Cached model file paths. | |
204 base::FilePath local_model_path_; | |
205 base::FilePath expired_model_path_; | |
206 base::FilePath invalid_model_path_; | |
207 | |
208 // Model URLS. | |
209 GURL remote_model_url_; | |
210 GURL invalid_model_url_; | |
211 GURL failed_model_url_; | |
212 | |
213 // Model Data. | |
214 RankerModel remote_model_; | |
215 RankerModel local_model_; | |
216 RankerModel expired_model_; | |
217 | |
218 private: | |
219 DISALLOW_COPY_AND_ASSIGN(RankerModelLoaderTest); | |
220 }; | |
221 | |
222 } // namespace | |
223 | |
224 TEST_F(RankerModelLoaderTest, NoLocalOrRemoteModel) { | |
225 ASSERT_TRUE(DoLoaderTest(base::FilePath(), GURL())); | |
226 | |
227 ASSERT_EQ(0U, validated_models_.size()); | |
groby-ooo-7-16
2017/02/14 20:56:48
Nit: The last two should probably be EXPECT_EQ, no
Roger McFarlane (Chromium)
2017/02/21 22:16:51
Done.
| |
228 ASSERT_EQ(0U, available_models_.size()); | |
229 } | |
230 | |
231 TEST_F(RankerModelLoaderTest, BadLocalAndRemoteModel) { | |
232 ASSERT_TRUE(DoLoaderTest(invalid_model_path_, invalid_model_url_)); | |
233 | |
234 ASSERT_EQ(0U, validated_models_.size()); | |
235 ASSERT_EQ(0U, available_models_.size()); | |
236 } | |
237 | |
238 TEST_F(RankerModelLoaderTest, LoadFromFileOnly) { | |
239 ASSERT_TRUE(DoLoaderTest(local_model_path_, GURL())); | |
240 | |
241 ASSERT_EQ(1U, validated_models_.size()); | |
242 ASSERT_EQ(1U, available_models_.size()); | |
243 ASSERT_TRUE(IsEqual(validated_models_[0], local_model_)); | |
244 ASSERT_TRUE(IsEqual(available_models_[0], local_model_)); | |
245 } | |
246 | |
247 TEST_F(RankerModelLoaderTest, LoadFromFileSkipsDownload) { | |
248 ASSERT_TRUE(DoLoaderTest(local_model_path_, remote_model_url_)); | |
249 | |
250 ASSERT_EQ(1U, validated_models_.size()); | |
251 ASSERT_EQ(1U, available_models_.size()); | |
252 ASSERT_TRUE(IsEqual(validated_models_[0], local_model_)); | |
253 ASSERT_TRUE(IsEqual(available_models_[0], local_model_)); | |
254 } | |
255 | |
256 TEST_F(RankerModelLoaderTest, LoadFromFileAndBadUrl) { | |
257 ASSERT_TRUE(DoLoaderTest(local_model_path_, invalid_model_url_)); | |
258 ASSERT_EQ(1U, validated_models_.size()); | |
259 ASSERT_EQ(1U, available_models_.size()); | |
260 ASSERT_TRUE(IsEqual(validated_models_[0], local_model_)); | |
261 ASSERT_TRUE(IsEqual(available_models_[0], local_model_)); | |
262 } | |
263 | |
264 TEST_F(RankerModelLoaderTest, LoadFromURLOnly) { | |
265 ASSERT_TRUE(DoLoaderTest(base::FilePath(), remote_model_url_)); | |
266 ASSERT_EQ(1U, validated_models_.size()); | |
267 ASSERT_EQ(1U, available_models_.size()); | |
268 ASSERT_TRUE(IsEquivalent(validated_models_[0], remote_model_)); | |
269 ASSERT_TRUE(IsEquivalent(available_models_[0], remote_model_)); | |
270 } | |
271 | |
272 TEST_F(RankerModelLoaderTest, LoadFromExpiredFileTriggersDownload) { | |
273 ASSERT_TRUE(DoLoaderTest(expired_model_path_, remote_model_url_)); | |
274 ASSERT_EQ(2U, validated_models_.size()); | |
275 ASSERT_EQ(2U, available_models_.size()); | |
276 ASSERT_TRUE(IsEquivalent(validated_models_[0], local_model_)); | |
277 ASSERT_TRUE(IsEquivalent(available_models_[0], local_model_)); | |
278 ASSERT_TRUE(IsEquivalent(validated_models_[1], remote_model_)); | |
279 ASSERT_TRUE(IsEquivalent(available_models_[1], remote_model_)); | |
280 } | |
281 | |
282 TEST_F(RankerModelLoaderTest, LoadFromBadFileTriggersDownload) { | |
283 ASSERT_TRUE(DoLoaderTest(invalid_model_path_, remote_model_url_)); | |
284 ASSERT_EQ(1U, validated_models_.size()); | |
285 ASSERT_EQ(1U, available_models_.size()); | |
286 ASSERT_TRUE(IsEquivalent(validated_models_[0], remote_model_)); | |
287 ASSERT_TRUE(IsEquivalent(available_models_[0], remote_model_)); | |
288 } | |
289 | |
290 TEST_F(RankerModelLoaderTest, IncompatibleCachedFileTriggersDownload) { | |
291 validate_response_.push_back(RankerModelStatus::INCOMPATIBLE); | |
292 | |
293 ASSERT_TRUE(DoLoaderTest(local_model_path_, remote_model_url_)); | |
294 ASSERT_EQ(2U, validated_models_.size()); | |
295 ASSERT_EQ(1U, available_models_.size()); | |
296 ASSERT_TRUE(IsEquivalent(validated_models_[0], local_model_)); | |
297 ASSERT_TRUE(IsEquivalent(validated_models_[1], remote_model_)); | |
298 ASSERT_TRUE(IsEquivalent(available_models_[0], remote_model_)); | |
299 } | |
300 | |
301 TEST_F(RankerModelLoaderTest, IncompatibleDownloadedFileKeepsExpired) { | |
302 validate_response_.push_back(RankerModelStatus::OK); | |
303 validate_response_.push_back(RankerModelStatus::INCOMPATIBLE); | |
304 | |
305 ASSERT_TRUE(DoLoaderTest(expired_model_path_, remote_model_url_)); | |
306 ASSERT_EQ(2U, validated_models_.size()); | |
307 ASSERT_EQ(1U, available_models_.size()); | |
308 ASSERT_TRUE(IsEquivalent(validated_models_[0], local_model_)); | |
309 ASSERT_TRUE(IsEquivalent(validated_models_[1], remote_model_)); | |
310 ASSERT_TRUE(IsEquivalent(available_models_[0], local_model_)); | |
311 } | |
OLD | NEW |