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 #ifndef COMPONENTS_GUEST_VIEW_BROWSER_TEST_GUEST_VIEW_MANAGER_H_
6 #define COMPONENTS_GUEST_VIEW_BROWSER_TEST_GUEST_VIEW_MANAGER_H_
8 #include "base/memory/linked_ptr.h"
9 #include "components/guest_view/browser/guest_view_manager.h"
10 #include "components/guest_view/browser/guest_view_manager_factory.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/test_utils.h"
14 namespace guest_view
{
16 class TestGuestViewManager
: public GuestViewManager
{
19 content::BrowserContext
* context
,
20 scoped_ptr
<GuestViewManagerDelegate
> delegate
);
21 ~TestGuestViewManager() override
;
23 void WaitForAllGuestsDeleted();
25 void WaitForLastGuestDeleted();
27 content::WebContents
* WaitForSingleGuestCreated();
28 content::WebContents
* WaitForNextGuestCreated();
29 void WaitForNumGuestsCreated(size_t count
);
31 void WaitForSingleViewGarbageCollected();
33 content::WebContents
* GetLastGuestCreated();
35 // Returns the number of guests currently still alive at the time of calling
37 size_t GetNumGuestsActive() const;
39 // Returns the size of the set of removed instance IDs.
40 size_t GetNumRemovedInstanceIDs() const;
42 using GuestViewCreateFunction
=
43 base::Callback
<GuestViewBase
*(content::WebContents
*)>;
46 void RegisterTestGuestViewType(
47 const GuestViewCreateFunction
& create_function
) {
48 auto registry_entry
= std::make_pair(
50 GuestViewData(create_function
, base::Bind(&T::CleanUp
)));
51 guest_view_registry_
.insert(registry_entry
);
54 // Returns the number of times EmbedderWillBeDestroyed() was called.
55 int num_embedder_processes_destroyed() const {
56 return num_embedder_processes_destroyed_
;
59 // Returns the number of guests that have been created since the creation of
60 // this GuestViewManager.
61 size_t num_guests_created() const { return num_guests_created_
; }
63 // Returns the number of GuestViews that have been garbage collected in
64 // JavaScript since the creation of this GuestViewManager.
65 int num_views_garbage_collected() const {
66 return num_views_garbage_collected_
;
69 // Returns the last guest instance ID removed from the manager.
70 int last_instance_id_removed() const { return last_instance_id_removed_
; }
72 // Returns the list of guests WebContentses that were created by this
74 void GetGuestWebContentsList(
75 std::vector
<content::WebContents
*>* guest_web_contents_list
);
78 FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest
, AddRemove
);
80 // GuestViewManager override:
81 void AddGuest(int guest_instance_id
,
82 content::WebContents
* guest_web_contents
) override
;
83 void RemoveGuest(int guest_instance_id
) override
;
84 void EmbedderProcessDestroyed(int embedder_process_id
) override
;
85 void ViewGarbageCollected(int embedder_process_id
,
86 int view_instance_id
) override
;
88 void WaitForViewGarbageCollected();
90 using GuestViewManager::last_instance_id_removed_
;
91 using GuestViewManager::removed_instance_ids_
;
93 int num_embedder_processes_destroyed_
;
94 size_t num_guests_created_
;
95 size_t expected_num_guests_created_
;
96 int num_views_garbage_collected_
;
97 bool waiting_for_guests_created_
;
99 std::vector
<linked_ptr
<content::WebContentsDestroyedWatcher
>>
100 guest_web_contents_watchers_
;
101 scoped_refptr
<content::MessageLoopRunner
> created_message_loop_runner_
;
102 scoped_refptr
<content::MessageLoopRunner
> num_created_message_loop_runner_
;
103 scoped_refptr
<content::MessageLoopRunner
> gc_message_loop_runner_
;
105 DISALLOW_COPY_AND_ASSIGN(TestGuestViewManager
);
108 // Test factory for creating test instances of GuestViewManager.
109 class TestGuestViewManagerFactory
: public GuestViewManagerFactory
{
111 TestGuestViewManagerFactory();
112 ~TestGuestViewManagerFactory() override
;
114 GuestViewManager
* CreateGuestViewManager(
115 content::BrowserContext
* context
,
116 scoped_ptr
<GuestViewManagerDelegate
> delegate
) override
;
119 TestGuestViewManager
* test_guest_view_manager_
;
121 DISALLOW_COPY_AND_ASSIGN(TestGuestViewManagerFactory
);
124 } // namespace guest_view
126 #endif // COMPONENTS_GUEST_VIEW_BROWSER_TEST_GUEST_VIEW_MANAGER_H_