blob: 20c00bc22c6009c0b597465777610ad7bc9c5096 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2021 The Chromium Authors
Gyuyoung Kime8779742021-06-01 14:55:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gyuyoung Kime8779742021-06-01 14:55:075#include "chrome/browser/ui/tab_ui_helper.h"
Peter Kastinga4863242024-12-23 00:19:436
7#include "chrome/browser/ui/browser.h"
Gyuyoung Kime8779742021-06-01 14:55:078#include "chrome/test/base/in_process_browser_test.h"
9#include "chrome/test/base/ui_test_utils.h"
10#include "content/public/browser/web_contents.h"
11#include "content/public/test/browser_test.h"
12#include "content/public/test/prerender_test_util.h"
13#include "net/dns/mock_host_resolver.h"
14#include "net/test/embedded_test_server/embedded_test_server.h"
15#include "third_party/blink/public/common/features.h"
16
17class TabUIHelperWithPrerenderingTest : public InProcessBrowserTest {
18 public:
19 TabUIHelperWithPrerenderingTest()
20 : prerender_test_helper_(base::BindRepeating(
21 &TabUIHelperWithPrerenderingTest::GetWebContents,
22 base::Unretained(this))) {}
23 ~TabUIHelperWithPrerenderingTest() override = default;
24 TabUIHelperWithPrerenderingTest(const TabUIHelperWithPrerenderingTest&) =
25 delete;
26 TabUIHelperWithPrerenderingTest& operator=(
27 const TabUIHelperWithPrerenderingTest&) = delete;
28
29 void SetUp() override {
Robert Lineb2a99c2023-08-24 02:42:2630 prerender_test_helper_.RegisterServerRequestMonitor(embedded_test_server());
Gyuyoung Kime8779742021-06-01 14:55:0731 InProcessBrowserTest::SetUp();
32 }
33
34 void SetUpOnMainThread() override {
Gyuyoung Kime8779742021-06-01 14:55:0735 host_resolver()->AddRule("*", "127.0.0.1");
36 ASSERT_TRUE(embedded_test_server()->Start());
37 }
38
39 content::test::PrerenderTestHelper& prerender_test_helper() {
40 return prerender_test_helper_;
41 }
42
43 content::WebContents* GetWebContents() {
44 return browser()->tab_strip_model()->GetActiveWebContents();
45 }
46
47 private:
48 content::test::PrerenderTestHelper prerender_test_helper_;
Gyuyoung Kime8779742021-06-01 14:55:0749};
50
51IN_PROC_BROWSER_TEST_F(TabUIHelperWithPrerenderingTest,
52 ShouldNotAffectTabUIHelperOnPrerendering) {
53 GURL initial_url = embedded_test_server()->GetURL("/empty.html");
54 GURL prerender_url =
55 embedded_test_server()->GetURL("/favicon/title2_with_favicon.html");
56 ASSERT_NE(ui_test_utils::NavigateToURL(browser(), initial_url), nullptr);
57
58 TabUIHelper* tab_ui_helper = TabUIHelper::FromWebContents(GetWebContents());
59 std::u16string primary_title = tab_ui_helper->GetTitle();
Thomas Lukaszewicz58e83f12023-05-11 05:00:1260 ui::ImageModel primary_favicon = tab_ui_helper->GetFavicon();
Gyuyoung Kime8779742021-06-01 14:55:0761 bool primary_should_hide_throbber = tab_ui_helper->ShouldHideThrobber();
62
63 // Set |create_by_session_restore_| to true to check if the value is changed
64 // after prerendering. It should not be changed because DidStopLoading is not
65 // called during the prerendering.
66 tab_ui_helper->set_created_by_session_restore(true);
67
68 // Prerender to another site.
69 prerender_test_helper().AddPrerender(prerender_url);
70
71 // Check if the prerendering doesn't affect the returned values of
72 // TabUIHelper.
73 EXPECT_EQ(primary_title, tab_ui_helper->GetTitle());
74 EXPECT_EQ(primary_favicon, tab_ui_helper->GetFavicon());
75 EXPECT_EQ(primary_should_hide_throbber, tab_ui_helper->ShouldHideThrobber());
76 // is_created_by_session_restore_for_testing should return true because
77 // DidStopLoading is not called.
78 EXPECT_TRUE(tab_ui_helper->is_created_by_session_restore_for_testing());
79
80 // Activate the prerendered page.
81 prerender_test_helper().NavigatePrimaryPage(prerender_url);
82
83 // Check if new values are different from the previous primary values after
84 // activating the prerendered page.
85 EXPECT_NE(primary_title, tab_ui_helper->GetTitle());
86 EXPECT_FALSE(primary_favicon == tab_ui_helper->GetFavicon());
87 EXPECT_FALSE(tab_ui_helper->ShouldHideThrobber());
88}