File: action_runner_unittest.cc

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 (575 lines) | stat: -rw-r--r-- 22,756 bytes parent folder | download | duplicates (3)
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
// 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.

#include "chrome/browser/enterprise/idle/action_runner.h"

#include "base/memory/raw_ptr.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "build/build_config.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h"
#include "chrome/browser/enterprise/idle/action.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/enterprise/idle/idle_pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "content/public/browser/browsing_data_remover.h"
#include "content/public/browser/storage_partition_config.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/test/views/chrome_views_test_base.h"
#endif  // !BUILDFLAG(IS_ANDROID)

namespace enterprise_idle {

using content::BrowsingDataRemover;

using testing::_;
using testing::IsEmpty;
using testing::Return;
using testing::UnorderedElementsAre;

namespace {

class FakeActionFactory : public ActionFactory {
 public:
  FakeActionFactory() = default;

  ActionQueue Build(Profile* profile,
                    const std::vector<ActionType>& action_types) override {
    ActionQueue actions;
    for (ActionType action_type : action_types) {
      auto it = associations_.find(action_type);
      if (it != associations_.end()) {
        actions.push(std::move(it->second));
        associations_.erase(it);
      }
    }
    return actions;
  }

  void Associate(ActionType action_type, std::unique_ptr<Action> action) {
    associations_[action_type] = std::move(action);
  }

 private:
  std::map<ActionType, std::unique_ptr<Action>> associations_;
};

class MockAction : public Action {
 public:
  explicit MockAction(ActionType action_type)
      : Action(static_cast<int>(action_type)) {}

  MOCK_METHOD2(Run, void(Profile*, Continuation));
  bool ShouldNotifyUserOfPendingDestructiveAction(Profile* profile) override {
    return false;
  }
};

// testing::InvokeArgument<N> does not work with base::OnceCallback, so we
// define our own gMock action to run the 2nd argument.
ACTION_P(RunContinuation, success) {
  std::move(const_cast<Action::Continuation&>(arg1)).Run(success);
}

}  // namespace

// TODO(crbug.com/40222234): Enable this when Android supports >1 Action.
#if !BUILDFLAG(IS_ANDROID)
// Tests that actions are run in sequence, in order of priority.
TEST(IdleActionRunnerTest, RunsActionsInSequence) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  FakeActionFactory action_factory;
  ActionRunner runner(&profile, &action_factory);

  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kCloseBrowsers));
  actions.Append(static_cast<int>(ActionType::kShowProfilePicker));
  profile.GetPrefs()->SetList(prefs::kIdleTimeoutActions, std::move(actions));

  auto close_browsers =
      std::make_unique<MockAction>(ActionType::kCloseBrowsers);
  auto show_profile_picker =
      std::make_unique<MockAction>(ActionType::kShowProfilePicker);
  testing::InSequence in_sequence;
  EXPECT_CALL(*close_browsers, Run(&profile, _))
      .WillOnce(RunContinuation(true));
  EXPECT_CALL(*show_profile_picker, Run(&profile, _))
      .WillOnce(RunContinuation(true));

  action_factory.Associate(ActionType::kCloseBrowsers,
                           std::move(close_browsers));
  action_factory.Associate(ActionType::kShowProfilePicker,
                           std::move(show_profile_picker));
  runner.Run();
}

// Tests that the order of actions in the pref doesn't matter. They still run
// by order of priority.
TEST(IdleActionRunnerTest, PrefOrderDoesNotMatter) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  FakeActionFactory action_factory;
  ActionRunner runner(&profile, &action_factory);

  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kCloseBrowsers));
  actions.Append(static_cast<int>(ActionType::kShowProfilePicker));
  profile.GetPrefs()->SetList(prefs::kIdleTimeoutActions, std::move(actions));

  auto close_browsers =
      std::make_unique<MockAction>(ActionType::kCloseBrowsers);
  auto show_profile_picker =
      std::make_unique<MockAction>(ActionType::kShowProfilePicker);
  testing::InSequence in_sequence;
  EXPECT_CALL(*close_browsers, Run(&profile, _))
      .WillOnce(RunContinuation(true));
  EXPECT_CALL(*show_profile_picker, Run(&profile, _))
      .WillOnce(RunContinuation(true));

  action_factory.Associate(ActionType::kCloseBrowsers,
                           std::move(close_browsers));
  action_factory.Associate(ActionType::kShowProfilePicker,
                           std::move(show_profile_picker));

  runner.Run();
}

