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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
ash / system / phonehub / phone_hub_recent_apps_view_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/phonehub/phone_hub_recent_apps_view.h"
#include "ash/constants/ash_features.h"
#include "ash/system/phonehub/phone_connected_view.h"
#include "ash/system/phonehub/phone_hub_recent_app_button.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ash/components/phonehub/app_stream_launcher_data_model.h"
#include "chromeos/ash/components/phonehub/fake_phone_hub_manager.h"
#include "chromeos/ash/components/phonehub/fake_recent_apps_interaction_handler.h"
#include "chromeos/ash/components/phonehub/notification.h"
#include "chromeos/ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
#include "ui/events/test/test_event.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/test/button_test_api.h"
namespace ash {
namespace {
using FeatureState = multidevice_setup::mojom::FeatureState;
using RecentAppsUiState =
phonehub::RecentAppsInteractionHandler::RecentAppsUiState;
using RecentAppsViewUiState = phone_hub_metrics::RecentAppsViewUiState;
constexpr char kRecentAppsStateOnBubbleOpenedHistogramName[] =
"PhoneHub.RecentApps.State.OnBubbleOpened";
constexpr char kRecentAppsTransitionToFailedLatencyHistogramName[] =
"PhoneHub.RecentApps.TransitionToFailed.Latency";
constexpr char kRecentAppsTransitionToSuccessLatencyHistogramName[] =
"PhoneHub.RecentApps.TransitionToSuccess.Latency";
const char16_t kAppName[] = u"Test App";
const char kPackageName[] = "com.google.testapp";
const int64_t kUserId = 0;
} // namespace
class RecentAppButtonsViewTest : public AshTestBase {
public:
RecentAppButtonsViewTest() = default;
~RecentAppButtonsViewTest() override = default;
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kEcheLauncher, features::kEcheSWA,
features::kEcheLauncherIconsInMoreAppsButton,
features::kEcheNetworkConnectionState},
/*disabled_features=*/{});
phone_hub_recent_apps_view_ = std::make_unique<PhoneHubRecentAppsView>(
&fake_recent_apps_interaction_handler_, &fake_phone_hub_manager_,
connected_view_);
}
void TearDown() override {
phone_hub_recent_apps_view_.reset();
AshTestBase::TearDown();
}
protected:
PhoneHubRecentAppsView* recent_apps_view() {
return phone_hub_recent_apps_view_.get();
}
void NotifyRecentAppAddedOrUpdated() {
auto app_metadata = phonehub::Notification::AppMetadata(
kAppName, kPackageName,
/*color_icon=*/gfx::Image(), /*monochrome_icon_mask=*/std::nullopt,
/*icon_color=*/std::nullopt,
/*icon_is_monochrome=*/true, kUserId,
phonehub::proto::AppStreamabilityStatus::STREAMABLE);
fake_recent_apps_interaction_handler_.NotifyRecentAppAddedOrUpdated(
app_metadata, base::Time::Now());
fake_phone_hub_manager_.fake_app_stream_launcher_data_model()->AddAppToList(
app_metadata);
}
void SimulateBubbleCloseAndOpen() {
phone_hub_recent_apps_view_.reset();
phone_hub_recent_apps_view_.release();
phone_hub_recent_apps_view_ = std::make_unique<PhoneHubRecentAppsView>(
&fake_recent_apps_interaction_handler_, &fake_phone_hub_manager_,
connected_view_);
}
size_t PackageNameToClickCount(const std::string& package_name) {
return fake_recent_apps_interaction_handler_.HandledRecentAppsCount(
package_name);
}
void FeatureStateChanged(FeatureState feature_state) {
fake_recent_apps_interaction_handler_.OnFeatureStateChanged(feature_state);
}
bool AppStreamLauncherShowState() {
return fake_phone_hub_manager_.fake_app_stream_launcher_data_model()
->GetShouldShowMiniLauncher();
}
void SetRecentAppsHandlerUiState(RecentAppsUiState ui_state) {
fake_recent_apps_interaction_handler_.set_ui_state_for_testing(ui_state);
}
views::View* GetLoadingView() {
return phone_hub_recent_apps_view_->get_loading_view_for_test();
}
views::ImageButton* GetErrorButton() {
return phone_hub_recent_apps_view_->get_error_button_for_test();
}
base::test::ScopedFeatureList feature_list_;
private:
std::unique_ptr<PhoneHubRecentAppsView> phone_hub_recent_apps_view_;
phonehub::FakeRecentAppsInteractionHandler
fake_recent_apps_interaction_handler_;
phonehub::FakePhoneHubManager fake_phone_hub_manager_;
raw_ptr<PhoneConnectedView> connected_view_;
};
TEST_F(RecentAppButtonsViewTest, TaskViewVisibility) {
// The recent app view is not visible if the NotifyRecentAppAddedOrUpdated
// function never be called, e.g. device boot.
EXPECT_FALSE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
// The feature state is enabled but no recent app has been added yet, we
// should not show the recent app buttons view.
FeatureStateChanged(FeatureState::kEnabledByUser);
recent_apps_view()->Update();
EXPECT_TRUE(recent_apps_view()->GetVisible());
EXPECT_FALSE(recent_apps_view()->recent_app_buttons_view_->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
// The feature state is disabled so we should not show all recent apps view.
FeatureStateChanged(FeatureState::kDisabledByUser);
recent_apps_view()->Update();
EXPECT_FALSE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
}
TEST_F(RecentAppButtonsViewTest,
TaskViewVisibility_NetworkConnectionFlagDisabled) {
feature_list_.Reset();
feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kEcheLauncher, features::kEcheSWA,
features::kEcheLauncherIconsInMoreAppsButton},
/*disabled_features=*/{features::kEcheNetworkConnectionState});
EXPECT_FALSE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
FeatureStateChanged(FeatureState::kEnabledByUser);
recent_apps_view()->Update();
EXPECT_TRUE(recent_apps_view()->GetVisible());
EXPECT_FALSE(recent_apps_view()->recent_app_buttons_view_->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
FeatureStateChanged(FeatureState::kDisabledByUser);
recent_apps_view()->Update();
EXPECT_FALSE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
}
TEST_F(RecentAppButtonsViewTest, LoadingStateVisibility) {
EXPECT_FALSE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
SetRecentAppsHandlerUiState(RecentAppsUiState::LOADING);
recent_apps_view()->Update();
EXPECT_TRUE(recent_apps_view()->GetVisible());
EXPECT_TRUE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
SetRecentAppsHandlerUiState(RecentAppsUiState::ITEMS_VISIBLE);
recent_apps_view()->Update();
EXPECT_TRUE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
EXPECT_TRUE(recent_apps_view()->recent_app_buttons_view_->GetVisible());
}
TEST_F(RecentAppButtonsViewTest, ConnectionFailedStateVisibility) {
EXPECT_FALSE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
SetRecentAppsHandlerUiState(RecentAppsUiState::CONNECTION_FAILED);
recent_apps_view()->Update();
EXPECT_TRUE(recent_apps_view()->GetVisible());
EXPECT_TRUE(GetLoadingView()->GetVisible());
EXPECT_TRUE(GetErrorButton()->GetVisible());
SetRecentAppsHandlerUiState(RecentAppsUiState::ITEMS_VISIBLE);
recent_apps_view()->Update();
EXPECT_TRUE(recent_apps_view()->GetVisible());
EXPECT_FALSE(GetLoadingView()->GetVisible());
EXPECT_FALSE(GetErrorButton()->GetVisible());
EXPECT_TRUE(recent_apps_view()->recent_app_buttons_view_->GetVisible());
}
TEST_F(RecentAppButtonsViewTest, SingleRecentAppButtonsView) {
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
recent_apps_view()->Update();
size_t expected_recent_app_button = 1;
EXPECT_TRUE(recent_apps_view()->GetVisible());
EXPECT_EQ(expected_recent_app_button,
recent_apps_view()->recent_app_buttons_view_->children().size());
}
TEST_F(RecentAppButtonsViewTest, MultipleRecentAppButtonsView) {
NotifyRecentAppAddedOrUpdated();
NotifyRecentAppAddedOrUpdated();
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
recent_apps_view()->Update();
size_t expected_recent_app_button = 3;
EXPECT_EQ(expected_recent_app_button,
recent_apps_view()->recent_app_buttons_view_->children().size());
for (views::View* child :
recent_apps_view()->recent_app_buttons_view_->children()) {
PhoneHubRecentAppButton* recent_app =
static_cast<PhoneHubRecentAppButton*>(child);
// Simulate clicking button using placeholder event.
views::test::ButtonTestApi(recent_app).NotifyClick(ui::test::TestEvent());
}
size_t expected_number_of_button_be_clicked = 3;
EXPECT_EQ(expected_number_of_button_be_clicked,
PackageNameToClickCount(kPackageName));
}
TEST_F(RecentAppButtonsViewTest,
MultipleRecentAppButtonsWithMoreAppsButtonView) {
NotifyRecentAppAddedOrUpdated();
NotifyRecentAppAddedOrUpdated();
NotifyRecentAppAddedOrUpdated();
NotifyRecentAppAddedOrUpdated();
NotifyRecentAppAddedOrUpdated();
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
recent_apps_view()->Update();
size_t expected_recent_app_button = 7;
EXPECT_EQ(expected_recent_app_button,
recent_apps_view()->recent_app_buttons_view_->children().size());
for (std::size_t i = 0;
i != recent_apps_view()->recent_app_buttons_view_->children().size();
i++) {
auto* child =
recent_apps_view()->recent_app_buttons_view_->children()[i].get();
if (i == 6) {
break;
}
PhoneHubRecentAppButton* recent_app =
static_cast<PhoneHubRecentAppButton*>(child);
// Simulate clicking button using placeholder event.
views::test::ButtonTestApi(recent_app).NotifyClick(ui::test::TestEvent());
}
size_t expected_number_of_button_be_clicked = 6;
EXPECT_EQ(expected_number_of_button_be_clicked,
PackageNameToClickCount(kPackageName));
}
TEST_F(RecentAppButtonsViewTest, LogRecentAppsStateOnBubbleOpened) {
base::HistogramTester histogram_tester;
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
SetRecentAppsHandlerUiState(RecentAppsUiState::HIDDEN);
SimulateBubbleCloseAndOpen();
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kLoading, 0);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kError, 0);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName, RecentAppsViewUiState::kApps,
0);
SetRecentAppsHandlerUiState(RecentAppsUiState::PLACEHOLDER_VIEW);
SimulateBubbleCloseAndOpen();
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kLoading, 0);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kError, 0);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName, RecentAppsViewUiState::kApps,
0);
SetRecentAppsHandlerUiState(RecentAppsUiState::LOADING);
SimulateBubbleCloseAndOpen();
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kLoading, 1);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kError, 0);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName, RecentAppsViewUiState::kApps,
0);
SetRecentAppsHandlerUiState(RecentAppsUiState::CONNECTION_FAILED);
SimulateBubbleCloseAndOpen();
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kLoading, 1);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kError, 1);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName, RecentAppsViewUiState::kApps,
0);
SetRecentAppsHandlerUiState(RecentAppsUiState::ITEMS_VISIBLE);
SimulateBubbleCloseAndOpen();
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kLoading, 1);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName,
RecentAppsViewUiState::kError, 1);
histogram_tester.ExpectBucketCount(
kRecentAppsStateOnBubbleOpenedHistogramName, RecentAppsViewUiState::kApps,
1);
}
TEST_F(RecentAppButtonsViewTest, LogRecentAppsTransitionToFailedLatency) {
base::HistogramTester histogram_tester;
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
SetRecentAppsHandlerUiState(RecentAppsUiState::LOADING);
SimulateBubbleCloseAndOpen();
SetRecentAppsHandlerUiState(RecentAppsUiState::CONNECTION_FAILED);
recent_apps_view()->Update();
histogram_tester.ExpectTimeBucketCount(
kRecentAppsTransitionToFailedLatencyHistogramName, base::Milliseconds(0),
1);
}
// TODO(crbug.com/1476926): Disabled due to flakiness.
TEST_F(RecentAppButtonsViewTest,
DISABLED_LogRecentAppsTransitionToSuccessLatency) {
base::HistogramTester histogram_tester;
NotifyRecentAppAddedOrUpdated();
FeatureStateChanged(FeatureState::kEnabledByUser);
SetRecentAppsHandlerUiState(RecentAppsUiState::LOADING);
SimulateBubbleCloseAndOpen();
SetRecentAppsHandlerUiState(RecentAppsUiState::ITEMS_VISIBLE);
recent_apps_view()->Update();
histogram_tester.ExpectTimeBucketCount(
kRecentAppsTransitionToSuccessLatencyHistogramName, base::Milliseconds(0),
1);
SetRecentAppsHandlerUiState(RecentAppsUiState::CONNECTION_FAILED);
SimulateBubbleCloseAndOpen();
SetRecentAppsHandlerUiState(RecentAppsUiState::ITEMS_VISIBLE);
recent_apps_view()->Update();
histogram_tester.ExpectTimeBucketCount(
kRecentAppsTransitionToSuccessLatencyHistogramName, base::Milliseconds(0),
2);
}
} // namespace ash