[email protected] | 6f2ac77 | 2014-07-24 22:22:08 | [diff] [blame] | 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 | |
oysteine | 2828c05 | 2014-12-19 22:25:58 | [diff] [blame] | 5 | #include "base/command_line.h" |
[email protected] | 6f2ac77 | 2014-07-24 22:22:08 | [diff] [blame] | 6 | #include "content/browser/transition_request_manager.h" |
oysteine | 2828c05 | 2014-12-19 22:25:58 | [diff] [blame] | 7 | #include "content/public/common/content_switches.h" |
| 8 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 6f2ac77 | 2014-07-24 22:22:08 | [diff] [blame] | 9 | #include "net/http/http_response_headers.h" |
| 10 | #include "net/http/http_util.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace content { |
| 14 | |
| 15 | class TransitionRequestManagerTest : public testing::Test { |
| 16 | public: |
oysteine | 2828c05 | 2014-12-19 22:25:58 | [diff] [blame] | 17 | TransitionRequestManagerTest() |
| 18 | : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 19 | base::CommandLine::ForCurrentProcess()->AppendSwitch( |
oysteine | 2828c05 | 2014-12-19 22:25:58 | [diff] [blame] | 20 | switches::kEnableExperimentalWebPlatformFeatures); |
| 21 | } |
| 22 | |
dcheng | fa85b15 | 2014-10-28 01:13:42 | [diff] [blame] | 23 | ~TransitionRequestManagerTest() override {} |
oysteine | 2828c05 | 2014-12-19 22:25:58 | [diff] [blame] | 24 | TestBrowserThreadBundle thread_bundle_; |
[email protected] | 6f2ac77 | 2014-07-24 22:22:08 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | TEST_F(TransitionRequestManagerTest, |
| 28 | ParseTransitionStylesheetsFromNullHeaders) { |
| 29 | const GURL url("http://www.test.com/"); |
| 30 | std::vector<GURL> entering_stylesheets; |
| 31 | scoped_refptr<net::HttpResponseHeaders> headers; |
| 32 | |
| 33 | TransitionRequestManager::ParseTransitionStylesheetsFromHeaders( |
| 34 | headers, entering_stylesheets, url); |
| 35 | ASSERT_TRUE(entering_stylesheets.empty()); |
| 36 | } |
| 37 | |
| 38 | TEST_F(TransitionRequestManagerTest, |
| 39 | ParseTransitionStylesheetsFromEmptyHeaders) { |
| 40 | const GURL url("http://www.test.com/"); |
| 41 | std::vector<GURL> entering_stylesheets; |
| 42 | |
| 43 | char headers_string[] = ""; |
| 44 | scoped_refptr<net::HttpResponseHeaders> headers( |
| 45 | new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 46 | headers_string, sizeof(headers_string)))); |
| 47 | |
| 48 | TransitionRequestManager::ParseTransitionStylesheetsFromHeaders( |
| 49 | headers, entering_stylesheets, url); |
| 50 | ASSERT_TRUE(entering_stylesheets.empty()); |
| 51 | } |
| 52 | |
| 53 | TEST_F(TransitionRequestManagerTest, |
| 54 | ParseTransitionStylesheetsFromHeadersForInvalidURL) { |
| 55 | const GURL url; |
| 56 | std::vector<GURL> entering_stylesheets; |
| 57 | |
| 58 | char headers_string[] = |
| 59 | "HTTP/1.0 200 OK\r\n" |
| 60 | "link: <transition.css>;rel=transition-entering-stylesheet;scope=*\r\n" |
| 61 | "\r\n"; |
| 62 | scoped_refptr<net::HttpResponseHeaders> headers( |
| 63 | new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 64 | headers_string, sizeof(headers_string)))); |
| 65 | |
| 66 | TransitionRequestManager::ParseTransitionStylesheetsFromHeaders( |
| 67 | headers, entering_stylesheets, url); |
| 68 | ASSERT_TRUE(entering_stylesheets.empty()); |
| 69 | } |
| 70 | |
| 71 | TEST_F(TransitionRequestManagerTest, ParseTransitionStylesheetsFromHeaders) { |
| 72 | const GURL url("http://www.test.com/"); |
| 73 | std::vector<GURL> entering_stylesheets; |
| 74 | |
| 75 | char headers_string[] = |
| 76 | "HTTP/1.0 200 OK\r\n" |
| 77 | "link: <transition.css>;rel=transition-entering-stylesheet;scope=*\r\n" |
| 78 | "\r\n"; |
| 79 | scoped_refptr<net::HttpResponseHeaders> headers( |
| 80 | new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 81 | headers_string, sizeof(headers_string)))); |
| 82 | |
| 83 | TransitionRequestManager::ParseTransitionStylesheetsFromHeaders( |
| 84 | headers, entering_stylesheets, url); |
| 85 | ASSERT_TRUE(entering_stylesheets.size() == 1); |
| 86 | ASSERT_STREQ((url.spec() + "transition.css").c_str(), |
| 87 | entering_stylesheets[0].spec().c_str()); |
| 88 | } |
| 89 | |
| 90 | TEST_F(TransitionRequestManagerTest, |
| 91 | ParseMultipleTransitionStylesheetsFromHeaders) { |
| 92 | const GURL url("http://www.test.com/"); |
| 93 | std::vector<GURL> entering_stylesheets; |
| 94 | |
| 95 | char headers_string[] = |
| 96 | "HTTP/1.0 200 OK\r\n" |
| 97 | "link: <transition0.css>;rel=transition-entering-stylesheet;scope=*\r\n" |
| 98 | "link: <transition1.css>;rel=transition-entering-stylesheet;scope=*\r\n" |
| 99 | "link: <transition2.css>;rel=transition-entering-stylesheet;scope=*\r\n" |
| 100 | "\r\n"; |
| 101 | scoped_refptr<net::HttpResponseHeaders> headers( |
| 102 | new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 103 | headers_string, sizeof(headers_string)))); |
| 104 | |
| 105 | TransitionRequestManager::ParseTransitionStylesheetsFromHeaders( |
| 106 | headers, entering_stylesheets, url); |
| 107 | ASSERT_TRUE(entering_stylesheets.size() == 3); |
| 108 | ASSERT_STREQ((url.spec() + "transition0.css").c_str(), |
| 109 | entering_stylesheets[0].spec().c_str()); |
| 110 | ASSERT_STREQ((url.spec() + "transition1.css").c_str(), |
| 111 | entering_stylesheets[1].spec().c_str()); |
| 112 | ASSERT_STREQ((url.spec() + "transition2.css").c_str(), |
| 113 | entering_stylesheets[2].spec().c_str()); |
| 114 | } |
| 115 | |
oysteine | 2828c05 | 2014-12-19 22:25:58 | [diff] [blame] | 116 | // Tests that the TransitionRequestManager correctly performs origin checks |
| 117 | // and fetches the correct transition data. |
| 118 | // |
| 119 | // We add data for http://www.foo.com and http://www.test.com URLs, then check |
| 120 | // that a navigation to test.com gives the latter data. A third URL should |
| 121 | // give no fallback data, before we add a fallback and check that it's now |
| 122 | // provided for the third URL. |
| 123 | TEST_F(TransitionRequestManagerTest, AllowedHostCheck) { |
| 124 | TransitionRequestManager* request_manager = |
| 125 | TransitionRequestManager::GetInstance(); |
| 126 | |
| 127 | const int render_process_id = 42; |
| 128 | const int render_frame_id = 4242; |
| 129 | |
| 130 | const std::string test_dot_com_css_selector("#correctTransitionElement"); |
| 131 | const std::string fallback_css_selector("#fallbackTransitionElement"); |
| 132 | const GURL test_dot_com_url("http://www.test.com"); |
| 133 | const std::string markup("<b>Hello World</b>"); |
| 134 | |
| 135 | // 1. Add transition data for http://www.foo.com URLs. |
| 136 | request_manager->AddPendingTransitionRequestData( |
| 137 | render_process_id, render_frame_id, "http://www.foo.com", |
| 138 | "#wrongTransition", markup, std::vector<TransitionElement>()); |
| 139 | // 2. Add transition data for http://www.test.com URLs |
| 140 | request_manager->AddPendingTransitionRequestData( |
| 141 | render_process_id, render_frame_id, test_dot_com_url.spec(), |
| 142 | test_dot_com_css_selector, markup, std::vector<TransitionElement>()); |
| 143 | |
| 144 | // 3. Check that a navigation to http://www.test.com finds the data from 2). |
| 145 | TransitionLayerData transition_layer_data; |
| 146 | bool found = request_manager->GetPendingTransitionRequest( |
| 147 | render_process_id, render_frame_id, test_dot_com_url, |
| 148 | &transition_layer_data); |
| 149 | ASSERT_TRUE(found); |
| 150 | EXPECT_TRUE(transition_layer_data.css_selector == test_dot_com_css_selector); |
| 151 | |
| 152 | // 4. Check that a navigation to http://www.unrelated.com finds no data. |
| 153 | const GURL unrelated_dot_com_url("http://www.unrelated.com"); |
| 154 | found = request_manager->GetPendingTransitionRequest( |
| 155 | render_process_id, render_frame_id, unrelated_dot_com_url, |
| 156 | &transition_layer_data); |
| 157 | EXPECT_FALSE(found); |
| 158 | |
| 159 | // 5. Add transition data for '*', i.e. matching any navigation. |
| 160 | request_manager->AddPendingTransitionRequestData( |
| 161 | render_process_id, render_frame_id, "*", fallback_css_selector, markup, |
| 162 | std::vector<TransitionElement>()); |
| 163 | |
| 164 | // 6. Check that a navigation to http://wwww.unrelated.com now finds an entry. |
| 165 | found = request_manager->GetPendingTransitionRequest( |
| 166 | render_process_id, render_frame_id, unrelated_dot_com_url, |
| 167 | &transition_layer_data); |
| 168 | ASSERT_TRUE(found); |
| 169 | EXPECT_TRUE(transition_layer_data.css_selector == fallback_css_selector); |
| 170 | } |
| 171 | |
[email protected] | 6f2ac77 | 2014-07-24 22:22:08 | [diff] [blame] | 172 | } // namespace content |