// Tests that when a higher-priority action fails, the lower-priority actions
// don't run.
TEST(IdleActionRunnerTest, OtherActionsDontRunOnFailure) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  FakeActionFactory action_factory;
  ActionRunner runner(&profile, &action_factory);

  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kCloseBrowsers));
  actions.Append(static_cast<int>(ActionType::kShowProfilePicker));
  profile.GetPrefs()->SetList(prefs::kIdleTimeoutActions, std::move(actions));

  auto close_browsers =
      std::make_unique<MockAction>(ActionType::kCloseBrowsers);
  auto show_profile_picker =
      std::make_unique<MockAction>(ActionType::kShowProfilePicker);

  // "show_profile_picker" shouldn't run, because "close_browsers" fails.
  testing::InSequence in_sequence;
  EXPECT_CALL(*close_browsers, Run(&profile, _))
      .WillOnce(RunContinuation(false));
  EXPECT_CALL(*show_profile_picker, Run(_, _)).Times(0);

  action_factory.Associate(ActionType::kCloseBrowsers,
                           std::move(close_browsers));
  action_factory.Associate(ActionType::kShowProfilePicker,
                           std::move(show_profile_picker));
  runner.Run();
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Tests that it does nothing when the "IdleTimeoutActions" pref is empty.
TEST(IdleActionRunnerTest, DoNothingWithEmptyPref) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  FakeActionFactory action_factory;
  ActionRunner runner(&profile, &action_factory);

  // "IdleTimeoutActions" is deliberately unset.
  auto clear_browsing_history =
      std::make_unique<MockAction>(ActionType::kClearBrowsingHistory);
  auto clear_cookies_and_site_data =
      std::make_unique<MockAction>(ActionType::kClearCookiesAndOtherSiteData);

  EXPECT_CALL(*clear_browsing_history, Run(_, _)).Times(0);
  EXPECT_CALL(*clear_cookies_and_site_data, Run(_, _)).Times(0);

  action_factory.Associate(ActionType::kClearBrowsingHistory,
                           std::move(clear_browsing_history));
  action_factory.Associate(ActionType::kClearCookiesAndOtherSiteData,
                           std::move(clear_cookies_and_site_data));
  runner.Run();
}

// TODO(crbug.com/40222234): Enable this when Android supports >1 Action.
#if !BUILDFLAG(IS_ANDROID)
// Tests that ActionRunner only runs the actions configured via the
// "IdleTimeoutActions" pref.
TEST(IdleActionRunnerTest, JustCloseBrowsers) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  FakeActionFactory action_factory;
  ActionRunner runner(&profile, &action_factory);

  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kCloseBrowsers));
  profile.GetPrefs()->SetList(prefs::kIdleTimeoutActions, std::move(actions));

  auto close_browsers =
      std::make_unique<MockAction>(ActionType::kCloseBrowsers);
  auto show_profile_picker =
      std::make_unique<MockAction>(ActionType::kShowProfilePicker);

  EXPECT_CALL(*close_browsers, Run(&profile, _))
      .WillOnce(RunContinuation(true));
  EXPECT_CALL(*show_profile_picker, Run(_, _)).Times(0);

  action_factory.Associate(ActionType::kCloseBrowsers,
                           std::move(close_browsers));
  action_factory.Associate(ActionType::kShowProfilePicker,
                           std::move(show_profile_picker));
  runner.Run();
}

// Tests that ActionRunner only runs the actions configured via the
// "IdleTimeoutActions" pref.
TEST(IdleActionRunnerTest, JustShowProfilePicker) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  FakeActionFactory action_factory;
  ActionRunner runner(&profile, &action_factory);

  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kShowProfilePicker));
  profile.GetPrefs()->SetList(prefs::kIdleTimeoutActions, std::move(actions));

  auto close_browsers =
      std::make_unique<MockAction>(ActionType::kCloseBrowsers);
  auto show_profile_picker =
      std::make_unique<MockAction>(ActionType::kShowProfilePicker);

  EXPECT_CALL(*close_browsers, Run(_, _)).Times(0);
  EXPECT_CALL(*show_profile_picker, Run(&profile, _))
      .WillOnce(RunContinuation(true));

  action_factory.Associate(ActionType::kCloseBrowsers,
                           std::move(close_browsers));
  action_factory.Associate(ActionType::kShowProfilePicker,
                           std::move(show_profile_picker));
  runner.Run();
}
#endif  // !BUILDFLAG(IS_ANDROID)

