1 // Copyright 2014 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 "extensions/browser/guest_view/guest_view_manager.h"
7 #include "content/public/browser/notification_service.h"
8 #include "content/public/test/test_browser_context.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "content/public/test/web_contents_tester.h"
11 #include "extensions/browser/extensions_test.h"
13 using content::WebContents
;
14 using content::WebContentsTester
;
16 namespace extensions
{
20 // This class allows us to access some private variables in
22 class TestGuestViewManager
: public GuestViewManager
{
24 explicit TestGuestViewManager(content::BrowserContext
* context
)
25 : GuestViewManager(context
) {}
27 int last_instance_id_removed_for_testing() {
28 return last_instance_id_removed_
;
31 size_t GetRemovedInstanceIdSize() { return removed_instance_ids_
.size(); }
34 using GuestViewManager::last_instance_id_removed_
;
35 using GuestViewManager::removed_instance_ids_
;
37 DISALLOW_COPY_AND_ASSIGN(TestGuestViewManager
);
40 } // namespace guestview
44 class GuestViewManagerTest
: public extensions::ExtensionsTest
{
46 GuestViewManagerTest() :
47 notification_service_(content::NotificationService::Create()) {}
48 ~GuestViewManagerTest() override
{}
50 scoped_ptr
<WebContents
> CreateWebContents() {
51 return scoped_ptr
<WebContents
>(
52 WebContentsTester::CreateTestWebContents(&browser_context_
, NULL
));
56 scoped_ptr
<content::NotificationService
> notification_service_
;
57 content::TestBrowserThreadBundle thread_bundle_
;
58 content::TestBrowserContext browser_context_
;
60 DISALLOW_COPY_AND_ASSIGN(GuestViewManagerTest
);
65 TEST_F(GuestViewManagerTest
, AddRemove
) {
66 content::TestBrowserContext browser_context
;
67 scoped_ptr
<guestview::TestGuestViewManager
> manager(
68 new guestview::TestGuestViewManager(&browser_context
));
70 scoped_ptr
<WebContents
> web_contents1(CreateWebContents());
71 scoped_ptr
<WebContents
> web_contents2(CreateWebContents());
72 scoped_ptr
<WebContents
> web_contents3(CreateWebContents());
74 EXPECT_EQ(0, manager
->last_instance_id_removed_for_testing());
76 EXPECT_TRUE(manager
->CanUseGuestInstanceID(1));
77 EXPECT_TRUE(manager
->CanUseGuestInstanceID(2));
78 EXPECT_TRUE(manager
->CanUseGuestInstanceID(3));
80 manager
->AddGuest(1, web_contents1
.get());
81 manager
->AddGuest(2, web_contents2
.get());
82 manager
->RemoveGuest(2);
84 // Since we removed 2, it would be an invalid ID.
85 EXPECT_TRUE(manager
->CanUseGuestInstanceID(1));
86 EXPECT_FALSE(manager
->CanUseGuestInstanceID(2));
87 EXPECT_TRUE(manager
->CanUseGuestInstanceID(3));
89 EXPECT_EQ(0, manager
->last_instance_id_removed_for_testing());
91 EXPECT_TRUE(manager
->CanUseGuestInstanceID(3));
93 manager
->AddGuest(3, web_contents3
.get());
94 manager
->RemoveGuest(1);
95 EXPECT_FALSE(manager
->CanUseGuestInstanceID(1));
96 EXPECT_FALSE(manager
->CanUseGuestInstanceID(2));
98 EXPECT_EQ(2, manager
->last_instance_id_removed_for_testing());
99 manager
->RemoveGuest(3);
100 EXPECT_EQ(3, manager
->last_instance_id_removed_for_testing());
102 EXPECT_FALSE(manager
->CanUseGuestInstanceID(1));
103 EXPECT_FALSE(manager
->CanUseGuestInstanceID(2));
104 EXPECT_FALSE(manager
->CanUseGuestInstanceID(3));
106 EXPECT_EQ(0u, manager
->GetRemovedInstanceIdSize());
109 } // namespace extensions