Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 // RenderViewFakeResourcesTest can be used as a base class for tests that need
      6 // to simulate loading network resources (such as http: urls) into a
      7 // RenderView.  It does this by handling the relevant IPC messages that the
      8 // renderer would normally send to the browser, and responding with static
      9 // content from an internal map.  A request for a url that is not in the map
     10 // will return a 404.  Currently, content is always returned as text/html, and
     11 // there is no support for handling POST data.
     12 //
     13 // RenderViewFakeResourcesTest sets up a MessageLoop (message_loop_) that
     14 // can be used by the subclass to post or process tasks.
     15 //
     16 // Note that since WebKit cannot be safely shut down and re-initialized,
     17 // any test that uses this base class should run as part of browser_tests
     18 // so that the test is launched in its own process.
     19 //
     20 // Typical usage:
     21 //
     22 // class MyTest : public RenderVieFakeResourcesTest {
     23 //  protected:
     24 //   virtual void SetUp() {
     25 //     RenderViewFakeResourcesTest::SetUp();
     26 //     <insert test-specific setup>
     27 //   }
     28 //
     29 //   virtual void TearDown() {
     30 //     <insert test-specific teardown>
     31 //     RenderViewFakeResourcesTest::TearDown();
     32 //   }
     33 //   ...
     34 // };
     35 //
     36 // TEST_F(MyTest, TestFoo) {
     37 //   responses_["http://host.com/"] = "<html><body>some content</body></html>";
     38 //   LoadURL("http://host.com/");
     39 //   ...
     40 // }
     41 
     42 #ifndef CONTENT_PUBLIC_TEST_RENDER_VIEW_FAKE_RESOURCES_TEST_H_
     43 #define CONTENT_PUBLIC_TEST_RENDER_VIEW_FAKE_RESOURCES_TEST_H_
     44 
     45 #include <map>
     46 #include <string>
     47 
     48 #include "base/memory/scoped_ptr.h"
     49 #include "base/message_loop/message_loop.h"
     50 #include "content/public/renderer/content_renderer_client.h"
     51 #include "content/public/renderer/render_view_visitor.h"
     52 #include "ipc/ipc_listener.h"
     53 #include "testing/gtest/include/gtest/gtest.h"
     54 
     55 struct ResourceHostMsg_Request;
     56 
     57 namespace IPC {
     58 class Channel;
     59 }
     60 
     61 namespace WebKit {
     62 class WebFrame;
     63 class WebHistoryItem;
     64 }
     65 
     66 namespace content {
     67 class MockRenderProcess;
     68 class RenderThreadImpl;
     69 
     70 class RenderViewFakeResourcesTest : public ::testing::Test,
     71                                     public IPC::Listener,
     72                                     public RenderViewVisitor {
     73  public:
     74   // IPC::Listener implementation.
     75   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     76 
     77   // RenderViewVisitor implementation.
     78   virtual bool Visit(RenderView* render_view) OVERRIDE;
     79 
     80  protected:
     81   RenderViewFakeResourcesTest();
     82   virtual ~RenderViewFakeResourcesTest();
     83 
     84   // Call the base class SetUp and TearDown methods as part of your
     85   // test fixture's SetUp / TearDown.
     86   virtual void SetUp() OVERRIDE;
     87   virtual void TearDown() OVERRIDE;
     88 
     89   RenderView* view();
     90 
     91   // Loads |url| into the RenderView, waiting for the load to finish.
     92   // Before loading the url, add any content that you want to return
     93   // to responses_.
     94   void LoadURL(const std::string& url);
     95 
     96   // Same as LoadURL, but sends a POST request.  Note that POST data is
     97   // not supported.
     98   void LoadURLWithPost(const std::string& url);
     99 
    100   // Navigates the main frame back in session history.
    101   void GoBack();
    102 
    103   // Navigates the main frame forward in session history.  Note that for
    104   // forward navigations, the caller needs to capture the WebHistoryItem
    105   // for the page to go forward to (before going back) and pass it to
    106   // this method.  The WebHistoryItem is available from the WebFrame.
    107   void GoForward(const WebKit::WebHistoryItem& history_item);
    108 
    109   // Returns the main WebFrame for our RenderView.
    110   WebKit::WebFrame* GetMainFrame();
    111 
    112   // IPC message handlers below
    113 
    114   // Notification that page load has finished.  Exit the message loop
    115   // so that the test can continue.
    116   void OnDidStopLoading();
    117 
    118   // Notification that the renderer wants to load a resource.
    119   // If the requested url is in responses_, we send the renderer a 200
    120   // and the supplied content, otherwise we send it a 404 error.
    121   void OnRequestResource(const IPC::Message& message,
    122                          int request_id,
    123                          const ResourceHostMsg_Request& request_data);
    124 
    125   // Notification that the render view we've created is ready to use.
    126   void OnRenderViewReady();
    127 
    128   static const int32 kViewId;  // arbitrary id for our testing view
    129 
    130   base::MessageLoopForIO message_loop_;
    131   ContentRendererClient content_renderer_client_;
    132   // channel that the renderer uses to talk to the browser.
    133   // For this test, we will handle the browser end of the channel.
    134   scoped_ptr<IPC::Channel> channel_;
    135   RenderThreadImpl* render_thread_;  // owned by mock_process_
    136   scoped_ptr<MockRenderProcess> mock_process_;
    137   RenderView* view_;  // not owned, deletes itself on close
    138 
    139   // Map of url -> response body for network requests from the renderer.
    140   // Any urls not in this map are served a 404 error.
    141   std::map<std::string, std::string> responses_;
    142 
    143  private:
    144   // A helper for GoBack and GoForward.
    145   void GoToOffset(int offset, const WebKit::WebHistoryItem& history_item);
    146 
    147   // The previous state for whether sandbox support was enabled in
    148   // RenderViewWebKitPlatformSupportImpl.
    149   bool sandbox_was_enabled_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(RenderViewFakeResourcesTest);
    152 };
    153 
    154 }  // namespace content
    155 
    156 #endif  // CONTENT_PUBLIC_TEST_RENDER_VIEW_FAKE_RESOURCES_TEST_H_
    157