Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2011 Google Inc. 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "WebPageSerializer.h"
     33 
     34 #include "URLTestHelpers.h"
     35 #include "WebFrame.h"
     36 #include "WebFrameClient.h"
     37 #include "WebView.h"
     38 #include "core/dom/Document.h"
     39 #include "public/platform/Platform.h"
     40 #include "public/platform/WebString.h"
     41 #include "public/platform/WebURL.h"
     42 #include "public/platform/WebURLRequest.h"
     43 #include "public/platform/WebURLResponse.h"
     44 #include "public/platform/WebUnitTestSupport.h"
     45 #include "public/web/WebDocument.h"
     46 
     47 #include <gtest/gtest.h>
     48 
     49 using namespace blink;
     50 using WebCore::Document;
     51 using blink::URLTestHelpers::toKURL;
     52 
     53 namespace {
     54 
     55 class TestWebFrameClient : public WebFrameClient {
     56 };
     57 
     58 class WebPageSerializerTest : public testing::Test {
     59 public:
     60     WebPageSerializerTest() : m_webView(0), m_supportedSchemes(static_cast<size_t>(3))
     61     {
     62         m_supportedSchemes[0] = "http";
     63         m_supportedSchemes[1] = "https";
     64         m_supportedSchemes[2] = "file";
     65     }
     66 
     67 protected:
     68     virtual void SetUp()
     69     {
     70         // Create and initialize the WebView.
     71         m_webView = WebView::create(0);
     72         m_mainFrame = WebFrame::create(&m_webFrameClient);
     73         m_webView->setMainFrame(m_mainFrame);
     74     }
     75 
     76     virtual void TearDown()
     77     {
     78         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
     79         m_webView->close();
     80         m_mainFrame->close();
     81     }
     82 
     83     void registerMockedURLLoad(const std::string& url, const WebString& fileName)
     84     {
     85         URLTestHelpers::registerMockedURLLoad(toKURL(url), fileName, WebString::fromUTF8("pageserialization/"), WebString::fromUTF8("text/html"));
     86     }
     87 
     88     void loadURLInTopFrame(const WebURL& url)
     89     {
     90         WebURLRequest urlRequest;
     91         urlRequest.initialize();
     92         urlRequest.setURL(url);
     93         m_webView->mainFrame()->loadRequest(urlRequest);
     94         // Make sure any pending request get served.
     95         Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
     96     }
     97 
     98     static bool webVectorContains(const WebVector<WebURL>& vector, const char* url)
     99     {
    100         return vector.contains(WebURL(toKURL(std::string(url))));
    101     }
    102 
    103     // Useful for debugging.
    104     static void printWebURLs(const WebVector<WebURL>& urls)
    105     {
    106         for (size_t i = 0; i < urls.size(); i++)
    107             printf("%s\n", urls[i].spec().data());
    108     }
    109 
    110     WebView* m_webView;
    111     WebVector<WebCString> m_supportedSchemes;
    112 
    113 private:
    114     TestWebFrameClient m_webFrameClient;
    115     WebFrame* m_mainFrame;
    116 };
    117 
    118 TEST_F(WebPageSerializerTest, HTMLNodes)
    119 {
    120     // Register the mocked frame and load it.
    121     WebURL topFrameURL = toKURL("http://www.test.com");
    122     registerMockedURLLoad("http://www.test.com", WebString::fromUTF8("simple_page.html"));
    123     registerMockedURLLoad("http://www.example.com/beautifull.css", WebString::fromUTF8("beautifull.css"));
    124     loadURLInTopFrame(topFrameURL);
    125 
    126     // Retrieve all resources.
    127     WebVector<WebURL> frames;
    128     WebVector<WebURL> resources;
    129     ASSERT_TRUE(WebPageSerializer::retrieveAllResources(
    130         m_webView, m_supportedSchemes, &resources, &frames));
    131 
    132     // Tests that all resources from the frame have been retrieved.
    133     EXPECT_EQ(1U, frames.size()); // There should be no duplicates.
    134     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com"));
    135 
    136     EXPECT_EQ(14U, resources.size()); // There should be no duplicates.
    137     EXPECT_TRUE(webVectorContains(resources, "http://www.example.com/beautifull.css"));
    138     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.js"));
    139     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/bodyBackground.jpg"));
    140     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.png"));
    141     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/imageButton.png"));
    142     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/tableBackground.png"));
    143     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/trBackground.png"));
    144     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/tdBackground.png"));
    145     EXPECT_TRUE(webVectorContains(resources, "http://www.evene.fr/citations/auteur.php?ida=46"));
    146     EXPECT_TRUE(webVectorContains(resources, "http://www.brainyquote.com/quotes/authors/c/charles_darwin.html"));
    147     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/why_deleted.html"));
    148     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/why_inserted.html"));
    149     EXPECT_TRUE(webVectorContains(resources, "https://www.secure.com/https.gif"));
    150     EXPECT_TRUE(webVectorContains(resources, "file://c/my_folder/file.gif"));
    151 }
    152 
    153 TEST_F(WebPageSerializerTest, MultipleFrames)
    154 {
    155     // Register the mocked frames.
    156     WebURL topFrameURL = toKURL("http://www.test.com");
    157     registerMockedURLLoad("http://www.test.com", WebString::fromUTF8("top_frame.html"));
    158     registerMockedURLLoad("http://www.test.com/simple_iframe.html",
    159                           WebString::fromUTF8("simple_iframe.html"));
    160     registerMockedURLLoad("http://www.test.com/object_iframe.html",
    161                           WebString::fromUTF8("object_iframe.html"));
    162     registerMockedURLLoad("http://www.test.com/embed_iframe.html",
    163                           WebString::fromUTF8("embed_iframe.html"));
    164     // If we don't register a mocked resource for awesome.png, it causes the
    165     // document loader of the iframe that has it as its src to assert on close,
    166     // not sure why.
    167     registerMockedURLLoad("http://www.test.com/awesome.png",
    168                           WebString::fromUTF8("awesome.png"));
    169 
    170     loadURLInTopFrame(topFrameURL);
    171     // OBJECT/EMBED have some delay to start to load their content. The first
    172     // serveAsynchronousMockedRequests call in loadURLInTopFrame() finishes
    173     // before the start.
    174     RefPtr<Document> document = static_cast<PassRefPtr<Document> >(m_webView->mainFrame()->document());
    175     document->updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasksSynchronously);
    176     Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
    177 
    178     // Retrieve all resources.
    179     WebVector<WebURL> frames;
    180     WebVector<WebURL> resources;
    181     ASSERT_TRUE(WebPageSerializer::retrieveAllResources(
    182         m_webView, m_supportedSchemes, &resources, &frames));
    183 
    184     // Tests that all resources from the frame have been retrieved.
    185     EXPECT_EQ(4U, frames.size()); // There should be no duplicates.
    186     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com"));
    187     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/simple_iframe.html"));
    188     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/object_iframe.html"));
    189     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/embed_iframe.html"));
    190 
    191     EXPECT_EQ(5U, resources.size()); // There should be no duplicates.
    192     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.png"));
    193     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/innerFrame.png"));
    194     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/flash.swf"));
    195     // FIXME: for some reason the following resources is missing on one of the bot
    196     //        causing the test to fail. Probably a plugin issue.
    197     // EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/music.mid"));
    198     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/object.png"));
    199     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/embed.png"));
    200 }
    201 
    202 }
    203