File: side_search_tab_contents_helper_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (264 lines) | stat: -rw-r--r-- 10,192 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2021 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/side_search/side_search_tab_contents_helper.h"

#include <memory>

#include "base/feature_list.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/side_search/side_search_config.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace {

constexpr char kSearchMatchUrl1[] = "https://www.search-url-1.com/";
constexpr char kSearchMatchUrl2[] = "https://www.search-url-2.com/";
constexpr char kNonMatchUrl[] = "https://www.tab-frame-url.com/";

bool IsSearchURLMatch(const GURL& url) {
  return url == kSearchMatchUrl1 || url == kSearchMatchUrl2;
}

}  // namespace

namespace test {

class SideSearchTabContentsHelperTest : public ::testing::Test {
 public:
  void SetUp() override {
    scoped_feature_list_.InitAndEnableFeature(features::kSideSearch);
    web_contents_ =
        content::WebContentsTester::CreateTestWebContents(&profile_, nullptr);
    SideSearchTabContentsHelper::CreateForWebContents(web_contents_.get());
    helper()->SetSidePanelContentsForTesting(
        content::WebContentsTester::CreateTestWebContents(&profile_, nullptr));
    // Basic configuration for testing that allows navigations to URLs matching
    // `kSearchMatchUrl1` and `kSearchMatchUrl2` to proceed within the side
    // panel and only allows showing the side panel on non-matching pages.
    auto* config = GetConfig();
    config->SetShouldNavigateInSidePanelCallback(
        base::BindRepeating(IsSearchURLMatch));
    config->SetCanShowSidePanelForURLCallback(base::BindRepeating(
        [](const GURL& url) { return !IsSearchURLMatch(url); }));
    config->SetGenerateSideSearchURLCallback(
        base::BindRepeating([](const GURL& url) { return url; }));
    Test::SetUp();
  }

 protected:
  void LoadURL(const char* url) {
    web_contents()->GetController().LoadURL(GURL(url), content::Referrer(),
                                            ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
                                            std::string());
    CommitPendingNavigation();
  }

  void GoBack() {
    ASSERT_TRUE(web_contents()->GetController().CanGoBack());
    web_contents()->GetController().GoBack();
    CommitPendingNavigation();
  }

  void GoForward() {
    ASSERT_TRUE(web_contents()->GetController().CanGoForward());
    web_contents()->GetController().GoForward();
    CommitPendingNavigation();
  }

  void CommitPendingNavigation() {
    content::WebContentsTester::For(web_contents())->CommitPendingNavigation();

    if (side_contents() && side_contents()->GetController().GetPendingEntry()) {
      content::WebContentsTester::For(side_contents())
          ->CommitPendingNavigation();
    }
  }

  content::NavigationEntry* GetLastCommittedSideContentsEntry() {
    DCHECK(side_contents());
    return side_contents()->GetController().GetLastCommittedEntry();
  }

  content::WebContents* web_contents() { return web_contents_.get(); }

  content::WebContents* side_contents() {
    return helper()->GetSidePanelContents();
  }

  SideSearchTabContentsHelper* helper() {
    return SideSearchTabContentsHelper::FromWebContents(web_contents_.get());
  }

  SideSearchConfig* GetConfig() { return SideSearchConfig::Get(&profile_); }

  void ResetWebContents() { web_contents_.reset(); }

  base::HistogramTester& histogram_tester() { return histogram_tester_; }

