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"
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"
21 class TranslateScriptTest
: public testing::Test
{
23 TranslateScriptTest() : testing::Test() {}
26 virtual void SetUp() {
27 script_
.reset(new TranslateScript
);
28 DCHECK(script_
.get());
29 TranslateDownloadManager::GetInstance()->set_application_locale("en");
32 virtual void TearDown() {
38 base::Bind(&TranslateScriptTest::OnComplete
, base::Unretained(this)));
41 net::TestURLFetcher
* GetTestURLFetcher() {
42 return url_fetcher_factory_
.GetFetcherByID(TranslateScript::kFetcherId
);
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
) {
57 net::TestURLFetcher
* fetcher
= GetTestURLFetcher();
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
),
85 net::GetValueForKeyInQuery(
86 url
, TranslateScript::kCallbackQueryName
, &callback
);
87 EXPECT_EQ(std::string(TranslateScript::kCallbackQueryValue
), callback
);
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
),
97 std::string javascript_loader_callback
;
98 net::GetValueForKeyInQuery(
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
,
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