Makes sure that all the guest WebContents are destroyed when exiting the tests.
[chromium-blink-merge.git] / extensions / browser / guest_view / test_guest_view_manager.cc
blobb52cb27f4b2f224c0f855452b99a317996b30f62
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/test_guest_view_manager.h"
7 #include "base/path_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "content/public/test/browser_test_utils.h"
10 #include "extensions/browser/app_window/app_window.h"
11 #include "extensions/browser/app_window/app_window_registry.h"
12 #include "extensions/browser/extension_host.h"
13 #include "extensions/browser/process_manager.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/extension_paths.h"
16 #include "extensions/shell/browser/shell_extension_system.h"
17 #include "extensions/shell/test/shell_test.h"
18 #include "extensions/test/extension_test_message_listener.h"
20 namespace extensions {
22 TestGuestViewManager::TestGuestViewManager(content::BrowserContext* context)
23 : GuestViewManager(context) {
26 TestGuestViewManager::~TestGuestViewManager() {
29 int TestGuestViewManager::GetNumGuests() const {
30 return guest_web_contents_by_instance_id_.size();
33 content::WebContents* TestGuestViewManager::GetLastGuestCreated() {
34 content::WebContents* web_contents = nullptr;
35 for (int i = current_instance_id_; i >= 0; i--) {
36 web_contents = GetGuestByInstanceID(i);
37 if (web_contents) {
38 break;
41 return web_contents;
44 void TestGuestViewManager::WaitForAllGuestsDeleted() {
45 // Make sure that every guest that was created have been removed.
46 for (auto& watcher : guest_web_contents_watchers_)
47 watcher->Wait();
50 void TestGuestViewManager::WaitForGuestCreated() {
51 created_message_loop_runner_ = new content::MessageLoopRunner;
52 created_message_loop_runner_->Run();
55 content::WebContents* TestGuestViewManager::WaitForSingleGuestCreated() {
56 if (GetNumGuests() == 0)
57 WaitForGuestCreated();
59 return GetLastGuestCreated();
62 void TestGuestViewManager::AddGuest(int guest_instance_id,
63 content::WebContents* guest_web_contents) {
64 GuestViewManager::AddGuest(guest_instance_id, guest_web_contents);
66 guest_web_contents_watchers_.push_back(
67 linked_ptr<content::WebContentsDestroyedWatcher>(
68 new content::WebContentsDestroyedWatcher(guest_web_contents)));
70 if (created_message_loop_runner_.get())
71 created_message_loop_runner_->Quit();
74 void TestGuestViewManager::RemoveGuest(int guest_instance_id) {
75 GuestViewManager::RemoveGuest(guest_instance_id);
78 // Test factory for creating test instances of GuestViewManager.
79 TestGuestViewManagerFactory::TestGuestViewManagerFactory()
80 : test_guest_view_manager_(NULL) {
83 TestGuestViewManagerFactory::~TestGuestViewManagerFactory() {
86 GuestViewManager* TestGuestViewManagerFactory::CreateGuestViewManager(
87 content::BrowserContext* context) {
88 return GetManager(context);
91 // This function gets called from GuestViewManager::FromBrowserContext(),
92 // where test_guest_view_manager_ is assigned to a linked_ptr that takes care
93 // of deleting it.
94 TestGuestViewManager* TestGuestViewManagerFactory::GetManager(
95 content::BrowserContext* context) {
96 DCHECK(!test_guest_view_manager_);
97 test_guest_view_manager_ = new TestGuestViewManager(context);
98 return test_guest_view_manager_;
101 } // namespace extensions