Broke ContentSettingBubbleModelTest.Plugins on Android.
[chromium-blink-merge.git] / content / browser / web_contents / web_contents_delegate_unittest.cc
blob9c5e0e7a2627892c01b8738a358fade336541881
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"
16 namespace content {
18 class MockWebContentsDelegate : public WebContentsDelegate {
19 public:
20 virtual ~MockWebContentsDelegate() {}
23 class WebContentsDelegateTest : public RenderViewHostImplTestHarness {
24 public:
25 WebContentsDelegateTest()
26 : file_user_blocking_thread_(
27 BrowserThread::FILE_USER_BLOCKING, &message_loop_),
28 io_thread_(BrowserThread::IO, &message_loop_) {
31 private:
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());
71 delegate.reset(NULL);
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.
76 contents_a.reset();
77 contents_b.reset();
80 } // namespace content