1 // Copyright 2013 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 "chrome/browser/translate/translate_script.h"
8 #include "base/command_line.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "net/base/load_flags.h"
12 #include "net/base/url_util.h"
13 #include "net/http/http_request_headers.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 class TranslateScriptTest
: public testing::Test
{
20 TranslateScriptTest() : testing::Test() {}
23 virtual void SetUp() {
24 script_
.reset(new TranslateScript
);
25 DCHECK(script_
.get());
28 virtual void TearDown() {
34 base::Bind(&TranslateScriptTest::OnComplete
, base::Unretained(this)));
37 net::TestURLFetcher
* GetTestURLFetcher() {
38 return url_fetcher_factory_
.GetFetcherByID(TranslateScript::kFetcherId
);
42 void OnComplete(bool success
, const std::string
& script
) {
45 scoped_ptr
<TranslateScript
> script_
;
46 net::TestURLFetcherFactory url_fetcher_factory_
;
48 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest
);
51 TEST_F(TranslateScriptTest
, CheckScriptParameters
) {
53 net::TestURLFetcher
* fetcher
= GetTestURLFetcher();
56 GURL
expected_url(TranslateScript::kScriptURL
);
57 GURL url
= fetcher
->GetOriginalURL();
58 EXPECT_TRUE(url
.is_valid());
59 EXPECT_EQ(expected_url
.GetOrigin().spec(), url
.GetOrigin().spec());
60 EXPECT_EQ(expected_url
.path(), url
.path());
62 int load_flags
= fetcher
->GetLoadFlags();
63 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES
,
64 load_flags
& net::LOAD_DO_NOT_SEND_COOKIES
);
65 EXPECT_EQ(net::LOAD_DO_NOT_SAVE_COOKIES
,
66 load_flags
& net::LOAD_DO_NOT_SAVE_COOKIES
);
68 std::string expected_extra_headers
=
69 base::StringPrintf("%s\r\n\r\n", TranslateScript::kRequestHeader
);
70 net::HttpRequestHeaders extra_headers
;
71 fetcher
->GetExtraRequestHeaders(&extra_headers
);
72 EXPECT_EQ(expected_extra_headers
, extra_headers
.ToString());
74 std::string always_use_ssl
;
75 net::GetValueForKeyInQuery(
76 url
, TranslateScript::kAlwaysUseSslQueryName
, &always_use_ssl
);
77 EXPECT_EQ(std::string(TranslateScript::kAlwaysUseSslQueryValue
),
81 net::GetValueForKeyInQuery(
82 url
, TranslateScript::kCallbackQueryName
, &callback
);
83 EXPECT_EQ(std::string(TranslateScript::kCallbackQueryValue
), callback
);
85 std::string css_loader_callback
;
86 net::GetValueForKeyInQuery(
87 url
, TranslateScript::kCssLoaderCallbackQueryName
, &css_loader_callback
);
88 EXPECT_EQ(std::string(TranslateScript::kCssLoaderCallbackQueryValue
),
91 std::string javascript_loader_callback
;
92 net::GetValueForKeyInQuery(
94 TranslateScript::kJavascriptLoaderCallbackQueryName
,
95 &javascript_loader_callback
);
96 EXPECT_EQ(std::string(TranslateScript::kJavascriptLoaderCallbackQueryValue
),
97 javascript_loader_callback
);
100 TEST_F(TranslateScriptTest
, CheckScriptURL
) {
101 const std::string
script_url("http://www.tamurayukari.com/mero-n.js");
102 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
103 command_line
->AppendSwitchASCII(switches::kTranslateScriptURL
,
107 net::TestURLFetcher
* fetcher
= GetTestURLFetcher();
108 ASSERT_TRUE(fetcher
);
110 GURL
expected_url(script_url
);
111 GURL url
= fetcher
->GetOriginalURL();
112 EXPECT_TRUE(url
.is_valid());
113 EXPECT_EQ(expected_url
.GetOrigin().spec(), url
.GetOrigin().spec());
114 EXPECT_EQ(expected_url
.path(), url
.path());