File: assistant_test_mixin.h

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 (145 lines) | stat: -rw-r--r-- 5,832 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
// 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.

#ifndef CHROME_BROWSER_UI_ASH_ASSISTANT_ASSISTANT_TEST_MIXIN_H_
#define CHROME_BROWSER_UI_ASH_ASSISTANT_ASSISTANT_TEST_MIXIN_H_

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

#include "base/time/time.h"
#include "chrome/browser/ui/ash/assistant/test_support/fake_s3_server.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"

class PrefService;

namespace ash {
class AssistantTestApi;
}

namespace ash::assistant {

class LoggedInUserMixin;

// Default wait time before we conclude the wait actions have timed out.
constexpr base::TimeDelta kDefaultWaitTimeout = base::Seconds(10);

// Creates everything required to test the Assistant in browser tests.
// This includes:
//     - Installing a fake Gaia server so the test user is able to use the
//       Assistant.
//     - Setting up a fake S3 server to spoof fake interactions.
//     - Enabling the Assistant service.
//     - Disabling all Assistant animations.
//
// See definition of `FakeS3Server` for an explanation of the different modes
// the fake S3 server can run in (specified by passing `FakeS3Mode` into the
// constructor).
class AssistantTestMixin : public InProcessBrowserTestMixin {
 public:
  AssistantTestMixin(InProcessBrowserTestMixinHost* host,
                     InProcessBrowserTest* test_base,
                     net::EmbeddedTestServer* embedded_test_server,
                     FakeS3Mode mode,
                     int test_data_version);

  AssistantTestMixin(const AssistantTestMixin&) = delete;
  AssistantTestMixin& operator=(const AssistantTestMixin&) = delete;

  ~AssistantTestMixin() override;

  // InProcessBrowserTestMixin overrides:
  void SetUpCommandLine(base::CommandLine* command_line) override;
  void SetUpOnMainThread() override;
  void TearDownOnMainThread() override;

  void DisableFakeS3Server();

  // Starts the Assistant service and wait until it is ready to process
  // queries. Should be called as the first action in every test.
  void StartAssistantAndWaitForReady(
      base::TimeDelta wait_timeout = kDefaultWaitTimeout);

  // Changes the user setting controlling if the user has enabled Assistant.
  void SetAssistantEnabled(bool enabled);

  // Changes the user setting controlling if the user prefers voice or keyboard.
  void SetPreferVoice(bool prefer_voice);

  // Submits a text query. Can only be used when the Assistant UI is visible and
  // displaying the query input text field.
  void SendTextQuery(const std::string& query);

  // Synchronize an async method call to make testing simpler. |func| is the
  // async method to be invoked, the inner callback is the result callback. The
  // result with type |T| will be the return value.
  //
  // NOTE: This is a template method. If you need to use it with a new type,
  // you may see a link error. You will need to manually instantiate for the
  // new type.  Please see .cc file for examples.
  template <typename T>
  T SyncCall(base::OnceCallback<void(base::OnceCallback<void(T)>)> func);

  // Waits until a card response is rendered that contains the given text.
  // If |expected_response| is not received in |wait_timeout|, this will fail
  // the test.
  void ExpectCardResponse(const std::string& expected_response,
                          base::TimeDelta wait_timeout = kDefaultWaitTimeout);

  // Waits until an error response is rendered that contains the given text. If
  // |expected_response| is not received in |wait_timeout|, this will fail the
  // test.
  void ExpectErrorResponse(const std::string& expected_response,
                           base::TimeDelta wait_timeout = kDefaultWaitTimeout);

  // Waits until a text response is rendered that contains the given text.
  // If |expected_response| is not received in |wait_timeout|, this will fail
  // the test.
  void ExpectTextResponse(const std::string& expected_response,
                          base::TimeDelta wait_timeout = kDefaultWaitTimeout);

  // Same as above but checks if any of the given responses are encountered.
  void ExpectAnyOfTheseTextResponses(
      const std::vector<std::string>& expected_responses,
      base::TimeDelta wait_timeout = kDefaultWaitTimeout);

  // Waits until a timers response is rendered that contains the given timers.
  // If the expected response is not received in |wait_timeout|, this will fail
  // the test.
  void ExpectTimersResponse(const std::vector<base::TimeDelta>& timers,
                            base::TimeDelta wait_timeout = kDefaultWaitTimeout);

  // Waits until a timers response is rendered and returns the time remaining of
  // the rendered timers. If a timers response is not received in |wait_timeout|
  // this will fail the test.
  std::vector<base::TimeDelta> ExpectAndReturnTimersResponse(
      base::TimeDelta wait_timeout = kDefaultWaitTimeout);

  // Presses the Assistant key, which will toggle the Assistant UI.
  void PressAssistantKey();

  // Returns true if the Assistant UI is currently visible.
  bool IsVisible();

  // Watches the view hierarchy for change and fails if
  // a view is updated / deleted / added before wait_timeout time elapses.
  void ExpectNoChange(base::TimeDelta wait_timeout = kDefaultWaitTimeout);

 private:
  PrefService* GetUserPreferences();
  void SendKeyPress(::ui::KeyboardCode key);
  void DisableAssistant();

  FakeS3Server fake_s3_server_;
  FakeS3Mode mode_;
  std::unique_ptr<AssistantTestApi> test_api_;
  std::unique_ptr<LoggedInUserMixin> user_mixin_;
};

}  // namespace ash::assistant

#endif  // CHROME_BROWSER_UI_ASH_ASSISTANT_ASSISTANT_TEST_MIXIN_H_