File: fake_autocomplete_controller.h

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 (125 lines) | stat: -rw-r--r-- 5,148 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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_OMNIBOX_BROWSER_FAKE_AUTOCOMPLETE_CONTROLLER_H_
#define COMPONENTS_OMNIBOX_BROWSER_FAKE_AUTOCOMPLETE_CONTROLLER_H_

#include <memory>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_match_type.h"
#include "components/omnibox/browser/fake_autocomplete_provider.h"

class FakeAutocompleteControllerObserver
    : public AutocompleteController::Observer {
 public:
  void OnResultChanged(AutocompleteController* controller,
                       bool default_match_changed) override;
  void OnAutocompleteStopTimerTriggered(
      const AutocompleteInput& input) override;
  int on_result_changed_call_count_ = 0;
  bool last_default_match_changed = false;
  int on_autocomplete_stop_timer_stopped_call_count = 0;
};

class FakeAutocompleteController : public AutocompleteController {
 public:
  FakeAutocompleteController(
      raw_ptr<base::test::SingleThreadTaskEnvironment> task_environment);
  ~FakeAutocompleteController() override;

  // Getter for `providers_`.
  template <typename T = FakeAutocompleteProvider>
  T& GetFakeProvider(size_t index = 0) {
    DCHECK_LT(index, providers_.size());
    return *static_cast<T*>(providers_[index].get());
  }

  // Helper to create `AutocompleteInput`.
  static AutocompleteInput CreateInput(
      std::u16string text,
      bool omit_async = false,
      bool prevent_inline_autocomplete = false);

  // Simulates an autocomplete pass.
  // - Also verifies exactly 1 notification occurs after the pass, at the
  //   correct time (which depends on the type of pass simulated), and no
  //   additional notification occurred. This means `SimulateAutocompletePass()`
  //   waits for the notification. So if the test needs to interrupt the
  //   notification, it can't use `SimulateAutocompletePass()`.
  // - Returns the match contents.
  // - Does not reset `internal_result_`. Can be used to simulate a sequence
  //   of passes.
  // - `input` is only used if `sync` is true.
  std::vector<std::string> SimulateAutocompletePass(
      bool sync,
      bool done,
      std::vector<AutocompleteMatch> matches,
      AutocompleteInput input = CreateInput(u"test"));

  // Simulates a `kSyncPassOnly` (the simplest pass) with a clean
  // `internal_result_`. See `SimulateAutocompletePass()`'s comment.
  std::vector<std::string> SimulateCleanAutocompletePass(
      std::vector<AutocompleteMatch> matches);

  // Simulates the expire pass.
  // - Also verifies exactly 1 notification occur at the correct time, and no
  //   additional notification occurred. This means `SimulateExpirePass()` waits
  //   for the notification. So if the test needs to interrupt the notification,
  //   it can't use `SimulateExpirePass()`.
  // - Returns the match contents.
  std::vector<std::string> SimulateExpirePass();

  // Helper to get minimal representation of the controller results: a vector of
  // each match's contents.
  std::vector<std::string> GetResultContents(bool published);

  // Verifies the controller sends a single notification at `delay_ms`, and no
  // notification before.
  void ExpectOnResultChanged(
      int delay_ms,
      AutocompleteController::UpdateType last_update_type);

  // Verifies the controller stops after `delay_ms` with no notification.
  // `explicit_stop` is whether the stop was user triggered, rather than the
  // stop timer triggering `Stop()`.
  void ExpectStopAfter(int delay_ms, bool explicit_stop = false);

  // Verifies neither a notification nor stop occur.
  void ExpectNoNotificationOrStop();

  // AutocompleteController (structs):
  using AutocompleteController::OldResult;

  // AutocompleteController (methods):
  using AutocompleteController::CheckWhetherDefaultMatchChanged;
  using AutocompleteController::MaybeRemoveCompanyEntityImages;
  using AutocompleteController::ShouldRunProvider;
  using AutocompleteController::UpdateAssociatedKeywords;
  using AutocompleteController::UpdateResult;
  using AutocompleteController::UpdateSearchboxStats;
  using AutocompleteController::UpdateShownInSession;

  // AutocompleteController (fields):
  using AutocompleteController::input_;
  using AutocompleteController::internal_result_;
  using AutocompleteController::keyword_provider_;
  using AutocompleteController::last_time_default_match_changed_;
  using AutocompleteController::last_update_type_;
  using AutocompleteController::metrics_;
  using AutocompleteController::providers_;
  using AutocompleteController::published_result_;
  using AutocompleteController::template_url_service_;

  // Used to verify the correct number of notifications occur.
  std::unique_ptr<FakeAutocompleteControllerObserver> observer_;
  // Used to simulate time passing.
  raw_ptr<base::test::SingleThreadTaskEnvironment> task_environment_;
};

#endif  // COMPONENTS_OMNIBOX_BROWSER_FAKE_AUTOCOMPLETE_CONTROLLER_H_