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

Side by Side Diff: components/translate/core/browser/translate_script_unittest.cc

Issue 2839433002: [translate] Fix shutdown race for translate ranker model loader. (Closed)
Patch Set: experiment: delay model load to first translate activity Created 3 years, 8 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_script.h" 5 #include "components/translate/core/browser/translate_script.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
groby-ooo-7-16 2017/04/24 19:12:46 Why ref_counted.h?
Roger McFarlane (Chromium) 2017/04/24 20:31:34 I had originally added a scoped_refptr<net::URLReq
10 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/test/scoped_task_scheduler.h"
13 #include "base/threading/thread_task_runner_handle.h"
11 #include "build/build_config.h" 14 #include "build/build_config.h"
12 #include "components/translate/core/browser/translate_download_manager.h" 15 #include "components/translate/core/browser/translate_download_manager.h"
13 #include "components/translate/core/common/translate_switches.h" 16 #include "components/translate/core/common/translate_switches.h"
14 #include "net/base/load_flags.h" 17 #include "net/base/load_flags.h"
15 #include "net/base/url_util.h" 18 #include "net/base/url_util.h"
16 #include "net/http/http_request_headers.h" 19 #include "net/http/http_request_headers.h"
17 #include "net/url_request/test_url_fetcher_factory.h" 20 #include "net/url_request/test_url_fetcher_factory.h"
21 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h" 23 #include "url/gurl.h"
20 24
21 namespace translate { 25 namespace translate {
22 26
23 class TranslateScriptTest : public testing::Test { 27 class TranslateScriptTest : public testing::Test {
24 public: 28 public:
25 TranslateScriptTest() : testing::Test() {} 29 TranslateScriptTest() : testing::Test() {}
26 30
27 protected: 31 protected:
28 void SetUp() override { 32 void SetUp() override {
29 script_.reset(new TranslateScript); 33 script_.reset(new TranslateScript);
30 DCHECK(script_.get()); 34 DCHECK(script_.get());
31 TranslateDownloadManager::GetInstance()->set_application_locale("en"); 35 TranslateDownloadManager::GetInstance()->set_application_locale("en");
36 TranslateDownloadManager::GetInstance()->set_request_context(
37 new net::TestURLRequestContextGetter(
38 base::ThreadTaskRunnerHandle::Get()));
32 } 39 }
33 40
34 void TearDown() override { script_.reset(); } 41 void TearDown() override { script_.reset(); }
35 42
36 void Request() { 43 void Request() {
37 script_->Request( 44 script_->Request(
38 base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this))); 45 base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this)));
39 } 46 }
40 47
41 net::TestURLFetcher* GetTestURLFetcher() { 48 net::TestURLFetcher* GetTestURLFetcher() {
42 return url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); 49 return url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId);
43 } 50 }
44 51
45 private: 52 private:
46 void OnComplete(bool success, const std::string& script) { 53 void OnComplete(bool success, const std::string& script) {
47 } 54 }
48 55
56 // Sets up the task scheduling/task-runner environment for each test.
57 base::test::ScopedTaskScheduler scoped_task_scheduler_;
58
59 // The translate script.
49 std::unique_ptr<TranslateScript> script_; 60 std::unique_ptr<TranslateScript> script_;
61
62 // Factory to create programmatic URL fetchers.
50 net::TestURLFetcherFactory url_fetcher_factory_; 63 net::TestURLFetcherFactory url_fetcher_factory_;
51 64
52 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest); 65 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest);
53 }; 66 };
54 67
55 TEST_F(TranslateScriptTest, CheckScriptParameters) { 68 TEST_F(TranslateScriptTest, CheckScriptParameters) {
56 Request(); 69 Request();
57 net::TestURLFetcher* fetcher = GetTestURLFetcher(); 70 net::TestURLFetcher* fetcher = GetTestURLFetcher();
58 ASSERT_TRUE(fetcher); 71 ASSERT_TRUE(fetcher);
59 72
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 ASSERT_TRUE(fetcher); 128 ASSERT_TRUE(fetcher);
116 129
117 GURL expected_url(script_url); 130 GURL expected_url(script_url);
118 GURL url = fetcher->GetOriginalURL(); 131 GURL url = fetcher->GetOriginalURL();
119 EXPECT_TRUE(url.is_valid()); 132 EXPECT_TRUE(url.is_valid());
120 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec()); 133 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec());
121 EXPECT_EQ(expected_url.path(), url.path()); 134 EXPECT_EQ(expected_url.path(), url.path());
122 } 135 }
123 136
124 } // namespace translate 137 } // namespace translate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698