File: glic_pinned_tab_manager_browsertest.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (289 lines) | stat: -rw-r--r-- 11,068 bytes parent folder | download | duplicates (3)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2025 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/glic/host/context/glic_pinned_tab_manager.h"

#include "base/containers/fixed_flat_map.h"
#include "base/test/test_future.h"
#include "base/test/test_mock_time_task_runner.h"
#include "chrome/browser/glic/host/glic.mojom.h"
#include "chrome/browser/glic/public/context/glic_sharing_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::ElementsAre;
using testing::Pointee;
using testing::Property;
using testing::Return;

namespace glic {

namespace {

MATCHER_P(HasTitle, title, "") {
  if (!arg->tab_data->title.has_value()) {
    *result_listener << "has no title";
    return false;
  }
  if (arg->tab_data->title.value() != title) {
    *result_listener << "has title "
                     << testing::PrintToString(arg->tab_data->title.value());
    return false;
  }
  return true;
}

template <typename Observer, typename Matcher>
void ExpectThatEventually(Observer& observer, const Matcher& matcher) {
  const base::TimeTicks start_time = base::TimeTicks::Now();
  const base::TimeDelta timeout = base::Seconds(5);

  while (base::TimeTicks::Now() - start_time < timeout) {
    auto value = observer.Take();
    if (testing::Matches(matcher)(value)) {
      SUCCEED();
      return;
    }
  }
  ADD_FAILURE() << "Timed out waiting for value to match.";
}

constexpr auto kUrlToTitleMap =
    base::MakeFixedFlatMap<std::string_view, std::string_view>({
        {"/why-cats-are-liquid", "The Physics of Feline Fluid Dynamics"},
        {"/sentient-toaster-manual", "My Toaster Is Evil: A User's Guide"},
        {"/zombie-squirrels", "The Looming Threat of the Undead Rodent"},
        {"/how-to-train-your-goldfish", "Advanced Goldfish Obedience Training"},
        {"/the-art-of-the-nap", "Competitive Napping: A Professional's Guide"},
        {"/advanced-sock-puppetry", "Guide to Advanced Sock Puppetry"},
        {"/pigeon-espionage",
         "Pigeons Aren't Real: The Government Drone Conspiracy"},
    });

}  // namespace

class FakePinCandidatesObserver : public mojom::PinCandidatesObserver {
 public:
  FakePinCandidatesObserver() = default;
  ~FakePinCandidatesObserver() override = default;

  mojo::PendingRemote<mojom::PinCandidatesObserver> Bind() {
    return receiver_.BindNewPipeAndPassRemote();
  }

  std::vector<mojom::PinCandidatePtr> Take() { return future_.Take(); }

  bool IsReady() { return future_.IsReady(); }

  // mojom::PinCandidatesObserver:
  void OnPinCandidatesChanged(
      std::vector<mojom::PinCandidatePtr> candidates) override {
    future_.SetValue(std::move(candidates));
  }

 private:
  base::test::TestFuture<std::vector<mojom::PinCandidatePtr>> future_;
  mojo::Receiver<mojom::PinCandidatesObserver> receiver_{this};
};

class GlicPinnedTabManagerWithOverrides : public GlicPinnedTabManager {
 public:
  using GlicPinnedTabManager::GlicPinnedTabManager;
  MOCK_METHOD(bool,
              IsBrowserValidForSharing,
              (BrowserWindowInterface*),
              (override));
  MOCK_METHOD(bool,
              IsValidForSharing,
              (content::WebContents*),
              (override));
  MOCK_METHOD(bool, IsGlicWindowShowing, (), (override));
};

class GlicPinnedTabManagerBrowserTest : public InProcessBrowserTest {
 public:
  GlicPinnedTabManagerBrowserTest()
      : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    https_server_.RegisterRequestHandler(
        base::BindRepeating(&GlicPinnedTabManagerBrowserTest::HandleRequest,
                            base::Unretained(this)));
    https_server_handle_ = https_server_.StartAndReturnHandle();
    ASSERT_TRUE(https_server_handle_);

    pinned_tab_manager_ = std::make_unique<GlicPinnedTabManagerWithOverrides>(
        browser()->profile(), /*window_controller=*/nullptr);
    ON_CALL(*pinned_tab_manager_, IsBrowserValidForSharing(_))
        .WillByDefault(Return(true));
    // TODO(mcrouse): Add tests for invalid candidates once testing harness for sharing manager is enabled.
    ON_CALL(*pinned_tab_manager_, IsValidForSharing(_))
        .WillByDefault(Return(true));

    ON_CALL(*pinned_tab_manager_, IsGlicWindowShowing())
        .WillByDefault(Return(false));
  }

  void TearDownOnMainThread() override {
    pinned_tab_manager_.reset();
    InProcessBrowserTest::TearDownOnMainThread();
  }

  // Helper function to create, navigate, set title, and add a new tab to the
  // current browser's tab strip.
  void CreateAndAddTab(const std::string& url_path) {
    GURL url = https_server_.GetURL(url_path);
    ui_test_utils::NavigateToURLWithDisposition(
        browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  }

 protected:
  std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
      const net::test_server::HttpRequest& request) {
    auto it = kUrlToTitleMap.find(request.relative_url);
    if (it == kUrlToTitleMap.end()) {
      return nullptr;
    }
    auto title = it->second;

    auto response = std::make_unique<net::test_server::BasicHttpResponse>();
    response->set_code(net::HTTP_OK);
    response->set_content_type("text/html");
    response->set_content("<html><head><title>" + std::string(title) +
                          "</title></head><body></body></html>");
    return response;
  }

