| 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 |
| 33 content::WebContents* TestGuestViewManager::GetLastGuestCreated() { | 39 content::WebContents* TestGuestViewManager::GetLastGuestCreated() { |
| 34 content::WebContents* web_contents = nullptr; | 40 content::WebContents* web_contents = nullptr; |
| 35 for (int i = current_instance_id_; i >= 0; i--) { | 41 for (int i = current_instance_id_; i >= 0; i--) { |
| 36 web_contents = GetGuestByInstanceID(i); | 42 web_contents = GetGuestByInstanceID(i); |
| 37 if (web_contents) { | 43 if (web_contents) { |
| 38 break; | 44 break; |
| 39 } | 45 } |
| 40 } | 46 } |
| 41 return web_contents; | 47 return web_contents; |
| 42 } | 48 } |
| 43 | 49 |
| 44 void TestGuestViewManager::WaitForAllGuestsDeleted() { | 50 void TestGuestViewManager::WaitForAllGuestsDeleted() { |
| 45 // Make sure that every guest that was created have been removed. | 51 // Make sure that every guest that was created have been removed. |
| 46 for (auto& watcher : guest_web_contents_watchers_) | 52 for (auto& watcher : guest_web_contents_watchers_) |
| 47 watcher->Wait(); | 53 watcher->Wait(); |
| 48 } | 54 } |
| 49 | 55 |
| 50 void TestGuestViewManager::WaitForGuestCreated() { | 56 void TestGuestViewManager::WaitForGuestCreated() { |
| 51 created_message_loop_runner_ = new content::MessageLoopRunner; | 57 created_message_loop_runner_ = new content::MessageLoopRunner; |
| 52 created_message_loop_runner_->Run(); | 58 created_message_loop_runner_->Run(); |
| 53 } | 59 } |
| 54 | 60 |
| 55 content::WebContents* TestGuestViewManager::WaitForSingleGuestCreated() { | 61 content::WebContents* TestGuestViewManager::WaitForSingleGuestCreated() { |
| 56 if (GetNumGuests() == 0) | 62 if (!GetNumGuestsActive()) { |
| 63 // Guests have been created and subsequently destroyed. |
| 64 if (num_guests_created() > 0) |
| 65 return nullptr; |
| 57 WaitForGuestCreated(); | 66 WaitForGuestCreated(); |
| 67 } |
| 58 | 68 |
| 59 return GetLastGuestCreated(); | 69 return GetLastGuestCreated(); |
| 60 } | 70 } |
| 61 | 71 |
| 62 void TestGuestViewManager::AddGuest(int guest_instance_id, | 72 void TestGuestViewManager::AddGuest(int guest_instance_id, |
| 63 content::WebContents* guest_web_contents) { | 73 content::WebContents* guest_web_contents) { |
| 64 GuestViewManager::AddGuest(guest_instance_id, guest_web_contents); | 74 GuestViewManager::AddGuest(guest_instance_id, guest_web_contents); |
| 65 | 75 |
| 66 guest_web_contents_watchers_.push_back( | 76 guest_web_contents_watchers_.push_back( |
| 67 linked_ptr<content::WebContentsDestroyedWatcher>( | 77 linked_ptr<content::WebContentsDestroyedWatcher>( |
| 68 new content::WebContentsDestroyedWatcher(guest_web_contents))); | 78 new content::WebContentsDestroyedWatcher(guest_web_contents))); |
| 69 | 79 |
| 80 ++num_guests_created_; |
| 81 |
| 70 if (created_message_loop_runner_.get()) | 82 if (created_message_loop_runner_.get()) |
| 71 created_message_loop_runner_->Quit(); | 83 created_message_loop_runner_->Quit(); |
| 72 } | 84 } |
| 73 | 85 |
| 74 void TestGuestViewManager::RemoveGuest(int guest_instance_id) { | 86 void TestGuestViewManager::RemoveGuest(int guest_instance_id) { |
| 75 GuestViewManager::RemoveGuest(guest_instance_id); | 87 GuestViewManager::RemoveGuest(guest_instance_id); |
| 76 } | 88 } |
| 77 | 89 |
| 78 // Test factory for creating test instances of GuestViewManager. | 90 // Test factory for creating test instances of GuestViewManager. |
| 79 TestGuestViewManagerFactory::TestGuestViewManagerFactory() | 91 TestGuestViewManagerFactory::TestGuestViewManagerFactory() |
| 80 : test_guest_view_manager_(NULL) { | 92 : test_guest_view_manager_(NULL) { |
| 81 } | 93 } |
| 82 | 94 |
| 83 TestGuestViewManagerFactory::~TestGuestViewManagerFactory() { | 95 TestGuestViewManagerFactory::~TestGuestViewManagerFactory() { |
| 84 } | 96 } |
| 85 | 97 |
| 86 GuestViewManager* TestGuestViewManagerFactory::CreateGuestViewManager( | 98 GuestViewManager* TestGuestViewManagerFactory::CreateGuestViewManager( |
| 87 content::BrowserContext* context) { | 99 content::BrowserContext* context, |
| 88 return GetManager(context); | 100 scoped_ptr<guestview::GuestViewManagerDelegate> delegate) { |
| 89 } | 101 if (!test_guest_view_manager_) { |
| 90 | 102 test_guest_view_manager_ = |
| 91 // This function gets called from GuestViewManager::FromBrowserContext(), | 103 new TestGuestViewManager(context, delegate.Pass()); |
| 92 // where test_guest_view_manager_ is assigned to a linked_ptr that takes care | 104 } |
| 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_; | 105 return test_guest_view_manager_; |
| 99 } | 106 } |
| 100 | 107 |
| 101 } // namespace extensions | 108 } // namespace extensions |
| OLD | NEW |