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 #ifndef CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
      6 #define CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
      7 
      8 #include <string>
      9 
     10 #include "base/command_line.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "base/strings/string16.h"
     14 #include "content/public/browser/native_web_keyboard_event.h"
     15 #include "content/public/common/main_function_params.h"
     16 #include "content/public/renderer/content_renderer_client.h"
     17 #include "content/public/test/mock_render_thread.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 #include "third_party/WebKit/public/platform/Platform.h"
     20 #include "third_party/WebKit/public/web/WebFrame.h"
     21 
     22 namespace WebKit {
     23 class WebHistoryItem;
     24 class WebWidget;
     25 }
     26 
     27 namespace gfx {
     28 class Rect;
     29 }
     30 
     31 namespace content {
     32 class MockRenderProcess;
     33 class RendererMainPlatformDelegate;
     34 class RendererWebKitPlatformSupportImplNoSandboxImpl;
     35 
     36 class RenderViewTest : public testing::Test {
     37  public:
     38   // A special WebKitPlatformSupportImpl class for getting rid off the
     39   // dependency to the sandbox, which is not available in RenderViewTest.
     40   class RendererWebKitPlatformSupportImplNoSandbox {
     41    public:
     42     RendererWebKitPlatformSupportImplNoSandbox();
     43     ~RendererWebKitPlatformSupportImplNoSandbox();
     44     WebKit::Platform* Get();
     45 
     46    private:
     47     scoped_ptr<RendererWebKitPlatformSupportImplNoSandboxImpl>
     48         webkit_platform_support_;
     49   };
     50 
     51   RenderViewTest();
     52   virtual ~RenderViewTest();
     53 
     54  protected:
     55   // Spins the message loop to process all messages that are currently pending.
     56   void ProcessPendingMessages();
     57 
     58   // Returns a pointer to the main frame.
     59   WebKit::WebFrame* GetMainFrame();
     60 
     61   // Executes the given JavaScript in the context of the main frame. The input
     62   // is a NULL-terminated UTF-8 string.
     63   void ExecuteJavaScript(const char* js);
     64 
     65   // Executes the given JavaScript and sets the int value it evaluates to in
     66   // |result|.
     67   // Returns true if the JavaScript was evaluated correctly to an int value,
     68   // false otherwise.
     69   bool ExecuteJavaScriptAndReturnIntValue(const string16& script, int* result);
     70 
     71   // Loads the given HTML into the main frame as a data: URL and blocks until
     72   // the navigation is committed.
     73   void LoadHTML(const char* html);
     74 
     75   // Navigates the main frame back or forward in session history and commits.
     76   // The caller must capture a WebHistoryItem for the target page. This is
     77   // available from the WebFrame.
     78   void GoBack(const WebKit::WebHistoryItem& item);
     79   void GoForward(const WebKit::WebHistoryItem& item);
     80 
     81   // Sends one native key event over IPC.
     82   void SendNativeKeyEvent(const NativeWebKeyboardEvent& key_event);
     83 
     84   // Send a raw keyboard event to the renderer.
     85   void SendWebKeyboardEvent(const WebKit::WebKeyboardEvent& key_event);
     86 
     87   // Send a raw mouse event to the renderer.
     88   void SendWebMouseEvent(const WebKit::WebMouseEvent& key_event);
     89 
     90   // Returns the bounds (coordinates and size) of the element with id
     91   // |element_id|.  Returns an empty rect if such an element was not found.
     92   gfx::Rect GetElementBounds(const std::string& element_id);
     93 
     94   // Sends a left mouse click in the middle of the element with id |element_id|.
     95   // Returns true if the event was sent, false otherwise (typically because
     96   // the element was not found).
     97   bool SimulateElementClick(const std::string& element_id);
     98 
     99   // Simulates |node| being focused.
    100   void SetFocused(const WebKit::WebNode& node);
    101 
    102   // Clears anything associated with the browsing history.
    103   void ClearHistory();
    104 
    105   // Simulates a navigation with a type of reload to the given url.
    106   void Reload(const GURL& url);
    107 
    108   // Returns the IPC message ID of the navigation message.
    109   uint32 GetNavigationIPCType();
    110 
    111   // Resize the view.
    112   void Resize(gfx::Size new_size,
    113               gfx::Rect resizer_rect,
    114               bool is_fullscreen);
    115 
    116   // These are all methods from RenderViewImpl that we expose to testing code.
    117   bool OnMessageReceived(const IPC::Message& msg);
    118   void DidNavigateWithinPage(WebKit::WebFrame* frame, bool is_new_navigation);
    119   void SendContentStateImmediately();
    120   WebKit::WebWidget* GetWebWidget();
    121 
    122   // testing::Test
    123   virtual void SetUp() OVERRIDE;
    124 
    125   virtual void TearDown() OVERRIDE;
    126 
    127   base::MessageLoop msg_loop_;
    128   scoped_ptr<MockRenderProcess> mock_process_;
    129   // We use a naked pointer because we don't want to expose RenderViewImpl in
    130   // the embedder's namespace.
    131   RenderView* view_;
    132   RendererWebKitPlatformSupportImplNoSandbox webkit_platform_support_;
    133   ContentRendererClient content_renderer_client_;
    134   scoped_ptr<MockRenderThread> render_thread_;
    135 
    136   // Used to setup the process so renderers can run.
    137   scoped_ptr<RendererMainPlatformDelegate> platform_;
    138   scoped_ptr<MainFunctionParams> params_;
    139   scoped_ptr<CommandLine> command_line_;
    140 
    141  private:
    142   void GoToOffset(int offset, const WebKit::WebHistoryItem& history_item);
    143 };
    144 
    145 }  // namespace content
    146 
    147 #endif  // CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
    148