  net::EmbeddedTestServer https_server_;
  net::test_server::EmbeddedTestServerHandle https_server_handle_;
  std::unique_ptr<GlicPinnedTabManagerWithOverrides> pinned_tab_manager_;
};

IN_PROC_BROWSER_TEST_F(GlicPinnedTabManagerBrowserTest,
                       ReturnsMultipleCandidatesSortedByActivation) {
  // By default, the browser starts with a single tab open to "about:blank".
  CreateAndAddTab("/why-cats-are-liquid");
  CreateAndAddTab("/sentient-toaster-manual");
  CreateAndAddTab("/zombie-squirrels");

  FakePinCandidatesObserver observer;
  auto options = mojom::GetPinCandidatesOptions::New();
  options->max_candidates = 3;
  pinned_tab_manager_->SubscribeToPinCandidates(std::move(options),
                                                observer.Bind());

  // The initial list should be sorted by creation time. Because the max number
  // of candidates is 3, the initial "about:blank" tab is not included.
  ExpectThatEventually(
      observer, ElementsAre(HasTitle("The Looming Threat of the Undead Rodent"),
                            HasTitle("My Toaster Is Evil: A User's Guide"),
                            HasTitle("The Physics of Feline Fluid Dynamics")));

  // Activate the oldest tab (index 1, since "about:blank" is at 0).
  browser()->tab_strip_model()->ActivateTabAt(1);

  // The activated tab should now be at the front of the list.
  ExpectThatEventually(
      observer, ElementsAre(HasTitle("The Physics of Feline Fluid Dynamics"),
                            HasTitle("The Looming Threat of the Undead Rodent"),
                            HasTitle("My Toaster Is Evil: A User's Guide")));
}

IN_PROC_BROWSER_TEST_F(GlicPinnedTabManagerBrowserTest,
                       SortsCandidatesByQuery) {
  // By default, the browser starts with a single tab open to "about:blank".
  CreateAndAddTab("/how-to-train-your-goldfish");
  CreateAndAddTab("/the-art-of-the-nap");
  CreateAndAddTab("/advanced-sock-puppetry");
  CreateAndAddTab("/pigeon-espionage");

  FakePinCandidatesObserver observer;
  auto options = mojom::GetPinCandidatesOptions::New();
  options->max_candidates = 4;
  options->query = "Guide";
  pinned_tab_manager_->SubscribeToPinCandidates(std::move(options),
                                                observer.Bind());

  // The list should be sorted by match type. Because the max number of
  // candidates is 4, the initial "about:blank" tab is not included.
  ExpectThatEventually(
      observer,
      ElementsAre(
          HasTitle("Guide to Advanced Sock Puppetry"),
          HasTitle("Competitive Napping: A Professional's Guide"),
          HasTitle("Pigeons Aren't Real: The Government Drone Conspiracy"),
          HasTitle("Advanced Goldfish Obedience Training")));
}

IN_PROC_BROWSER_TEST_F(GlicPinnedTabManagerBrowserTest, PinTabs) {
  CreateAndAddTab("/why-cats-are-liquid");

  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  tabs::TabInterface* tab_interface =
      tabs::TabInterface::GetFromContents(tab_strip_model->GetWebContentsAt(1));
  ASSERT_TRUE(tab_interface);
  const tabs::TabHandle tab_handle = tab_interface->GetHandle();

  base::test::TestFuture<tabs::TabInterface*, bool> pin_status_future;
  auto subscription = pinned_tab_manager_->AddTabPinningStatusChangedCallback(
      pin_status_future.GetRepeatingCallback());

  // Pin a tab and verify it was pinned.
  EXPECT_TRUE(pinned_tab_manager_->PinTabs({tab_handle}));
  EXPECT_TRUE(pinned_tab_manager_->IsTabPinned(tab_handle));
  EXPECT_EQ(1u, pinned_tab_manager_->GetNumPinnedTabs());


  // Check that the callback was called with pinned=true.
  {
    auto [result_interface, result_pinned] = pin_status_future.Get();
    EXPECT_EQ(tab_interface, result_interface);
    EXPECT_TRUE(result_pinned);
  }
}

IN_PROC_BROWSER_TEST_F(GlicPinnedTabManagerBrowserTest, unpinTabs) {
  CreateAndAddTab("/why-cats-are-liquid");

  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  tabs::TabInterface* tab_interface =
      tabs::TabInterface::GetFromContents(tab_strip_model->GetWebContentsAt(1));
  ASSERT_TRUE(tab_interface);
  const tabs::TabHandle tab_handle = tab_interface->GetHandle();

  // Pin a tab and verify it was pinned.
  EXPECT_TRUE(pinned_tab_manager_->PinTabs({tab_handle}));
  EXPECT_TRUE(pinned_tab_manager_->IsTabPinned(tab_handle));
  EXPECT_EQ(1u, pinned_tab_manager_->GetNumPinnedTabs());


  base::test::TestFuture<tabs::TabInterface*, bool> pin_status_future;
  auto subscription = pinned_tab_manager_->AddTabPinningStatusChangedCallback(
      pin_status_future.GetRepeatingCallback());

  // Unpin the tab and verify it was unpinned.
  EXPECT_TRUE(pinned_tab_manager_->UnpinTabs({tab_handle}));
  EXPECT_FALSE(pinned_tab_manager_->IsTabPinned(tab_handle));
  EXPECT_EQ(0u, pinned_tab_manager_->GetNumPinnedTabs());

  // Check that the callback was called with pinned=false.
  {
    auto [result_interface, result_pinned] = pin_status_future.Get();
    EXPECT_EQ(tab_interface, result_interface);
    EXPECT_FALSE(result_pinned);
  }
}

}  // namespace glic