| OLD | NEW |
| 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/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/test/scoped_task_scheduler.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 12 #include "components/translate/core/browser/translate_download_manager.h" | 14 #include "components/translate/core/browser/translate_download_manager.h" |
| 13 #include "components/translate/core/common/translate_switches.h" | 15 #include "components/translate/core/common/translate_switches.h" |
| 14 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 15 #include "net/base/url_util.h" | 17 #include "net/base/url_util.h" |
| 16 #include "net/http/http_request_headers.h" | 18 #include "net/http/http_request_headers.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
| 20 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 20 | 23 |
| 21 namespace translate { | 24 namespace translate { |
| 22 | 25 |
| 23 class TranslateScriptTest : public testing::Test { | 26 class TranslateScriptTest : public testing::Test { |
| 24 public: | 27 public: |
| 25 TranslateScriptTest() : testing::Test() {} | 28 TranslateScriptTest() : testing::Test() {} |
| 26 | 29 |
| 27 protected: | 30 protected: |
| 28 void SetUp() override { | 31 void SetUp() override { |
| 29 script_.reset(new TranslateScript); | 32 script_.reset(new TranslateScript); |
| 30 DCHECK(script_.get()); | 33 DCHECK(script_.get()); |
| 31 TranslateDownloadManager::GetInstance()->set_application_locale("en"); | 34 TranslateDownloadManager::GetInstance()->set_application_locale("en"); |
| 35 TranslateDownloadManager::GetInstance()->set_request_context( |
| 36 new net::TestURLRequestContextGetter( |
| 37 base::ThreadTaskRunnerHandle::Get())); |
| 32 } | 38 } |
| 33 | 39 |
| 34 void TearDown() override { script_.reset(); } | 40 void TearDown() override { script_.reset(); } |
| 35 | 41 |
| 36 void Request() { | 42 void Request() { |
| 37 script_->Request( | 43 script_->Request( |
| 38 base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this))); | 44 base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this))); |
| 39 } | 45 } |
| 40 | 46 |
| 41 net::TestURLFetcher* GetTestURLFetcher() { | 47 net::TestURLFetcher* GetTestURLFetcher() { |
| 42 return url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); | 48 return url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); |
| 43 } | 49 } |
| 44 | 50 |
| 45 private: | 51 private: |
| 46 void OnComplete(bool success, const std::string& script) { | 52 void OnComplete(bool success, const std::string& script) { |
| 47 } | 53 } |
| 48 | 54 |
| 55 // Sets up the task scheduling/task-runner environment for each test. |
| 56 base::test::ScopedTaskScheduler scoped_task_scheduler_; |
| 57 |
| 58 // The translate script. |
| 49 std::unique_ptr<TranslateScript> script_; | 59 std::unique_ptr<TranslateScript> script_; |
| 60 |
| 61 // Factory to create programmatic URL fetchers. |
| 50 net::TestURLFetcherFactory url_fetcher_factory_; | 62 net::TestURLFetcherFactory url_fetcher_factory_; |
| 51 | 63 |
| 52 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest); | 64 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest); |
| 53 }; | 65 }; |
| 54 | 66 |
| 55 TEST_F(TranslateScriptTest, CheckScriptParameters) { | 67 TEST_F(TranslateScriptTest, CheckScriptParameters) { |
| 56 Request(); | 68 Request(); |
| 57 net::TestURLFetcher* fetcher = GetTestURLFetcher(); | 69 net::TestURLFetcher* fetcher = GetTestURLFetcher(); |
| 58 ASSERT_TRUE(fetcher); | 70 ASSERT_TRUE(fetcher); |
| 59 | 71 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 ASSERT_TRUE(fetcher); | 127 ASSERT_TRUE(fetcher); |
| 116 | 128 |
| 117 GURL expected_url(script_url); | 129 GURL expected_url(script_url); |
| 118 GURL url = fetcher->GetOriginalURL(); | 130 GURL url = fetcher->GetOriginalURL(); |
| 119 EXPECT_TRUE(url.is_valid()); | 131 EXPECT_TRUE(url.is_valid()); |
| 120 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec()); | 132 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec()); |
| 121 EXPECT_EQ(expected_url.path(), url.path()); | 133 EXPECT_EQ(expected_url.path(), url.path()); |
| 122 } | 134 } |
| 123 | 135 |
| 124 } // namespace translate | 136 } // namespace translate |
| OLD | NEW |