 private:
  content::BrowserTaskEnvironment task_environment_;
  content::RenderViewHostTestEnabler rvh_enabler_;
  TestingProfile profile_;
  std::unique_ptr<content::WebContents> web_contents_;
  base::HistogramTester histogram_tester_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

// TODO(crbug.com/1384174): Update this test to pass and re-enable.
TEST_F(SideSearchTabContentsHelperTest,
       DISABLED_LastSearchURLUpdatesCorrectly) {
  // When a tab is first opened there should be no last encountered search URL.
  EXPECT_FALSE(helper()->last_search_url().has_value());
  EXPECT_TRUE(GetLastCommittedSideContentsEntry()->IsInitialEntry());

  // Navigating to a matching search URL should update the `last_search_url`.
  LoadURL(kSearchMatchUrl1);
  EXPECT_EQ(kSearchMatchUrl1, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl1, GetLastCommittedSideContentsEntry()->GetURL());

  // Navigating to a non-matching search URL should not change the
  // `last_search_url`.
  LoadURL(kNonMatchUrl);
  EXPECT_EQ(kSearchMatchUrl1, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl1, GetLastCommittedSideContentsEntry()->GetURL());

  // Navigating again to a new matching search url should update the
  // `last_search_url`.
  LoadURL(kSearchMatchUrl2);
  EXPECT_EQ(kSearchMatchUrl2, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl2, GetLastCommittedSideContentsEntry()->GetURL());

  // Going backwards to the non-matching search URL should not update the
  // `last_search_url`.
  GoBack();
  EXPECT_EQ(kSearchMatchUrl2, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl2, GetLastCommittedSideContentsEntry()->GetURL());

  // Going back to the original search URL should update the `last_search_url`.
  GoBack();
  EXPECT_EQ(kSearchMatchUrl1, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl1, GetLastCommittedSideContentsEntry()->GetURL());

  // Going forward to the non-matching search URL shouldn't change the
  // `last_search_url`.
  GoForward();
  EXPECT_EQ(kSearchMatchUrl1, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl1, GetLastCommittedSideContentsEntry()->GetURL());

  // Going forward to the latest matching search URL should update the
  // `last_search_url` to that latest URL.
  GoForward();
  EXPECT_EQ(kSearchMatchUrl2, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl2, GetLastCommittedSideContentsEntry()->GetURL());
}

// TODO(crbug.com/1384174): Update this test to pass and re-enable.
TEST_F(SideSearchTabContentsHelperTest,
       DISABLED_IndicatesWhenSidePanelShouldBeShown) {
  // With no initial navigation the side panel should not be showing.
  EXPECT_FALSE(helper()->CanShowSidePanelForCommittedNavigation());

  // If no previous matching search URL has been seen for this tab contents the
  // side panel should not show.
  LoadURL(kNonMatchUrl);
  EXPECT_FALSE(helper()->CanShowSidePanelForCommittedNavigation());

  // The side panel should not be visible on matching search pages.
  LoadURL(kSearchMatchUrl1);
  EXPECT_FALSE(helper()->CanShowSidePanelForCommittedNavigation());

  // If a matching page has previously been seen the side panel may be opened
  // on non-matching pages.
  LoadURL(kNonMatchUrl);
  EXPECT_TRUE(helper()->CanShowSidePanelForCommittedNavigation());
}

TEST_F(SideSearchTabContentsHelperTest, ClearsSidePanelContentsWhenAsked) {
  EXPECT_NE(nullptr, helper()->side_panel_contents_for_testing());
  helper()->ClearSidePanelContents();
  EXPECT_EQ(nullptr, helper()->side_panel_contents_for_testing());
}

TEST_F(SideSearchTabContentsHelperTest,
       SidePanelProcessGoneResetsSideContents) {
  EXPECT_NE(nullptr, helper()->side_panel_contents_for_testing());
  helper()->SidePanelProcessGone();
  EXPECT_EQ(nullptr, helper()->side_panel_contents_for_testing());
}

TEST_F(SideSearchTabContentsHelperTest, ClearsInternalStateWhenConfigChanges) {
  // When a tab is first opened there should be no last encountered search URL.
  EXPECT_FALSE(helper()->last_search_url().has_value());
  EXPECT_TRUE(!GetLastCommittedSideContentsEntry() ||
              GetLastCommittedSideContentsEntry()->IsInitialEntry());

  // Navigate to a search URL.
  LoadURL(kSearchMatchUrl1);
  EXPECT_EQ(kSearchMatchUrl1, helper()->last_search_url());
  EXPECT_EQ(kSearchMatchUrl1, GetLastCommittedSideContentsEntry()->GetURL());

  // Set the toggled bit to true.
  helper()->set_toggled_open(true);

  // Reset config state and notify observers that the config has changed.
  GetConfig()->ResetStateAndNotifyConfigChanged();

  // Check to make sure state has been reset.
  EXPECT_FALSE(helper()->last_search_url().has_value());
  EXPECT_FALSE(helper()->toggled_open());
  EXPECT_EQ(nullptr, helper()->side_panel_contents_for_testing());
}

// TODO(crbug.com/1384174): Update this test to pass and re-enable.
TEST_F(SideSearchTabContentsHelperTest, DISABLED_EmitsReturnedToSRPMetrics) {
  // Navigating to a matching search. Then navigate to a non-matching URL and
  // navigate back, doing so twice.
  LoadURL(kSearchMatchUrl1);
  LoadURL(kNonMatchUrl);
  GoBack();
  LoadURL(kNonMatchUrl);
  GoBack();
  LoadURL(kNonMatchUrl);

  // Return metrics should not yet have been emitted.
  histogram_tester().ExpectUniqueSample("SideSearch.TimesReturnedBackToSRP", 2,
                                        0);

  // Navigating to a new search URL should cause the previous metrics to have
  // been emitted.
  LoadURL(kSearchMatchUrl2);
  histogram_tester().ExpectUniqueSample("SideSearch.TimesReturnedBackToSRP", 2,
                                        1);

  // Navigating to a matching search. Then navigate to a non-matching URL and
  // navigate back, doing so three times.
  LoadURL(kNonMatchUrl);
  GoBack();
  LoadURL(kNonMatchUrl);
  GoBack();
  LoadURL(kNonMatchUrl);
  GoBack();
  LoadURL(kNonMatchUrl);

  // Return metrics for this interaction should not yet have been emitted.
  histogram_tester().ExpectBucketCount("SideSearch.TimesReturnedBackToSRP", 3,
                                       0);

  // Resetting the web contents should result in these metrics being emitted.
  ResetWebContents();
  histogram_tester().ExpectBucketCount("SideSearch.TimesReturnedBackToSRP", 3,
                                       1);
}

}  // namespace test