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 // See IDS_ABOUT_TERMS_OF_SERVICE for the complete text.
29 const char kOfflineEULAWarning
[] = "Chrome OS Terms";
32 class TermsOfServiceProcessBrowserTest
: public InProcessBrowserTest
{
35 class TestURLFetcherCallback
{
37 scoped_ptr
<net::FakeURLFetcher
> CreateURLFetcher(
39 net::URLFetcherDelegate
* d
,
40 const std::string
& response_data
,
41 net::HttpStatusCode response_code
,
42 net::URLRequestStatus::Status status
) {
43 scoped_ptr
<net::FakeURLFetcher
> fetcher(
44 new net::FakeURLFetcher(url
, d
, response_data
, response_code
, status
));
45 OnRequestCreate(url
, fetcher
.get());
46 return fetcher
.Pass();
48 MOCK_METHOD2(OnRequestCreate
,
49 void(const GURL
&, net::FakeURLFetcher
*));
52 void AddMimeHeader(const GURL
& url
, net::FakeURLFetcher
* fetcher
) {
53 scoped_refptr
<net::HttpResponseHeaders
> download_headers
=
54 new net::HttpResponseHeaders("");
55 download_headers
->AddHeader("Content-Type: text/html");
56 fetcher
->set_response_headers(download_headers
);
59 // Load chrome://terms. Make sure online version is shown.
60 IN_PROC_BROWSER_TEST_F(TermsOfServiceProcessBrowserTest
, LoadOnline
) {
61 TestURLFetcherCallback url_callback
;
62 net::FakeURLFetcherFactory
factory(
64 base::Bind(&TestURLFetcherCallback::CreateURLFetcher
,
65 base::Unretained(&url_callback
)));
66 factory
.SetFakeResponse(GURL(kEULAURL
), kFakeOnlineEULA
,
67 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
68 EXPECT_CALL(url_callback
, OnRequestCreate(GURL(kEULAURL
), _
))
70 .WillRepeatedly(Invoke(AddMimeHeader
));
72 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUITermsURL
));
73 content::WebContents
* web_contents
=
74 browser()->tab_strip_model()->GetActiveWebContents();
75 EXPECT_EQ(1, ui_test_utils::FindInPage(web_contents
,
76 base::ASCIIToUTF16(kFakeOnlineEULA
),
83 // Load chrome://terms with no internet connectivity.
84 // Make sure offline version is shown.
85 IN_PROC_BROWSER_TEST_F(TermsOfServiceProcessBrowserTest
, LoadOffline
) {
86 net::FakeURLFetcherFactory
factory(NULL
);
87 factory
.SetFakeResponse(GURL(kEULAURL
), "", net::HTTP_INTERNAL_SERVER_ERROR
,
88 net::URLRequestStatus::FAILED
);
90 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUITermsURL
));
91 content::WebContents
* web_contents
=
92 browser()->tab_strip_model()->GetActiveWebContents();
94 #if defined(GOOGLE_CHROME_BUILD)
96 ui_test_utils::FindInPage(web_contents
,
97 base::ASCIIToUTF16(kOfflineEULAWarning
),
104 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
106 "window.domAutomationController.send(document.body.textContent)",
108 EXPECT_NE(std::string(), body
);