1/* 2 * Copyright (c) 2013, Opera Software ASA. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Opera Software ASA nor the names of its 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32 33#include "FrameTestHelpers.h" 34#include "URLTestHelpers.h" 35#include "WebFrameClient.h" 36#include "WebFrameImpl.h" 37#include "WebSettings.h" 38#include "WebViewImpl.h" 39#include <gtest/gtest.h> 40#include "core/page/Page.h" 41#include "core/page/PageSerializer.h" 42#include "core/platform/SerializedResource.h" 43#include "public/platform/Platform.h" 44#include "public/platform/WebString.h" 45#include "public/platform/WebThread.h" 46#include "public/platform/WebURL.h" 47#include "public/platform/WebURLRequest.h" 48#include "public/platform/WebURLResponse.h" 49#include "public/platform/WebUnitTestSupport.h" 50#include "wtf/Vector.h" 51 52using namespace WebCore; 53using namespace WebKit; 54using WebKit::FrameTestHelpers::runPendingTasks; 55using WebKit::URLTestHelpers::toKURL; 56using WebKit::URLTestHelpers::registerMockedURLLoad; 57 58namespace { 59 60class TestWebFrameClient : public WebFrameClient { 61public: 62 virtual ~TestWebFrameClient() { } 63}; 64 65class PageSerializerTest : public testing::Test { 66public: 67 PageSerializerTest() 68 : m_folder(WebString::fromUTF8("pageserializer/")) 69 , m_baseUrl(toKURL("http://www.test.com")) 70 { 71 } 72 73protected: 74 virtual void SetUp() 75 { 76 // Create and initialize the WebView. 77 m_webViewImpl = static_cast<WebViewImpl*>(WebView::create(0)); 78 79 // We want the images to load and JavaScript to be on. 80 WebSettings* settings = m_webViewImpl->settings(); 81 settings->setImagesEnabled(true); 82 settings->setLoadsImagesAutomatically(true); 83 settings->setJavaScriptEnabled(true); 84 85 m_webViewImpl->initializeMainFrame(&m_webFrameClient); 86 } 87 88 virtual void TearDown() 89 { 90 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 91 m_webViewImpl->close(); 92 m_webViewImpl = 0; 93 } 94 95 void setBaseUrl(const char* url) 96 { 97 m_baseUrl = toKURL(url); 98 } 99 100 void setBaseFolder(const char* folder) 101 { 102 m_folder = WebString::fromUTF8(folder); 103 } 104 105 void registerURL(const char* file, const char* mimeType) 106 { 107 registerMockedURLLoad(KURL(m_baseUrl, file), WebString::fromUTF8(file), m_folder, WebString::fromUTF8(mimeType)); 108 } 109 110 void registerErrorURL(const char* file, int statusCode) 111 { 112 WebURLError error; 113 error.reason = 0xdead + statusCode; 114 error.domain = "PageSerializerTest"; 115 116 WebURLResponse response; 117 response.initialize(); 118 response.setMIMEType("text/html"); 119 response.setHTTPStatusCode(statusCode); 120 121 Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_baseUrl, file), response, error); 122 } 123 124 void serialize(const char* url) 125 { 126 WebURLRequest urlRequest; 127 urlRequest.initialize(); 128 urlRequest.setURL(KURL(m_baseUrl, url)); 129 m_webViewImpl->mainFrame()->loadRequest(urlRequest); 130 // Make sure any pending request get served. 131 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); 132 // Some requests get delayed, run the timer. 133 runPendingTasks(); 134 // Server the delayed resources. 135 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); 136 137 PageSerializer serializer(&m_resources); 138 serializer.serialize(m_webViewImpl->mainFrameImpl()->frame()->page()); 139 } 140 141 Vector<SerializedResource>& getResources() 142 { 143 return m_resources; 144 } 145 146 bool isSerialized(const char* url, const char* mimeType) 147 { 148 KURL kURL = KURL(m_baseUrl, url); 149 WTF::String mime(mimeType); 150 for (size_t i = 0; i < m_resources.size(); ++i) { 151 const SerializedResource& resource = m_resources[i]; 152 if (resource.url == kURL && !resource.data->isEmpty() && equalIgnoringCase(resource.mimeType, mime)) 153 return true; 154 } 155 return false; 156 } 157 158 WebViewImpl* m_webViewImpl; 159 160private: 161 TestWebFrameClient m_webFrameClient; 162 WebString m_folder; 163 KURL m_baseUrl; 164 Vector<SerializedResource> m_resources; 165}; 166 167 168TEST_F(PageSerializerTest, InputImage) 169{ 170 setBaseFolder("pageserializer/input-image/"); 171 172 registerURL("input-image.html", "text/html"); 173 registerURL("button.png", "image/png"); 174 registerErrorURL("non-existing-button.png", 404); 175 176 serialize("input-image.html"); 177 178 EXPECT_TRUE(isSerialized("button.png", "image/png")); 179 EXPECT_FALSE(isSerialized("non-existing-button.png", "image/png")); 180} 181 182} 183