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 void SetUp() override
{
27 script_
.reset(new TranslateScript
);
28 DCHECK(script_
.get());
29 TranslateDownloadManager::GetInstance()->set_application_locale("en");
32 void TearDown() override
{ script_
.reset(); }
36 base::Bind(&TranslateScriptTest::OnComplete
, base::Unretained(this)));
39 net::TestURLFetcher
* GetTestURLFetcher() {
40 return url_fetcher_factory_
.GetFetcherByID(TranslateScript::kFetcherId
);
44 void OnComplete(bool success
, const std::string
& script
) {
47 scoped_ptr
<TranslateScript
> script_
;
48 net::TestURLFetcherFactory url_fetcher_factory_
;
50 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest
);
53 TEST_F(TranslateScriptTest
, CheckScriptParameters
) {
55 net::TestURLFetcher
* fetcher
= GetTestURLFetcher();
58 GURL
expected_url(TranslateScript::kScriptURL
);
59 GURL url
= fetcher
->GetOriginalURL();
60 EXPECT_TRUE(url
.is_valid());
61 EXPECT_EQ(expected_url
.GetOrigin().spec(), url
.GetOrigin().spec());
62 EXPECT_EQ(expected_url
.path(), url
.path());
64 int load_flags
= fetcher
->GetLoadFlags();
65 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES
,
66 load_flags
& net::LOAD_DO_NOT_SEND_COOKIES
);
67 EXPECT_EQ(net::LOAD_DO_NOT_SAVE_COOKIES
,
68 load_flags
& net::LOAD_DO_NOT_SAVE_COOKIES
);
70 std::string expected_extra_headers
=
71 base::StringPrintf("%s\r\n\r\n", TranslateScript::kRequestHeader
);
72 net::HttpRequestHeaders extra_headers
;
73 fetcher
->GetExtraRequestHeaders(&extra_headers
);
74 EXPECT_EQ(expected_extra_headers
, extra_headers
.ToString());
76 std::string always_use_ssl
;
77 net::GetValueForKeyInQuery(
78 url
, TranslateScript::kAlwaysUseSslQueryName
, &always_use_ssl
);
79 EXPECT_EQ(std::string(TranslateScript::kAlwaysUseSslQueryValue
),
83 net::GetValueForKeyInQuery(
84 url
, TranslateScript::kCallbackQueryName
, &callback
);
85 EXPECT_EQ(std::string(TranslateScript::kCallbackQueryValue
), callback
);
88 // iOS does not have specific loaders for the isolated world.
89 std::string css_loader_callback
;
90 net::GetValueForKeyInQuery(
91 url
, TranslateScript::kCssLoaderCallbackQueryName
, &css_loader_callback
);
92 EXPECT_EQ(std::string(TranslateScript::kCssLoaderCallbackQueryValue
),
95 std::string javascript_loader_callback
;
96 net::GetValueForKeyInQuery(
98 TranslateScript::kJavascriptLoaderCallbackQueryName
,
99 &javascript_loader_callback
);
100 EXPECT_EQ(std::string(TranslateScript::kJavascriptLoaderCallbackQueryValue
),
101 javascript_loader_callback
);
102 #endif // !defined(OS_IOS)
105 TEST_F(TranslateScriptTest
, CheckScriptURL
) {
106 const std::string
script_url("http://www.tamurayukari.com/mero-n.js");
107 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
108 command_line
->AppendSwitchASCII(translate::switches::kTranslateScriptURL
,
112 net::TestURLFetcher
* fetcher
= GetTestURLFetcher();
113 ASSERT_TRUE(fetcher
);
115 GURL
expected_url(script_url
);
116 GURL url
= fetcher
->GetOriginalURL();
117 EXPECT_TRUE(url
.is_valid());
118 EXPECT_EQ(expected_url
.GetOrigin().spec(), url
.GetOrigin().spec());
119 EXPECT_EQ(expected_url
.path(), url
.path());
122 } // namespace translate