WebPageNewSerializerTest.cpp [plain text]
#include "config.h"
#include "WebFrame.h"
#include "WebFrameClient.h"
#include "WebPageSerializer.h"
#include "WebScriptSource.h"
#include "WebSettings.h"
#include "WebString.h"
#include "WebURL.h"
#include "WebURLRequest.h"
#include "WebURLResponse.h"
#include "WebView.h"
#include <googleurl/src/gurl.h>
#include <gtest/gtest.h>
#include <webkit/support/webkit_support.h>
using namespace WebKit;
namespace {
class TestWebFrameClient : public WebFrameClient {
public:
TestWebFrameClient() : m_scriptEnabled(false) { }
virtual bool allowScript(WebFrame*, bool ) { return m_scriptEnabled; }
bool m_scriptEnabled;
};
class WebPageNewSerializeTest : public testing::Test {
public:
WebPageNewSerializeTest()
: m_htmlMimeType(WebString::fromUTF8("text/html"))
, m_xhtmlMimeType(WebString::fromUTF8("application/xhtml+xml"))
, m_cssMimeType(WebString::fromUTF8("text/css"))
, m_pngMimeType(WebString::fromUTF8("image/png"))
{
}
protected:
virtual void SetUp()
{
m_webView = WebView::create(0);
WebSettings* settings = m_webView->settings();
settings->setImagesEnabled(true);
settings->setLoadsImagesAutomatically(true);
m_webView->initializeMainFrame(&m_webFrameClient);
}
virtual void TearDown()
{
webkit_support::UnregisterAllMockedURLs();
m_webView->close();
}
void registerMockedURLLoad(const WebURL& url, const WebString& fileName, const WebString& mimeType)
{
WebURLResponse response;
response.initialize();
response.setMIMEType(mimeType);
response.setHTTPStatusCode(200);
std::string filePath = webkit_support::GetWebKitRootDir().utf8();
filePath.append("/Source/WebKit/chromium/tests/data/pageserializer/");
filePath.append(fileName.utf8());
webkit_support::RegisterMockedURL(url, response, WebString::fromUTF8(filePath));
}
void loadURLInTopFrame(const GURL& url)
{
WebURLRequest urlRequest;
urlRequest.initialize();
urlRequest.setURL(WebURL(url));
m_webView->mainFrame()->loadRequest(urlRequest);
webkit_support::ServeAsynchronousMockedRequests();
webkit_support::RunAllPendingMessages();
webkit_support::ServeAsynchronousMockedRequests();
}
void enableJS()
{
m_webFrameClient.m_scriptEnabled = true;
}
void runOnLoad()
{
m_webView->mainFrame()->executeScript(WebScriptSource(WebString::fromUTF8("onLoad()")));
}
const WebString& htmlMimeType() const { return m_htmlMimeType; }
const WebString& xhtmlMimeType() const { return m_xhtmlMimeType; }
const WebString& cssMimeType() const { return m_cssMimeType; }
const WebString& pngMimeType() const { return m_pngMimeType; }
static bool resourceVectorContains(const WebVector<WebPageSerializer::Resource>& resources, char* url, char* mimeType)
{
WebURL webURL = WebURL(GURL(url));
for (size_t i = 0; i < resources.size(); ++i) {
const WebPageSerializer::Resource& resource = resources[i];
if (resource.url == webURL && !resource.data.isEmpty() && !resource.mimeType.compare(WebCString(mimeType)))
return true;
}
return false;
}
WebView* m_webView;
private:
WebString m_htmlMimeType;
WebString m_xhtmlMimeType;
WebString m_cssMimeType;
WebString m_pngMimeType;
TestWebFrameClient m_webFrameClient;
};
TEST_F(WebPageNewSerializeTest, DISABLED_PageWithFrames)
{
WebURL topFrameURL = GURL("http://www.test.com");
registerMockedURLLoad(topFrameURL, WebString::fromUTF8("top_frame.html"), htmlMimeType());
registerMockedURLLoad(GURL("http://www.test.com/iframe.html"), WebString::fromUTF8("iframe.html"), htmlMimeType());
registerMockedURLLoad(GURL("http://www.test.com/iframe2.html"), WebString::fromUTF8("iframe2.html"), htmlMimeType());
registerMockedURLLoad(GURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/green_background.png"), WebString::fromUTF8("green_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), pngMimeType());
loadURLInTopFrame(topFrameURL);
WebVector<WebPageSerializer::Resource> resources;
WebPageSerializer::serialize(m_webView, &resources);
ASSERT_FALSE(resources.isEmpty());
const WebPageSerializer::Resource& resource = resources[0];
EXPECT_TRUE(resource.url == GURL("http://www.test.com"));
EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
EXPECT_FALSE(resource.data.isEmpty());
EXPECT_EQ(6, resources.size()); EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe.html", "text/html"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe2.html", "text/html"));
}
TEST_F(WebPageNewSerializeTest, DISABLED_CSSResources)
{
WebURL topFrameURL = GURL("http://www.test.com");
registerMockedURLLoad(topFrameURL, WebString::fromUTF8("css_test_page.html"), htmlMimeType());
registerMockedURLLoad(GURL("http://www.test.com/link_styles.css"), WebString::fromUTF8("link_styles.css"), cssMimeType());
registerMockedURLLoad(GURL("http://www.test.com/import_style_from_link.css"), WebString::fromUTF8("import_style_from_link.css"), cssMimeType());
registerMockedURLLoad(GURL("http://www.test.com/import_styles.css"), WebString::fromUTF8("import_styles.css"), cssMimeType());
registerMockedURLLoad(GURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/orange_background.png"), WebString::fromUTF8("orange_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/yellow_background.png"), WebString::fromUTF8("yellow_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/green_background.png"), WebString::fromUTF8("green_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/purple_background.png"), WebString::fromUTF8("purple_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/ul-dot.png"), WebString::fromUTF8("ul-dot.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/ol-dot.png"), WebString::fromUTF8("ol-dot.png"), pngMimeType());
enableJS();
loadURLInTopFrame(topFrameURL);
runOnLoad();
WebVector<WebPageSerializer::Resource> resources;
WebPageSerializer::serialize(m_webView, &resources);
ASSERT_FALSE(resources.isEmpty());
const WebPageSerializer::Resource& resource = resources[0];
EXPECT_TRUE(resource.url == GURL("http://www.test.com"));
EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
EXPECT_FALSE(resource.data.isEmpty());
EXPECT_EQ(12, resources.size()); EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/link_styles.css", "text/css"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_styles.css", "text/css"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_style_from_link.css", "text/css"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/yellow_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/purple_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ul-dot.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ol-dot.png", "image/png"));
}
TEST_F(WebPageNewSerializeTest, DISABLED_BlankFrames)
{
WebURL topFrameURL = GURL("http://www.test.com");
registerMockedURLLoad(topFrameURL, WebString::fromUTF8("blank_frames.html"), htmlMimeType());
registerMockedURLLoad(GURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/orange_background.png"), WebString::fromUTF8("orange_background.png"), pngMimeType());
registerMockedURLLoad(GURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), pngMimeType());
enableJS();
loadURLInTopFrame(topFrameURL);
runOnLoad();
WebVector<WebPageSerializer::Resource> resources;
WebPageSerializer::serialize(m_webView, &resources);
ASSERT_FALSE(resources.isEmpty());
const WebPageSerializer::Resource& resource = resources[0];
EXPECT_TRUE(resource.url == GURL("http://www.test.com"));
EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
EXPECT_FALSE(resource.data.isEmpty());
EXPECT_EQ(7, resources.size()); EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/0", "text/html"));
EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/1", "text/html"));
EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/2", "text/html"));
}
TEST_F(WebPageNewSerializeTest, DISABLED_SerializeXMLHasRightDeclaration)
{
WebURL topFrameURL = GURL("http://www.test.com/simple.xhtml");
registerMockedURLLoad(topFrameURL, WebString::fromUTF8("simple.xhtml"), xhtmlMimeType());
loadURLInTopFrame(topFrameURL);
WebVector<WebPageSerializer::Resource> resources;
WebPageSerializer::serialize(m_webView, &resources);
ASSERT_FALSE(resources.isEmpty());
ASSERT_EQ(1, resources.size());
std::string xml = resources[0].data;
size_t pos = xml.find("<?xml version=");
ASSERT_TRUE(pos != std::string::npos);
pos = xml.find("<?xml version=", pos + 1);
ASSERT_TRUE(pos == std::string::npos);
}
}