// A basic implementation of BrowsingDataRemover, that doesn't remove any data.
//
// Doesn't use gmock methods, since we could technically accept any of the 4
// Remove*() methods. Instead, it saves the arguments for BrowsingDataRemover's
class FakeBrowsingDataRemover : public BrowsingDataRemover {
 public:
  void SetEmbedderDelegate(
      content::BrowsingDataRemoverDelegate* embedder_delegate) override {
    NOTREACHED();
  }
  bool DoesOriginMatchMaskForTesting(
      uint64_t origin_type_mask,
      const url::Origin& origin,
      storage::SpecialStoragePolicy* special_storage_policy) override {
    NOTREACHED();
  }
  void Remove(const base::Time& delete_begin,
              const base::Time& delete_end,
              uint64_t remove_mask,
              uint64_t origin_type_mask) override {
    DCHECK_EQ(nullptr, observer_);
    remove_mask_ = remove_mask;
    origin_type_mask_ = origin_type_mask;
  }
  void RemoveWithFilter(const base::Time& delete_begin,
                        const base::Time& delete_end,
                        uint64_t remove_mask,
                        uint64_t origin_type_mask,
                        std::unique_ptr<content::BrowsingDataFilterBuilder>
                            filter_builder) override {
    Remove(delete_begin, delete_end, remove_mask, origin_type_mask);
  }
  void RemoveAndReply(const base::Time& delete_begin,
                      const base::Time& delete_end,
                      uint64_t remove_mask,
                      uint64_t origin_type_mask,
                      Observer* observer) override {
    DCHECK_EQ(observer, observer_);
    remove_mask_ = remove_mask;
    origin_type_mask_ = origin_type_mask;
    observer->OnBrowsingDataRemoverDone(failed_data_types_);
  }
  void RemoveWithFilterAndReply(
      const base::Time& delete_begin,
      const base::Time& delete_end,
      uint64_t remove_mask,
      uint64_t origin_type_mask,
      std::unique_ptr<content::BrowsingDataFilterBuilder> filter_builder,
      Observer* observer) override {
    RemoveAndReply(delete_begin, delete_end, remove_mask, origin_type_mask,
                   observer);
  }
  void AddObserver(Observer* observer) override {
    DCHECK_EQ(nullptr, observer_);
    observer_ = observer;
  }
  void RemoveObserver(Observer* observer) override {
    DCHECK_EQ(observer, observer_);
    observer_ = nullptr;
  }
  void SetWouldCompleteCallbackForTesting(
      const base::RepeatingCallback<
          void(base::OnceClosure continue_to_completion)>& callback) override {
    NOTREACHED();
  }
  const base::Time& GetLastUsedBeginTimeForTesting() override { NOTREACHED(); }
  uint64_t GetLastUsedRemovalMaskForTesting() override { return remove_mask_; }
  uint64_t GetLastUsedOriginTypeMaskForTesting() override {
    return origin_type_mask_;
  }
  std::optional<content::StoragePartitionConfig>
  GetLastUsedStoragePartitionConfigForTesting() override {
    return std::nullopt;
  }
  uint64_t GetPendingTaskCountForTesting() override { return 0; }

  void SetFailedDataTypesForTesting(uint64_t failed_data_types) {
    failed_data_types_ = failed_data_types;
  }

 private:
  base::Time begin_time_;
  uint64_t remove_mask_ = 0;
  uint64_t origin_type_mask_ = 0;
  uint64_t failed_data_types_ = 0;

  raw_ptr<Observer> observer_ = nullptr;
};

#if !BUILDFLAG(IS_ANDROID)
class IdleActionRunnerClearDataTest : public ChromeViewsTestBase {
 protected:
  void SetUp() override {
    set_native_widget_type(NativeWidgetType::kDesktop);
    ChromeViewsTestBase::SetUp();
  }

  void TearDown() override { ChromeViewsTestBase::TearDown(); }

