File: chrome_autocomplete_provider_client_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (174 lines) | stat: -rw-r--r-- 6,293 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
// 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/autocomplete/chrome_autocomplete_provider_client.h"

#include <memory>

#include "base/functional/bind.h"
#include "chrome/browser/ui/lens/test_lens_search_controller.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/tabs/test_util.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/lens/lens_overlay_invocation_source.h"
#include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/omnibox_controller.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/fake_service_worker_context.h"
#include "content/public/test/test_storage_partition.h"
#include "gmock/gmock.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h"

namespace {

class MockLensSearchController : public lens::TestLensSearchController {
 public:
  explicit MockLensSearchController(tabs::TabInterface* tab)
      : lens::TestLensSearchController(tab) {}

  MOCK_METHOD(void,
              OpenLensOverlay,
              (lens::LensOverlayInvocationSource invocation_source),
              (override));

  MOCK_METHOD(void,
              StartContextualization,
              (lens::LensOverlayInvocationSource invocation_source),
              (override));
};

class TestTabFeatures : public tabs::TabFeatures {
 protected:
  std::unique_ptr<LensSearchController> CreateLensController(
      tabs::TabInterface* tab) override {
    return std::make_unique<MockLensSearchController>(tab);
  }
};

std::unique_ptr<tabs::TabFeatures> CreateTabFeatures() {
  return std::make_unique<TestTabFeatures>();
}

}  // namespace

class ChromeAutocompleteProviderClientTest : public InProcessBrowserTest {
 protected:
  ChromeAutocompleteProviderClientTest() {
    tabs::TabFeatures::ReplaceTabFeaturesForTesting(
        base::BindRepeating(&CreateTabFeatures));
  }

  ~ChromeAutocompleteProviderClientTest() override {
    tabs::TabFeatures::ReplaceTabFeaturesForTesting(base::NullCallback());
  }

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    client_ = std::make_unique<ChromeAutocompleteProviderClient>(
        browser()->profile());
    storage_partition_.set_service_worker_context(&service_worker_context_);
    client_->set_storage_partition(&storage_partition_);
  }

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

  ChromeAutocompleteProviderClient* GetAutocompleteProviderClient() {
    return static_cast<ChromeAutocompleteProviderClient*>(
        BrowserView::GetBrowserViewForBrowser(browser())
            ->toolbar()
            ->location_bar()
            ->omnibox_view()
            ->controller()
            ->autocomplete_controller()
            ->autocomplete_provider_client());
  }

  MockLensSearchController* GetLensSearchController() {
    return static_cast<MockLensSearchController*>(
        browser()
            ->GetActiveTabInterface()
            ->GetTabFeatures()
            ->lens_search_controller());
  }

  // Replaces the client with one using an incognito profile. Note that this is
  // a one-way operation. Once a TEST_F calls this, all interactions with
  // |client_| will be off the record.
  void GoOffTheRecord() {
    client_ = std::make_unique<ChromeAutocompleteProviderClient>(
        browser()->profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true));
  }

  std::unique_ptr<ChromeAutocompleteProviderClient> client_;
  content::FakeServiceWorkerContext service_worker_context_;
  tabs::PreventTabFeatureInitialization prevent_;

 private:
  content::TestStoragePartition storage_partition_;
};

IN_PROC_BROWSER_TEST_F(ChromeAutocompleteProviderClientTest,
                       OpenLensOverlay_Show) {
  EXPECT_CALL(*GetLensSearchController(), OpenLensOverlay(testing::_))
      .Times(1)
      .WillOnce(testing::Invoke(
          [](lens::LensOverlayInvocationSource invocation_source) {
            EXPECT_EQ(lens::LensOverlayInvocationSource::kOmniboxPageAction,
                      invocation_source);
          }));

  GetAutocompleteProviderClient()->OpenLensOverlay(/*show=*/true);
}

IN_PROC_BROWSER_TEST_F(ChromeAutocompleteProviderClientTest,
                       OpenLensOverlay_DontShow) {
  EXPECT_CALL(*GetLensSearchController(), StartContextualization(testing::_))
      .Times(1)
      .WillOnce(testing::Invoke(
          [](lens::LensOverlayInvocationSource invocation_source) {
            EXPECT_EQ(lens::LensOverlayInvocationSource::kOmnibox,
                      invocation_source);
          }));

  GetAutocompleteProviderClient()->OpenLensOverlay(/*show=*/false);
}

IN_PROC_BROWSER_TEST_F(ChromeAutocompleteProviderClientTest,
                       StartServiceWorker) {
  GURL destination_url("https://google.com/search?q=puppies");

  client_->StartServiceWorker(destination_url);
  EXPECT_TRUE(service_worker_context_
                  .start_service_worker_for_navigation_hint_called());
}

IN_PROC_BROWSER_TEST_F(ChromeAutocompleteProviderClientTest,
                       DontStartServiceWorkerInIncognito) {
  GURL destination_url("https://google.com/search?q=puppies");

  GoOffTheRecord();
  client_->StartServiceWorker(destination_url);
  EXPECT_FALSE(service_worker_context_
                   .start_service_worker_for_navigation_hint_called());
}

IN_PROC_BROWSER_TEST_F(ChromeAutocompleteProviderClientTest,
                       DontStartServiceWorkerIfSuggestDisabled) {
  GURL destination_url("https://google.com/search?q=puppies");

  browser()->profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled,
                                               false);
  client_->StartServiceWorker(destination_url);
  EXPECT_FALSE(service_worker_context_
                   .start_service_worker_for_navigation_hint_called());
}