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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
ash / clipboard / clipboard_nudge_controller_unittest.cc [blame]
// Copyright 2020 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/clipboard/clipboard_nudge_controller.h"
#include <array>
#include <memory>
#include <utility>
#include "ash/clipboard/clipboard_history.h"
#include "ash/clipboard/clipboard_history_controller_impl.h"
#include "ash/clipboard/clipboard_nudge_constants.h"
#include "ash/clipboard/test_support/clipboard_history_item_builder.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/public/cpp/system/anchored_nudge_manager.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "chromeos/crosapi/mojom/clipboard_history.mojom.h"
#include "ui/base/clipboard/clipboard_data.h"
#include "ui/base/clipboard/clipboard_non_backed.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/views/widget/widget_observer.h"
namespace ash {
namespace {
using crosapi::mojom::ClipboardHistoryControllerShowSource::kAccelerator;
constexpr char kClipboardNudgeId[] = "ClipboardContextualNudge";
// The array of all clipboard nudge types.
constexpr std::array<ClipboardNudgeType, ClipboardNudgeType::kMax + 1>
kAllClipboardNudgeTypes = {ClipboardNudgeType::kOnboardingNudge,
ClipboardNudgeType::kZeroStateNudge,
ClipboardNudgeType::kScreenshotNotificationNudge,
ClipboardNudgeType::kDuplicateCopyNudge};
} // namespace
class ClipboardNudgeControllerTestBase : public AshTestBase {
public:
ClipboardNudgeControllerTestBase()
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
base::SimpleTestClock* clock() { return &test_clock_; }
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
nudge_controller_ =
Shell::Get()->clipboard_history_controller()->nudge_controller();
nudge_controller_->OverrideClockForTesting(&test_clock_);
test_clock_.Advance(base::Seconds(360));
}
void TearDown() override {
nudge_controller_->ClearClockOverrideForTesting();
AshTestBase::TearDown();
}
void ShowNudgeForType(ClipboardNudgeType nudge_type) {
switch (nudge_type) {
case ClipboardNudgeType::kOnboardingNudge:
case ClipboardNudgeType::kZeroStateNudge:
case ClipboardNudgeType::kDuplicateCopyNudge:
nudge_controller_->ShowNudge(nudge_type);
return;
case ClipboardNudgeType::kScreenshotNotificationNudge:
nudge_controller_->MarkScreenshotNotificationShown();
return;
}
}
// Returns an item with arbitrary data for simulating clipboard writes.
ClipboardHistoryItem CreateItem() const {
ClipboardHistoryItemBuilder builder;
builder.SetText("text");
return builder.Build();
}
bool IsClipboardNudgeShown() {
return AnchoredNudgeManager::Get()->IsNudgeShown(kClipboardNudgeId);
}
base::HistogramTester& histograms() { return histograms_; }
// Owned by ClipboardHistoryController.
raw_ptr<ClipboardNudgeController, DanglingUntriaged> nudge_controller_;
private:
base::SimpleTestClock test_clock_;
// Histogram value verifier.
base::HistogramTester histograms_;
};
// A test to verify basic clipboard nudge behavior.
class ClipboardNudgeControllerTest : public ClipboardNudgeControllerTestBase {
public:
NudgeCatalogName GetClipboardNudgeCatalogName() {
return Shell::Get()->anchored_nudge_manager()->GetNudgeCatalogNameForTest(
kClipboardNudgeId);
}
};
// Checks that clipboard state advances after the nudge controller is fed the
// right event type.
TEST_F(ClipboardNudgeControllerTest, ShouldShowNudgeAfterCorrectSequence) {
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kInit,
nudge_controller_->onboarding_state_for_testing());
// Checks that the first copy advances state as expected.
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstCopy,
nudge_controller_->onboarding_state_for_testing());
// Checks that the first paste advances state as expected.
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
// Checks that the second copy advances state as expected.
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kSecondCopy,
nudge_controller_->onboarding_state_for_testing());
// Check that clipboard nudge has not yet been created.
EXPECT_FALSE(IsClipboardNudgeShown());
// Checks that the second paste resets state as expected.
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kInit,
nudge_controller_->onboarding_state_for_testing());
// Check that clipboard nudge has been created.
EXPECT_TRUE(IsClipboardNudgeShown());
}
// Checks that the clipboard state does not advace if too much time passes
// during the copy paste sequence.
TEST_F(ClipboardNudgeControllerTest, NudgeTimeOut) {
// Perform copy -> paste -> copy sequence.
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
nudge_controller_->OnClipboardDataRead();
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
// Advance time to cause the nudge timer to time out.
clock()->Advance(kMaxTimeBetweenPaste);
nudge_controller_->OnClipboardDataRead();
// Paste event should reset clipboard state to `kFirstPaste`.
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
}
// Checks that multiple pastes refreshes the |kMaxTimeBetweenPaste| timer that
// determines whether too much time has passed to show the nudge.
TEST_F(ClipboardNudgeControllerTest, NudgeDoesNotTimeOutWithSparsePastes) {
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
// Perform 5 pastes over 2.5*|kMaxTimeBetweenPaste|.
for (int paste_cycle = 0; paste_cycle < 5; paste_cycle++) {
SCOPED_TRACE("paste cycle " + base::NumberToString(paste_cycle));
clock()->Advance(kMaxTimeBetweenPaste / 2);
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
}
// Check that clipboard nudge has not yet been created.
EXPECT_FALSE(IsClipboardNudgeShown());
// Check that HandleClipboardChanged() will advance nudge_controller's
// ClipboardState.
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kSecondCopy,
nudge_controller_->onboarding_state_for_testing());
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kInit,
nudge_controller_->onboarding_state_for_testing());
// Check that clipboard nudge has been created.
EXPECT_TRUE(IsClipboardNudgeShown());
}
// Checks that consecutive copy events does not advance the clipboard state.
TEST_F(ClipboardNudgeControllerTest, RepeatedCopyDoesNotAdvanceState) {
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstCopy,
nudge_controller_->onboarding_state_for_testing());
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstCopy,
nudge_controller_->onboarding_state_for_testing());
}
// Checks that consecutive paste events does not advance the clipboard state.
TEST_F(ClipboardNudgeControllerTest, RepeatedPasteDoesNotAdvanceState) {
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstCopy,
nudge_controller_->onboarding_state_for_testing());
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
}
// Verifies that administrative events does not advance clipboard state.
TEST_F(ClipboardNudgeControllerTest, AdminWriteDoesNotAdvanceState) {
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
nudge_controller_->OnClipboardDataRead();
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
auto data = std::make_unique<ui::ClipboardData>();
data->set_text("test");
// Write the data to the clipboard, clipboard state should not advance.
ui::ClipboardNonBacked::GetForCurrentThread()->WriteClipboardData(
std::move(data));
EXPECT_EQ(ClipboardNudgeController::OnboardingState::kFirstPaste,
nudge_controller_->onboarding_state_for_testing());
}
// Verifies that controller cleans up and closes an old nudge before displaying
// another one.
TEST_F(ClipboardNudgeControllerTest, ShowZeroStateNudgeAfterOngoingNudge) {
ShowNudgeForType(ClipboardNudgeType::kOnboardingNudge);
ASSERT_TRUE(IsClipboardNudgeShown());
EXPECT_EQ(GetClipboardNudgeCatalogName(),
NudgeCatalogName::kClipboardHistoryOnboarding);
ShowNudgeForType(ClipboardNudgeType::kZeroStateNudge);
ASSERT_TRUE(IsClipboardNudgeShown());
EXPECT_EQ(GetClipboardNudgeCatalogName(),
NudgeCatalogName::kClipboardHistoryZeroState);
}
// Asserts that all nudge metric related histograms start at 0.
TEST_F(ClipboardNudgeControllerTest, NudgeMetrics_StartAtZero) {
histograms().ExpectTotalCount(kClipboardHistoryOnboardingNudgeOpenTime, 0);
histograms().ExpectTotalCount(kClipboardHistoryOnboardingNudgePasteTime, 0);
histograms().ExpectTotalCount(kClipboardHistoryZeroStateNudgeOpenTime, 0);
histograms().ExpectTotalCount(kClipboardHistoryZeroStateNudgePasteTime, 0);
histograms().ExpectTotalCount(kClipboardHistoryScreenshotNotificationOpenTime,
0);
histograms().ExpectTotalCount(
kClipboardHistoryScreenshotNotificationPasteTime, 0);
}
// Tests that nudge `TimeToAction` metric is logged after the nudge has been
// shown and clipboard history is opened through the accelerator.
TEST_F(ClipboardNudgeControllerTest, TimeToActionMetric) {
constexpr char kNudgeShownCount[] = "Ash.NotifierFramework.Nudge.ShownCount";
constexpr char kNudgeTimeToActionWithin1m[] =
"Ash.NotifierFramework.Nudge.TimeToAction.Within1m";
constexpr char kNudgeTimeToActionWithin1h[] =
"Ash.NotifierFramework.Nudge.TimeToAction.Within1h";
constexpr NudgeCatalogName kOnboardingCatalogName =
NudgeCatalogName::kClipboardHistoryOnboarding;
constexpr NudgeCatalogName kZeroStateCatalogName =
NudgeCatalogName::kClipboardHistoryZeroState;
ShowNudgeForType(ClipboardNudgeType::kOnboardingNudge);
task_environment()->FastForwardBy(base::Hours(1));
ShowNudgeForType(ClipboardNudgeType::kZeroStateNudge);
// Open clipboard history through the accelerator after one hour of having
// shown the onboarding nudge, and right after showing the zero state nudge.
nudge_controller_->OnClipboardHistoryMenuShown(
/*show_source=*/crosapi::mojom::ClipboardHistoryControllerShowSource::
kAccelerator);
histograms().ExpectTotalCount(kNudgeShownCount, 2);
histograms().ExpectBucketCount(kNudgeTimeToActionWithin1m,
kZeroStateCatalogName, 1);
histograms().ExpectBucketCount(kNudgeTimeToActionWithin1h,
kOnboardingCatalogName, 1);
}
// A parameterized test base to verify metric recording for each type of nudge.
class ClipboardNudgeMetricTest
: public ClipboardNudgeControllerTestBase,
public testing::WithParamInterface<ClipboardNudgeType> {
public:
ClipboardNudgeMetricTest() = default;
ClipboardNudgeType GetNudgeType() const { return GetParam(); }
};
INSTANTIATE_TEST_SUITE_P(
All,
ClipboardNudgeMetricTest,
/*nudge_type=*/testing::ValuesIn(kAllClipboardNudgeTypes));
// Tests that the metrics are recorded as expected when opening the standalone
// clipboard history menu after a nudge shown.
TEST_P(ClipboardNudgeMetricTest, ShowMenuAfterNudgeShown) {
const ClipboardNudgeType type_being_tested = GetNudgeType();
ShowNudgeForType(type_being_tested);
constexpr int kFastForwardSeconds = 2;
clock()->Advance(base::Seconds(kFastForwardSeconds));
nudge_controller_->OnClipboardHistoryMenuShown(/*show_source=*/kAccelerator);
// Only the metric that records the time delta between the nudge shown and
// the menu opening for `type_being_tested` should be recorded.
for (const auto nudge_type : kAllClipboardNudgeTypes) {
histograms().ExpectBucketCount(GetMenuOpenTimeDeltaHistogram(nudge_type),
kFastForwardSeconds,
nudge_type == type_being_tested ? 1 : 0);
histograms().ExpectTotalCount(
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type), 0);
}
}
// Tests that the metrics are recorded as expected when pasting the clipboard
// history data after a nudge shown.
TEST_P(ClipboardNudgeMetricTest, PasteAfterNudgeShown) {
const ClipboardNudgeType type_being_tested = GetNudgeType();
ShowNudgeForType(type_being_tested);
constexpr int kFastForwardSeconds = 2;
clock()->Advance(base::Seconds(kFastForwardSeconds));
nudge_controller_->OnClipboardHistoryPasted();
// Only the metric that records the time delta between the nudge shown and the
// clipboard data paste for `type_being_tested` should be recorded.
for (const auto nudge_type : kAllClipboardNudgeTypes) {
histograms().ExpectTotalCount(GetMenuOpenTimeDeltaHistogram(nudge_type), 0);
histograms().ExpectBucketCount(
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type),
kFastForwardSeconds, nudge_type == type_being_tested ? 1 : 0);
}
}
// Tests that showing a nudge once allows at most one histogram entry to be
// recorded.
TEST_P(ClipboardNudgeMetricTest, LogOnceForSingleNudgeShown) {
const ClipboardNudgeType type_being_tested = GetNudgeType();
ShowNudgeForType(type_being_tested);
constexpr int kFastForwardSeconds = 2;
clock()->Advance(base::Seconds(kFastForwardSeconds));
nudge_controller_->OnClipboardHistoryMenuShown(/*show_source=*/kAccelerator);
nudge_controller_->OnClipboardHistoryPasted();
for (const auto nudge_type : kAllClipboardNudgeTypes) {
histograms().ExpectBucketCount(GetMenuOpenTimeDeltaHistogram(nudge_type),
kFastForwardSeconds,
nudge_type == type_being_tested ? 1 : 0);
histograms().ExpectBucketCount(
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type),
kFastForwardSeconds, nudge_type == type_being_tested ? 1 : 0);
}
nudge_controller_->OnClipboardHistoryMenuShown(/*show_source=*/kAccelerator);
nudge_controller_->OnClipboardHistoryPasted();
// The metrics should not be recorded for the second menu shown or clipboard
// data paste.
for (const auto nudge_type : kAllClipboardNudgeTypes) {
histograms().ExpectBucketCount(GetMenuOpenTimeDeltaHistogram(nudge_type),
kFastForwardSeconds,
nudge_type == type_being_tested ? 1 : 0);
histograms().ExpectBucketCount(
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type),
kFastForwardSeconds, nudge_type == type_being_tested ? 1 : 0);
}
}
// Tests that new histogram entries can be recorded each time a nudge is shown.
TEST_P(ClipboardNudgeMetricTest, LogTwiceAfterShowingTwice) {
const ClipboardNudgeType nudge_type = GetNudgeType();
ShowNudgeForType(nudge_type);
constexpr int kFirstFastForwardSeconds = 2;
clock()->Advance(base::Seconds(kFirstFastForwardSeconds));
nudge_controller_->OnClipboardHistoryMenuShown(/*show_source=*/kAccelerator);
nudge_controller_->OnClipboardHistoryPasted();
ShowNudgeForType(nudge_type);
// Perform menu shown and data paste but with a different fast forward time
// delta.
constexpr int kSecondFastForwardSeconds = 5;
clock()->Advance(base::Seconds(kSecondFastForwardSeconds));
nudge_controller_->OnClipboardHistoryMenuShown(/*show_source=*/kAccelerator);
nudge_controller_->OnClipboardHistoryPasted();
const char* menu_shown_time_delta_histogram =
GetMenuOpenTimeDeltaHistogram(nudge_type);
histograms().ExpectTotalCount(menu_shown_time_delta_histogram, 2);
histograms().ExpectBucketCount(menu_shown_time_delta_histogram,
kFirstFastForwardSeconds, 1);
histograms().ExpectBucketCount(menu_shown_time_delta_histogram,
kSecondFastForwardSeconds, 1);
const char* paste_time_delta_histogram =
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type);
histograms().ExpectTotalCount(paste_time_delta_histogram, 2);
histograms().ExpectBucketCount(paste_time_delta_histogram,
kFirstFastForwardSeconds, 1);
histograms().ExpectBucketCount(paste_time_delta_histogram,
kSecondFastForwardSeconds, 1);
}
// Tests that showing the standalone clipboard history menu from a source other
// than the accelerator does not log any metrics, because the nudge only
// mentions the accelerator as an option for opening the menu.
TEST_P(ClipboardNudgeMetricTest, NotLogForNonAcceleratorMenuShown) {
using crosapi::mojom::ClipboardHistoryControllerShowSource;
const ClipboardNudgeType nudge_type = GetNudgeType();
ShowNudgeForType(nudge_type);
for (size_t i =
static_cast<size_t>(ClipboardHistoryControllerShowSource::kMinValue);
i < static_cast<size_t>(ClipboardHistoryControllerShowSource::kMaxValue);
++i) {
auto show_source = static_cast<ClipboardHistoryControllerShowSource>(i);
if (show_source != kAccelerator) {
nudge_controller_->OnClipboardHistoryMenuShown(show_source);
}
}
histograms().ExpectTotalCount(GetMenuOpenTimeDeltaHistogram(nudge_type), 0);
histograms().ExpectTotalCount(
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type), 0);
}
class ClipboardHistoryRefreshNudgeTest
: public ClipboardNudgeControllerTestBase {
public:
// Time deltas that are less/more than the minimum time interval required to
// show a capped nudge.
static constexpr base::TimeDelta kLessThanMinInterval =
kCappedNudgeMinInterval - base::Hours(1);
static constexpr base::TimeDelta kMoreThanMinInterval =
kCappedNudgeMinInterval + base::Hours(1);
ClipboardHistoryRefreshNudgeTest() = default;
void HideClipboardNudge() {
AnchoredNudgeManager::Get()->Cancel(kClipboardNudgeId);
}
void WaitForClipboardWriteConfirmed() {
EXPECT_TRUE(operation_confirmed_future_.Take());
}
private:
// ClipboardNudgeControllerTestBase:
void SetUp() override {
ClipboardNudgeControllerTestBase::SetUp();
Shell::Get()
->clipboard_history_controller()
->set_confirmed_operation_callback_for_test(
operation_confirmed_future_.GetRepeatingCallback());
}
base::test::TestFuture<bool> operation_confirmed_future_;
};
// Verifies that the duplicate copy nudge is able to show after the onboarding
// nudge hits the shown count limit.
TEST_F(ClipboardHistoryRefreshNudgeTest,
DuplicateCopyAfterOnboardingNudgesHitLimit) {
// Show the onboarding nudge three times.
base::HistogramTester histogram_tester;
ShowNudgeForType(kOnboardingNudge);
clock()->Advance(kMoreThanMinInterval);
ShowNudgeForType(kOnboardingNudge);
clock()->Advance(kMoreThanMinInterval);
ShowNudgeForType(kOnboardingNudge);
clock()->Advance(kMoreThanMinInterval);
HideClipboardNudge();
histogram_tester.ExpectUniqueSample(kClipboardHistoryOnboardingNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/3);
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Write duplicate text to clipboard history.
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Check the existence of the system nudge.
EXPECT_TRUE(IsClipboardNudgeShown());
// Check the show count of the clipboard history duplicate copy nudge.
histogram_tester.ExpectUniqueSample(
kClipboardHistoryDuplicateCopyNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/1);
}
// Verifies the show count limit of the duplicate copy nudge.
TEST_F(ClipboardHistoryRefreshNudgeTest, DuplicateCopyShowCountLimit) {
base::HistogramTester histogram_tester;
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Write duplicate text to clipboard history.
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Write duplicate text to clipboard history for the second time. Advance by
// enough time that a capped nudge is able to show.
clock()->Advance(kMoreThanMinInterval);
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Write duplicate text to clipboard history for the third time.
clock()->Advance(kMoreThanMinInterval);
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Check the show count of the clipboard history duplicate copy nudge.
histogram_tester.ExpectUniqueSample(
kClipboardHistoryDuplicateCopyNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/3);
// Write duplicate text to clipboard history for the forth time.
HideClipboardNudge();
clock()->Advance(kMoreThanMinInterval);
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Check that the duplicate copy nudge does NOT show if the clipboard history
// refresh feature is enabled. Because the show count has hit the limit.
EXPECT_FALSE(IsClipboardNudgeShown());
histogram_tester.ExpectUniqueSample(
kClipboardHistoryDuplicateCopyNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/3);
}
// Ensure that the duplicate copy nudge is not shown too soon after the previous
// showing.
TEST_F(ClipboardHistoryRefreshNudgeTest, DuplicateCopyTimeIntervalThreshold) {
base::HistogramTester histogram_tester;
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Write duplicate text to clipboard history.
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
histogram_tester.ExpectUniqueSample(
kClipboardHistoryDuplicateCopyNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/1);
HideClipboardNudge();
// Advance the time with an interval less than the threshold.
clock()->Advance(kLessThanMinInterval);
// Write duplicate text to clipboard history again.
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Check that the duplicate copy nudge does NOT show.
EXPECT_FALSE(IsClipboardNudgeShown());
// Check the show count of the clipboard history duplicate copy nudge.
histogram_tester.ExpectUniqueSample(
kClipboardHistoryDuplicateCopyNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/1);
}
// Checks that showing onboarding nudges after duplicate copy works as expected.
TEST_F(ClipboardHistoryRefreshNudgeTest,
ShowOnboardingNudgeAfterDuplicateCopy) {
base::HistogramTester histogram_tester;
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
// Write duplicate text to clipboard history.
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste).WriteText(u"text");
WaitForClipboardWriteConfirmed();
histogram_tester.ExpectUniqueSample(
kClipboardHistoryDuplicateCopyNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/1);
histogram_tester.ExpectUniqueSample(kClipboardHistoryOnboardingNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/0);
HideClipboardNudge();
// Advance the time with an interval less than the threshold.
clock()->Advance(kLessThanMinInterval);
// Trigger the onboarding nudge by subsequent copies and pastes.
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
nudge_controller_->OnClipboardDataRead();
nudge_controller_->OnClipboardHistoryItemAdded(CreateItem());
nudge_controller_->OnClipboardDataRead();
// The onboarding nudge should NOT show if the duplicate copy nudge showed
// before due to the time interval threshold.
histogram_tester.ExpectUniqueSample(kClipboardHistoryOnboardingNudgeShowCount,
/*sample=*/true,
/*expected_bucket_count=*/0);
}
} // namespace ash