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.
4
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/tabs/tab_strip_model.h"
8 #include "chrome/common/url_constants.h"
9 #include "chrome/test/base/in_process_browser_test.h"
10 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_status_code.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_request_status.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17
18 using ::testing::Exactly;
19 using ::testing::Invoke;
20 using ::testing::_;
21
22 namespace {
23
24 const char kEULAURL[] =
25 "https://www.google.com/intl/en-US/chrome/eula_text.html";
26 const char kFakeOnlineEULA[] = "No obligations at all";
27 #if defined(GOOGLE_CHROME_BUILD)
28 const char kOfflineEULAWarning[] = "A copy of the Google Terms of Service";
29 #endif
30
31 class TermsOfServiceProcessBrowserTest : public InProcessBrowserTest {
32 };
33
34 class TestURLFetcherCallback {
35 public:
CreateURLFetcher(const GURL & url,net::URLFetcherDelegate * d,const std::string & response_data,net::HttpStatusCode response_code,net::URLRequestStatus::Status status)36 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
37 const GURL& url,
38 net::URLFetcherDelegate* d,
39 const std::string& response_data,
40 net::HttpStatusCode response_code,
41 net::URLRequestStatus::Status status) {
42 scoped_ptr<net::FakeURLFetcher> fetcher(
43 new net::FakeURLFetcher(url, d, response_data, response_code, status));
44 OnRequestCreate(url, fetcher.get());
45 return fetcher.Pass();
46 }
47 MOCK_METHOD2(OnRequestCreate,
48 void(const GURL&, net::FakeURLFetcher*));
49 };
50
AddMimeHeader(const GURL & url,net::FakeURLFetcher * fetcher)51 void AddMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher) {
52 scoped_refptr<net::HttpResponseHeaders> download_headers =
53 new net::HttpResponseHeaders("");
54 download_headers->AddHeader("Content-Type: text/html");
55 fetcher->set_response_headers(download_headers);
56 }
57
58 // Load chrome://terms. Make sure online version is shown.
IN_PROC_BROWSER_TEST_F(TermsOfServiceProcessBrowserTest,LoadOnline)59 IN_PROC_BROWSER_TEST_F(TermsOfServiceProcessBrowserTest, LoadOnline) {
60 TestURLFetcherCallback url_callback;
61 net::FakeURLFetcherFactory factory(
62 NULL,
63 base::Bind(&TestURLFetcherCallback::CreateURLFetcher,
64 base::Unretained(&url_callback)));
65 factory.SetFakeResponse(GURL(kEULAURL), kFakeOnlineEULA,
66 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
67 EXPECT_CALL(url_callback, OnRequestCreate(GURL(kEULAURL), _))
68 .Times(Exactly(1))
69 .WillRepeatedly(Invoke(AddMimeHeader));
70
71 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUITermsURL));
72 content::WebContents* web_contents =
73 browser()->tab_strip_model()->GetActiveWebContents();
74 EXPECT_EQ(1, ui_test_utils::FindInPage(web_contents,
75 base::ASCIIToUTF16(kFakeOnlineEULA),
76 true,
77 true,
78 NULL,
79 NULL));
80 }
81
82 // Load chrome://terms with no internet connectivity.
83 // Make sure offline version is shown.
IN_PROC_BROWSER_TEST_F(TermsOfServiceProcessBrowserTest,LoadOffline)84 IN_PROC_BROWSER_TEST_F(TermsOfServiceProcessBrowserTest, LoadOffline) {
85 net::FakeURLFetcherFactory factory(NULL);
86 factory.SetFakeResponse(GURL(kEULAURL), "", net::HTTP_INTERNAL_SERVER_ERROR,
87 net::URLRequestStatus::FAILED);
88
89 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUITermsURL));
90 content::WebContents* web_contents =
91 browser()->tab_strip_model()->GetActiveWebContents();
92
93 #if defined(GOOGLE_CHROME_BUILD)
94 EXPECT_NE(0,
95 ui_test_utils::FindInPage(web_contents,
96 base::ASCIIToUTF16(kOfflineEULAWarning),
97 true,
98 true,
99 NULL,
100 NULL));
101 #else
102 std::string body;
103 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
104 web_contents,
105 "window.domAutomationController.send(document.body.textContent)",
106 &body));
107 EXPECT_NE(std::string(), body);
108 #endif
109 }
110
111 } // namespace
112