Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / transition_request_manager_unittest.cc
blob02978b7e43a2d65d35c2585176c649a1dcc1d365
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.
5 #include "base/command_line.h"
6 #include "content/browser/transition_request_manager.h"
7 #include "content/public/common/content_switches.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "net/http/http_response_headers.h"
10 #include "net/http/http_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace content {
15 class TransitionRequestManagerTest : public testing::Test {
16 public:
17 TransitionRequestManagerTest()
18 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {
19 base::CommandLine::ForCurrentProcess()->AppendSwitch(
20 switches::kEnableExperimentalWebPlatformFeatures);
23 ~TransitionRequestManagerTest() override {}
24 TestBrowserThreadBundle thread_bundle_;
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;
33 TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
34 headers, entering_stylesheets, url);
35 ASSERT_TRUE(entering_stylesheets.empty());
38 TEST_F(TransitionRequestManagerTest,
39 ParseTransitionStylesheetsFromEmptyHeaders) {
40 const GURL url("http://www.test.com/");
41 std::vector<GURL> entering_stylesheets;
43 char headers_string[] = "";
44 scoped_refptr<net::HttpResponseHeaders> headers(
45 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
46 headers_string, sizeof(headers_string))));
48 TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
49 headers, entering_stylesheets, url);
50 ASSERT_TRUE(entering_stylesheets.empty());
53 TEST_F(TransitionRequestManagerTest,
54 ParseTransitionStylesheetsFromHeadersForInvalidURL) {
55 const GURL url;
56 std::vector<GURL> entering_stylesheets;
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))));
66 TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
67 headers, entering_stylesheets, url);
68 ASSERT_TRUE(entering_stylesheets.empty());
71 TEST_F(TransitionRequestManagerTest, ParseTransitionStylesheetsFromHeaders) {
72 const GURL url("http://www.test.com/");
73 std::vector<GURL> entering_stylesheets;
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))));
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());
90 TEST_F(TransitionRequestManagerTest,
91 ParseMultipleTransitionStylesheetsFromHeaders) {
92 const GURL url("http://www.test.com/");
93 std::vector<GURL> entering_stylesheets;
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))));
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());
116 // Tests that the TransitionRequestManager correctly performs origin checks
117 // and fetches the correct transition data.
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();
127 const int render_process_id = 42;
128 const int render_frame_id = 4242;
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>");
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>());
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);
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);
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>());
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);
172 } // namespace content