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.
5 #include "chrome/test/base/chrome_render_view_test.h"
7 #include "base/debug/leak_annotations.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/chrome_content_browser_client.h"
10 #include "chrome/common/chrome_content_client.h"
11 #include "chrome/common/render_messages.h"
12 #include "chrome/renderer/chrome_content_renderer_client.h"
13 #include "chrome/renderer/spellchecker/spellcheck.h"
14 #include "chrome/test/base/chrome_unit_test_suite.h"
15 #include "components/autofill/content/renderer/autofill_agent.h"
16 #include "components/autofill/content/renderer/password_autofill_agent.h"
17 #include "components/autofill/content/renderer/test_password_autofill_agent.h"
18 #include "components/autofill/content/renderer/test_password_generation_agent.h"
19 #include "content/public/browser/native_web_keyboard_event.h"
20 #include "content/public/common/renderer_preferences.h"
21 #include "content/public/renderer/render_view.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "third_party/WebKit/public/platform/WebURLRequest.h"
24 #include "third_party/WebKit/public/web/WebFrame.h"
25 #include "third_party/WebKit/public/web/WebInputEvent.h"
26 #include "third_party/WebKit/public/web/WebKit.h"
27 #include "third_party/WebKit/public/web/WebScriptController.h"
28 #include "third_party/WebKit/public/web/WebScriptSource.h"
29 #include "third_party/WebKit/public/web/WebView.h"
31 #if defined(ENABLE_EXTENSIONS)
32 #include "chrome/renderer/extensions/chrome_extensions_dispatcher_delegate.h"
33 #include "extensions/browser/extension_function_dispatcher.h"
34 #include "extensions/common/extension.h"
35 #include "extensions/renderer/dispatcher.h"
36 #include "extensions/renderer/event_bindings.h"
39 using autofill::AutofillAgent
;
40 using autofill::PasswordAutofillAgent
;
41 using autofill::PasswordGenerationAgent
;
42 using blink::WebFrame
;
43 using blink::WebInputEvent
;
44 using blink::WebMouseEvent
;
45 using blink::WebScriptController
;
46 using blink::WebScriptSource
;
47 using blink::WebString
;
48 using blink::WebURLRequest
;
49 using content::RenderFrame
;
50 using testing::NiceMock
;
51 using testing::Return
;
56 // An autofill agent that treats all typing as user gesture.
57 class MockAutofillAgent
: public AutofillAgent
{
59 MockAutofillAgent(RenderFrame
* render_frame
,
60 PasswordAutofillAgent
* password_autofill_agent
,
61 PasswordGenerationAgent
* password_generation_agent
)
62 : AutofillAgent(render_frame
,
63 password_autofill_agent
,
64 password_generation_agent
) {
65 ON_CALL(*this, IsUserGesture()).WillByDefault(Return(true));
68 ~MockAutofillAgent() override
{}
70 MOCK_CONST_METHOD0(IsUserGesture
, bool());
73 DISALLOW_COPY_AND_ASSIGN(MockAutofillAgent
);
78 ChromeRenderViewTest::ChromeRenderViewTest()
79 : password_autofill_agent_(NULL
),
80 password_generation_(NULL
),
81 autofill_agent_(NULL
),
82 chrome_render_thread_(NULL
) {
85 ChromeRenderViewTest::~ChromeRenderViewTest() {
88 void ChromeRenderViewTest::SetUp() {
89 ChromeUnitTestSuite::InitializeProviders();
90 ChromeUnitTestSuite::InitializeResourceBundle();
92 chrome_render_thread_
= new ChromeMockRenderThread();
93 render_thread_
.reset(chrome_render_thread_
);
95 content::RenderViewTest::SetUp();
97 // RenderFrame doesn't expose its Agent objects, because it has no need to
98 // store them directly (they're stored as RenderFrameObserver*). So just
99 // create another set.
100 password_autofill_agent_
=
101 new autofill::TestPasswordAutofillAgent(view_
->GetMainRenderFrame());
102 password_generation_
=
103 new autofill::TestPasswordGenerationAgent(view_
->GetMainRenderFrame(),
104 password_autofill_agent_
);
105 autofill_agent_
= new NiceMock
<MockAutofillAgent
>(view_
->GetMainRenderFrame(),
106 password_autofill_agent_
,
107 password_generation_
);
110 void ChromeRenderViewTest::TearDown() {
111 base::RunLoop().RunUntilIdle();
112 #if defined(ENABLE_EXTENSIONS)
113 ChromeContentRendererClient
* client
=
114 static_cast<ChromeContentRendererClient
*>(content_renderer_client_
.get());
115 client
->GetExtensionDispatcherForTest()->OnRenderProcessShutdown();
118 #if defined(LEAK_SANITIZER)
119 // Do this before shutting down V8 in RenderViewTest::TearDown().
120 // http://crbug.com/328552
121 __lsan_do_leak_check();
123 content::RenderViewTest::TearDown();
126 content::ContentClient
* ChromeRenderViewTest::CreateContentClient() {
127 return new ChromeContentClient();
130 content::ContentBrowserClient
*
131 ChromeRenderViewTest::CreateContentBrowserClient() {
132 return new chrome::ChromeContentBrowserClient();
135 content::ContentRendererClient
*
136 ChromeRenderViewTest::CreateContentRendererClient() {
137 ChromeContentRendererClient
* client
= new ChromeContentRendererClient();
138 #if defined(ENABLE_EXTENSIONS)
139 extension_dispatcher_delegate_
.reset(
140 new ChromeExtensionsDispatcherDelegate());
141 client
->SetExtensionDispatcherForTest(
142 new extensions::Dispatcher(extension_dispatcher_delegate_
.get()));
144 #if defined(ENABLE_SPELLCHECK)
145 client
->SetSpellcheck(new SpellCheck());
150 void ChromeRenderViewTest::EnableUserGestureSimulationForAutofill() {
151 EXPECT_CALL(*(static_cast<MockAutofillAgent
*>(autofill_agent_
)),
152 IsUserGesture()).WillRepeatedly(Return(true));
155 void ChromeRenderViewTest::DisableUserGestureSimulationForAutofill() {
156 EXPECT_CALL(*(static_cast<MockAutofillAgent
*>(autofill_agent_
)),
157 IsUserGesture()).WillRepeatedly(Return(false));