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 "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"
18 using ::testing::Exactly
;
19 using ::testing::Invoke
;
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";
31 class TermsOfServiceProcessBrowserTest
: public InProcessBrowserTest
{
34 class TestURLFetcherCallback
{
36 scoped_ptr
<net::FakeURLFetcher
> CreateURLFetcher(
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();
47 MOCK_METHOD2(OnRequestCreate
,
48 void(const GURL
&, net::FakeURLFetcher
*));
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
);
58 // Load chrome://terms. Make sure online version is shown.
59 IN_PROC_BROWSER_TEST_F(TermsOfServiceProcessBrowserTest
, LoadOnline
) {
60 TestURLFetcherCallback url_callback
;
61 net::FakeURLFetcherFactory
factory(
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
), _
))
69 .WillRepeatedly(Invoke(AddMimeHeader
));
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
),
82 // Load chrome://terms with no internet connectivity.
83 // Make sure offline version is shown.
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
);
89 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUITermsURL
));
90 content::WebContents
* web_contents
=
91 browser()->tab_strip_model()->GetActiveWebContents();
93 #if defined(GOOGLE_CHROME_BUILD)
95 ui_test_utils::FindInPage(web_contents
,
96 base::ASCIIToUTF16(kOfflineEULAWarning
),
103 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
105 "window.domAutomationController.send(document.body.textContent)",
107 EXPECT_NE(std::string(), body
);