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 "components/guest_view/browser/test_guest_view_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "components/guest_view/browser/guest_view_manager_delegate.h"
10 namespace guest_view
{
12 TestGuestViewManager::TestGuestViewManager(
13 content::BrowserContext
* context
,
14 scoped_ptr
<GuestViewManagerDelegate
> delegate
)
15 : GuestViewManager(context
, delegate
.Pass()),
16 num_embedder_processes_destroyed_(0),
17 num_guests_created_(0),
18 expected_num_guests_created_(0),
19 num_views_garbage_collected_(0),
20 waiting_for_guests_created_(false) {
23 TestGuestViewManager::~TestGuestViewManager() {
26 size_t TestGuestViewManager::GetNumGuestsActive() const {
27 return guest_web_contents_by_instance_id_
.size();
30 size_t TestGuestViewManager::GetNumRemovedInstanceIDs() const {
31 return removed_instance_ids_
.size();
34 content::WebContents
* TestGuestViewManager::GetLastGuestCreated() {
35 content::WebContents
* web_contents
= nullptr;
36 for (int i
= current_instance_id_
; i
>= 0; i
--) {
37 web_contents
= GetGuestByInstanceID(i
);
45 void TestGuestViewManager::WaitForAllGuestsDeleted() {
46 // Make sure that every guest that was created has been removed.
47 for (auto& watcher
: guest_web_contents_watchers_
)
51 void TestGuestViewManager::WaitForLastGuestDeleted() {
52 // Wait for the last guest that was created to be deleted.
53 guest_web_contents_watchers_
.back()->Wait();
56 content::WebContents
* TestGuestViewManager::WaitForSingleGuestCreated() {
57 if (!GetNumGuestsActive()) {
58 // Guests have been created and subsequently destroyed.
59 if (num_guests_created() > 0)
61 WaitForNumGuestsCreated(1u);
64 return GetLastGuestCreated();
67 content::WebContents
* TestGuestViewManager::WaitForNextGuestCreated() {
68 created_message_loop_runner_
= new content::MessageLoopRunner();
69 created_message_loop_runner_
->Run();
70 return GetLastGuestCreated();
73 void TestGuestViewManager::WaitForNumGuestsCreated(size_t count
) {
74 if (count
== num_guests_created_
)
77 waiting_for_guests_created_
= true;
78 expected_num_guests_created_
= count
;
80 num_created_message_loop_runner_
= new content::MessageLoopRunner
;
81 num_created_message_loop_runner_
->Run();
84 void TestGuestViewManager::WaitForViewGarbageCollected() {
85 gc_message_loop_runner_
= new content::MessageLoopRunner
;
86 gc_message_loop_runner_
->Run();
89 void TestGuestViewManager::WaitForSingleViewGarbageCollected() {
90 if (!num_views_garbage_collected())
91 WaitForViewGarbageCollected();
94 void TestGuestViewManager::AddGuest(int guest_instance_id
,
95 content::WebContents
* guest_web_contents
) {
96 GuestViewManager::AddGuest(guest_instance_id
, guest_web_contents
);
98 guest_web_contents_watchers_
.push_back(
99 linked_ptr
<content::WebContentsDestroyedWatcher
>(
100 new content::WebContentsDestroyedWatcher(guest_web_contents
)));
102 if (created_message_loop_runner_
.get())
103 created_message_loop_runner_
->Quit();
105 ++num_guests_created_
;
106 if (!waiting_for_guests_created_
&&
107 num_guests_created_
!= expected_num_guests_created_
) {
111 if (num_created_message_loop_runner_
.get())
112 num_created_message_loop_runner_
->Quit();
115 void TestGuestViewManager::GetGuestWebContentsList(
116 std::vector
<content::WebContents
*>* guest_web_contents_list
) {
117 for (auto& watcher
: guest_web_contents_watchers_
)
118 guest_web_contents_list
->push_back(watcher
->web_contents());
121 void TestGuestViewManager::RemoveGuest(int guest_instance_id
) {
122 GuestViewManager::RemoveGuest(guest_instance_id
);
125 void TestGuestViewManager::EmbedderProcessDestroyed(int embedder_process_id
) {
126 ++num_embedder_processes_destroyed_
;
127 GuestViewManager::EmbedderProcessDestroyed(embedder_process_id
);
130 void TestGuestViewManager::ViewGarbageCollected(int embedder_process_id
,
131 int view_instance_id
) {
132 GuestViewManager::ViewGarbageCollected(embedder_process_id
, view_instance_id
);
133 ++num_views_garbage_collected_
;
134 if (gc_message_loop_runner_
.get())
135 gc_message_loop_runner_
->Quit();
138 // Test factory for creating test instances of GuestViewManager.
139 TestGuestViewManagerFactory::TestGuestViewManagerFactory()
140 : test_guest_view_manager_(NULL
) {
143 TestGuestViewManagerFactory::~TestGuestViewManagerFactory() {
146 GuestViewManager
* TestGuestViewManagerFactory::CreateGuestViewManager(
147 content::BrowserContext
* context
,
148 scoped_ptr
<GuestViewManagerDelegate
> delegate
) {
149 if (!test_guest_view_manager_
) {
150 test_guest_view_manager_
=
151 new TestGuestViewManager(context
, delegate
.Pass());
153 return test_guest_view_manager_
;
156 } // namespace guest_view