OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/guest_view/test_guest_view_manager.h" | 5 #include "extensions/browser/guest_view/test_guest_view_manager.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "content/public/test/browser_test_utils.h" | 9 #include "content/public/test/browser_test_utils.h" |
10 #include "extensions/browser/app_window/app_window.h" | 10 #include "extensions/browser/app_window/app_window.h" |
11 #include "extensions/browser/app_window/app_window_registry.h" | 11 #include "extensions/browser/app_window/app_window_registry.h" |
12 #include "extensions/browser/extension_host.h" | 12 #include "extensions/browser/extension_host.h" |
| 13 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h
" |
13 #include "extensions/browser/process_manager.h" | 14 #include "extensions/browser/process_manager.h" |
14 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
15 #include "extensions/common/extension_paths.h" | 16 #include "extensions/common/extension_paths.h" |
16 #include "extensions/shell/browser/shell_extension_system.h" | 17 #include "extensions/shell/browser/shell_extension_system.h" |
17 #include "extensions/shell/test/shell_test.h" | 18 #include "extensions/shell/test/shell_test.h" |
18 #include "extensions/test/extension_test_message_listener.h" | 19 #include "extensions/test/extension_test_message_listener.h" |
19 | 20 |
| 21 using guestview::GuestViewManagerDelegate; |
| 22 |
20 namespace extensions { | 23 namespace extensions { |
21 | 24 |
22 TestGuestViewManager::TestGuestViewManager(content::BrowserContext* context) | 25 TestGuestViewManager::TestGuestViewManager( |
23 : GuestViewManager(context) { | 26 content::BrowserContext* context, |
| 27 scoped_ptr<GuestViewManagerDelegate> delegate) |
| 28 : GuestViewManager(context, delegate.Pass()), |
| 29 num_guests_created_(0) { |
24 } | 30 } |
25 | 31 |
26 TestGuestViewManager::~TestGuestViewManager() { | 32 TestGuestViewManager::~TestGuestViewManager() { |
27 } | 33 } |
28 | 34 |
29 int TestGuestViewManager::GetNumGuests() const { | 35 int TestGuestViewManager::GetNumGuestsActive() const { |
30 return guest_web_contents_by_instance_id_.size(); | 36 return guest_web_contents_by_instance_id_.size(); |
31 } | 37 } |
32 | 38 |
| 39 int TestGuestViewManager::GetNumRemovedInstanceIDs() const { |
| 40 return removed_instance_ids_.size(); |
| 41 } |
| 42 |
33 content::WebContents* TestGuestViewManager::GetLastGuestCreated() { | 43 content::WebContents* TestGuestViewManager::GetLastGuestCreated() { |
34 content::WebContents* web_contents = nullptr; | 44 content::WebContents* web_contents = nullptr; |
35 for (int i = current_instance_id_; i >= 0; i--) { | 45 for (int i = current_instance_id_; i >= 0; i--) { |
36 web_contents = GetGuestByInstanceID(i); | 46 web_contents = GetGuestByInstanceID(i); |
37 if (web_contents) { | 47 if (web_contents) { |
38 break; | 48 break; |
39 } | 49 } |
40 } | 50 } |
41 return web_contents; | 51 return web_contents; |
42 } | 52 } |
43 | 53 |
44 void TestGuestViewManager::WaitForAllGuestsDeleted() { | 54 void TestGuestViewManager::WaitForAllGuestsDeleted() { |
45 // Make sure that every guest that was created have been removed. | 55 // Make sure that every guest that was created have been removed. |
46 for (auto& watcher : guest_web_contents_watchers_) | 56 for (auto& watcher : guest_web_contents_watchers_) |
47 watcher->Wait(); | 57 watcher->Wait(); |
48 } | 58 } |
49 | 59 |
50 void TestGuestViewManager::WaitForGuestCreated() { | 60 void TestGuestViewManager::WaitForGuestCreated() { |
51 created_message_loop_runner_ = new content::MessageLoopRunner; | 61 created_message_loop_runner_ = new content::MessageLoopRunner; |
52 created_message_loop_runner_->Run(); | 62 created_message_loop_runner_->Run(); |
53 } | 63 } |
54 | 64 |
55 content::WebContents* TestGuestViewManager::WaitForSingleGuestCreated() { | 65 content::WebContents* TestGuestViewManager::WaitForSingleGuestCreated() { |
56 if (GetNumGuests() == 0) | 66 if (!GetNumGuestsActive()) { |
| 67 // Guests have been created and subsequently destroyed. |
| 68 if (num_guests_created() > 0) |
| 69 return nullptr; |
57 WaitForGuestCreated(); | 70 WaitForGuestCreated(); |
| 71 } |
58 | 72 |
59 return GetLastGuestCreated(); | 73 return GetLastGuestCreated(); |
60 } | 74 } |
61 | 75 |
62 void TestGuestViewManager::AddGuest(int guest_instance_id, | 76 void TestGuestViewManager::AddGuest(int guest_instance_id, |
63 content::WebContents* guest_web_contents) { | 77 content::WebContents* guest_web_contents) { |
64 GuestViewManager::AddGuest(guest_instance_id, guest_web_contents); | 78 GuestViewManager::AddGuest(guest_instance_id, guest_web_contents); |
65 | 79 |
66 guest_web_contents_watchers_.push_back( | 80 guest_web_contents_watchers_.push_back( |
67 linked_ptr<content::WebContentsDestroyedWatcher>( | 81 linked_ptr<content::WebContentsDestroyedWatcher>( |
68 new content::WebContentsDestroyedWatcher(guest_web_contents))); | 82 new content::WebContentsDestroyedWatcher(guest_web_contents))); |
69 | 83 |
| 84 ++num_guests_created_; |
| 85 |
70 if (created_message_loop_runner_.get()) | 86 if (created_message_loop_runner_.get()) |
71 created_message_loop_runner_->Quit(); | 87 created_message_loop_runner_->Quit(); |
72 } | 88 } |
73 | 89 |
74 void TestGuestViewManager::RemoveGuest(int guest_instance_id) { | 90 void TestGuestViewManager::RemoveGuest(int guest_instance_id) { |
75 GuestViewManager::RemoveGuest(guest_instance_id); | 91 GuestViewManager::RemoveGuest(guest_instance_id); |
76 } | 92 } |
77 | 93 |
78 // Test factory for creating test instances of GuestViewManager. | 94 // Test factory for creating test instances of GuestViewManager. |
79 TestGuestViewManagerFactory::TestGuestViewManagerFactory() | 95 TestGuestViewManagerFactory::TestGuestViewManagerFactory() |
80 : test_guest_view_manager_(NULL) { | 96 : test_guest_view_manager_(NULL) { |
81 } | 97 } |
82 | 98 |
83 TestGuestViewManagerFactory::~TestGuestViewManagerFactory() { | 99 TestGuestViewManagerFactory::~TestGuestViewManagerFactory() { |
84 } | 100 } |
85 | 101 |
86 GuestViewManager* TestGuestViewManagerFactory::CreateGuestViewManager( | 102 GuestViewManager* TestGuestViewManagerFactory::CreateGuestViewManager( |
87 content::BrowserContext* context) { | 103 content::BrowserContext* context, |
88 return GetManager(context); | 104 scoped_ptr<guestview::GuestViewManagerDelegate> delegate) { |
89 } | 105 if (!test_guest_view_manager_) { |
90 | 106 test_guest_view_manager_ = |
91 // This function gets called from GuestViewManager::FromBrowserContext(), | 107 new TestGuestViewManager(context, delegate.Pass()); |
92 // where test_guest_view_manager_ is assigned to a linked_ptr that takes care | 108 } |
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_; | 109 return test_guest_view_manager_; |
99 } | 110 } |
100 | 111 |
101 } // namespace extensions | 112 } // namespace extensions |
OLD | NEW |