File: autocomplete_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (342 lines) | stat: -rw-r--r-- 14,222 bytes parent folder | download | duplicates (2)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/mock_callback.h"
#include "base/time/time.h"
#include "base/version_info/version_info.h"
#include "build/build_config.h"
#include "chrome/browser/autofill/autocomplete_history_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/webdata_services/web_data_service_factory.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/test_autofill_manager_injector.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager_test_utils.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#include "components/autofill/core/browser/foundations/test_autofill_manager_waiter.h"
#include "components/autofill/core/browser/single_field_fillers/autocomplete/autocomplete_history_manager.h"
#include "components/autofill/core/browser/suggestions/suggestion.h"
#include "components/autofill/core/browser/suggestions/suggestion_test_helpers.h"
#include "components/autofill/core/browser/suggestions/suggestions_context.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/browser/test_utils/test_autofill_clock.h"
#include "components/autofill/core/common/autofill_clock.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/switches.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"

namespace autofill {

namespace {

using ::base::UTF8ToUTF16;
using ::testing::_;
using ::testing::AssertionResult;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;

const char kDefaultAutocompleteInputId[] = "n300";
const char kSimpleFormFileName[] = "autocomplete_simple_form.html";

class AutocompleteTest : public InProcessBrowserTest {
 public:
  class TestAutofillManager : public BrowserAutofillManager {
   public:
    explicit TestAutofillManager(ContentAutofillDriver* driver)
        : BrowserAutofillManager(driver) {}

    TestAutofillManagerWaiter& text_field_change_waiter() {
      return text_field_change_waiter_;
    }
    TestAutofillManagerWaiter& forms_seen_waiter() {
      return forms_seen_waiter_;
    }
    TestAutofillManagerWaiter& form_submitted_waiter() {
      return form_submitted_waiter_;
    }

   private:
    TestAutofillManagerWaiter text_field_change_waiter_{
        *this,
        {AutofillManagerEvent::kTextFieldValueChanged}};
    TestAutofillManagerWaiter forms_seen_waiter_{
        *this,
        {AutofillManagerEvent::kFormsSeen}};
    TestAutofillManagerWaiter form_submitted_waiter_{
        *this,
        {AutofillManagerEvent::kFormSubmitted}};
  };

  TestAutofillManager* autofill_manager() {
    return autofill_manager(web_contents()->GetPrimaryMainFrame());
  }

  TestAutofillManager* autofill_manager(content::RenderFrameHost* rfh) {
    return autofill_manager_injector_[rfh];
  }

 protected:
  void SetUpOnMainThread() override {
    active_browser_ = browser();

    ASSERT_TRUE(embedded_test_server()->Start());
  }

  void TearDownOnMainThread() override {
    // RunUntilIdle() is necessary because otherwise, under the hood
    // PasswordFormManager::OnFetchComplete() callback is run after this test is
    // destroyed meaning that OsCryptImpl will be used instead of OsCryptMocker,
    // causing this test to fail.
    base::RunLoop().RunUntilIdle();
    // Make sure to close any showing popups prior to tearing down the UI.
    ContentAutofillDriver::GetForRenderFrameHost(
        web_contents()->GetPrimaryMainFrame())
        ->GetAutofillManager()
        .client()
        .HideAutofillSuggestions(SuggestionHidingReason::kTabGone);
    active_browser_ = nullptr;
  }

  // Necessary to avoid flakiness or failure due to input arriving
  // before the first compositor commit.
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(blink::switches::kAllowPreCommitInput);
  }

  // Uses the browser to open the file named `filename` based on the given
  // `disposition`.
  void NavigateToFile(const char* filename,
                      const WindowOpenDisposition& disposition =
                          WindowOpenDisposition::NEW_FOREGROUND_TAB) {
    GURL url =
        embedded_test_server()->GetURL(base::StrCat({"/autofill/", filename}));
    NavigateParams params(active_browser_, url, ui::PAGE_TRANSITION_LINK);
    params.disposition = disposition;
    ui_test_utils::NavigateToURL(&params);
  }

  // Fills in the default input with |value|, submits the form and waits
  // for the value to have been saved in the DB or skipped, via observers.
  void FillInputAndSubmit(const std::string& value) {
    const std::string js =
        base::StringPrintf(R"(document.getElementById('%s').focus();
           document.onclick = function() {
             document.getElementById('testform').submit();
           };)",
                           kDefaultAutocompleteInputId);
    ASSERT_TRUE(content::ExecJs(web_contents(), js));

