1 // Copyright (c) 2012 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 "chrome_frame/test/mock_ie_event_sink_actions.h"
6 #include "chrome_frame/test/mock_ie_event_sink_test.h"
7 #include "base/rand_util.h"
9 namespace chrome_frame_test
{
13 TestData(const std::string
& value
, bool in_header
, LoadedInRenderer expected
)
15 in_header_(in_header
),
17 name_(base::IntToString(base::RandInt(0, 1000))) {
20 LoadedInRenderer
GetExpectedRenderer() const {
24 std::wstring
GetPath() const {
25 return ASCIIToWide("/" + name_
);
28 std::wstring
GetUrl(MockWebServer
* server_mock
) const {
29 return server_mock
->Resolve(ASCIIToWide(name_
));
32 void ExpectOnServer(MockWebServer
* server_mock
) const {
33 EXPECT_CALL(*server_mock
, Get(testing::_
, GetPath(), testing::_
))
34 .Times(testing::AnyNumber())
35 .WillRepeatedly(SendFast(GetHeaders(), GetHtml()));
38 std::string
GetHeaders() const {
39 std::ostringstream headers
;
40 headers
<< "HTTP/1.1 200 OK\r\n"
41 << "Connection: close\r\n"
42 << "Content-Type: text/html\r\n"
43 << "Cache-Control: no-cache\r\n";
45 headers
<< "X-UA-COMPATIBLE: " << value_
<< "\r\n";
50 std::string
GetHtml() const {
51 std::ostringstream html
;
52 html
<< "<html><head>";
54 html
<< "<meta http-equiv=\"x-ua-compatible\" content=\"" << value_
57 html
<< "</head><body>This is some text.</body></html>";
62 LoadedInRenderer expected_
;
68 // Determines the major version of the installed IE
69 // Returns -1 in case of failure, 0 if the version is newer than currently known
70 int GetIEMajorVersion() {
71 switch (GetInstalledIEVersion()) {
85 ADD_FAILURE() << "Failed to detect IE version.";
93 int ie_version
= GetIEMajorVersion();
99 return ie_version
- 1;
104 int ie_version
= GetIEMajorVersion();
105 switch (ie_version
) {
110 return ie_version
+ 1;
115 int ie_version
= GetIEMajorVersion();
116 switch (ie_version
) {
125 std::string
HeaderValue(int ie_version
) {
126 if (ie_version
== -1) {
127 return "IE=8; Chrome=1";
129 return std::string("IE=8; Chrome=IE") + base::IntToString(ie_version
);
133 std::string
CorruptHeaderValue(int ie_version
) {
134 return HeaderValue(ie_version
) + ".0";
137 std::string
LongHeaderValue(int ie_version
) {
138 std::string long_value
= HeaderValue(ie_version
) + "; " +
139 std::string(256, 'a') + "=bbb";
140 DCHECK_GT(long_value
.length(), 256u);
141 DCHECK_LT(long_value
.length(), 512u);
146 : public MockIEEventSinkTest
,
147 public testing::TestWithParam
<TestData
> {
152 INSTANTIATE_TEST_CASE_P(MetaTag
, HeaderTest
, testing::Values(
153 TestData(HeaderValue(LowVersion()), false, IN_IE
),
154 TestData(HeaderValue(EqualVersion()), false, IN_CF
),
155 TestData(HeaderValue(HighVersion()), false, IN_CF
),
156 TestData(LongHeaderValue(LowVersion()), false, IN_IE
),
157 TestData(LongHeaderValue(EqualVersion()), false, IN_CF
),
158 TestData(LongHeaderValue(HighVersion()), false, IN_CF
)));
159 INSTANTIATE_TEST_CASE_P(HttpHeader
, HeaderTest
, testing::Values(
160 TestData(HeaderValue(LowVersion()), true, IN_IE
),
161 TestData(HeaderValue(EqualVersion()), true, IN_CF
),
162 TestData(HeaderValue(HighVersion()), true, IN_CF
),
163 TestData(LongHeaderValue(LowVersion()), true, IN_IE
),
164 TestData(LongHeaderValue(EqualVersion()), true, IN_CF
),
165 TestData(LongHeaderValue(HighVersion()), true, IN_CF
)));
166 INSTANTIATE_TEST_CASE_P(CorruptValueHeader
, HeaderTest
, testing::Values(
167 TestData(CorruptHeaderValue(LowVersion()), true, IN_IE
),
168 TestData(CorruptHeaderValue(EqualVersion()), true, IN_IE
),
169 TestData(CorruptHeaderValue(HighVersion()), true, IN_IE
),
170 TestData(CorruptHeaderValue(LowVersion()), false, IN_IE
),
171 TestData(CorruptHeaderValue(EqualVersion()), false, IN_IE
),
172 TestData(CorruptHeaderValue(HighVersion()), false, IN_IE
)));
174 TEST_P(HeaderTest
, HandleXUaCompatibleHeader
) {
175 std::wstring url
= GetParam().GetUrl(&server_mock_
);
176 LoadedInRenderer expected_renderer
= GetParam().GetExpectedRenderer();
178 GetParam().ExpectOnServer(&server_mock_
);
179 ie_mock_
.ExpectNavigation(expected_renderer
, url
);
181 EXPECT_CALL(ie_mock_
, OnLoad(expected_renderer
, testing::StrEq(url
)))
182 .WillOnce(CloseBrowserMock(&ie_mock_
));
184 LaunchIEAndNavigate(url
);
187 } // namespace chrome_frame_test