test_guest_view_manager.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.
  4. #ifndef COMPONENTS_GUEST_VIEW_BROWSER_TEST_GUEST_VIEW_MANAGER_H_
  5. #define COMPONENTS_GUEST_VIEW_BROWSER_TEST_GUEST_VIEW_MANAGER_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "base/bind.h"
  9. #include "base/gtest_prod_util.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "components/guest_view/browser/guest_view_manager.h"
  12. #include "components/guest_view/browser/guest_view_manager_factory.h"
  13. #include "content/public/browser/render_frame_host.h"
  14. #include "content/public/test/browser_test_utils.h"
  15. namespace guest_view {
  16. class TestGuestViewManager : public GuestViewManager {
  17. public:
  18. TestGuestViewManager(content::BrowserContext* context,
  19. std::unique_ptr<GuestViewManagerDelegate> delegate);
  20. TestGuestViewManager(const TestGuestViewManager&) = delete;
  21. TestGuestViewManager& operator=(const TestGuestViewManager&) = delete;
  22. ~TestGuestViewManager() override;
  23. void WaitForAllGuestsDeleted();
  24. void WaitForLastGuestDeleted();
  25. // While the GuestViewBase directly represents a guest view, the
  26. // RenderFrameHost version exposes the guest view's main frame for the ease of
  27. // testing.
  28. //
  29. // All the WebContents versions APIs (here and on) will be removed during the
  30. // MPArch migration. Consider using GuestViewBase or RenderFrameHost versions,
  31. // unless necessary.
  32. //
  33. // TODO(crbug.com/1261928): Remove all the WebContents version.
  34. GuestViewBase* WaitForSingleGuestViewCreated();
  35. content::RenderFrameHost* WaitForSingleGuestRenderFrameHostCreated();
  36. content::WebContents* DeprecatedWaitForSingleGuestCreated();
  37. GuestViewBase* WaitForNextGuestViewCreated();
  38. content::RenderFrameHost* WaitForNextGuestRenderFrameHostCreated();
  39. content::WebContents* DeprecatedWaitForNextGuestCreated();
  40. void WaitForNumGuestsCreated(size_t count);
  41. void WaitForSingleViewGarbageCollected();
  42. GuestViewBase* GetLastGuestViewCreated();
  43. content::RenderFrameHost* GetLastGuestRenderFrameHostCreated();
  44. content::WebContents* DeprecatedGetLastGuestCreated();
  45. void WaitUntilAttached(GuestViewBase* guest_view);
  46. // Returns the number of guests currently still alive at the time of calling
  47. // this method.
  48. size_t GetNumGuestsActive() const;
  49. // Returns the size of the set of removed instance IDs.
  50. size_t GetNumRemovedInstanceIDs() const;
  51. // Returns the number of times EmbedderWillBeDestroyed() was called.
  52. int num_embedder_processes_destroyed() const {
  53. return num_embedder_processes_destroyed_;
  54. }
  55. // Returns the number of guests that have been created since the creation of
  56. // this GuestViewManager.
  57. size_t num_guests_created() const { return num_guests_created_; }
  58. // Returns the number of GuestViews that have been garbage collected in
  59. // JavaScript since the creation of this GuestViewManager.
  60. int num_views_garbage_collected() const {
  61. return num_views_garbage_collected_;
  62. }
  63. // Returns the last guest instance ID removed from the manager.
  64. int last_instance_id_removed() const { return last_instance_id_removed_; }
  65. // Returns the list of guests that were created by this manager.
  66. void DeprecatedGetGuestWebContentsList(
  67. std::vector<content::WebContents*>* guest_web_contents_list);
  68. void GetGuestRenderFrameHostList(
  69. std::vector<content::RenderFrameHost*>* guest_render_frame_host_list);
  70. private:
  71. FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest, AddRemove);
  72. // guest_view::GuestViewManager:
  73. void AddGuest(int guest_instance_id,
  74. content::WebContents* guest_web_contents) override;
  75. void EmbedderProcessDestroyed(int embedder_process_id) override;
  76. void ViewGarbageCollected(int embedder_process_id,
  77. int view_instance_id) override;
  78. void AttachGuest(int embedder_process_id,
  79. int element_instance_id,
  80. int guest_instance_id,
  81. const base::Value::Dict& attach_params) override;
  82. void WaitForViewGarbageCollected();
  83. using GuestViewManager::last_instance_id_removed_;
  84. using GuestViewManager::removed_instance_ids_;
  85. int num_embedder_processes_destroyed_;
  86. size_t num_guests_created_;
  87. size_t expected_num_guests_created_;
  88. int num_views_garbage_collected_;
  89. bool waiting_for_guests_created_;
  90. // Tracks the life time of the GuestView's main FrameTreeNode. The main FTN
  91. // has the same lifesspan as the GuestView.
  92. std::vector<std::unique_ptr<content::FrameDeletedObserver>>
  93. guest_view_watchers_;
  94. std::unique_ptr<base::RunLoop> created_run_loop_;
  95. std::unique_ptr<base::RunLoop> num_created_run_loop_;
  96. raw_ptr<GuestViewBase> waiting_for_attach_;
  97. std::unique_ptr<base::RunLoop> attached_run_loop_;
  98. std::unique_ptr<base::RunLoop> gc_run_loop_;
  99. };
  100. // Test factory for creating test instances of GuestViewManager.
  101. class TestGuestViewManagerFactory : public GuestViewManagerFactory {
  102. public:
  103. TestGuestViewManagerFactory();
  104. TestGuestViewManagerFactory(const TestGuestViewManagerFactory&) = delete;
  105. TestGuestViewManagerFactory& operator=(const TestGuestViewManagerFactory&) =
  106. delete;
  107. ~TestGuestViewManagerFactory() override;
  108. GuestViewManager* CreateGuestViewManager(
  109. content::BrowserContext* context,
  110. std::unique_ptr<GuestViewManagerDelegate> delegate) override;
  111. private:
  112. raw_ptr<TestGuestViewManager> test_guest_view_manager_;
  113. };
  114. } // namespace guest_view
  115. #endif // COMPONENTS_GUEST_VIEW_BROWSER_TEST_GUEST_VIEW_MANAGER_H_