ProjectingObserverChromeos: Drop DBusThreadManager dependency for better testing.
[chromium-blink-merge.git] / components / translate / core / browser / translate_script_unittest.cc
blob8fb99b652b163616c11c8eb674fd2a7e30e29438
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
3 // found in the LICENSE file.
5 #include "components/translate/core/browser/translate_script.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/strings/stringprintf.h"
10 #include "components/translate/core/browser/translate_download_manager.h"
11 #include "components/translate/core/common/translate_switches.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/url_util.h"
14 #include "net/http/http_request_headers.h"
15 #include "net/url_request/test_url_fetcher_factory.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 namespace translate {
21 class TranslateScriptTest : public testing::Test {
22 public:
23 TranslateScriptTest() : testing::Test() {}
25 protected:
26 virtual void SetUp() {
27 script_.reset(new TranslateScript);
28 DCHECK(script_.get());
29 TranslateDownloadManager::GetInstance()->set_application_locale("en");
32 virtual void TearDown() {
33 script_.reset();
36 void Request() {
37 script_->Request(
38 base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this)));
41 net::TestURLFetcher* GetTestURLFetcher() {
42 return url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId);
45 private:
46 void OnComplete(bool success, const std::string& script) {
49 scoped_ptr<TranslateScript> script_;
50 net::TestURLFetcherFactory url_fetcher_factory_;
52 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest);
55 TEST_F(TranslateScriptTest, CheckScriptParameters) {
56 Request();
57 net::TestURLFetcher* fetcher = GetTestURLFetcher();
58 ASSERT_TRUE(fetcher);
60 GURL expected_url(TranslateScript::kScriptURL);
61 GURL url = fetcher->GetOriginalURL();
62 EXPECT_TRUE(url.is_valid());
63 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec());
64 EXPECT_EQ(expected_url.path(), url.path());
66 int load_flags = fetcher->GetLoadFlags();
67 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES,
68 load_flags & net::LOAD_DO_NOT_SEND_COOKIES);
69 EXPECT_EQ(net::LOAD_DO_NOT_SAVE_COOKIES,
70 load_flags & net::LOAD_DO_NOT_SAVE_COOKIES);
72 std::string expected_extra_headers =
73 base::StringPrintf("%s\r\n\r\n", TranslateScript::kRequestHeader);
74 net::HttpRequestHeaders extra_headers;
75 fetcher->GetExtraRequestHeaders(&extra_headers);
76 EXPECT_EQ(expected_extra_headers, extra_headers.ToString());
78 std::string always_use_ssl;
79 net::GetValueForKeyInQuery(
80 url, TranslateScript::kAlwaysUseSslQueryName, &always_use_ssl);
81 EXPECT_EQ(std::string(TranslateScript::kAlwaysUseSslQueryValue),
82 always_use_ssl);
84 std::string callback;
85 net::GetValueForKeyInQuery(
86 url, TranslateScript::kCallbackQueryName, &callback);
87 EXPECT_EQ(std::string(TranslateScript::kCallbackQueryValue), callback);
89 #if !defined(OS_IOS)
90 // iOS does not have specific loaders for the isolated world.
91 std::string css_loader_callback;
92 net::GetValueForKeyInQuery(
93 url, TranslateScript::kCssLoaderCallbackQueryName, &css_loader_callback);
94 EXPECT_EQ(std::string(TranslateScript::kCssLoaderCallbackQueryValue),
95 css_loader_callback);
97 std::string javascript_loader_callback;
98 net::GetValueForKeyInQuery(
99 url,
100 TranslateScript::kJavascriptLoaderCallbackQueryName,
101 &javascript_loader_callback);
102 EXPECT_EQ(std::string(TranslateScript::kJavascriptLoaderCallbackQueryValue),
103 javascript_loader_callback);
104 #endif // !defined(OS_IOS)
107 TEST_F(TranslateScriptTest, CheckScriptURL) {
108 const std::string script_url("http://www.tamurayukari.com/mero-n.js");
109 CommandLine* command_line = CommandLine::ForCurrentProcess();
110 command_line->AppendSwitchASCII(translate::switches::kTranslateScriptURL,
111 script_url);
113 Request();
114 net::TestURLFetcher* fetcher = GetTestURLFetcher();
115 ASSERT_TRUE(fetcher);
117 GURL expected_url(script_url);
118 GURL url = fetcher->GetOriginalURL();
119 EXPECT_TRUE(url.is_valid());
120 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec());
121 EXPECT_EQ(expected_url.path(), url.path());
124 } // namespace translate