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 "base/compiler_specific.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h"
8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/browser/renderer_host/test_render_view_host.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/web_contents_delegate.h"
12 #include "content/public/test/test_browser_context.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class MockWebContentsDelegate
: public WebContentsDelegate
{
20 virtual ~MockWebContentsDelegate() {}
23 class WebContentsDelegateTest
: public RenderViewHostImplTestHarness
{
25 WebContentsDelegateTest()
26 : file_user_blocking_thread_(
27 BrowserThread::FILE_USER_BLOCKING
, &message_loop_
),
28 io_thread_(BrowserThread::IO
, &message_loop_
) {
32 TestBrowserThread file_user_blocking_thread_
;
33 TestBrowserThread io_thread_
;
36 TEST_F(WebContentsDelegateTest
, UnregisterInDestructor
) {
37 scoped_ptr
<WebContentsImpl
> contents_a(static_cast<WebContentsImpl
*>(
38 WebContents::Create(WebContents::CreateParams(browser_context_
.get()))));
39 scoped_ptr
<WebContentsImpl
> contents_b(static_cast<WebContentsImpl
*>(
40 WebContents::Create(WebContents::CreateParams(browser_context_
.get()))));
41 EXPECT_EQ(NULL
, contents_a
->GetDelegate());
42 EXPECT_EQ(NULL
, contents_b
->GetDelegate());
44 scoped_ptr
<MockWebContentsDelegate
> delegate(new MockWebContentsDelegate());
46 // Setting a delegate should work correctly.
47 contents_a
->SetDelegate(delegate
.get());
48 EXPECT_EQ(delegate
.get(), contents_a
->GetDelegate());
49 EXPECT_TRUE(contents_b
->GetDelegate() == NULL
);
51 // A delegate can be a delegate to multiple WebContentsImpl.
52 contents_b
->SetDelegate(delegate
.get());
53 EXPECT_EQ(delegate
.get(), contents_a
->GetDelegate());
54 EXPECT_EQ(delegate
.get(), contents_b
->GetDelegate());
56 // Setting the same delegate multiple times should work correctly.
57 contents_b
->SetDelegate(delegate
.get());
58 EXPECT_EQ(delegate
.get(), contents_a
->GetDelegate());
59 EXPECT_EQ(delegate
.get(), contents_b
->GetDelegate());
61 // Setting delegate to NULL should work correctly.
62 contents_b
->SetDelegate(NULL
);
63 EXPECT_EQ(delegate
.get(), contents_a
->GetDelegate());
64 EXPECT_TRUE(contents_b
->GetDelegate() == NULL
);
66 // Destroying the delegate while it is still the delegate for a
67 // WebContentsImpl should unregister it.
68 contents_b
->SetDelegate(delegate
.get());
69 EXPECT_EQ(delegate
.get(), contents_a
->GetDelegate());
70 EXPECT_EQ(delegate
.get(), contents_b
->GetDelegate());
72 EXPECT_TRUE(contents_a
->GetDelegate() == NULL
);
73 EXPECT_TRUE(contents_b
->GetDelegate() == NULL
);
75 // Destroy the WebContentses and run the message loop to prevent leaks.
80 } // namespace content