summaryrefslogtreecommitdiffstats
path: root/content/browser/transition_request_manager_unittest.cc
blob: 02978b7e43a2d65d35c2585176c649a1dcc1d365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "content/browser/transition_request_manager.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

class TransitionRequestManagerTest : public testing::Test {
 public:
  TransitionRequestManagerTest()
      : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        switches::kEnableExperimentalWebPlatformFeatures);
  }

  ~TransitionRequestManagerTest() override {}
  TestBrowserThreadBundle thread_bundle_;
};

TEST_F(TransitionRequestManagerTest,
       ParseTransitionStylesheetsFromNullHeaders) {
  const GURL url("http://www.test.com/");
  std::vector<GURL> entering_stylesheets;
  scoped_refptr<net::HttpResponseHeaders> headers;

  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
      headers, entering_stylesheets, url);
  ASSERT_TRUE(entering_stylesheets.empty());
}

TEST_F(TransitionRequestManagerTest,
       ParseTransitionStylesheetsFromEmptyHeaders) {
  const GURL url("http://www.test.com/");
  std::vector<GURL> entering_stylesheets;

  char headers_string[] = "";
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
          headers_string, sizeof(headers_string))));

  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
      headers, entering_stylesheets, url);
  ASSERT_TRUE(entering_stylesheets.empty());
}

TEST_F(TransitionRequestManagerTest,
       ParseTransitionStylesheetsFromHeadersForInvalidURL) {
  const GURL url;
  std::vector<GURL> entering_stylesheets;

  char headers_string[] =
      "HTTP/1.0 200 OK\r\n"
      "link: <transition.css>;rel=transition-entering-stylesheet;scope=*\r\n"
      "\r\n";
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
          headers_string, sizeof(headers_string))));

  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
      headers, entering_stylesheets, url);
  ASSERT_TRUE(entering_stylesheets.empty());
}

TEST_F(TransitionRequestManagerTest, ParseTransitionStylesheetsFromHeaders) {
  const GURL url("http://www.test.com/");
  std::vector<GURL> entering_stylesheets;

  char headers_string[] =
      "HTTP/1.0 200 OK\r\n"
      "link: <transition.css>;rel=transition-entering-stylesheet;scope=*\r\n"
      "\r\n";
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
          headers_string, sizeof(headers_string))));

  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
      headers, entering_stylesheets, url);
  ASSERT_TRUE(entering_stylesheets.size() == 1);
  ASSERT_STREQ((url.spec() + "transition.css").c_str(),
               entering_stylesheets[0].spec().c_str());
}

TEST_F(TransitionRequestManagerTest,
       ParseMultipleTransitionStylesheetsFromHeaders) {
  const GURL url("http://www.test.com/");
  std::vector<GURL> entering_stylesheets;

  char headers_string[] =
      "HTTP/1.0 200 OK\r\n"
      "link: <transition0.css>;rel=transition-entering-stylesheet;scope=*\r\n"
      "link: <transition1.css>;rel=transition-entering-stylesheet;scope=*\r\n"
      "link: <transition2.css>;rel=transition-entering-stylesheet;scope=*\r\n"
      "\r\n";
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
          headers_string, sizeof(headers_string))));

  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
      headers, entering_stylesheets, url);
  ASSERT_TRUE(entering_stylesheets.size() == 3);
  ASSERT_STREQ((url.spec() + "transition0.css").c_str(),
               entering_stylesheets[0].spec().c_str());
  ASSERT_STREQ((url.spec() + "transition1.css").c_str(),
               entering_stylesheets[1].spec().c_str());
  ASSERT_STREQ((url.spec() + "transition2.css").c_str(),
               entering_stylesheets[2].spec().c_str());
}

// Tests that the TransitionRequestManager correctly performs origin checks
// and fetches the correct transition data.
//
// We add data for http://www.foo.com and http://www.test.com URLs, then check
// that a navigation to test.com gives the latter data. A third URL should
// give no fallback data, before we add a fallback and check that it's now
// provided for the third URL.
TEST_F(TransitionRequestManagerTest, AllowedHostCheck) {
  TransitionRequestManager* request_manager =
      TransitionRequestManager::GetInstance();

  const int render_process_id = 42;
  const int render_frame_id = 4242;

  const std::string test_dot_com_css_selector("#correctTransitionElement");
  const std::string fallback_css_selector("#fallbackTransitionElement");
  const GURL test_dot_com_url("http://www.test.com");
  const std::string markup("<b>Hello World</b>");

  // 1. Add transition data for http://www.foo.com URLs.
  request_manager->AddPendingTransitionRequestData(
      render_process_id, render_frame_id, "http://www.foo.com",
      "#wrongTransition", markup, std::vector<TransitionElement>());
  // 2. Add transition data for http://www.test.com URLs
  request_manager->AddPendingTransitionRequestData(
      render_process_id, render_frame_id, test_dot_com_url.spec(),
      test_dot_com_css_selector, markup, std::vector<TransitionElement>());

  // 3. Check that a navigation to http://www.test.com finds the data from 2).
  TransitionLayerData transition_layer_data;
  bool found = request_manager->GetPendingTransitionRequest(
      render_process_id, render_frame_id, test_dot_com_url,
      &transition_layer_data);
  ASSERT_TRUE(found);
  EXPECT_TRUE(transition_layer_data.css_selector == test_dot_com_css_selector);

  // 4. Check that a navigation to http://www.unrelated.com finds no data.
  const GURL unrelated_dot_com_url("http://www.unrelated.com");
  found = request_manager->GetPendingTransitionRequest(
      render_process_id, render_frame_id, unrelated_dot_com_url,
      &transition_layer_data);
  EXPECT_FALSE(found);

  // 5. Add transition data for '*', i.e. matching any navigation.
  request_manager->AddPendingTransitionRequestData(
      render_process_id, render_frame_id, "*", fallback_css_selector, markup,
      std::vector<TransitionElement>());

  // 6. Check that a navigation to http://wwww.unrelated.com now finds an entry.
  found = request_manager->GetPendingTransitionRequest(
      render_process_id, render_frame_id, unrelated_dot_com_url,
      &transition_layer_data);
  ASSERT_TRUE(found);
  EXPECT_TRUE(transition_layer_data.css_selector == fallback_css_selector);
}

}  // namespace content