Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/renderer_host/page_impl.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 8 | #include "base/memory/weak_ptr.h" |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 9 | #include "base/test/scoped_feature_list.h" |
John Abd-El-Malek | ea3ba3c | 2021-06-14 12:05:48 | [diff] [blame] | 10 | #include "build/build_config.h" |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 11 | #include "content/browser/renderer_host/frame_tree.h" |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 12 | #include "content/browser/renderer_host/frame_tree_node.h" |
| 13 | #include "content/browser/renderer_host/navigation_request.h" |
| 14 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 15 | #include "content/browser/web_contents/web_contents_impl.h" |
| 16 | #include "content/common/content_navigation_policy.h" |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 17 | #include "content/public/browser/page.h" |
| 18 | #include "content/public/browser/page_user_data.h" |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 19 | #include "content/public/browser/render_frame_host.h" |
| 20 | #include "content/public/browser/web_contents.h" |
| 21 | #include "content/public/common/content_features.h" |
| 22 | #include "content/public/test/back_forward_cache_util.h" |
| 23 | #include "content/public/test/browser_test.h" |
| 24 | #include "content/public/test/browser_test_utils.h" |
| 25 | #include "content/public/test/content_browser_test.h" |
| 26 | #include "content/public/test/content_browser_test_utils.h" |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 27 | #include "content/public/test/mock_web_contents_observer.h" |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 28 | #include "content/public/test/prerender_test_util.h" |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 29 | #include "content/public/test/render_frame_host_test_support.h" |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 30 | #include "content/public/test/test_navigation_observer.h" |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 31 | #include "content/public/test/test_utils.h" |
| 32 | #include "content/public/test/url_loader_interceptor.h" |
| 33 | #include "content/shell/browser/shell.h" |
| 34 | #include "content/test/content_browser_test_utils_internal.h" |
| 35 | #include "net/dns/mock_host_resolver.h" |
| 36 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 37 | |
| 38 | namespace content { |
| 39 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | int next_id = 0; |
| 43 | |
| 44 | // Example class which inherits the PageUserData, all the data is |
| 45 | // associated to the lifetime of the page. |
| 46 | class Data : public PageUserData<Data> { |
| 47 | public: |
| 48 | ~Data() override; |
| 49 | |
| 50 | base::WeakPtr<Data> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); } |
| 51 | |
| 52 | int unique_id() { return unique_id_; } |
| 53 | |
| 54 | private: |
| 55 | explicit Data(Page& page) : PageUserData(page) { unique_id_ = ++next_id; } |
| 56 | |
| 57 | friend class content::PageUserData<Data>; |
| 58 | |
| 59 | int unique_id_; |
| 60 | |
| 61 | base::WeakPtrFactory<Data> weak_ptr_factory_{this}; |
| 62 | |
| 63 | PAGE_USER_DATA_KEY_DECL(); |
| 64 | }; |
| 65 | |
Daniel Cheng | 383df85 | 2021-10-02 03:28:01 | [diff] [blame] | 66 | PAGE_USER_DATA_KEY_IMPL(Data); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 67 | |
| 68 | Data::~Data() { |
| 69 | // Both Page and RenderFrameHost should be non-null and valid before Data |
| 70 | // deletion, as they will be destroyed after PageUserData destruction. |
| 71 | EXPECT_TRUE(&page()); |
| 72 | EXPECT_TRUE(&(page().GetMainDocument())); |
| 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 77 | class PageImplTest : public ContentBrowserTest { |
| 78 | public: |
| 79 | ~PageImplTest() override = default; |
| 80 | |
| 81 | protected: |
| 82 | void SetUpOnMainThread() override { |
| 83 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 84 | ContentBrowserTest::SetUpOnMainThread(); |
| 85 | } |
| 86 | |
| 87 | WebContentsImpl* web_contents() const { |
| 88 | return static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 89 | } |
| 90 | |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 91 | RenderFrameHostImpl* primary_main_frame_host() { |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 92 | return web_contents()->GetPrimaryFrameTree().root()->current_frame_host(); |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 93 | } |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 94 | |
| 95 | PageImpl& page() { return primary_main_frame_host()->GetPage(); } |
| 96 | |
| 97 | Data* CreateOrGetDataForPage(Page& page) { |
| 98 | Data* data = Data::GetOrCreateForPage(page); |
| 99 | EXPECT_TRUE(data); |
| 100 | return data; |
| 101 | } |
| 102 | |
| 103 | void EnsureEqualPageUserData(Data* data_a, Data* data_b) { |
| 104 | EXPECT_EQ(data_a->unique_id(), data_b->unique_id()); |
| 105 | } |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 106 | }; |
| 107 | |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 108 | class PageImplPrerenderBrowserTest : public PageImplTest { |
| 109 | public: |
| 110 | PageImplPrerenderBrowserTest() |
| 111 | : prerender_helper_( |
| 112 | base::BindRepeating(&PageImplPrerenderBrowserTest::GetWebContents, |
| 113 | base::Unretained(this))) {} |
| 114 | |
Ian Vollick | 6bc0ee2 | 2021-08-04 23:36:26 | [diff] [blame] | 115 | void SetUp() override { |
Robert Lin | eb2a99c | 2023-08-24 02:42:26 | [diff] [blame] | 116 | prerender_helper_.RegisterServerRequestMonitor(embedded_test_server()); |
Ian Vollick | 6bc0ee2 | 2021-08-04 23:36:26 | [diff] [blame] | 117 | PageImplTest::SetUp(); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | content::test::PrerenderTestHelper& prerender_test_helper() { |
| 121 | return prerender_helper_; |
| 122 | } |
| 123 | |
| 124 | content::WebContents* GetWebContents() { return web_contents(); } |
| 125 | |
| 126 | protected: |
| 127 | test::PrerenderTestHelper prerender_helper_; |
| 128 | }; |
| 129 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 130 | // Test that Page and PageUserData objects are same for main RenderFrameHosts |
| 131 | // and subframes which belong to the same Page. |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 132 | IN_PROC_BROWSER_TEST_F(PageImplTest, AllFramesBelongToTheSamePage) { |
| 133 | ASSERT_TRUE(embedded_test_server()->Start()); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 134 | |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 135 | GURL url_a(embedded_test_server()->GetURL( |
| 136 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 137 | |
| 138 | // 1) Navigate to a(b). |
| 139 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 140 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 141 | RenderFrameHostImpl* rfh_b = rfh_a->child_at(0)->current_frame_host(); |
| 142 | |
| 143 | // 2) Check Page for RenderFrameHosts a and b, they both should point to same |
| 144 | // Page object. |
Sreeja Kamishetty | 7c91ab2 | 2021-06-03 13:29:52 | [diff] [blame] | 145 | PageImpl& page_a = rfh_a->GetPage(); |
| 146 | PageImpl& page_b = rfh_b->GetPage(); |
| 147 | EXPECT_EQ(&page_a, &page_b); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 148 | EXPECT_TRUE(page_a.IsPrimary()); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 149 | |
| 150 | // 3) Check that PageUserData objects for both pages a and b have same |
| 151 | // unique_id's. |
| 152 | EnsureEqualPageUserData(CreateOrGetDataForPage(page_a), |
| 153 | CreateOrGetDataForPage(page_b)); |
| 154 | } |
| 155 | |
| 156 | // Test that Page and PageUserData objects are accessible inside |
| 157 | // RenderFrameDeleted callback. |
| 158 | IN_PROC_BROWSER_TEST_F(PageImplTest, RenderFrameHostDeleted) { |
| 159 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 160 | GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 161 | GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html")); |
| 162 | |
| 163 | // 1) Navigate to A. |
| 164 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
| 165 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
| 166 | PageImpl& page_a = rfh_a->GetPage(); |
| 167 | base::WeakPtr<Data> data = CreateOrGetDataForPage(page_a)->GetWeakPtr(); |
| 168 | RenderFrameDeletedObserver delete_rfh_a(rfh_a); |
| 169 | |
| 170 | // 2) PageUserData associated with page_a should be valid when |
| 171 | // RenderFrameDeleted callback is invoked. |
| 172 | testing::NiceMock<MockWebContentsObserver> observer(shell()->web_contents()); |
| 173 | EXPECT_CALL(observer, RenderFrameDeleted(testing::_)) |
| 174 | .WillOnce( |
| 175 | testing::Invoke([data, rfh_a](RenderFrameHost* render_frame_host) { |
| 176 | // Both PageUserData and Page objects should be accessible before |
| 177 | // RenderFrameHost deletion. |
| 178 | EXPECT_EQ(rfh_a, render_frame_host); |
| 179 | DCHECK(&render_frame_host->GetPage()); |
| 180 | EXPECT_TRUE(data); |
| 181 | })); |
| 182 | |
| 183 | // Test needs rfh_a to be deleted after navigating but it doesn't happen with |
| 184 | // BackForwardCache as it is stored in cache. |
| 185 | DisableBackForwardCacheForTesting(web_contents(), |
Rakina Zata Amni | 30af706 | 2022-01-19 23:46:36 | [diff] [blame] | 186 | BackForwardCache::TEST_REQUIRES_NO_CACHING); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 187 | |
| 188 | // 3) Navigate to B, deleting rfh_a. |
| 189 | EXPECT_TRUE(NavigateToURL(shell(), url_b)); |
| 190 | delete_rfh_a.WaitUntilDeleted(); |
| 191 | } |
| 192 | |
| 193 | // Test basic functionality of PageUserData. |
| 194 | IN_PROC_BROWSER_TEST_F(PageImplTest, GetCreateAndDeleteUserDataForPage) { |
| 195 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 196 | GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 197 | |
| 198 | // 1) Navigate to A. |
| 199 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
| 200 | PageImpl& page_a = page(); |
| 201 | |
| 202 | // 2) Get the Data associated with this Page. It should be null |
| 203 | // before creation. |
| 204 | Data* data = Data::GetForPage(page_a); |
| 205 | EXPECT_FALSE(data); |
| 206 | |
| 207 | // 3) Create Data and check that GetForPage shouldn't return null |
| 208 | // now. |
| 209 | Data::CreateForPage(page_a); |
| 210 | base::WeakPtr<Data> created_data = Data::GetForPage(page_a)->GetWeakPtr(); |
| 211 | EXPECT_TRUE(created_data); |
| 212 | |
| 213 | // 4) Delete Data and check that GetForPage should return null. |
| 214 | Data::DeleteForPage(page_a); |
| 215 | EXPECT_FALSE(created_data); |
| 216 | EXPECT_FALSE(Data::GetForPage(page_a)); |
| 217 | } |
| 218 | |
| 219 | // Test GetOrCreateForPage API of PageUserData. |
| 220 | IN_PROC_BROWSER_TEST_F(PageImplTest, GetOrCreateForPage) { |
| 221 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 222 | GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 223 | |
| 224 | // 1) Navigate to A. |
| 225 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
| 226 | PageImpl& page_a = page(); |
| 227 | |
| 228 | // 2) Get the Data associated with this RenderFrameHost. It should be null |
| 229 | // before creation. |
| 230 | Data* data = Data::GetForPage(page_a); |
| 231 | EXPECT_FALSE(data); |
| 232 | |
| 233 | // 3) |GetOrCreateForPage| should create Data. |
| 234 | base::WeakPtr<Data> created_data = |
| 235 | Data::GetOrCreateForPage(page_a)->GetWeakPtr(); |
| 236 | EXPECT_TRUE(created_data); |
| 237 | |
| 238 | // 4) Another call to |GetOrCreateForPage| should not create the |
| 239 | // new data and the previous data created in 3) should be preserved. |
| 240 | Data* new_created_data = Data::GetOrCreateForPage(page_a); |
| 241 | EXPECT_TRUE(created_data); |
| 242 | EnsureEqualPageUserData(created_data.get(), new_created_data); |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 243 | } |
| 244 | |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 245 | // Test that the Page object doesn't change for new subframe RFHs after |
| 246 | // subframes does a cross-site or same-site navigation. |
| 247 | // |
| 248 | // 1) Navigate to A(A1, B). |
| 249 | // 2) Navigate cross site A1 to C. |
| 250 | // 3) Navigate same site B to B2. |
| 251 | IN_PROC_BROWSER_TEST_F(PageImplTest, PageObjectAfterSubframeNavigation) { |
| 252 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 253 | |
| 254 | // 1) Navigate to A1(A2, B). |
| 255 | GURL main_url(embedded_test_server()->GetURL( |
| 256 | "a.com", "/cross_site_iframe_factory.html?a(a,b)")); |
| 257 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 258 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 259 | RenderFrameHostImpl* rfh_a2 = rfh_a->child_at(0)->current_frame_host(); |
| 260 | RenderFrameHostImpl* rfh_b = rfh_a->child_at(1)->current_frame_host(); |
| 261 | |
| 262 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 263 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 264 | |
| 265 | // 2) Check that Page for A1, A2, B point to same object. |
| 266 | PageImpl& page_a = rfh_a->GetPage(); |
| 267 | PageImpl& page_a2 = rfh_a2->GetPage(); |
| 268 | PageImpl& page_b = rfh_b->GetPage(); |
| 269 | EXPECT_EQ(&page_a, &page_a2); |
| 270 | EXPECT_EQ(&page_a2, &page_b); |
| 271 | |
| 272 | // 3) Navigate subframe cross-site from A2 -> C. |
| 273 | GURL url_c = embedded_test_server()->GetURL("c.com", "/title1.html"); |
| 274 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), url_c)); |
| 275 | |
| 276 | // 4) Page object of new subframe C should be same as page_a. |
| 277 | RenderFrameHostImpl* rfh_c = rfh_a->child_at(0)->current_frame_host(); |
| 278 | PageImpl& page_c = rfh_c->GetPage(); |
| 279 | EXPECT_EQ(&page_c, &page_a); |
| 280 | |
| 281 | // 5) Navigate subframe same-site from B -> B2. |
| 282 | GURL url_b = embedded_test_server()->GetURL("b.com", "/title2.html"); |
| 283 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(1), url_b)); |
| 284 | |
| 285 | // 6) Page object of new subframe B2 should be same as page_a. |
| 286 | RenderFrameHostImpl* rfh_b2 = rfh_a->child_at(1)->current_frame_host(); |
| 287 | PageImpl& page_b2 = rfh_b2->GetPage(); |
| 288 | EXPECT_EQ(&page_b2, &page_a); |
| 289 | } |
| 290 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 291 | // Test that Page and PageUserData object remains the same for pending page |
| 292 | // before and after commit. |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 293 | IN_PROC_BROWSER_TEST_F(PageImplTest, PageObjectBeforeAndAfterCommit) { |
| 294 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 295 | GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 296 | GURL url_b(embedded_test_server()->GetURL("b.com", "/title2.html")); |
| 297 | |
| 298 | // Isolate "b.com" so we are guaranteed to get a different process |
| 299 | // for navigations to this origin on Android. Doing this ensures that a |
| 300 | // speculative RenderFrameHost is used. |
| 301 | IsolateOriginsForTesting(embedded_test_server(), shell()->web_contents(), |
| 302 | {"b.com"}); |
| 303 | |
| 304 | // 1) Navigate to A. |
| 305 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 306 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 307 | PageImpl& page_a = rfh_a->GetPage(); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 308 | base::WeakPtr<Data> data_a = CreateOrGetDataForPage(page_a)->GetWeakPtr(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 309 | |
| 310 | // 2) Start navigation to B, but don't commit yet. |
| 311 | TestNavigationManager manager(shell()->web_contents(), url_b); |
| 312 | shell()->LoadURL(url_b); |
Jiacheng Guo | 4bdd0be | 2024-06-11 23:35:21 | [diff] [blame] | 313 | manager.WaitForSpeculativeRenderFrameHostCreation(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 314 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 315 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 316 | RenderFrameHostImpl* pending_rfh = |
| 317 | root->render_manager()->speculative_frame_host(); |
| 318 | NavigationRequest* navigation_request = root->navigation_request(); |
Rakina Zata Amni | 8d4ed05 | 2022-12-07 23:03:30 | [diff] [blame] | 319 | EXPECT_EQ(navigation_request->GetAssociatedRFHType(), |
Rakina Zata Amni | 7af54b1 | 2022-06-28 10:36:17 | [diff] [blame] | 320 | NavigationRequest::AssociatedRenderFrameHostType::SPECULATIVE); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 321 | EXPECT_TRUE(pending_rfh); |
| 322 | |
| 323 | // 3) While there is a speculative RenderFrameHost in the root FrameTreeNode, |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 324 | // get the Page associated with this RenderFrameHost and PageUserData |
| 325 | // associated with this Page. |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 326 | PageImpl& pending_rfh_page = pending_rfh->GetPage(); |
| 327 | EXPECT_NE(&pending_rfh_page, &page_a); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 328 | EXPECT_TRUE(page_a.IsPrimary()); |
| 329 | EXPECT_FALSE(pending_rfh_page.IsPrimary()); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 330 | base::WeakPtr<Data> data_before_commit = |
| 331 | CreateOrGetDataForPage(pending_rfh_page)->GetWeakPtr(); |
| 332 | EXPECT_NE(data_before_commit.get()->unique_id(), data_a.get()->unique_id()); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 333 | |
| 334 | // 4) Let the navigation finish and make sure it has succeeded. |
Fergal Daly | 105305ad | 2023-01-16 08:21:52 | [diff] [blame] | 335 | ASSERT_TRUE(manager.WaitForNavigationFinished()); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 336 | EXPECT_EQ(url_b, |
| 337 | web_contents()->GetPrimaryMainFrame()->GetLastCommittedURL()); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 338 | |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 339 | RenderFrameHostImpl* rfh_b = primary_main_frame_host(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 340 | EXPECT_EQ(pending_rfh, rfh_b); |
| 341 | PageImpl& rfh_b_page = rfh_b->GetPage(); |
| 342 | |
| 343 | // 5) Check |pending_rfh_page| and |rfh_b_page| point to the same object. |
| 344 | EXPECT_EQ(&pending_rfh_page, &rfh_b_page); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 345 | EXPECT_TRUE(rfh_b_page.IsPrimary()); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 346 | } |
| 347 | |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 348 | // Test that WebContentsObserver::PrimaryPageChanged is invoked on primary page |
| 349 | // changes after page related data is updated e.g. LastCommittedURL. |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 350 | IN_PROC_BROWSER_TEST_F(PageImplTest, PrimaryPageChangedOnCrossSiteNavigation) { |
| 351 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 352 | GURL url_a(embedded_test_server()->GetURL( |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 353 | "a.com", "/cross_site_iframe_factory.html?a(b, c)")); |
| 354 | GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html")); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 355 | |
| 356 | // 1) Navigate to A. |
| 357 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 358 | Page* invoked_page; |
| 359 | GURL last_committed_url; |
Sreeja Kamishetty | 799a0ca | 2021-12-10 10:43:02 | [diff] [blame] | 360 | int http_status_code; |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 361 | |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 362 | // 2) Invoke MockWebContentsObserver to check the values inside |
| 363 | // PrimaryPageChanged(Page&) match the ones inside |
| 364 | // DidFinishNavigation(NavigationHandle*). |
| 365 | WebContents* contents = web_contents(); |
| 366 | testing::NiceMock<MockWebContentsObserver> web_contents_observer(contents); |
| 367 | testing::InSequence s; |
| 368 | |
| 369 | { |
| 370 | // 3) Stores the values of page invoked on PrimaryPageChanged and |
Sreeja Kamishetty | 799a0ca | 2021-12-10 10:43:02 | [diff] [blame] | 371 | // LastCommittedUrl, HttpStatusCode to match with ones inside |
| 372 | // DidFinishNavigation and page after navigation. |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 373 | EXPECT_CALL(web_contents_observer, PrimaryPageChanged(testing::_)) |
Sreeja Kamishetty | 799a0ca | 2021-12-10 10:43:02 | [diff] [blame] | 374 | .WillOnce(testing::Invoke([&invoked_page, &last_committed_url, |
| 375 | &http_status_code, url_b, this](Page& page) { |
| 376 | invoked_page = &page; |
| 377 | last_committed_url = page.GetMainDocument().GetLastCommittedURL(); |
| 378 | http_status_code = web_contents() |
| 379 | ->GetController() |
| 380 | .GetVisibleEntry() |
| 381 | ->GetHttpStatusCode(); |
| 382 | EXPECT_EQ(last_committed_url, url_b); |
| 383 | EXPECT_TRUE(page.IsPrimary()); |
| 384 | EXPECT_EQ(&web_contents()->GetPrimaryPage(), &page); |
| 385 | })); |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 386 | |
| 387 | EXPECT_CALL(web_contents_observer, DidFinishNavigation(testing::_)) |
Sreeja Kamishetty | 799a0ca | 2021-12-10 10:43:02 | [diff] [blame] | 388 | .WillOnce(testing::Invoke([&last_committed_url, &http_status_code]( |
| 389 | NavigationHandle* navigation_handle) { |
| 390 | EXPECT_EQ(navigation_handle->GetURL(), last_committed_url); |
| 391 | EXPECT_EQ(http_status_code, navigation_handle->GetWebContents() |
| 392 | ->GetController() |
| 393 | .GetVisibleEntry() |
| 394 | ->GetHttpStatusCode()); |
| 395 | })); |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // 4) Navigate to B. PrimaryPageChanged and DidFinishNavigation should be |
| 399 | // triggered for new `page_b` and should match `invoked_page`. |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 400 | EXPECT_TRUE(NavigateToURL(shell(), url_b)); |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 401 | RenderFrameHostImpl* rfh_b = primary_main_frame_host(); |
| 402 | PageImpl& page_b = rfh_b->GetPage(); |
| 403 | EXPECT_EQ(&page_b, invoked_page); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 404 | } |
| 405 | |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 406 | // Test that a new Page object is created for a same-site same-RFH navigation. |
Dominique Fauteux-Chapleau | 3e8d345 | 2021-07-14 17:20:02 | [diff] [blame] | 407 | IN_PROC_BROWSER_TEST_F(PageImplTest, SameSiteSameRenderFrameHostNavigation) { |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 408 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 409 | GURL url_a1(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 410 | GURL url_a2(embedded_test_server()->GetURL("a.com", "/title2.html")); |
| 411 | |
| 412 | // 1) Navigate to A1. |
| 413 | EXPECT_TRUE(NavigateToURL(shell(), url_a1)); |
Dominique Fauteux-Chapleau | 3e8d345 | 2021-07-14 17:20:02 | [diff] [blame] | 414 | RenderFrameHostImplWrapper main_rfh_a1(primary_main_frame_host()); |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 415 | base::WeakPtr<Page> page_a1 = main_rfh_a1->GetPage().GetWeakPtr(); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 416 | testing::NiceMock<MockWebContentsObserver> page_changed_observer( |
| 417 | web_contents()); |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 418 | base::WeakPtr<Data> data = CreateOrGetDataForPage(*page_a1)->GetWeakPtr(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 419 | |
Dominique Fauteux-Chapleau | 3e8d345 | 2021-07-14 17:20:02 | [diff] [blame] | 420 | // 2) Navigate to A2. This will result in invoking PrimaryPageChanged |
| 421 | // callback. |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 422 | EXPECT_CALL(page_changed_observer, PrimaryPageChanged(testing::_)).Times(1); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 423 | EXPECT_TRUE(NavigateToURL(shell(), url_a2)); |
Dominique Fauteux-Chapleau | 3e8d345 | 2021-07-14 17:20:02 | [diff] [blame] | 424 | RenderFrameHostImplWrapper main_rfh_a2(primary_main_frame_host()); |
| 425 | EXPECT_EQ(CanSameSiteMainFrameNavigationsChangeRenderFrameHosts(), |
| 426 | main_rfh_a1.get() != main_rfh_a2.get()); |
| 427 | PageImpl& page_a2 = main_rfh_a2.get()->GetPage(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 428 | |
Mingyu Lei | f208fa2 | 2022-09-08 04:10:49 | [diff] [blame] | 429 | if (IsBackForwardCacheEnabled()) { |
| 430 | // 3a) With back/forward cache enabled, both Page objects should be in |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 431 | // existence at the same time. |
| 432 | EXPECT_TRUE(page_a1); |
| 433 | EXPECT_NE(page_a1.get(), &page_a2); |
| 434 | // And the user data associated with the page (now in bfcache) should also |
| 435 | // still be alive. |
| 436 | EXPECT_TRUE(data); |
| 437 | } else { |
| 438 | // 3b) Otherwise, check that the old Page object was destroyed. There is no |
| 439 | // other way to validate that the old Page and new Page are different: |
| 440 | // comparing pointer values is not a stable test, since the new Page could |
| 441 | // be reallocated at the same address. |
| 442 | EXPECT_FALSE(page_a1); |
| 443 | // Similarly, expect any PageUserData from the old Page to be gone. |
Dominique Fauteux-Chapleau | 3e8d345 | 2021-07-14 17:20:02 | [diff] [blame] | 444 | EXPECT_FALSE(data); |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 445 | } |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Test that a new Page object is created when RenderFrame is recreated after |
| 449 | // crash. |
| 450 | IN_PROC_BROWSER_TEST_F(PageImplTest, NewPageObjectCreatedOnFrameCrash) { |
| 451 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 452 | GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 453 | |
| 454 | // 1) Navigate to A. |
| 455 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 456 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 457 | base::WeakPtr<Page> page_a = rfh_a->GetPage().GetWeakPtr(); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 458 | testing::NiceMock<MockWebContentsObserver> page_changed_observer( |
| 459 | web_contents()); |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 460 | base::WeakPtr<Data> data = CreateOrGetDataForPage(*page_a)->GetWeakPtr(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 461 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 462 | // 2) Make the renderer crash this should not reset the Page or delete the |
| 463 | // PageUserData. |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 464 | RenderProcessHost* renderer_process = rfh_a->GetProcess(); |
| 465 | RenderProcessHostWatcher crash_observer( |
| 466 | renderer_process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 467 | renderer_process->Shutdown(0); |
| 468 | crash_observer.Wait(); |
| 469 | EXPECT_TRUE(&(rfh_a->GetPage())); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 470 | EXPECT_TRUE(data); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 471 | |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 472 | // 3) Re-initialize RenderFrame, this should result in invoking |
| 473 | // PrimaryPageChanged callback. |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 474 | EXPECT_CALL(page_changed_observer, PrimaryPageChanged(testing::_)).Times(1); |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 475 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 476 | root->render_manager()->InitializeMainRenderFrameForImmediateUse(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 477 | |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 478 | // 4) Check that the old Page object was destroyed. There is no other way to |
| 479 | // validate that the old Page and new Page are different: comparing pointer |
| 480 | // values is not a stable test, since the new Page could be reallocated at the |
| 481 | // same address. |
| 482 | EXPECT_FALSE(page_a); |
| 483 | // Similarly, expect any PageUserData from the old Page to be gone. |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 484 | EXPECT_FALSE(data); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // Test that a new Page object is created when we do a same-site navigation |
| 488 | // after renderer crashes. |
| 489 | IN_PROC_BROWSER_TEST_F(PageImplTest, SameSiteNavigationAfterFrameCrash) { |
| 490 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 491 | GURL url_a1(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 492 | GURL url_a2(embedded_test_server()->GetURL("a.com", "/title2.html")); |
| 493 | |
| 494 | // 1) Navigate to A1. |
| 495 | EXPECT_TRUE(NavigateToURL(shell(), url_a1)); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 496 | RenderFrameHostImpl* rfh_a1 = primary_main_frame_host(); |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 497 | base::WeakPtr<Page> page_a1 = rfh_a1->GetPage().GetWeakPtr(); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 498 | testing::NiceMock<MockWebContentsObserver> page_changed_observer( |
| 499 | web_contents()); |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 500 | base::WeakPtr<Data> data = CreateOrGetDataForPage(*page_a1)->GetWeakPtr(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 501 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 502 | // 2) Crash the renderer hosting current RFH. This should not reset the Page |
| 503 | // or delete the PageUserData. |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 504 | RenderProcessHost* renderer_process = rfh_a1->GetProcess(); |
| 505 | RenderProcessHostWatcher crash_observer( |
| 506 | renderer_process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 507 | renderer_process->Shutdown(0); |
| 508 | crash_observer.Wait(); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 509 | EXPECT_TRUE(&(web_contents()->GetPrimaryMainFrame()->GetPage())); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 510 | EXPECT_TRUE(data); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 511 | |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 512 | // 3) Navigate same-site to A2. This will result in invoking |
| 513 | // PrimaryPageChanged callback after new Page creation. |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 514 | EXPECT_CALL(page_changed_observer, PrimaryPageChanged(testing::_)).Times(1); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 515 | EXPECT_TRUE(NavigateToURL(shell(), url_a2)); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 516 | |
Daniel Cheng | bafeee4 | 2021-09-27 23:27:57 | [diff] [blame] | 517 | // 4) Check that the old Page object was destroyed. There is no other way to |
| 518 | // validate that the old Page and new Page are different: comparing pointer |
| 519 | // values is not a stable test, since the new Page could be reallocated at the |
| 520 | // same address. |
| 521 | EXPECT_FALSE(page_a1); |
| 522 | // Similarly, expect any PageUserData from the old Page to be gone. |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 523 | EXPECT_FALSE(data); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Test PageImpl with BackForwardCache feature enabled. |
| 527 | class PageImplWithBackForwardCacheTest : public PageImplTest { |
| 528 | public: |
| 529 | PageImplWithBackForwardCacheTest() { |
| 530 | scoped_feature_list_.InitWithFeaturesAndParameters( |
Ming-Ying Chung | 7263618 | 2023-02-28 12:16:04 | [diff] [blame] | 531 | GetDefaultEnabledBackForwardCacheFeaturesForTesting( |
| 532 | /*ignore_outstanding_network_request=*/false), |
| 533 | GetDefaultDisabledBackForwardCacheFeaturesForTesting()); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | private: |
| 537 | base::test::ScopedFeatureList scoped_feature_list_; |
| 538 | }; |
| 539 | |
| 540 | // Tests that PageImpl object is not cleared on storing and restoring a Page |
| 541 | // from back-forward cache. |
| 542 | IN_PROC_BROWSER_TEST_F(PageImplWithBackForwardCacheTest, |
| 543 | BackForwardCacheNavigation) { |
| 544 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 545 | GURL url_a(embedded_test_server()->GetURL( |
| 546 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 547 | GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html")); |
| 548 | |
| 549 | // 1) Navigate to A(B). |
| 550 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 551 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 552 | RenderFrameHostImpl* rfh_b = rfh_a->child_at(0)->current_frame_host(); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 553 | testing::NiceMock<MockWebContentsObserver> page_changed_observer( |
| 554 | web_contents()); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 555 | |
| 556 | // 2) Get the PageImpl object associated with A and B RenderFrameHost. |
| 557 | PageImpl& page_a = rfh_a->GetPage(); |
| 558 | PageImpl& page_b = rfh_b->GetPage(); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 559 | Data* data = CreateOrGetDataForPage(page_a); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 560 | |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 561 | // 3) Navigate to C. PrimaryPageChanged should be triggered as A(B) is stored |
| 562 | // in BackForwardCache. |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 563 | EXPECT_CALL(page_changed_observer, PrimaryPageChanged(testing::_)).Times(1); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 564 | EXPECT_TRUE(NavigateToURL(shell(), url_c)); |
| 565 | EXPECT_TRUE(rfh_a->IsInBackForwardCache()); |
| 566 | EXPECT_TRUE(rfh_b->IsInBackForwardCache()); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 567 | EXPECT_FALSE(page_a.IsPrimary()); |
| 568 | EXPECT_FALSE(page_b.IsPrimary()); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 569 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 570 | // 4) PageImpl associated with document should point to the same object. |
| 571 | // PageUserData should not be deleted on navigating away with |
| 572 | // BackForwardCache. |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 573 | EXPECT_EQ(&page_a, &(rfh_a->GetPage())); |
| 574 | EXPECT_EQ(&page_b, &(rfh_b->GetPage())); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 575 | EXPECT_TRUE(data); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 576 | |
| 577 | // 5) Go back to A(B) and the Page object before and after restore should |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 578 | // point to the same object. PrimaryPageChanged should still be triggered when |
| 579 | // primary page changes to the existing page restored from the |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 580 | // BackForwardCache point to the same object and PageUserData should not be |
| 581 | // deleted. |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 582 | EXPECT_CALL(page_changed_observer, PrimaryPageChanged(testing::_)).Times(1); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 583 | web_contents()->GetController().GoBack(); |
| 584 | EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 585 | EXPECT_EQ(&page_a, &(rfh_a->GetPage())); |
| 586 | EXPECT_EQ(&page_b, &(rfh_b->GetPage())); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 587 | EXPECT_TRUE(page_a.IsPrimary()); |
| 588 | EXPECT_TRUE(page_b.IsPrimary()); |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 589 | EXPECT_TRUE(data); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | // Tests that PageImpl object is correct for IsPrimary. |
| 593 | IN_PROC_BROWSER_TEST_F(PageImplPrerenderBrowserTest, IsPrimary) { |
| 594 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 595 | |
| 596 | // Navigate to a site. |
| 597 | GURL url_a = embedded_test_server()->GetURL("/empty.html"); |
| 598 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
| 599 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
| 600 | EXPECT_TRUE(rfh_a->GetPage().IsPrimary()); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 601 | testing::NiceMock<MockWebContentsObserver> page_changed_observer( |
| 602 | web_contents()); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 603 | |
| 604 | // Prerender to another site. |
| 605 | GURL prerender_url = embedded_test_server()->GetURL("/title2.html"); |
| 606 | prerender_helper_.AddPrerender(prerender_url); |
Avi Drissman | 580a3da6 | 2024-09-04 16:16:56 | [diff] [blame] | 607 | FrameTreeNodeId host_id = |
| 608 | prerender_test_helper().GetHostForUrl(prerender_url); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 609 | content::test::PrerenderHostObserver host_observer(*web_contents(), host_id); |
| 610 | content::RenderFrameHost* prerender_frame = |
| 611 | prerender_test_helper().GetPrerenderedMainFrameHost(host_id); |
| 612 | Page& prerender_page = prerender_frame->GetPage(); |
| 613 | EXPECT_FALSE(prerender_page.IsPrimary()); |
| 614 | |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 615 | // Navigate to the prerendered site. PrimaryPageChanged should only be |
| 616 | // triggered on activation. |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 617 | EXPECT_CALL(page_changed_observer, PrimaryPageChanged(testing::_)).Times(1); |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 618 | prerender_helper_.NavigatePrimaryPage(prerender_url); |
| 619 | EXPECT_TRUE(host_observer.was_activated()); |
| 620 | EXPECT_EQ(&prerender_page, &(primary_main_frame_host()->GetPage())); |
| 621 | EXPECT_TRUE(prerender_page.IsPrimary()); |
| 622 | } |
| 623 | |
| 624 | // Tests that IsPrimary returns false when pending deletion. |
| 625 | IN_PROC_BROWSER_TEST_F(PageImplPrerenderBrowserTest, IsPrimaryPendingDeletion) { |
| 626 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 627 | |
| 628 | GURL url_a(embedded_test_server()->GetURL( |
| 629 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 630 | GURL url_d(embedded_test_server()->GetURL("d.com", "/title1.html")); |
| 631 | |
| 632 | EXPECT_TRUE(NavigateToURL(shell(), url_a)); |
| 633 | RenderFrameHostImpl* rfh_a = primary_main_frame_host(); |
| 634 | RenderFrameHostImpl* rfh_b = rfh_a->child_at(0)->current_frame_host(); |
| 635 | EXPECT_TRUE(rfh_a->GetPage().IsPrimary()); |
| 636 | EXPECT_TRUE(rfh_b->GetPage().IsPrimary()); |
| 637 | LeaveInPendingDeletionState(rfh_a); |
| 638 | LeaveInPendingDeletionState(rfh_b); |
| 639 | |
| 640 | EXPECT_TRUE(NavigateToURL(shell(), url_d)); |
| 641 | |
| 642 | EXPECT_FALSE(rfh_a->GetPage().IsPrimary()); |
| 643 | EXPECT_FALSE(rfh_b->GetPage().IsPrimary()); |
Sreeja Kamishetty | 9a09585 | 2021-06-04 17:26:33 | [diff] [blame] | 644 | } |
| 645 | |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 646 | } // namespace content |