File: per_profile_webui_tracker_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (200 lines) | stat: -rw-r--r-- 8,509 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/top_chrome/per_profile_webui_tracker.h"

#include "base/scoped_observation.h"
#include "chrome/browser/ui/webui/top_chrome/webui_contents_preload_state.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace {
constexpr char kWebUIUrl1[] = "chrome://example";
constexpr char kWebUIUrl2[] = "chrome://example2";
}  // namespace

class PerProfileWebUITrackerTest : public ChromeRenderViewHostTestHarness {
 public:
  PerProfileWebUITrackerTest() : tracker_(PerProfileWebUITracker::Create()) {}
  ~PerProfileWebUITrackerTest() override = default;
  PerProfileWebUITrackerTest(const PerProfileWebUITrackerTest&) = delete;
  PerProfileWebUITrackerTest& operator=(const PerProfileWebUITrackerTest&) =
      delete;

  PerProfileWebUITracker* tracker() { return tracker_.get(); }

 private:
  std::unique_ptr<PerProfileWebUITracker> tracker_;
};

TEST_F(PerProfileWebUITrackerTest, Basic) {
  std::unique_ptr<content::WebContents> web_contents_1, web_contents_2;
  web_contents_1 = CreateTestWebContents();
  web_contents_2 = CreateTestWebContents();
  content::WebContentsTester::For(web_contents_1.get())
      ->NavigateAndCommit(GURL(kWebUIUrl1));
  content::WebContentsTester::For(web_contents_2.get())
      ->NavigateAndCommit(GURL(kWebUIUrl2));

  ASSERT_EQ(web_contents_1->GetBrowserContext(), profile());

  tracker()->AddWebContents(web_contents_1.get());
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));
  // web_contents_2 is not tracked, therefore ProfileHasWebUI() returns false
  // for kWebUIUrl2.
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl2));

  tracker()->AddWebContents(web_contents_2.get());
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl2));

  // Deleting web_contents_1 causes kWebUIUrl1 to be no longer present, while
  // kWebUIUrl2 is still present.
  web_contents_1.reset();
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl2));

  web_contents_2.reset();
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl2));
}

// Tests that when a profile opens two WebUIs of the same URL, the tracker
// will indicate that the WebUI is still present after closing one of them.
TEST_F(PerProfileWebUITrackerTest, TwoWebUIsOfSameURL) {
  std::unique_ptr<content::WebContents> web_contents_1, web_contents_2;
  web_contents_1 = CreateTestWebContents();
  web_contents_2 = CreateTestWebContents();
  // Both WebContents navigate to kWebUIUrl1.
  content::WebContentsTester::For(web_contents_1.get())
      ->NavigateAndCommit(GURL(kWebUIUrl1));
  content::WebContentsTester::For(web_contents_2.get())
      ->NavigateAndCommit(GURL(kWebUIUrl1));

  ASSERT_EQ(web_contents_1->GetBrowserContext(), profile());

  tracker()->AddWebContents(web_contents_1.get());
  tracker()->AddWebContents(web_contents_2.get());
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));

  // Close one WebUI, the WebUI is still present under this profile.
  web_contents_1.reset();
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));

  // Close the other WebUI, no WebUI is present under this profile.
  web_contents_2.reset();
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));
}

// Tests that the tracker distinguishes different profiles.
TEST_F(PerProfileWebUITrackerTest, DifferentProfiles) {
  std::unique_ptr<TestingProfile> profile1 = CreateTestingProfile();
  std::unique_ptr<TestingProfile> profile2 = CreateTestingProfile();
  std::unique_ptr<content::WebContents> web_contents_1 =
      content::WebContentsTester::CreateTestWebContents(profile1.get(),
                                                        nullptr);
  std::unique_ptr<content::WebContents> web_contents_2 =
      content::WebContentsTester::CreateTestWebContents(profile2.get(),
                                                        nullptr);
  content::WebContentsTester::For(web_contents_1.get())
      ->NavigateAndCommit(GURL(kWebUIUrl1));
  content::WebContentsTester::For(web_contents_2.get())
      ->NavigateAndCommit(GURL(kWebUIUrl2));
  tracker()->AddWebContents(web_contents_1.get());
  tracker()->AddWebContents(web_contents_2.get());
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile1.get(), kWebUIUrl1));
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile1.get(), kWebUIUrl2));
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile2.get(), kWebUIUrl1));
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile2.get(), kWebUIUrl2));
}

