• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "components/translate/core/browser/translate_script.h"
6 
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"
18 
19 namespace translate {
20 
21 class TranslateScriptTest : public testing::Test {
22  public:
TranslateScriptTest()23   TranslateScriptTest() : testing::Test() {}
24 
25  protected:
SetUp()26   virtual void SetUp() {
27     script_.reset(new TranslateScript);
28     DCHECK(script_.get());
29     TranslateDownloadManager::GetInstance()->set_application_locale("en");
30   }
31 
TearDown()32   virtual void TearDown() {
33     script_.reset();
34   }
35 
Request()36   void Request() {
37     script_->Request(
38         base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this)));
39   }
40 
GetTestURLFetcher()41   net::TestURLFetcher* GetTestURLFetcher() {
42     return url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId);
43   }
44 
45  private:
OnComplete(bool success,const std::string & script)46   void OnComplete(bool success, const std::string& script) {
47   }
48 
49   scoped_ptr<TranslateScript> script_;
50   net::TestURLFetcherFactory url_fetcher_factory_;
51 
52   DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest);
53 };
54 
TEST_F(TranslateScriptTest,CheckScriptParameters)55 TEST_F(TranslateScriptTest, CheckScriptParameters) {
56   Request();
57   net::TestURLFetcher* fetcher = GetTestURLFetcher();
58   ASSERT_TRUE(fetcher);
59 
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());
65 
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);
71 
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());
77 
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);
83 
84   std::string callback;
85   net::GetValueForKeyInQuery(
86       url, TranslateScript::kCallbackQueryName, &callback);
87   EXPECT_EQ(std::string(TranslateScript::kCallbackQueryValue), callback);
88 
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);
96 
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)
105 }
106 
TEST_F(TranslateScriptTest,CheckScriptURL)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);
112 
113   Request();
114   net::TestURLFetcher* fetcher = GetTestURLFetcher();
115   ASSERT_TRUE(fetcher);
116 
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());
122 }
123 
124 }  // namespace translate
125