  TestingProfile* profile() { return &profile_; }
  FakeBrowsingDataRemover* remover() { return &browsing_data_remover_; }

 private:
  TestingProfile profile_;
  FakeBrowsingDataRemover browsing_data_remover_;
};

TEST_F(IdleActionRunnerClearDataTest, ClearBrowsingHistory) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearBrowsingHistory));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));
  EXPECT_EQ(chrome_browsing_data_remover::DATA_TYPE_HISTORY,
            remover()->GetLastUsedRemovalMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, ClearDownloadHistory) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearDownloadHistory));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_DOWNLOADS,
            remover()->GetLastUsedRemovalMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, ClearCookies) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearCookiesAndOtherSiteData));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(chrome_browsing_data_remover::DATA_TYPE_SITE_DATA,
            remover()->GetLastUsedRemovalMaskForTesting());
  EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
            remover()->GetLastUsedOriginTypeMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, ClearCache) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearCachedImagesAndFiles));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(BrowsingDataRemover::DATA_TYPE_CACHE,
            remover()->GetLastUsedRemovalMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, ClearPasswordSignin) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearPasswordSignin));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(chrome_browsing_data_remover::DATA_TYPE_PASSWORDS,
            remover()->GetLastUsedRemovalMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, ClearAutofill) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearAutofill));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(chrome_browsing_data_remover::DATA_TYPE_FORM_DATA,
            remover()->GetLastUsedRemovalMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, ClearSiteSettings) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearSiteSettings));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(chrome_browsing_data_remover::DATA_TYPE_CONTENT_SETTINGS,
            remover()->GetLastUsedRemovalMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, ClearHostedAppData) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearHostedAppData));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(chrome_browsing_data_remover::DATA_TYPE_SITE_DATA,
            remover()->GetLastUsedRemovalMaskForTesting());
  EXPECT_EQ(BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB,
            remover()->GetLastUsedOriginTypeMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, MultipleTypes) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  base::Value::List actions;
  actions.Append(static_cast<int>(ActionType::kClearBrowsingHistory));
  actions.Append(static_cast<int>(ActionType::kClearDownloadHistory));
  actions.Append(static_cast<int>(ActionType::kClearAutofill));
  profile()->GetPrefs()->SetList(prefs::kIdleTimeoutActions,
                                 std::move(actions));

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionRunner runner(profile(), ActionFactory::GetInstance());
  runner.Run();
  task_environment()->FastForwardBy(base::Seconds(30));

  EXPECT_EQ(chrome_browsing_data_remover::DATA_TYPE_HISTORY |
                BrowsingDataRemover::DATA_TYPE_DOWNLOADS |
                chrome_browsing_data_remover::DATA_TYPE_FORM_DATA,
            remover()->GetLastUsedRemovalMaskForTesting());
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", true, 1);
}

TEST_F(IdleActionRunnerClearDataTest, MultipleTypesAndFailure) {
  std::unique_ptr<base::HistogramTester> histogram_tester =
      std::make_unique<base::HistogramTester>();
  remover()->SetFailedDataTypesForTesting(
      chrome_browsing_data_remover::DATA_TYPE_HISTORY |
      BrowsingDataRemover::DATA_TYPE_DOWNLOADS |
      chrome_browsing_data_remover::DATA_TYPE_FORM_DATA);

  ActionFactory::GetInstance()->SetBrowsingDataRemoverForTesting(remover());
  ActionFactory::ActionQueue actions = ActionFactory::GetInstance()->Build(
      profile(),
      {ActionType::kClearBrowsingHistory, ActionType::kClearDownloadHistory,
       ActionType::kClearAutofill});
  ASSERT_EQ(1u, actions.size());
  EXPECT_EQ(static_cast<int>(ActionType::kClearBrowsingHistory),
            actions.top()->priority());

  // The callback should run with success=false.
  base::MockCallback<Action::Continuation> cb;
  base::RunLoop run_loop;
  EXPECT_CALL(cb, Run(/*success=*/false))
      .WillOnce(base::test::RunClosure(run_loop.QuitClosure()));

  actions.top()->Run(profile(), cb.Get());
  run_loop.Run();
  histogram_tester->ExpectUniqueSample(
      "Enterprise.IdleTimeoutPolicies.Success.ClearBrowsingData", false, 1);
}
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace enterprise_idle