    for (const char c : value) {
      ui::DomKey key = ui::DomKey::FromCharacter(c);
      ui::KeyboardCode key_code = ui::NonPrintableDomKeyToKeyboardCode(key);
      ui::DomCode code = ui::UsLayoutKeyboardCodeToDomCode(key_code);
      content::SimulateKeyPress(web_contents(), key, code, key_code, false,
                                false, false, false);
      ASSERT_TRUE(autofill_manager()->text_field_change_waiter().Wait(1));
    }

    // Simulate a mouse click to submit the form because form submissions not
    // triggered by user gestures are ignored.
    content::SimulateMouseClick(
        active_browser_->tab_strip_model()->GetActiveWebContents(), 0,
        blink::WebMouseEvent::Button::kLeft);
    ASSERT_TRUE(autofill_manager()->form_submitted_waiter().Wait(1));

    // Wait for data to have been saved in the DB.
    WaitForPendingDBTasks(*GetWebDataService());
  }

  // The retention policy clean-up is run once per major version during
  // initialization. This function triggers it by reinitializing the
  // `autocomplete_history_manager()` and waiting for the cleanup to complete.
  void TriggerRetentionPolicyCleanup() {
    pref_service()->SetInteger(prefs::kAutocompleteLastVersionRetentionPolicy,
                               version_info::GetMajorVersionNumberAsInt() - 1);
    autocomplete_history_manager()->Init(
        WebDataServiceFactory::GetAutofillWebDataForProfile(
            current_profile(), ServiceAccessType::EXPLICIT_ACCESS),
        pref_service(), current_profile()->IsOffTheRecord());
    WaitForPendingDBTasks(*GetWebDataService());
  }

  void set_active_browser(Browser* browser) { active_browser_ = browser; }

  AutocompleteHistoryManager* autocomplete_history_manager() {
    return AutocompleteHistoryManagerFactory::GetForProfile(current_profile());
  }

  PrefService* pref_service() { return active_browser_->profile()->GetPrefs(); }

  std::vector<Suggestion> GetAutocompleteSuggestions(
      const std::string& input_name,
      const std::string& prefix) {
    base::MockCallback<SingleFieldFillRouter::OnSuggestionsReturnedCallback>
        mock_callback;
    std::vector<Suggestion> suggestions;
    EXPECT_CALL(mock_callback, Run).WillOnce(testing::SaveArg<1>(&suggestions));
    SingleFieldFillRouter::OnSuggestionsReturnedCallback callback =
        mock_callback.Get();
    EXPECT_TRUE(autocomplete_history_manager()->OnGetSingleFieldSuggestions(
        test::CreateTestFormField(/*label=*/"", input_name, prefix,
                                  FormControlType::kInputText),
        autofill_manager()->client(), callback));

    // Make sure the DB task gets executed.
    WaitForPendingDBTasks(*GetWebDataService());

    return suggestions;
  }

  scoped_refptr<AutofillWebDataService> GetWebDataService() {
    return WebDataServiceFactory::GetAutofillWebDataForProfile(
        current_profile(), ServiceAccessType::EXPLICIT_ACCESS);
  }

 private:
  content::WebContents* web_contents() {
    return active_browser_->tab_strip_model()->GetActiveWebContents();
  }

  Profile* current_profile() { return active_browser_->profile(); }

  test::AutofillBrowserTestEnvironment autofill_test_environment_;
  TestAutofillManagerInjector<TestAutofillManager> autofill_manager_injector_;
  raw_ptr<Browser> active_browser_ = nullptr;
};

// Tests that a user can save a simple Autocomplete value.
IN_PROC_BROWSER_TEST_F(AutocompleteTest, SubmitSimpleValue_Saves) {
  std::string prefix = "Some";
  std::string test_value = "SomeName!";
  NavigateToFile(kSimpleFormFileName);
  ASSERT_TRUE(autofill_manager()->forms_seen_waiter().Wait(1));
  FillInputAndSubmit(test_value);
  EXPECT_THAT(GetAutocompleteSuggestions(kDefaultAutocompleteInputId, prefix),
              SuggestionVectorMainTextsAre(Suggestion::Text(
                  UTF8ToUTF16(test_value), Suggestion::Text::IsPrimary(true))));
}

// Tests that we don't save new autocomplete entries when in Incognito.
IN_PROC_BROWSER_TEST_F(AutocompleteTest,
                       SubmitSimpleValue_OTR_DoesNotSave) {
  set_active_browser(CreateIncognitoBrowser());

  std::string prefix = "Some";
  std::string test_value = "SomeName!";
  NavigateToFile(kSimpleFormFileName, WindowOpenDisposition::OFF_THE_RECORD);
  ASSERT_TRUE(autofill_manager()->forms_seen_waiter().Wait(1));
  FillInputAndSubmit(test_value);
  EXPECT_THAT(GetAutocompleteSuggestions(kDefaultAutocompleteInputId, ""),
              IsEmpty());
}

