1 // Copyright (c) 2012 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 5 #ifndef CONTENT_TEST_TEST_WEB_CONTENTS_H_ 6 #define CONTENT_TEST_TEST_WEB_CONTENTS_H_ 7 8 #include <stdint.h> 9 10 #include <list> 11 #include <map> 12 #include <memory> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 17 #include "base/optional.h" 18 #include "base/unguessable_token.h" 19 #include "content/browser/site_instance_impl.h" 20 #include "content/browser/web_contents/web_contents_impl.h" 21 #include "content/public/test/web_contents_tester.h" 22 #include "content/test/test_render_frame_host.h" 23 #include "content/test/test_render_view_host.h" 24 #include "mojo/public/cpp/bindings/pending_remote.h" 25 #include "mojo/public/cpp/bindings/remote.h" 26 #include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom-forward.h" 27 #include "ui/base/page_transition_types.h" 28 29 class GURL; 30 class SkBitmap; 31 32 namespace gfx { 33 class Size; 34 } 35 36 namespace content { 37 38 struct Referrer; 39 class RenderViewHost; 40 class TestRenderViewHost; 41 class WebContentsTester; 42 43 // Subclass WebContentsImpl to ensure it creates TestRenderViewHosts 44 // and does not do anything involving views. 45 class TestWebContents : public WebContentsImpl, public WebContentsTester { 46 public: 47 ~TestWebContents() override; 48 49 static std::unique_ptr<TestWebContents> Create( 50 BrowserContext* browser_context, 51 scoped_refptr<SiteInstance> instance); 52 static TestWebContents* Create(const CreateParams& params); 53 54 // WebContentsImpl overrides (returning the same values, but in Test* types) 55 TestRenderFrameHost* GetMainFrame() override; 56 TestRenderViewHost* GetRenderViewHost() override; 57 // Overrides to avoid establishing Mojo connection with renderer process. 58 int DownloadImage(const GURL& url, 59 bool is_favicon, 60 uint32_t preferred_size, 61 uint32_t max_bitmap_size, 62 bool bypass_cache, 63 ImageDownloadCallback callback) override; 64 const GURL& GetLastCommittedURL() override; 65 const base::string16& GetTitle() override; 66 67 // WebContentsTester implementation. 68 void CommitPendingNavigation() override; 69 TestRenderFrameHost* GetPendingMainFrame() override; 70 71 void NavigateAndCommit( 72 const GURL& url, 73 ui::PageTransition transition = ui::PAGE_TRANSITION_LINK) override; 74 75 void NavigateAndFail(const GURL& url, int error_code) override; 76 void TestSetIsLoading(bool value) override; 77 void TestDidNavigate(RenderFrameHost* render_frame_host, 78 int nav_entry_id, 79 bool did_create_new_entry, 80 const GURL& url, 81 ui::PageTransition transition) override; 82 void TestDidNavigateWithSequenceNumber(RenderFrameHost* render_frame_host, 83 int nav_entry_id, 84 bool did_create_new_entry, 85 const GURL& url, 86 const Referrer& referrer, 87 ui::PageTransition transition, 88 bool was_within_same_document, 89 int item_sequence_number, 90 int document_sequence_number); 91 void SetOpener(WebContents* opener) override; 92 const std::string& GetSaveFrameHeaders() override; 93 const base::string16& GetSuggestedFileName() override; 94 bool HasPendingDownloadImage(const GURL& url) override; 95 bool TestDidDownloadImage( 96 const GURL& url, 97 int http_status_code, 98 const std::vector<SkBitmap>& bitmaps, 99 const std::vector<gfx::Size>& original_bitmap_sizes) override; 100 void SetLastCommittedURL(const GURL& url) override; 101 void SetTitle(const base::string16& new_title) override; 102 void SetMainFrameMimeType(const std::string& mime_type) override; 103 const std::string& GetContentsMimeType() override; 104 void SetIsCurrentlyAudible(bool audible) override; 105 void TestDidReceiveMouseDownEvent() override; 106 void TestDidFinishLoad(const GURL& url) override; 107 void TestDidFailLoadWithError(const GURL& url, int error_code) override; 108 109 // True if a cross-site navigation is pending. 110 bool CrossProcessNavigationPending(); 111 112 // Prevent interaction with views. 113 bool CreateRenderViewForRenderManager( 114 RenderViewHost* render_view_host, 115 const base::Optional<base::UnguessableToken>& opener_frame_token, 116 int proxy_routing_id) override; 117 118 // Returns a clone of this TestWebContents. The returned object is also a 119 // TestWebContents. The caller owns the returned object. 120 std::unique_ptr<WebContents> Clone() override; 121 122 // Allow mocking of the RenderViewHostDelegateView. 123 RenderViewHostDelegateView* GetDelegateView() override; set_delegate_view(RenderViewHostDelegateView * view)124 void set_delegate_view(RenderViewHostDelegateView* view) { 125 delegate_view_override_ = view; 126 } 127 128 // Allows us to simulate that a contents was created via CreateNewWindow. 129 void AddPendingContents(std::unique_ptr<WebContentsImpl> contents, 130 const GURL& target_url); 131 132 // Establish expected arguments for |SetHistoryOffsetAndLength()|. When 133 // |SetHistoryOffsetAndLength()| is called, the arguments are compared 134 // with the expected arguments specified here. 135 void ExpectSetHistoryOffsetAndLength(int history_offset, 136 int history_length); 137 138 // Compares the arguments passed in with the expected arguments passed in 139 // to |ExpectSetHistoryOffsetAndLength()|. 140 void SetHistoryOffsetAndLength(int history_offset, 141 int history_length) override; 142 143 bool GetPauseSubresourceLoadingCalled() override; 144 145 void ResetPauseSubresourceLoadingCalled() override; 146 147 void SetLastActiveTime(base::TimeTicks last_active_time) override; 148 149 void TestIncrementBluetoothConnectedDeviceCount() override; 150 void TestDecrementBluetoothConnectedDeviceCount() override; 151 152 base::UnguessableToken GetAudioGroupId() override; 153 154 const blink::PortalToken& CreatePortal( 155 std::unique_ptr<WebContents> portal_web_contents) override; 156 WebContents* GetPortalContents(const blink::PortalToken&) override; 157 158 void OnWebPreferencesChanged() override; 159 160 // If set, *web_preferences_changed_counter_ is incremented when 161 // OnWebPreferencesChanged() is called. set_web_preferences_changed_counter(int * counter)162 void set_web_preferences_changed_counter(int* counter) { 163 web_preferences_changed_counter_ = counter; 164 } 165 166 protected: 167 // The deprecated WebContentsTester still needs to subclass this. 168 explicit TestWebContents(BrowserContext* browser_context); 169 170 private: 171 // WebContentsImpl overrides 172 RenderFrameHostDelegate* CreateNewWindow( 173 RenderFrameHost* opener, 174 const mojom::CreateNewWindowParams& params, 175 bool is_new_browsing_instance, 176 bool has_user_gesture, 177 SessionStorageNamespace* session_storage_namespace) override; 178 RenderWidgetHostImpl* CreateNewPopupWidget( 179 AgentSchedulingGroupHost& agent_scheduling_group, 180 int32_t route_id, 181 mojo::PendingAssociatedReceiver<blink::mojom::PopupWidgetHost> 182 blink_popup_widget_host, 183 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> 184 blink_widget_host, 185 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) 186 override; 187 void ShowCreatedWindow(RenderFrameHost* opener, 188 int route_id, 189 WindowOpenDisposition disposition, 190 const gfx::Rect& initial_rect, 191 bool user_gesture) override; 192 void ShowCreatedWidget(int process_id, 193 int route_id, 194 const gfx::Rect& initial_rect) override; 195 void SaveFrameWithHeaders(const GURL& url, 196 const Referrer& referrer, 197 const std::string& headers, 198 const base::string16& suggested_filename) override; ReattachToOuterWebContentsFrame()199 void ReattachToOuterWebContentsFrame() override {} 200 201 RenderViewHostDelegateView* delegate_view_override_; 202 203 // See set_web_preferences_changed_counter() above. May be nullptr. 204 int* web_preferences_changed_counter_; 205 // Expectations for arguments of |SetHistoryOffsetAndLength()|. 206 bool expect_set_history_offset_and_length_; 207 int expect_set_history_offset_and_length_history_offset_; 208 int expect_set_history_offset_and_length_history_length_; 209 std::string save_frame_headers_; 210 base::string16 suggested_filename_; 211 // Map keyed by image URL. Values are <id, callback> pairs. 212 std::map<GURL, std::list<std::pair<int, ImageDownloadCallback>>> 213 pending_image_downloads_; 214 GURL last_committed_url_; 215 base::Optional<base::string16> title_; 216 bool pause_subresource_loading_called_; 217 base::UnguessableToken audio_group_id_; 218 }; 219 220 } // namespace content 221 222 #endif // CONTENT_TEST_TEST_WEB_CONTENTS_H_ 223