Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: components/machine_intelligence/ranker_model_loader_unittest.cc

Issue 2925733002: Move ranker_model_loader to a new component. (Closed)
Patch Set: Adressing sdefresne's comments. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/machine_intelligence/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/translate/core/browser/proto/ranker_model.pb.h" 25 #include "components/machine_intelligence/proto/ranker_model.pb.h"
26 #include "components/translate/core/browser/proto/translate_ranker_model.pb.h" 26 #include "components/machine_intelligence/proto/translate_ranker_model.pb.h"
27 #include "components/translate/core/browser/ranker_model.h" 27 #include "components/machine_intelligence/ranker_model.h"
28 #include "components/translate/core/browser/translate_download_manager.h"
29 #include "net/url_request/test_url_fetcher_factory.h" 28 #include "net/url_request/test_url_fetcher_factory.h"
30 #include "net/url_request/url_request_test_util.h" 29 #include "net/url_request/url_request_test_util.h"
31 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
32 31
33 namespace { 32 namespace {
34 33
35 using base::TaskScheduler; 34 using base::TaskScheduler;
36 using chrome_intelligence::RankerModel; 35 using machine_intelligence::RankerModel;
37 using translate::RankerModelLoader; 36 using machine_intelligence::RankerModelLoader;
38 using translate::RankerModelStatus; 37 using machine_intelligence::RankerModelStatus;
39 using translate::TranslateDownloadManager;
40 38
41 const char kInvalidModelData[] = "not a valid model"; 39 const char kInvalidModelData[] = "not a valid model";
42 const int kInvalidModelSize = sizeof(kInvalidModelData) - 1; 40 const int kInvalidModelSize = sizeof(kInvalidModelData) - 1;
43 41
44 class RankerModelLoaderTest : public ::testing::Test { 42 class RankerModelLoaderTest : public ::testing::Test {
45 protected: 43 protected:
46 RankerModelLoaderTest(); 44 RankerModelLoaderTest();
47 45
48 void SetUp() override; 46 void SetUp() override;
49 47
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 83
86 // Sets up the task scheduling/task-runner environment for each test. 84 // Sets up the task scheduling/task-runner environment for each test.
87 base::test::ScopedTaskScheduler scoped_task_scheduler_; 85 base::test::ScopedTaskScheduler scoped_task_scheduler_;
88 86
89 // Override the default URL fetcher to return custom responses for tests. 87 // Override the default URL fetcher to return custom responses for tests.
90 net::FakeURLFetcherFactory url_fetcher_factory_; 88 net::FakeURLFetcherFactory url_fetcher_factory_;
91 89
92 // Temporary directory for model files. 90 // Temporary directory for model files.
93 base::ScopedTempDir scoped_temp_dir_; 91 base::ScopedTempDir scoped_temp_dir_;
94 92
95 // Cache and reset the application locale for each test. 93 // Used for URLFetcher.
96 std::string locale_; 94 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
97
98 // Used to initialize the translate download manager.
99 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
100 95
101 // A queue of responses to return from Validate(). If empty, validate will 96 // A queue of responses to return from Validate(). If empty, validate will
102 // return 'OK'. 97 // return 'OK'.
103 std::deque<RankerModelStatus> validate_model_response_; 98 std::deque<RankerModelStatus> validate_model_response_;
104 99
105 // A cached to remember the model validation calls. 100 // A cached to remember the model validation calls.
106 std::vector<std::unique_ptr<RankerModel>> validated_models_; 101 std::vector<std::unique_ptr<RankerModel>> validated_models_;
107 102
108 // A cache to remember the OnModelAvailable calls. 103 // A cache to remember the OnModelAvailable calls.
109 std::vector<std::unique_ptr<RankerModel>> available_models_; 104 std::vector<std::unique_ptr<RankerModel>> available_models_;
(...skipping 14 matching lines...) Expand all
124 RankerModel expired_model_; 119 RankerModel expired_model_;
125 120
126 private: 121 private:
127 DISALLOW_COPY_AND_ASSIGN(RankerModelLoaderTest); 122 DISALLOW_COPY_AND_ASSIGN(RankerModelLoaderTest);
128 }; 123 };
129 124
130 RankerModelLoaderTest::RankerModelLoaderTest() 125 RankerModelLoaderTest::RankerModelLoaderTest()
131 : url_fetcher_factory_(nullptr) {} 126 : url_fetcher_factory_(nullptr) {}
132 127
133 void RankerModelLoaderTest::SetUp() { 128 void RankerModelLoaderTest::SetUp() {
134 // Setup the translate download manager. 129 request_context_getter_ =
135 locale_ = TranslateDownloadManager::GetInstance()->application_locale();
136 request_context_ =
137 new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get()); 130 new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get());
138 TranslateDownloadManager::GetInstance()->set_application_locale("fr-CA");
139 TranslateDownloadManager::GetInstance()->set_request_context(
140 request_context_.get());
141 131
142 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); 132 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
143 const auto& temp_dir_path = scoped_temp_dir_.GetPath(); 133 const auto& temp_dir_path = scoped_temp_dir_.GetPath();
144 134
145 // Setup the model file paths. 135 // Setup the model file paths.
146 local_model_path_ = temp_dir_path.AppendASCII("local_model.bin"); 136 local_model_path_ = temp_dir_path.AppendASCII("local_model.bin");
147 expired_model_path_ = temp_dir_path.AppendASCII("expired_model.bin"); 137 expired_model_path_ = temp_dir_path.AppendASCII("expired_model.bin");
148 invalid_model_path_ = temp_dir_path.AppendASCII("invalid_model.bin"); 138 invalid_model_path_ = temp_dir_path.AppendASCII("invalid_model.bin");
149 139
150 // Setup the model URLs. 140 // Setup the model URLs.
151 remote_model_url_ = GURL("https://some.url.net/good.model.bin"); 141 remote_model_url_ = GURL("https://some.url.net/good.model.bin");
152 invalid_model_url_ = GURL("https://some.url.net/bad.model.bin"); 142 invalid_model_url_ = GURL("https://some.url.net/bad.model.bin");
153 failed_model_url_ = GURL("https://some.url.net/fail"); 143 failed_model_url_ = GURL("https://some.url.net/fail");
154 144
155 // Initialize the model data. 145 // Initialize the model data.
156 ASSERT_NO_FATAL_FAILURE(InitRemoteModels()); 146 ASSERT_NO_FATAL_FAILURE(InitRemoteModels());
157 ASSERT_NO_FATAL_FAILURE(InitLocalModels()); 147 ASSERT_NO_FATAL_FAILURE(InitLocalModels());
158 } 148 }
159 149
160 void RankerModelLoaderTest::TearDown() { 150 void RankerModelLoaderTest::TearDown() {
161 base::RunLoop().RunUntilIdle(); 151 base::RunLoop().RunUntilIdle();
162 TranslateDownloadManager::GetInstance()->set_application_locale(locale_);
163 TranslateDownloadManager::GetInstance()->set_request_context(nullptr);
164 } 152 }
165 153
166 // static 154 // static
167 std::unique_ptr<RankerModel> RankerModelLoaderTest::Clone( 155 std::unique_ptr<RankerModel> RankerModelLoaderTest::Clone(
168 const RankerModel& model) { 156 const RankerModel& model) {
169 auto copy = base::MakeUnique<RankerModel>(); 157 auto copy = base::MakeUnique<RankerModel>();
170 *copy->mutable_proto() = model.proto(); 158 *copy->mutable_proto() = model.proto();
171 return copy; 159 return copy;
172 } 160 }
173 161
(...skipping 14 matching lines...) Expand all
188 176
189 return IsEqual(*copy_m1, *copy_m2); 177 return IsEqual(*copy_m1, *copy_m2);
190 } 178 }
191 179
192 bool RankerModelLoaderTest::DoLoaderTest(const base::FilePath& model_path, 180 bool RankerModelLoaderTest::DoLoaderTest(const base::FilePath& model_path,
193 const GURL& model_url) { 181 const GURL& model_url) {
194 auto loader = base::MakeUnique<RankerModelLoader>( 182 auto loader = base::MakeUnique<RankerModelLoader>(
195 base::Bind(&RankerModelLoaderTest::ValidateModel, base::Unretained(this)), 183 base::Bind(&RankerModelLoaderTest::ValidateModel, base::Unretained(this)),
196 base::Bind(&RankerModelLoaderTest::OnModelAvailable, 184 base::Bind(&RankerModelLoaderTest::OnModelAvailable,
197 base::Unretained(this)), 185 base::Unretained(this)),
198 model_path, model_url, "RankerModelLoaderTest"); 186 request_context_getter_.get(), model_path, model_url,
187 "RankerModelLoaderTest");
199 loader->NotifyOfRankerActivity(); 188 loader->NotifyOfRankerActivity();
200 base::RunLoop().RunUntilIdle(); 189 base::RunLoop().RunUntilIdle();
201 190
202 return true; 191 return true;
203 } 192 }
204 193
205 void RankerModelLoaderTest::InitRemoteModels() { 194 void RankerModelLoaderTest::InitRemoteModels() {
206 InitModel(remote_model_url_, base::Time(), base::TimeDelta(), &remote_model_); 195 InitModel(remote_model_url_, base::Time(), base::TimeDelta(), &remote_model_);
207 url_fetcher_factory_.SetFakeResponse( 196 url_fetcher_factory_.SetFakeResponse(
208 remote_model_url_, remote_model_.SerializeAsString(), net::HTTP_OK, 197 remote_model_url_, remote_model_.SerializeAsString(), net::HTTP_OK,
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 validate_model_response_.push_back(RankerModelStatus::OK); 350 validate_model_response_.push_back(RankerModelStatus::OK);
362 validate_model_response_.push_back(RankerModelStatus::INCOMPATIBLE); 351 validate_model_response_.push_back(RankerModelStatus::INCOMPATIBLE);
363 352
364 ASSERT_TRUE(DoLoaderTest(expired_model_path_, remote_model_url_)); 353 ASSERT_TRUE(DoLoaderTest(expired_model_path_, remote_model_url_));
365 ASSERT_EQ(2U, validated_models_.size()); 354 ASSERT_EQ(2U, validated_models_.size());
366 ASSERT_EQ(1U, available_models_.size()); 355 ASSERT_EQ(1U, available_models_.size());
367 EXPECT_TRUE(IsEquivalent(*validated_models_[0], local_model_)); 356 EXPECT_TRUE(IsEquivalent(*validated_models_[0], local_model_));
368 EXPECT_TRUE(IsEquivalent(*validated_models_[1], remote_model_)); 357 EXPECT_TRUE(IsEquivalent(*validated_models_[1], remote_model_));
369 EXPECT_TRUE(IsEquivalent(*available_models_[0], local_model_)); 358 EXPECT_TRUE(IsEquivalent(*available_models_[0], local_model_));
370 } 359 }
OLDNEW
« no previous file with comments | « components/machine_intelligence/ranker_model_loader.cc ('k') | components/machine_intelligence/ranker_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698