2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2013-09-10 21:42:53 +02:00
|
|
|
#include <map>
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_request.h"
|
2013-09-10 21:42:53 +02:00
|
|
|
#include "include/cef_runnable.h"
|
2013-11-21 21:59:28 +01:00
|
|
|
#include "tests/cefclient/client_app.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "tests/unittests/test_handler.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
#include "tests/unittests/test_util.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
// Verify Set/Get methods for CefRequest, CefPostData and CefPostDataElement.
|
|
|
|
TEST(RequestTest, SetGet) {
|
|
|
|
// CefRequest CreateRequest
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefRequest> request(CefRequest::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(request.get() != NULL);
|
|
|
|
|
|
|
|
CefString url = "http://tests/run.html";
|
|
|
|
CefString method = "POST";
|
|
|
|
CefRequest::HeaderMap setHeaders, getHeaders;
|
|
|
|
setHeaders.insert(std::make_pair("HeaderA", "ValueA"));
|
|
|
|
setHeaders.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
|
|
|
|
// CefPostData CreatePostData
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostData> postData(CefPostData::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(postData.get() != NULL);
|
|
|
|
|
|
|
|
// CefPostDataElement CreatePostDataElement
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostDataElement> element1(CefPostDataElement::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(element1.get() != NULL);
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostDataElement> element2(CefPostDataElement::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(element2.get() != NULL);
|
|
|
|
|
|
|
|
// CefPostDataElement SetToFile
|
|
|
|
CefString file = "c:\\path\\to\\file.ext";
|
|
|
|
element1->SetToFile(file);
|
|
|
|
ASSERT_EQ(PDE_TYPE_FILE, element1->GetType());
|
|
|
|
ASSERT_EQ(file, element1->GetFile());
|
|
|
|
|
|
|
|
// CefPostDataElement SetToBytes
|
|
|
|
char bytes[] = "Test Bytes";
|
|
|
|
element2->SetToBytes(sizeof(bytes), bytes);
|
|
|
|
ASSERT_EQ(PDE_TYPE_BYTES, element2->GetType());
|
|
|
|
ASSERT_EQ(sizeof(bytes), element2->GetBytesCount());
|
|
|
|
char bytesOut[sizeof(bytes)];
|
|
|
|
element2->GetBytes(sizeof(bytes), bytesOut);
|
|
|
|
ASSERT_TRUE(!memcmp(bytes, bytesOut, sizeof(bytes)));
|
|
|
|
|
|
|
|
// CefPostData AddElement
|
|
|
|
postData->AddElement(element1);
|
|
|
|
postData->AddElement(element2);
|
|
|
|
ASSERT_EQ((size_t)2, postData->GetElementCount());
|
|
|
|
|
|
|
|
// CefPostData RemoveElement
|
|
|
|
postData->RemoveElement(element1);
|
|
|
|
ASSERT_EQ((size_t)1, postData->GetElementCount());
|
|
|
|
|
|
|
|
// CefPostData RemoveElements
|
|
|
|
postData->RemoveElements();
|
|
|
|
ASSERT_EQ((size_t)0, postData->GetElementCount());
|
|
|
|
|
|
|
|
postData->AddElement(element1);
|
|
|
|
postData->AddElement(element2);
|
|
|
|
ASSERT_EQ((size_t)2, postData->GetElementCount());
|
|
|
|
CefPostData::ElementVector elements;
|
|
|
|
postData->GetElements(elements);
|
|
|
|
CefPostData::ElementVector::const_iterator it = elements.begin();
|
|
|
|
for (size_t i = 0; it != elements.end(); ++it, ++i) {
|
|
|
|
if (i == 0)
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataElementEqual(element1, (*it).get());
|
2012-04-03 03:34:16 +02:00
|
|
|
else if (i == 1)
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataElementEqual(element2, (*it).get());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CefRequest SetURL
|
|
|
|
request->SetURL(url);
|
|
|
|
ASSERT_EQ(url, request->GetURL());
|
|
|
|
|
|
|
|
// CefRequest SetMethod
|
|
|
|
request->SetMethod(method);
|
|
|
|
ASSERT_EQ(method, request->GetMethod());
|
|
|
|
|
|
|
|
// CefRequest SetHeaderMap
|
|
|
|
request->SetHeaderMap(setHeaders);
|
|
|
|
request->GetHeaderMap(getHeaders);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMapEqual(setHeaders, getHeaders, false);
|
2012-04-03 03:34:16 +02:00
|
|
|
getHeaders.clear();
|
|
|
|
|
|
|
|
// CefRequest SetPostData
|
|
|
|
request->SetPostData(postData);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataEqual(postData, request->GetPostData());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
request = CefRequest::Create();
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(request.get() != NULL);
|
|
|
|
|
|
|
|
// CefRequest Set
|
|
|
|
request->Set(url, method, postData, setHeaders);
|
|
|
|
ASSERT_EQ(url, request->GetURL());
|
|
|
|
ASSERT_EQ(method, request->GetMethod());
|
|
|
|
request->GetHeaderMap(getHeaders);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMapEqual(setHeaders, getHeaders, false);
|
2012-04-03 03:34:16 +02:00
|
|
|
getHeaders.clear();
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataEqual(postData, request->GetPostData());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void CreateRequest(CefRefPtr<CefRequest>& request) {
|
2012-06-19 18:29:49 +02:00
|
|
|
request = CefRequest::Create();
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(request.get() != NULL);
|
|
|
|
|
|
|
|
request->SetURL("http://tests/run.html");
|
|
|
|
request->SetMethod("POST");
|
|
|
|
|
|
|
|
CefRequest::HeaderMap headers;
|
|
|
|
headers.insert(std::make_pair("HeaderA", "ValueA"));
|
|
|
|
headers.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
request->SetHeaderMap(headers);
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostData> postData(CefPostData::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(postData.get() != NULL);
|
|
|
|
|
|
|
|
CefRefPtr<CefPostDataElement> element1(
|
2012-06-19 18:29:49 +02:00
|
|
|
CefPostDataElement::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT_TRUE(element1.get() != NULL);
|
|
|
|
char bytes[] = "Test Bytes";
|
|
|
|
element1->SetToBytes(sizeof(bytes), bytes);
|
|
|
|
postData->AddElement(element1);
|
|
|
|
|
|
|
|
request->SetPostData(postData);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RequestSendRecvTestHandler : public TestHandler {
|
|
|
|
public:
|
|
|
|
RequestSendRecvTestHandler() {}
|
|
|
|
|
|
|
|
virtual void RunTest() OVERRIDE {
|
|
|
|
// Create the test request
|
|
|
|
CreateRequest(request_);
|
|
|
|
|
|
|
|
// Create the browser
|
2014-07-10 20:22:09 +02:00
|
|
|
CreateBrowser("");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE {
|
|
|
|
TestHandler::OnAfterCreated(browser);
|
|
|
|
|
|
|
|
// Load the test request
|
|
|
|
browser->GetMainFrame()->LoadRequest(request_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request) OVERRIDE {
|
|
|
|
// Verify that the request is the same
|
2012-06-19 18:29:49 +02:00
|
|
|
TestRequestEqual(request_, request, true);
|
2013-09-10 21:42:53 +02:00
|
|
|
EXPECT_EQ(RT_MAIN_FRAME, request->GetResourceType());
|
2014-07-10 20:22:09 +02:00
|
|
|
EXPECT_EQ(request_->GetTransitionType(), request->GetTransitionType());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
got_before_resource_load_.yes();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual CefRefPtr<CefResourceHandler> GetResourceHandler(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request) OVERRIDE {
|
|
|
|
// Verify that the request is the same
|
2012-06-19 18:29:49 +02:00
|
|
|
TestRequestEqual(request_, request, true);
|
2013-09-10 21:42:53 +02:00
|
|
|
EXPECT_EQ(RT_MAIN_FRAME, request->GetResourceType());
|
2014-07-10 20:22:09 +02:00
|
|
|
EXPECT_EQ(request_->GetTransitionType(), request->GetTransitionType());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
got_resource_handler_.yes();
|
|
|
|
|
|
|
|
DestroyTest();
|
|
|
|
|
|
|
|
// No results
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRequest> request_;
|
|
|
|
|
|
|
|
TrackCallback got_before_resource_load_;
|
|
|
|
TrackCallback got_resource_handler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Verify send and recieve
|
|
|
|
TEST(RequestTest, SendRecv) {
|
|
|
|
CefRefPtr<RequestSendRecvTestHandler> handler =
|
|
|
|
new RequestSendRecvTestHandler();
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
ASSERT_TRUE(handler->got_before_resource_load_);
|
|
|
|
ASSERT_TRUE(handler->got_resource_handler_);
|
|
|
|
}
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-07-10 20:22:09 +02:00
|
|
|
class LoadStringTestHandler : public TestHandler,
|
|
|
|
public CefStringVisitor {
|
|
|
|
public:
|
|
|
|
LoadStringTestHandler() {
|
|
|
|
source_ = "<html><head></head><body>Test</body></html>";
|
|
|
|
url_ = "http://tests/run.html";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void RunTest() OVERRIDE {
|
|
|
|
CreateBrowser("");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE {
|
|
|
|
TestHandler::OnAfterCreated(browser);
|
|
|
|
|
|
|
|
browser->GetMainFrame()->LoadString(source_, url_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) OVERRIDE {
|
|
|
|
EXPECT_EQ(url_, frame->GetURL().ToString());
|
|
|
|
|
|
|
|
CefRefPtr<CefStringVisitor> visitor(this);
|
|
|
|
frame->GetSource(visitor);
|
|
|
|
|
|
|
|
got_on_load_end_.yes();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Visit(const CefString& source) OVERRIDE {
|
|
|
|
EXPECT_EQ(source_, source.ToString());
|
|
|
|
got_source_.yes();
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string source_;
|
|
|
|
std::string url_;
|
|
|
|
|
|
|
|
TrackCallback got_on_load_end_;
|
|
|
|
TrackCallback got_source_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMPLEMENT_REFCOUNTING(LoadStringTestHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(RequestTest, LoadStringTest) {
|
|
|
|
CefRefPtr<LoadStringTestHandler> handler =
|
|
|
|
new LoadStringTestHandler();
|
|
|
|
|
|
|
|
handler->ExecuteTest();
|
|
|
|
ASSERT_TRUE(handler->got_on_load_end_);
|
|
|
|
ASSERT_TRUE(handler->got_source_);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-11-21 21:59:28 +01:00
|
|
|
const char kTypeTestCompleteMsg[] = "RequestTest.Type";
|
2013-09-10 21:42:53 +02:00
|
|
|
const char kTypeTestOrigin[] = "http://tests-requesttt.com/";
|
|
|
|
|
|
|
|
static struct TypeExpected {
|
|
|
|
const char* file;
|
2013-11-21 21:59:28 +01:00
|
|
|
bool browser_side; // True if this expectation applies to the browser side.
|
2013-09-12 19:44:54 +02:00
|
|
|
bool navigation; // True if this expectation represents a navigation.
|
2013-09-10 21:42:53 +02:00
|
|
|
cef_transition_type_t transition_type;
|
|
|
|
cef_resource_type_t resource_type;
|
|
|
|
int expected_count;
|
|
|
|
} g_type_expected[] = {
|
|
|
|
// Initial main frame load due to browser creation.
|
2013-11-21 21:59:28 +01:00
|
|
|
{"main.html", true, true, TT_EXPLICIT, RT_MAIN_FRAME, 1},
|
|
|
|
{"main.html", false, true, TT_EXPLICIT, RT_SUB_RESOURCE, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Sub frame load.
|
2013-11-21 21:59:28 +01:00
|
|
|
{"sub.html", true, true, TT_LINK, RT_SUB_FRAME, 1},
|
|
|
|
{"sub.html", false, true, TT_EXPLICIT, RT_SUB_RESOURCE, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Stylesheet load.
|
2013-11-21 21:59:28 +01:00
|
|
|
{"style.css", true, false, TT_LINK, RT_STYLESHEET, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Script load.
|
2013-11-21 21:59:28 +01:00
|
|
|
{"script.js", true, false, TT_LINK, RT_SCRIPT, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Image load.
|
2013-11-21 21:59:28 +01:00
|
|
|
{"image.png", true, false, TT_LINK, RT_IMAGE, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Font load.
|
2013-11-21 21:59:28 +01:00
|
|
|
{"font.ttf", true, false, TT_LINK, RT_FONT_RESOURCE, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// XHR load.
|
2013-11-21 21:59:28 +01:00
|
|
|
{"xhr.html", true, false, TT_LINK, RT_XHR, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class TypeExpectations {
|
|
|
|
public:
|
2013-11-21 21:59:28 +01:00
|
|
|
TypeExpectations(bool browser_side, bool navigation)
|
|
|
|
: browser_side_(browser_side),
|
|
|
|
navigation_(navigation) {
|
2013-09-10 21:42:53 +02:00
|
|
|
// Build the map of relevant requests.
|
2013-09-17 11:17:12 +02:00
|
|
|
for (int i = 0;
|
|
|
|
i < static_cast<int>(sizeof(g_type_expected) / sizeof(TypeExpected));
|
|
|
|
++i) {
|
2013-11-21 21:59:28 +01:00
|
|
|
if (g_type_expected[i].browser_side != browser_side_ ||
|
|
|
|
(navigation_ && g_type_expected[i].navigation != navigation_))
|
2013-09-10 21:42:53 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
request_count_.insert(std::make_pair(i, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify that a request has been received. Returns true if the request is
|
|
|
|
// something we care about.
|
|
|
|
bool GotRequest(CefRefPtr<CefRequest> request) {
|
|
|
|
const std::string& url = request->GetURL();
|
|
|
|
if (url.find(kTypeTestOrigin) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::string& file = url.substr(sizeof(kTypeTestOrigin)-1);
|
|
|
|
cef_transition_type_t transition_type = request->GetTransitionType();
|
|
|
|
cef_resource_type_t resource_type = request->GetResourceType();
|
|
|
|
|
|
|
|
const int index = GetExpectedIndex(file, transition_type, resource_type);
|
|
|
|
EXPECT_GE(index, 0)
|
|
|
|
<< "File: " << file.c_str()
|
2013-11-21 21:59:28 +01:00
|
|
|
<< "; Browser Side: " << browser_side_
|
2013-09-12 19:44:54 +02:00
|
|
|
<< "; Navigation: " << navigation_
|
2013-09-10 21:42:53 +02:00
|
|
|
<< "; Transition Type: " << transition_type
|
|
|
|
<< "; Resource Type: " << resource_type;
|
|
|
|
|
|
|
|
RequestCount::iterator it = request_count_.find(index);
|
|
|
|
EXPECT_TRUE(it != request_count_.end());
|
|
|
|
|
|
|
|
const int actual_count = ++it->second;
|
|
|
|
const int expected_count = g_type_expected[index].expected_count;
|
|
|
|
EXPECT_LE(actual_count, expected_count)
|
|
|
|
<< "File: " << file.c_str()
|
2013-11-21 21:59:28 +01:00
|
|
|
<< "; Browser Side: " << browser_side_
|
2013-09-12 19:44:54 +02:00
|
|
|
<< "; Navigation: " << navigation_
|
2013-09-10 21:42:53 +02:00
|
|
|
<< "; Transition Type: " << transition_type
|
|
|
|
<< "; Resource Type: " << resource_type;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if all expectations have been met.
|
|
|
|
bool IsDone(bool assert) {
|
2013-09-17 11:17:12 +02:00
|
|
|
for (int i = 0;
|
|
|
|
i < static_cast<int>(sizeof(g_type_expected) / sizeof(TypeExpected));
|
|
|
|
++i) {
|
2013-11-21 21:59:28 +01:00
|
|
|
if (g_type_expected[i].browser_side != browser_side_ ||
|
|
|
|
(navigation_ && g_type_expected[i].navigation != navigation_))
|
2013-09-10 21:42:53 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
RequestCount::const_iterator it = request_count_.find(i);
|
|
|
|
EXPECT_TRUE(it != request_count_.end());
|
|
|
|
if (it->second != g_type_expected[i].expected_count) {
|
|
|
|
if (assert) {
|
|
|
|
EXPECT_EQ(g_type_expected[i].expected_count, it->second)
|
|
|
|
<< "File: " << g_type_expected[i].file
|
2013-11-21 21:59:28 +01:00
|
|
|
<< "; Browser Side: " << browser_side_
|
2013-09-12 19:44:54 +02:00
|
|
|
<< "; Navigation: " << navigation_
|
2013-09-10 21:42:53 +02:00
|
|
|
<< "; Transition Type: " << g_type_expected[i].transition_type
|
|
|
|
<< "; Resource Type: " << g_type_expected[i].resource_type;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Returns the index for the specified navigation.
|
|
|
|
int GetExpectedIndex(const std::string& file,
|
|
|
|
cef_transition_type_t transition_type,
|
|
|
|
cef_resource_type_t resource_type) {
|
2013-09-17 11:17:12 +02:00
|
|
|
for (int i = 0;
|
|
|
|
i < static_cast<int>(sizeof(g_type_expected) / sizeof(TypeExpected));
|
|
|
|
++i) {
|
2013-09-10 21:42:53 +02:00
|
|
|
if (g_type_expected[i].file == file &&
|
2013-11-21 21:59:28 +01:00
|
|
|
g_type_expected[i].browser_side == browser_side_ &&
|
2013-09-12 19:44:54 +02:00
|
|
|
(!navigation_ || g_type_expected[i].navigation == navigation_) &&
|
2013-09-10 21:42:53 +02:00
|
|
|
g_type_expected[i].transition_type == transition_type &&
|
|
|
|
g_type_expected[i].resource_type == resource_type) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-21 21:59:28 +01:00
|
|
|
bool browser_side_;
|
2013-09-12 19:44:54 +02:00
|
|
|
bool navigation_;
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Map of TypeExpected index to actual request count.
|
|
|
|
typedef std::map<int, int> RequestCount;
|
|
|
|
RequestCount request_count_;
|
|
|
|
};
|
|
|
|
|
2013-11-21 21:59:28 +01:00
|
|
|
// Renderer side.
|
|
|
|
class TypeRendererTest : public ClientApp::RenderDelegate {
|
|
|
|
public:
|
|
|
|
TypeRendererTest() :
|
|
|
|
expectations_(false, true) {}
|
|
|
|
|
|
|
|
virtual bool OnBeforeNavigation(CefRefPtr<ClientApp> app,
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
cef_navigation_type_t navigation_type,
|
|
|
|
bool is_redirect) OVERRIDE {
|
|
|
|
if (expectations_.GotRequest(request) && expectations_.IsDone(false))
|
|
|
|
SendTestResults(browser);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Send the test results.
|
|
|
|
void SendTestResults(CefRefPtr<CefBrowser> browser) {
|
|
|
|
// Check if the test has failed.
|
|
|
|
bool result = !TestFailed();
|
|
|
|
|
|
|
|
// Return the result to the browser process.
|
|
|
|
CefRefPtr<CefProcessMessage> return_msg =
|
|
|
|
CefProcessMessage::Create(kTypeTestCompleteMsg);
|
|
|
|
CefRefPtr<CefListValue> args = return_msg->GetArgumentList();
|
|
|
|
EXPECT_TRUE(args.get());
|
|
|
|
EXPECT_TRUE(args->SetBool(0, result));
|
|
|
|
EXPECT_TRUE(browser->SendProcessMessage(PID_BROWSER, return_msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeExpectations expectations_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TypeRendererTest);
|
|
|
|
};
|
|
|
|
|
2013-09-10 21:42:53 +02:00
|
|
|
// Browser side.
|
|
|
|
class TypeTestHandler : public TestHandler {
|
|
|
|
public:
|
|
|
|
TypeTestHandler() :
|
2013-11-21 21:59:28 +01:00
|
|
|
browse_expectations_(true, true),
|
|
|
|
load_expectations_(true, false),
|
|
|
|
get_expectations_(true, false),
|
2013-09-10 21:42:53 +02:00
|
|
|
completed_browser_side_(false),
|
2013-11-21 21:59:28 +01:00
|
|
|
completed_render_side_(false),
|
2013-09-12 19:44:54 +02:00
|
|
|
destroyed_(false) {}
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
virtual void RunTest() OVERRIDE {
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "main.html",
|
|
|
|
"<html>"
|
|
|
|
"<head>"
|
|
|
|
"<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\">"
|
|
|
|
"<script type=\"text/javascript\" src=\"script.js\"></script>"
|
|
|
|
"</head>"
|
|
|
|
"<body><p>Main</p>"
|
|
|
|
"<script>xhr = new XMLHttpRequest();"
|
|
|
|
"xhr.open('GET', 'xhr.html', false);"
|
|
|
|
"xhr.send();</script>"
|
|
|
|
"<iframe src=\"sub.html\"></iframe>"
|
|
|
|
"<img src=\"image.png\">"
|
|
|
|
"</body></html>",
|
|
|
|
"text/html");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "sub.html",
|
|
|
|
"<html>Sub</html>",
|
|
|
|
"text/html");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "style.css",
|
|
|
|
"@font-face {"
|
|
|
|
" font-family: custom_font;"
|
|
|
|
" src: url('font.ttf');"
|
|
|
|
"}"
|
|
|
|
"p {"
|
|
|
|
" font-family: custom_font;"
|
|
|
|
"}",
|
|
|
|
"text/css");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "script.js",
|
|
|
|
"<!-- -->",
|
|
|
|
"text/javascript");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "image.png",
|
|
|
|
"<!-- -->",
|
|
|
|
"image/png");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "font.ttf",
|
|
|
|
"<!-- -->",
|
|
|
|
"font/ttf");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "xhr.html",
|
|
|
|
"<html>XHR</html>",
|
|
|
|
"text/html");
|
|
|
|
|
|
|
|
CreateBrowser(std::string(kTypeTestOrigin) + "main.html");
|
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
CefPostDelayedTask(TID_UI,
|
2013-09-12 19:44:54 +02:00
|
|
|
NewCefRunnableMethod(this, &TypeTestHandler::DestroyTest),
|
2013-09-10 21:42:53 +02:00
|
|
|
2000);
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:44:54 +02:00
|
|
|
virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
bool is_redirect) OVERRIDE {
|
|
|
|
browse_expectations_.GotRequest(request);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-10 21:42:53 +02:00
|
|
|
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request) OVERRIDE {
|
2013-09-12 19:44:54 +02:00
|
|
|
load_expectations_.GotRequest(request);
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual CefRefPtr<CefResourceHandler> GetResourceHandler(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request) OVERRIDE {
|
|
|
|
if (get_expectations_.GotRequest(request) &&
|
|
|
|
get_expectations_.IsDone(false)) {
|
|
|
|
completed_browser_side_ = true;
|
|
|
|
// Destroy the test on the UI thread.
|
|
|
|
CefPostTask(TID_UI,
|
2013-11-21 21:59:28 +01:00
|
|
|
NewCefRunnableMethod(this, &TypeTestHandler::DestroyTestIfComplete));
|
2013-09-10 21:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TestHandler::GetResourceHandler(browser, frame, request);
|
|
|
|
}
|
|
|
|
|
2013-11-21 21:59:28 +01:00
|
|
|
virtual bool OnProcessMessageReceived(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefProcessId source_process,
|
|
|
|
CefRefPtr<CefProcessMessage> message) OVERRIDE {
|
|
|
|
const std::string& msg_name = message->GetName();
|
|
|
|
if (msg_name == kTypeTestCompleteMsg) {
|
|
|
|
// Test that the renderer side succeeded.
|
|
|
|
CefRefPtr<CefListValue> args = message->GetArgumentList();
|
|
|
|
EXPECT_TRUE(args.get());
|
|
|
|
EXPECT_TRUE(args->GetBool(0));
|
|
|
|
|
|
|
|
completed_render_side_ = true;
|
|
|
|
DestroyTestIfComplete();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message not handled.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-10 21:42:53 +02:00
|
|
|
private:
|
2013-11-21 21:59:28 +01:00
|
|
|
void DestroyTestIfComplete() {
|
|
|
|
if (destroyed_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (completed_browser_side_ && completed_render_side_)
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:44:54 +02:00
|
|
|
virtual void DestroyTest() OVERRIDE {
|
|
|
|
if (destroyed_)
|
2013-09-10 21:42:53 +02:00
|
|
|
return;
|
2013-09-12 19:44:54 +02:00
|
|
|
destroyed_ = true;
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Verify test expectations.
|
|
|
|
EXPECT_TRUE(completed_browser_side_);
|
2013-11-21 21:59:28 +01:00
|
|
|
EXPECT_TRUE(completed_render_side_);
|
2013-09-12 19:44:54 +02:00
|
|
|
EXPECT_TRUE(browse_expectations_.IsDone(true));
|
|
|
|
EXPECT_TRUE(load_expectations_.IsDone(true));
|
2013-09-10 21:42:53 +02:00
|
|
|
EXPECT_TRUE(get_expectations_.IsDone(true));
|
|
|
|
|
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:44:54 +02:00
|
|
|
TypeExpectations browse_expectations_;
|
|
|
|
TypeExpectations load_expectations_;
|
2013-09-10 21:42:53 +02:00
|
|
|
TypeExpectations get_expectations_;
|
|
|
|
|
|
|
|
bool completed_browser_side_;
|
2013-11-21 21:59:28 +01:00
|
|
|
bool completed_render_side_;
|
2013-09-12 19:44:54 +02:00
|
|
|
bool destroyed_;
|
2013-09-10 21:42:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Verify the order of navigation-related callbacks.
|
|
|
|
TEST(RequestTest, ResourceAndTransitionType) {
|
|
|
|
CefRefPtr<TypeTestHandler> handler =
|
|
|
|
new TypeTestHandler();
|
|
|
|
handler->ExecuteTest();
|
|
|
|
}
|
2013-11-21 21:59:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Entry point for creating request renderer test objects.
|
|
|
|
// Called from client_app_delegates.cc.
|
|
|
|
void CreateRequestRendererTests(ClientApp::RenderDelegateSet& delegates) {
|
|
|
|
delegates.insert(new TypeRendererTest);
|
|
|
|
}
|