// Tests that we don't save new autocomplete entries when Autocomplete was
// disabled by the user.
IN_PROC_BROWSER_TEST_F(AutocompleteTest,
                       SubmitSimpleValue_Disabled_DoesNotSave) {
  pref_service()->SetBoolean(prefs::kAutofillProfileEnabled, false);
  std::string prefix = "Some";
  std::string test_value = "SomeName!";
  NavigateToFile(kSimpleFormFileName);
  ASSERT_TRUE(autofill_manager()->forms_seen_waiter().Wait(1));
  FillInputAndSubmit(test_value);
  EXPECT_THAT(GetAutocompleteSuggestions(kDefaultAutocompleteInputId, ""),
              IsEmpty());
}

// Tests that initialization of the AutocompleteHistoryManager sets the
// retention policy last version ran preference when the flag is enabled.
IN_PROC_BROWSER_TEST_F(AutocompleteTest,
                       RetentionPolicy_Init_SavesVersionPref) {
  // Navigate to a file and wait, this will make sure we instantiate
  // AutocompleteHistoryManager.
  NavigateToFile(kSimpleFormFileName);
  ASSERT_TRUE(autofill_manager()->forms_seen_waiter().Wait(1));

  // The checkup is executed asynchronously on startup and may not have
  // finished, yet.
  WaitForPrefValue(pref_service(),
                   prefs::kAutocompleteLastVersionRetentionPolicy,
                   base::Value(version_info::GetMajorVersionNumberAsInt()));

  int saved_version = pref_service()->GetInteger(
      prefs::kAutocompleteLastVersionRetentionPolicy);
  EXPECT_EQ(version_info::GetMajorVersionNumberAsInt(), saved_version);
}

// Tests that the retention policy cleanup removes an expired entry.
IN_PROC_BROWSER_TEST_F(AutocompleteTest,
                       RetentionPolicy_RemovesExpiredEntry) {
  TestAutofillClock test_clock(AutofillClock::Now());

  // Add an entry.
  std::string prefix = "Some";
  std::string test_value = "SomeName!";
  NavigateToFile(kSimpleFormFileName);
  ASSERT_TRUE(autofill_manager()->forms_seen_waiter().Wait(1));
  FillInputAndSubmit(test_value);
  EXPECT_THAT(GetAutocompleteSuggestions(kDefaultAutocompleteInputId, prefix),
              SuggestionVectorMainTextsAre(Suggestion::Text(
                  UTF8ToUTF16(test_value), Suggestion::Text::IsPrimary(true))));

  // Advance time to expire the entry.
  test_clock.Advance(2 * kAutocompleteRetentionPolicyPeriod);

  TriggerRetentionPolicyCleanup();

  EXPECT_THAT(GetAutocompleteSuggestions(kDefaultAutocompleteInputId, ""),
              IsEmpty());
}

// Tests that the retention policy cleanup does not remove a valid entry (e.g.
// 20 days old).
IN_PROC_BROWSER_TEST_F(AutocompleteTest,
                       RetentionPolicy_DoesNot_RemoveValidEntry) {
  TestAutofillClock test_clock(AutofillClock::Now());

  // Add an entry.
  std::string prefix = "Some";
  std::string test_value = "SomeName!";
  NavigateToFile(kSimpleFormFileName);
  ASSERT_TRUE(autofill_manager()->forms_seen_waiter().Wait(1));
  FillInputAndSubmit(test_value);
  EXPECT_THAT(GetAutocompleteSuggestions(kDefaultAutocompleteInputId, prefix),
              SuggestionVectorMainTextsAre(Suggestion::Text(
                  UTF8ToUTF16(test_value), Suggestion::Text::IsPrimary(true))));

  // Advance time by less than `kAutocompleteRetentionPolicyPeriod`.
  test_clock.Advance(kAutocompleteRetentionPolicyPeriod - base::Days(2));

  TriggerRetentionPolicyCleanup();

  // Verify that the entry is still there.
  EXPECT_THAT(GetAutocompleteSuggestions(kDefaultAutocompleteInputId, prefix),
              SuggestionVectorMainTextsAre(Suggestion::Text(
                  UTF8ToUTF16(test_value), Suggestion::Text::IsPrimary(true))));
}

}  // namespace

}  // namespace autofill