// Tests that the tracker work as expected when a WebContents navigates from
// about:blank to a WebUI.
TEST_F(PerProfileWebUITrackerTest, Navigation) {
  std::unique_ptr<content::WebContents> web_contents = CreateTestWebContents();
  ASSERT_EQ(web_contents->GetBrowserContext(), profile());

  // The WebContents is not yet navigated.
  tracker()->AddWebContents(web_contents.get());
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));

  // Navigate to a WebUI.
  content::WebContentsTester::For(web_contents.get())
      ->NavigateAndCommit(GURL(kWebUIUrl1));
  EXPECT_TRUE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));

  // Navigate away.
  content::WebContentsTester::For(web_contents.get())
      ->NavigateAndCommit(GURL("about:blank"));
  EXPECT_FALSE(tracker()->ProfileHasWebUI(profile(), kWebUIUrl1));
}

// Test for ProfileHasBackgroundWebUI(), which should return true if a URL has
// WebUI instances that are not yet shown.
TEST_F(PerProfileWebUITrackerTest, BackgroundWebUI) {
  std::unique_ptr<content::WebContents> web_contents = CreateTestWebContents();
  content::WebContentsTester* web_contents_tester =
      content::WebContentsTester::For(web_contents.get());
  web_contents_tester->NavigateAndCommit(GURL(kWebUIUrl1));

  tracker()->AddWebContents(web_contents.get());

  auto make_webui_foreground = [](content::WebContents* web_contents) {
    WebUIContentsPreloadState::CreateForWebContents(web_contents);
    WebUIContentsPreloadState::FromWebContents(web_contents)
        ->request_time = base::TimeTicks::Now();
  };

  auto make_webui_background = [](content::WebContents* web_contents) {
    WebUIContentsPreloadState::CreateForWebContents(web_contents);
    auto* state = WebUIContentsPreloadState::FromWebContents(web_contents);
    state->request_time.reset();
    state->preloaded = true;
  };

  make_webui_background(web_contents.get());
  EXPECT_TRUE(tracker()->ProfileHasBackgroundWebUI(profile(), kWebUIUrl1));

  // Show a new foreground WebUI of the same URL. HasBackgroundWebUI() should
  // still return true.
  std::unique_ptr<content::WebContents> web_contents2 =
      CreateTestWebContents();
  content::WebContentsTester* web_contents_tester2 =
      content::WebContentsTester::For(web_contents2.get());
  web_contents_tester2->NavigateAndCommit(GURL(kWebUIUrl1));
  make_webui_foreground(web_contents2.get());
  EXPECT_TRUE(tracker()->ProfileHasBackgroundWebUI(profile(), kWebUIUrl1));

  // Make the original `web_contents` foreground. HasBackgroundWebUI() should
  // return false.
  make_webui_foreground(web_contents.get());
  EXPECT_FALSE(tracker()->ProfileHasBackgroundWebUI(profile(), kWebUIUrl1));
}

class MockTrackerObserver : public PerProfileWebUITracker::Observer {
 public:
  MOCK_METHOD(void,
              OnWebContentsDestroyed,
              (content::WebContents*),
              (override));
  MOCK_METHOD(void,
              OnWebContentsPrimaryPageChanged,
              (content::WebContents*),
              (override));
};

// Tests that the observer of tracker is notified of WebContents destroy.
TEST_F(PerProfileWebUITrackerTest, Observer) {
  std::unique_ptr<content::WebContents> web_contents = CreateTestWebContents();
  ASSERT_EQ(web_contents->GetBrowserContext(), profile());
  tracker()->AddWebContents(web_contents.get());

  MockTrackerObserver mock_observer;
  base::ScopedObservation<PerProfileWebUITracker,
                          PerProfileWebUITracker::Observer>
      observation{&mock_observer};
  EXPECT_CALL(mock_observer, OnWebContentsDestroyed(web_contents.get()));
  observation.Observe(tracker());
  web_contents.reset();
}