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 "WebFrame.h"
     35 #include "WebFrameClient.h"
     36 #include "WebString.h"
     37 #include "WebURL.h"
     38 #include "WebURLRequest.h"
     39 #include "WebURLResponse.h"
     40 #include "WebView.h"
     41 
     42 #include <googleurl/src/gurl.h>
     43 #include <gtest/gtest.h>
     44 #include <webkit/support/webkit_support.h>
     45 
     46 using namespace WebKit;
     47 
     48 namespace {
     49 
     50 class TestWebFrameClient : public WebFrameClient {
     51 };
     52 
     53 class WebPageSerializerTest : public testing::Test {
     54 public:
     55     WebPageSerializerTest() : m_webView(0), m_supportedSchemes(3U)
     56     {
     57         m_supportedSchemes[0] = "http";
     58         m_supportedSchemes[1] = "https";
     59         m_supportedSchemes[2] = "file";
     60     }
     61 
     62 protected:
     63     virtual void SetUp()
     64     {
     65         // Create and initialize the WebView.
     66         m_webView = WebView::create(0);
     67         m_webView->initializeMainFrame(&m_webFrameClient);
     68     }
     69 
     70     virtual void TearDown()
     71     {
     72         webkit_support::UnregisterAllMockedURLs();
     73         m_webView->close();
     74     }
     75 
     76     void registerMockedURLLoad(const WebURL& url, const WebString& fileName)
     77     {
     78         WebURLResponse response;
     79         response.initialize();
     80         response.setMIMEType("text/html");
     81         std::string filePath = webkit_support::GetWebKitRootDir().utf8();
     82         filePath.append("/Source/WebKit/chromium/tests/data/pageserialization/");
     83         filePath.append(fileName.utf8());
     84         webkit_support::RegisterMockedURL(url, response, WebString::fromUTF8(filePath));
     85     }
     86 
     87     void loadURLInTopFrame(const GURL& url)
     88     {
     89         WebURLRequest urlRequest;
     90         urlRequest.initialize();
     91         urlRequest.setURL(WebURL(url));
     92         m_webView->mainFrame()->loadRequest(urlRequest);
     93         // Make sure any pending request get served.
     94         webkit_support::ServeAsynchronousMockedRequests();
     95     }
     96 
     97     static bool webVectorContains(const WebVector<WebURL>& vector, char* url)
     98     {
     99         return vector.contains(WebURL(GURL(url)));
    100     }
    101 
    102     // Useful for debugging.
    103     static void printWebURLs(const WebVector<WebURL>& urls)
    104     {
    105         for (size_t i = 0; i < urls.size(); i++)
    106             printf("%s\n", urls[i].spec().data());
    107     }
    108 
    109     WebView* m_webView;
    110     WebVector<WebCString> m_supportedSchemes;
    111 
    112 private:
    113     TestWebFrameClient m_webFrameClient;
    114 };
    115 
    116 TEST_F(WebPageSerializerTest, HTMLNodes)
    117 {
    118     // Register the mocked frame and load it.
    119     WebURL topFrameURL = GURL("http://www.test.com");
    120     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("simple_page.html"));
    121     loadURLInTopFrame(topFrameURL);
    122 
    123     // Retrieve all resources.
    124     WebVector<WebURL> frames;
    125     WebVector<WebURL> resources;
    126     ASSERT_TRUE(WebPageSerializer::retrieveAllResources(
    127         m_webView, m_supportedSchemes, &resources, &frames));
    128 
    129     // Tests that all resources from the frame have been retrieved.
    130     EXPECT_EQ(1, frames.size()); // There should be no duplicates.
    131     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com"));
    132 
    133     EXPECT_EQ(14, resources.size()); // There should be no duplicates.
    134     EXPECT_TRUE(webVectorContains(resources, "http://www.example.com/beautifull.css"));
    135     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.js"));
    136     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/bodyBackground.jpg"));
    137     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.png"));
    138     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/imageButton.png"));
    139     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/tableBackground.png"));
    140     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/trBackground.png"));
    141     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/tdBackground.png"));
    142     EXPECT_TRUE(webVectorContains(resources, "http://www.evene.fr/citations/auteur.php?ida=46"));
    143     EXPECT_TRUE(webVectorContains(resources, "http://www.brainyquote.com/quotes/authors/c/charles_darwin.html"));
    144     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/why_deleted.html"));
    145     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/why_inserted.html"));
    146     EXPECT_TRUE(webVectorContains(resources, "https://www.secure.com/https.gif"));
    147     EXPECT_TRUE(webVectorContains(resources, "file://c/my_folder/file.gif"));
    148 }
    149 
    150 TEST_F(WebPageSerializerTest, MultipleFrames)
    151 {
    152     // Register the mocked frames.
    153     WebURL topFrameURL = GURL("http://www.test.com");
    154     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("top_frame.html"));
    155     registerMockedURLLoad(GURL("http://www.test.com/simple_iframe.html"),
    156                           WebString::fromUTF8("simple_iframe.html"));
    157     registerMockedURLLoad(GURL("http://www.test.com/object_iframe.html"),
    158                           WebString::fromUTF8("object_iframe.html"));
    159     registerMockedURLLoad(GURL("http://www.test.com/embed_iframe.html"),
    160                           WebString::fromUTF8("embed_iframe.html"));
    161     // If we don't register a mocked resource for awesome.png, it causes the
    162     // document loader of the iframe that has it as its src to assert on close,
    163     // not sure why.
    164     registerMockedURLLoad(GURL("http://www.test.com/awesome.png"),
    165                           WebString::fromUTF8("awesome.png"));
    166 
    167     loadURLInTopFrame(topFrameURL);
    168 
    169     // Retrieve all resources.
    170     WebVector<WebURL> frames;
    171     WebVector<WebURL> resources;
    172     ASSERT_TRUE(WebPageSerializer::retrieveAllResources(
    173         m_webView, m_supportedSchemes, &resources, &frames));
    174 
    175     // Tests that all resources from the frame have been retrieved.
    176     EXPECT_EQ(4, frames.size()); // There should be no duplicates.
    177     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com"));
    178     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/simple_iframe.html"));
    179     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/object_iframe.html"));
    180     EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/embed_iframe.html"));
    181 
    182     EXPECT_EQ(5, resources.size()); // There should be no duplicates.
    183     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.png"));
    184     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/innerFrame.png"));
    185     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/flash.swf"));
    186     // FIXME: for some reason the following resources is missing on one of the bot
    187     //        causing the test to fail. Probably a plugin issue.
    188     // EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/music.mid"));
    189     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/object.png"));
    190     EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/embed.png"));
    191 }
    192 
    193 }
    194