-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
assistant_browsertest.cc
279 lines (209 loc) · 8.68 KB
/
assistant_browsertest.cc
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/components/audio/cras_audio_handler.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/time/time.h"
#include "chrome/browser/ui/ash/assistant/assistant_test_mixin.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/assistant/test_support/expect_utils.h"
#include "chromeos/dbus/power_manager/backlight.pb.h"
#include "chromeos/services/assistant/public/cpp/features.h"
#include "chromeos/services/assistant/service.h"
#include "content/public/test/browser_test.h"
namespace chromeos {
namespace assistant {
namespace {
using ::ash::CrasAudioHandler;
// Please remember to set auth token when running in |kProxy| mode.
constexpr auto kMode = FakeS3Mode::kReplay;
// Update this when you introduce breaking changes to existing tests.
constexpr int kVersion = 1;
constexpr int kStartBrightnessPercent = 50;
// Ensures that |value_| is within the range {min_, max_}. If it isn't, this
// will print a nice error message.
#define EXPECT_WITHIN_RANGE(min_, value_, max_) \
({ \
EXPECT_TRUE(min_ <= value_ && value_ <= max_) \
<< "Expected " << value_ << " to be within the range " \
<< "{" << min_ << ", " << max_ << "}."; \
})
} // namespace
using chromeos::assistant::test::ExpectResult;
class AssistantBrowserTest : public MixinBasedInProcessBrowserTest {
public:
AssistantBrowserTest() {
// TODO(b/190633242): enable sandbox in browser tests.
feature_list_.InitAndDisableFeature(
chromeos::assistant::features::kEnableLibAssistantSandbox);
}
~AssistantBrowserTest() override = default;
AssistantTestMixin* tester() { return &tester_; }
void ShowAssistantUi() {
if (!tester()->IsVisible())
tester()->PressAssistantKey();
}
void CloseAssistantUi() {
if (tester()->IsVisible())
tester()->PressAssistantKey();
}
void InitializeBrightness() {
auto* power_manager = chromeos::PowerManagerClient::Get();
power_manager::SetBacklightBrightnessRequest request;
request.set_percent(kStartBrightnessPercent);
request.set_transition(
power_manager::SetBacklightBrightnessRequest_Transition_INSTANT);
request.set_cause(
power_manager::SetBacklightBrightnessRequest_Cause_USER_REQUEST);
chromeos::PowerManagerClient::Get()->SetScreenBrightness(request);
// Wait for the initial value to settle.
ExpectResult(
true, base::BindLambdaForTesting([&]() {
constexpr double kEpsilon = 0.1;
auto current_brightness = tester()->SyncCall(base::BindOnce(
&chromeos::PowerManagerClient::GetScreenBrightnessPercent,
base::Unretained(power_manager)));
return current_brightness &&
std::abs(kStartBrightnessPercent -
current_brightness.value()) < kEpsilon;
}));
}
void ExpectBrightnessUp() {
auto* power_manager = chromeos::PowerManagerClient::Get();
// Check the brightness changes
ExpectResult(
true, base::BindLambdaForTesting([&]() {
constexpr double kEpsilon = 1;
auto current_brightness = tester()->SyncCall(base::BindOnce(
&chromeos::PowerManagerClient::GetScreenBrightnessPercent,
base::Unretained(power_manager)));
return current_brightness && (current_brightness.value() -
kStartBrightnessPercent) > kEpsilon;
}));
}
void ExpectBrightnessDown() {
auto* power_manager = chromeos::PowerManagerClient::Get();
// Check the brightness changes
ExpectResult(
true, base::BindLambdaForTesting([&]() {
constexpr double kEpsilon = 1;
auto current_brightness = tester()->SyncCall(base::BindOnce(
&chromeos::PowerManagerClient::GetScreenBrightnessPercent,
base::Unretained(power_manager)));
return current_brightness && (kStartBrightnessPercent -
current_brightness.value()) > kEpsilon;
}));
}
private:
base::test::ScopedFeatureList feature_list_;
AssistantTestMixin tester_{&mixin_host_, this, embedded_test_server(), kMode,
kVersion};
DISALLOW_COPY_AND_ASSIGN(AssistantBrowserTest);
};
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest,
ShouldOpenAssistantUiWhenPressingAssistantKey) {
tester()->StartAssistantAndWaitForReady();
tester()->PressAssistantKey();
EXPECT_TRUE(tester()->IsVisible());
}
// TODO(b/184802501): Fix this flaky test.
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest,
DISABLED_ShouldDisplayTextResponse) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
tester()->SendTextQuery("test");
tester()->ExpectAnyOfTheseTextResponses({
"No one told me there would be a test",
"You're coming in loud and clear",
"debug OK",
"I can assure you, this thing's on",
"Is this thing on?",
});
}
// Flaky. See https://crbug.com/1196560.
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest,
DISABLED_ShouldDisplayCardResponse) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
ASSERT_TRUE(tester()->IsVisible());
tester()->SendTextQuery("What is the highest mountain in the world?");
tester()->ExpectCardResponse("Mount Everest");
}
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest, ShouldTurnUpVolume) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
ASSERT_TRUE(tester()->IsVisible());
auto* cras = CrasAudioHandler::Get();
constexpr int kStartVolumePercent = 50;
cras->SetOutputVolumePercent(kStartVolumePercent);
EXPECT_EQ(kStartVolumePercent, cras->GetOutputVolumePercent());
tester()->SendTextQuery("turn up volume");
ExpectResult(true, base::BindRepeating(
[](CrasAudioHandler* cras) {
return cras->GetOutputVolumePercent() >
kStartVolumePercent;
},
cras));
}
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest, ShouldTurnDownVolume) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
ASSERT_TRUE(tester()->IsVisible());
auto* cras = CrasAudioHandler::Get();
constexpr int kStartVolumePercent = 50;
cras->SetOutputVolumePercent(kStartVolumePercent);
EXPECT_EQ(kStartVolumePercent, cras->GetOutputVolumePercent());
tester()->SendTextQuery("turn down volume");
ExpectResult(true, base::BindRepeating(
[](CrasAudioHandler* cras) {
return cras->GetOutputVolumePercent() <
kStartVolumePercent;
},
cras));
}
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest, ShouldTurnUpBrightness) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
ASSERT_TRUE(tester()->IsVisible());
InitializeBrightness();
tester()->SendTextQuery("turn up brightness");
ExpectBrightnessUp();
}
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest, ShouldTurnDownBrightness) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
ASSERT_TRUE(tester()->IsVisible());
InitializeBrightness();
tester()->SendTextQuery("turn down brightness");
ExpectBrightnessDown();
}
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest,
ShouldPuntWhenChangingUnsupportedSetting) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
ASSERT_TRUE(tester()->IsVisible());
tester()->SendTextQuery("enable night mode");
tester()->ExpectTextResponse("Night Mode isn't available on your device");
}
// TODO(crbug.com/1112278): Disabled because it's flaky.
IN_PROC_BROWSER_TEST_F(AssistantBrowserTest,
DISABLED_ShouldShowSingleErrorOnNetworkDown) {
tester()->StartAssistantAndWaitForReady();
ShowAssistantUi();
ASSERT_TRUE(tester()->IsVisible());
tester()->DisableFakeS3Server();
base::RunLoop().RunUntilIdle();
tester()->SendTextQuery("Is this thing on?");
tester()->ExpectErrorResponse(
"Something went wrong. Try again in a few seconds");
// Make sure no further changes happen to the view hierarchy.
tester()->ExpectNoChange(base::TimeDelta::FromSeconds(1));
// This is necessary to prevent a UserInitiatedVoicelessActivity from
// blocking test harness teardown while we wait on assistant to finish
// the interaction.
CloseAssistantUi();
}
} // namespace assistant
} // namespace chromeos