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