OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/ranker_model_loader.h" | 5 #include "components/ranker/ranker_model_loader.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 #include <initializer_list> | 8 #include <initializer_list> |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/task_scheduler/post_task.h" | 19 #include "base/task_scheduler/post_task.h" |
20 #include "base/task_scheduler/task_scheduler.h" | 20 #include "base/task_scheduler/task_scheduler.h" |
21 #include "base/test/scoped_feature_list.h" | 21 #include "base/test/scoped_feature_list.h" |
22 #include "base/test/scoped_task_scheduler.h" | 22 #include "base/test/scoped_task_scheduler.h" |
23 #include "base/test/test_simple_task_runner.h" | 23 #include "base/test/test_simple_task_runner.h" |
24 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
25 #include "components/metrics/proto/translate_event.pb.h" | 25 #include "components/ranker/proto/ranker_model.pb.h" |
26 #include "components/prefs/scoped_user_pref_update.h" | 26 #include "components/ranker/proto/translate_ranker_model.pb.h" |
27 #include "components/sync_preferences/testing_pref_service_syncable.h" | 27 #include "components/ranker/ranker_model.h" |
28 #include "components/translate/core/browser/proto/ranker_model.pb.h" | 28 // FIXME |
29 #include "components/translate/core/browser/proto/translate_ranker_model.pb.h" | 29 // #include "components/translate/core/browser/translate_download_manager.h" |
30 #include "components/translate/core/browser/ranker_model.h" | |
31 #include "components/translate/core/browser/translate_download_manager.h" | |
32 #include "components/translate/core/browser/translate_prefs.h" | |
33 #include "net/url_request/test_url_fetcher_factory.h" | 30 #include "net/url_request/test_url_fetcher_factory.h" |
34 #include "net/url_request/url_request_test_util.h" | 31 #include "net/url_request/url_request_test_util.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
36 | 33 |
37 namespace { | 34 namespace { |
38 | 35 |
39 using base::TaskScheduler; | 36 using base::TaskScheduler; |
40 using chrome_intelligence::RankerModel; | 37 using ranker::RankerModel; |
41 using translate::RankerModelLoader; | 38 using ranker::RankerModelLoader; |
42 using translate::RankerModelStatus; | 39 using ranker::RankerModelStatus; |
43 using translate::TranslateDownloadManager; | |
44 | 40 |
45 const char kInvalidModelData[] = "not a valid model"; | 41 const char kInvalidModelData[] = "not a valid model"; |
46 const int kInvalidModelSize = sizeof(kInvalidModelData) - 1; | 42 const int kInvalidModelSize = sizeof(kInvalidModelData) - 1; |
47 | 43 |
48 class RankerModelLoaderTest : public ::testing::Test { | 44 class RankerModelLoaderTest : public ::testing::Test { |
49 protected: | 45 protected: |
50 RankerModelLoaderTest(); | 46 RankerModelLoaderTest(); |
51 | 47 |
52 void SetUp() override; | 48 void SetUp() override; |
53 | 49 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 | 85 |
90 // Sets up the task scheduling/task-runner environment for each test. | 86 // Sets up the task scheduling/task-runner environment for each test. |
91 base::test::ScopedTaskScheduler scoped_task_scheduler_; | 87 base::test::ScopedTaskScheduler scoped_task_scheduler_; |
92 | 88 |
93 // Override the default URL fetcher to return custom responses for tests. | 89 // Override the default URL fetcher to return custom responses for tests. |
94 net::FakeURLFetcherFactory url_fetcher_factory_; | 90 net::FakeURLFetcherFactory url_fetcher_factory_; |
95 | 91 |
96 // Temporary directory for model files. | 92 // Temporary directory for model files. |
97 base::ScopedTempDir scoped_temp_dir_; | 93 base::ScopedTempDir scoped_temp_dir_; |
98 | 94 |
99 // Cache and reset the application locale for each test. | 95 // FIXME |
Roger McFarlane (Chromium)
2017/06/06 09:48:30
?
hamelphi
2017/06/09 18:49:15
Done.
| |
100 std::string locale_; | |
101 | |
102 // Used to initialize the translate download manager. | 96 // Used to initialize the translate download manager. |
103 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 97 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
104 | 98 |
105 // A queue of responses to return from Validate(). If empty, validate will | 99 // A queue of responses to return from Validate(). If empty, validate will |
106 // return 'OK'. | 100 // return 'OK'. |
107 std::deque<RankerModelStatus> validate_model_response_; | 101 std::deque<RankerModelStatus> validate_model_response_; |
108 | 102 |
109 // A cached to remember the model validation calls. | 103 // A cached to remember the model validation calls. |
110 std::vector<std::unique_ptr<RankerModel>> validated_models_; | 104 std::vector<std::unique_ptr<RankerModel>> validated_models_; |
111 | 105 |
(...skipping 17 matching lines...) Expand all Loading... | |
129 | 123 |
130 private: | 124 private: |
131 DISALLOW_COPY_AND_ASSIGN(RankerModelLoaderTest); | 125 DISALLOW_COPY_AND_ASSIGN(RankerModelLoaderTest); |
132 }; | 126 }; |
133 | 127 |
134 RankerModelLoaderTest::RankerModelLoaderTest() | 128 RankerModelLoaderTest::RankerModelLoaderTest() |
135 : url_fetcher_factory_(nullptr) {} | 129 : url_fetcher_factory_(nullptr) {} |
136 | 130 |
137 void RankerModelLoaderTest::SetUp() { | 131 void RankerModelLoaderTest::SetUp() { |
138 // Setup the translate download manager. | 132 // Setup the translate download manager. |
139 locale_ = TranslateDownloadManager::GetInstance()->application_locale(); | |
Roger McFarlane (Chromium)
2017/06/06 09:48:30
ah, yes, I remember what this was for... it just m
hamelphi
2017/06/09 18:49:15
Not relevant anymore.
| |
140 request_context_ = | 133 request_context_ = |
141 new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get()); | 134 new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get()); |
142 TranslateDownloadManager::GetInstance()->set_application_locale("fr-CA"); | 135 // TranslateDownloadManager::GetInstance()->set_request_context( |
143 TranslateDownloadManager::GetInstance()->set_request_context( | 136 // request_context_.get()); |
Roger McFarlane (Chromium)
2017/06/06 09:48:31
I would expect this to abort download if not set.
hamelphi
2017/06/09 18:49:15
Not relevant anymore.
| |
144 request_context_.get()); | |
145 | 137 |
146 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 138 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
147 const auto& temp_dir_path = scoped_temp_dir_.GetPath(); | 139 const auto& temp_dir_path = scoped_temp_dir_.GetPath(); |
148 | 140 |
149 // Setup the model file paths. | 141 // Setup the model file paths. |
150 local_model_path_ = temp_dir_path.AppendASCII("local_model.bin"); | 142 local_model_path_ = temp_dir_path.AppendASCII("local_model.bin"); |
151 expired_model_path_ = temp_dir_path.AppendASCII("expired_model.bin"); | 143 expired_model_path_ = temp_dir_path.AppendASCII("expired_model.bin"); |
152 invalid_model_path_ = temp_dir_path.AppendASCII("invalid_model.bin"); | 144 invalid_model_path_ = temp_dir_path.AppendASCII("invalid_model.bin"); |
153 | 145 |
154 // Setup the model URLs. | 146 // Setup the model URLs. |
155 remote_model_url_ = GURL("https://some.url.net/good.model.bin"); | 147 remote_model_url_ = GURL("https://some.url.net/good.model.bin"); |
156 invalid_model_url_ = GURL("https://some.url.net/bad.model.bin"); | 148 invalid_model_url_ = GURL("https://some.url.net/bad.model.bin"); |
157 failed_model_url_ = GURL("https://some.url.net/fail"); | 149 failed_model_url_ = GURL("https://some.url.net/fail"); |
158 | 150 |
159 // Initialize the model data. | 151 // Initialize the model data. |
160 ASSERT_NO_FATAL_FAILURE(InitRemoteModels()); | 152 ASSERT_NO_FATAL_FAILURE(InitRemoteModels()); |
161 ASSERT_NO_FATAL_FAILURE(InitLocalModels()); | 153 ASSERT_NO_FATAL_FAILURE(InitLocalModels()); |
162 } | 154 } |
163 | 155 |
164 void RankerModelLoaderTest::TearDown() { | 156 void RankerModelLoaderTest::TearDown() { |
165 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
166 TranslateDownloadManager::GetInstance()->set_application_locale(locale_); | 158 // TranslateDownloadManager::GetInstance()->set_request_context(nullptr); |
167 TranslateDownloadManager::GetInstance()->set_request_context(nullptr); | |
168 } | 159 } |
169 | 160 |
170 // static | 161 // static |
171 std::unique_ptr<RankerModel> RankerModelLoaderTest::Clone( | 162 std::unique_ptr<RankerModel> RankerModelLoaderTest::Clone( |
172 const RankerModel& model) { | 163 const RankerModel& model) { |
173 auto copy = base::MakeUnique<RankerModel>(); | 164 auto copy = base::MakeUnique<RankerModel>(); |
174 *copy->mutable_proto() = model.proto(); | 165 *copy->mutable_proto() = model.proto(); |
175 return copy; | 166 return copy; |
176 } | 167 } |
177 | 168 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 validate_model_response_.push_back(RankerModelStatus::OK); | 356 validate_model_response_.push_back(RankerModelStatus::OK); |
366 validate_model_response_.push_back(RankerModelStatus::INCOMPATIBLE); | 357 validate_model_response_.push_back(RankerModelStatus::INCOMPATIBLE); |
367 | 358 |
368 ASSERT_TRUE(DoLoaderTest(expired_model_path_, remote_model_url_)); | 359 ASSERT_TRUE(DoLoaderTest(expired_model_path_, remote_model_url_)); |
369 ASSERT_EQ(2U, validated_models_.size()); | 360 ASSERT_EQ(2U, validated_models_.size()); |
370 ASSERT_EQ(1U, available_models_.size()); | 361 ASSERT_EQ(1U, available_models_.size()); |
371 EXPECT_TRUE(IsEquivalent(*validated_models_[0], local_model_)); | 362 EXPECT_TRUE(IsEquivalent(*validated_models_[0], local_model_)); |
372 EXPECT_TRUE(IsEquivalent(*validated_models_[1], remote_model_)); | 363 EXPECT_TRUE(IsEquivalent(*validated_models_[1], remote_model_)); |
373 EXPECT_TRUE(IsEquivalent(*available_models_[0], local_model_)); | 364 EXPECT_TRUE(IsEquivalent(*available_models_[0], local_model_)); |
374 } | 365 } |
OLD | NEW |