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.
4
5#include "extensions/browser/guest_view/guest_view_manager.h"
6
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"
12
13using content::WebContents;
14using content::WebContentsTester;
15
16namespace extensions {
17
18namespace guestview {
19
20// This class allows us to access some private variables in
21// GuestViewManager.
22class TestGuestViewManager : public GuestViewManager {
23 public:
24  explicit TestGuestViewManager(content::BrowserContext* context)
25      : GuestViewManager(context) {}
26
27  int last_instance_id_removed_for_testing() {
28    return last_instance_id_removed_;
29  }
30
31  size_t GetRemovedInstanceIdSize() { return removed_instance_ids_.size(); }
32
33 private:
34  using GuestViewManager::last_instance_id_removed_;
35  using GuestViewManager::removed_instance_ids_;
36
37  DISALLOW_COPY_AND_ASSIGN(TestGuestViewManager);
38};
39
40} // namespace guestview
41
42namespace {
43
44class GuestViewManagerTest : public extensions::ExtensionsTest {
45 public:
46  GuestViewManagerTest() :
47    notification_service_(content::NotificationService::Create()) {}
48  virtual ~GuestViewManagerTest() {}
49
50  scoped_ptr<WebContents> CreateWebContents() {
51    return scoped_ptr<WebContents>(
52        WebContentsTester::CreateTestWebContents(&browser_context_, NULL));
53  }
54
55 private:
56  scoped_ptr<content::NotificationService> notification_service_;
57  content::TestBrowserThreadBundle thread_bundle_;
58  content::TestBrowserContext browser_context_;
59
60  DISALLOW_COPY_AND_ASSIGN(GuestViewManagerTest);
61};
62
63}  // namespace
64
65TEST_F(GuestViewManagerTest, AddRemove) {
66  content::TestBrowserContext browser_context;
67  scoped_ptr<guestview::TestGuestViewManager> manager(
68      new guestview::TestGuestViewManager(&browser_context));
69
70  scoped_ptr<WebContents> web_contents1(CreateWebContents());
71  scoped_ptr<WebContents> web_contents2(CreateWebContents());
72  scoped_ptr<WebContents> web_contents3(CreateWebContents());
73
74  EXPECT_EQ(0, manager->last_instance_id_removed_for_testing());
75
76  EXPECT_TRUE(manager->CanUseGuestInstanceID(1));
77  EXPECT_TRUE(manager->CanUseGuestInstanceID(2));
78  EXPECT_TRUE(manager->CanUseGuestInstanceID(3));
79
80  manager->AddGuest(1, web_contents1.get());
81  manager->AddGuest(2, web_contents2.get());
82  manager->RemoveGuest(2);
83
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));
88
89  EXPECT_EQ(0, manager->last_instance_id_removed_for_testing());
90
91  EXPECT_TRUE(manager->CanUseGuestInstanceID(3));
92
93  manager->AddGuest(3, web_contents3.get());
94  manager->RemoveGuest(1);
95  EXPECT_FALSE(manager->CanUseGuestInstanceID(1));
96  EXPECT_FALSE(manager->CanUseGuestInstanceID(2));
97
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());
101
102  EXPECT_FALSE(manager->CanUseGuestInstanceID(1));
103  EXPECT_FALSE(manager->CanUseGuestInstanceID(2));
104  EXPECT_FALSE(manager->CanUseGuestInstanceID(3));
105
106  EXPECT_EQ(0u, manager->GetRemovedInstanceIdSize());
107}
108
109}  // namespace extensions
110