• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 blink {
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     blink::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   blink::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 base::string16& script,
70                                           int* result);
71 
72   // Loads the given HTML into the main frame as a data: URL and blocks until
73   // the navigation is committed.
74   void LoadHTML(const char* html);
75 
76   // Navigates the main frame back or forward in session history and commits.
77   // The caller must capture a WebHistoryItem for the target page. This is
78   // available from the WebFrame.
79   void GoBack(const blink::WebHistoryItem& item);
80   void GoForward(const blink::WebHistoryItem& item);
81 
82   // Sends one native key event over IPC.
83   void SendNativeKeyEvent(const NativeWebKeyboardEvent& key_event);
84 
85   // Send a raw keyboard event to the renderer.
86   void SendWebKeyboardEvent(const blink::WebKeyboardEvent& key_event);
87 
88   // Send a raw mouse event to the renderer.
89   void SendWebMouseEvent(const blink::WebMouseEvent& key_event);
90 
91   // Returns the bounds (coordinates and size) of the element with id
92   // |element_id|.  Returns an empty rect if such an element was not found.
93   gfx::Rect GetElementBounds(const std::string& element_id);
94 
95   // Sends a left mouse click in the middle of the element with id |element_id|.
96   // Returns true if the event was sent, false otherwise (typically because
97   // the element was not found).
98   bool SimulateElementClick(const std::string& element_id);
99 
100   // Simulates |node| being focused.
101   void SetFocused(const blink::WebNode& node);
102 
103   // Clears anything associated with the browsing history.
104   void ClearHistory();
105 
106   // Simulates a navigation with a type of reload to the given url.
107   void Reload(const GURL& url);
108 
109   // Returns the IPC message ID of the navigation message.
110   uint32 GetNavigationIPCType();
111 
112   // Resize the view.
113   void Resize(gfx::Size new_size,
114               gfx::Rect resizer_rect,
115               bool is_fullscreen);
116 
117   // These are all methods from RenderViewImpl that we expose to testing code.
118   bool OnMessageReceived(const IPC::Message& msg);
119   void DidNavigateWithinPage(blink::WebFrame* frame, bool is_new_navigation);
120   void SendContentStateImmediately();
121   blink::WebWidget* GetWebWidget();
122 
123   // testing::Test
124   virtual void SetUp() OVERRIDE;
125 
126   virtual void TearDown() OVERRIDE;
127 
128   base::MessageLoop msg_loop_;
129   scoped_ptr<MockRenderProcess> mock_process_;
130   // We use a naked pointer because we don't want to expose RenderViewImpl in
131   // the embedder's namespace.
132   RenderView* view_;
133   RendererWebKitPlatformSupportImplNoSandbox webkit_platform_support_;
134   ContentRendererClient content_renderer_client_;
135   scoped_ptr<MockRenderThread> render_thread_;
136 
137   // Used to setup the process so renderers can run.
138   scoped_ptr<RendererMainPlatformDelegate> platform_;
139   scoped_ptr<MainFunctionParams> params_;
140   scoped_ptr<CommandLine> command_line_;
141 
142  private:
143   void GoToOffset(int offset, const blink::WebHistoryItem& history_item);
144 };
145 
146 }  // namespace content
147 
148 #endif  // CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
149