File: trigger_manager_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 (559 lines) | stat: -rw-r--r-- 26,247 bytes parent folder | download | duplicates (6)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/safe_browsing/content/browser/triggers/trigger_manager.h"

#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/safe_browsing/content/browser/threat_details.h"
#include "components/safe_browsing/content/browser/triggers/trigger_throttler.h"
#include "components/safe_browsing/core/common/features.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_web_contents_factory.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::Key;
using testing::Return;
using testing::UnorderedElementsAre;

namespace safe_browsing {

// Mock ThreatDetails class that makes FinishCollection a no-op.
class MockThreatDetails : public ThreatDetails {
 public:
  MockThreatDetails() = default;

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

  ~MockThreatDetails() override = default;
  MOCK_METHOD4(
      FinishCollection,
      void(bool did_proceed,
           int num_visits,
           std::unique_ptr<security_interstitials::InterstitialInteractionMap>
               interstitial_interactions,
           std::optional<int64_t> warning_shown_ts));
};

class MockThreatDetailsFactory : public ThreatDetailsFactory {
 public:
  ~MockThreatDetailsFactory() override = default;

  std::unique_ptr<ThreatDetails> CreateThreatDetails(
      BaseUIManager* ui_manager,
      content::WebContents* web_contents,
      const security_interstitials::UnsafeResource& unsafe_resource,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
      history::HistoryService* history_service,
      ReferrerChainProvider* referrer_chain_provider,
      bool trim_to_ad_tags,
      ThreatDetailsDoneCallback done_callback) override {
    return std::make_unique<MockThreatDetails>();
  }
};

class MockTriggerThrottler : public TriggerThrottler {
 public:
  MockTriggerThrottler() : TriggerThrottler(nullptr) {}
  MOCK_CONST_METHOD1(TriggerCanFire, bool(TriggerType trigger_type));
};

class TriggerManagerTest : public ::testing::Test {
 public:
  TriggerManagerTest() : trigger_manager_(nullptr, nullptr) {}

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

  ~TriggerManagerTest() override = default;

  void SetUp() override {
    ThreatDetails::RegisterFactory(&mock_threat_details_factory_);

    // Register any prefs that are needed by the trigger manager. By default,
    // enable Safe Browsing and Extended Reporting.
    safe_browsing::RegisterProfilePrefs(pref_service_.registry());
    SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, true);
    SetPref(prefs::kSafeBrowsingScoutReportingEnabled, true);

    MockTriggerThrottler* mock_throttler = new MockTriggerThrottler();
    ON_CALL(*mock_throttler, TriggerCanFire(_)).WillByDefault(Return(true));
    // Trigger Manager takes ownership of the mock throttler.
    trigger_manager_.set_trigger_throttler(mock_throttler);
  }

  void SetPref(const std::string& pref, bool value) {
    pref_service_.SetBoolean(pref, value);
  }

  void SetManagedPref(const std::string& pref, bool value) {
    pref_service_.SetManagedPref(pref, std::make_unique<base::Value>(value));
  }

  bool GetPref(const std::string& pref) {
    return pref_service_.GetBoolean(pref);
  }

  void SetTriggerHasQuota(const TriggerType trigger_type, bool has_quota) {
    MockTriggerThrottler* mock_throttler = static_cast<MockTriggerThrottler*>(
        trigger_manager_.trigger_throttler_.get());
    EXPECT_CALL(*mock_throttler, TriggerCanFire(trigger_type))
        .WillOnce(Return(has_quota));
  }

  content::WebContents* CreateWebContents() {
    DCHECK(!browser_context_.IsOffTheRecord())
        << "CreateWebContents() should not be called after "
           "CreateIncognitoWebContents()";
    return web_contents_factory_.CreateWebContents(&browser_context_);
  }

  content::WebContents* CreateIncognitoWebContents() {
    browser_context_.set_is_off_the_record(true);
    return web_contents_factory_.CreateWebContents(&browser_context_);
  }

  bool StartCollectingThreatDetails(const TriggerType trigger_type,
                                    content::WebContents* web_contents) {
    TriggerManager::DataCollectionPermissions permissions =
        TriggerManager::GetDataCollectionPermissions(pref_service_,
                                                     web_contents);
    return trigger_manager_.StartCollectingThreatDetails(
        trigger_type, web_contents, security_interstitials::UnsafeResource(),
        nullptr, nullptr, nullptr, permissions);
  }

  bool FinishCollectingThreatDetails(const TriggerType trigger_type,
                                     content::WebContents* web_contents,
                                     bool expect_report_sent) {
    WebContentsKey web_contents_key = GetWebContentsKey(web_contents);
    if (expect_report_sent) {
      MockThreatDetails* threat_details = static_cast<MockThreatDetails*>(
          trigger_manager_.data_collectors_map_[web_contents_key]
              .threat_details.get());
      EXPECT_CALL(*threat_details, FinishCollection(_, _, _, _)).Times(1);
    }
    TriggerManager::DataCollectionPermissions permissions =
        TriggerManager::GetDataCollectionPermissions(pref_service_,
                                                     web_contents);
    auto result = trigger_manager_.FinishCollectingThreatDetails(
        trigger_type, web_contents_key, base::TimeDelta(), false, 0,
        permissions);

    // Invoke the callback if the report was to be sent.
    if (expect_report_sent) {
      // Allow the ThreatDetails to complete, then remove it.
      base::RunLoop().RunUntilIdle();
      trigger_manager_.ThreatDetailsDone(web_contents_key);
    }

    return result.IsReportSent();
  }

  const DataCollectorsMap& data_collectors_map() {
    return trigger_manager_.data_collectors_map_;
  }

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;

 private:
  TriggerManager trigger_manager_;
  MockThreatDetailsFactory mock_threat_details_factory_;
  content::BrowserTaskEnvironment task_environment_;
  content::TestBrowserContext browser_context_;
  content::TestWebContentsFactory web_contents_factory_;
  TestingPrefServiceSimple pref_service_;
  std::unique_ptr<base::test::ScopedFeatureList> feature_list_;
};

TEST_F(TriggerManagerTest, StartAndFinishCollectingThreatDetails) {
  // Basic workflow is to start and finish data collection with a single
  // WebContents.
  content::WebContents* web_contents1 = CreateWebContents();
  WebContentsKey web_contents_key1 = GetWebContentsKey(web_contents1);
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents1));
  EXPECT_THAT(data_collectors_map(),
              UnorderedElementsAre(Key(web_contents_key1)));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_NE(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents1, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_EQ(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);

  // More complex scenarios can happen, where collection happens on two
  // WebContents at the same time, possibly starting and completing in different
  // order.
  content::WebContents* web_contents2 = CreateWebContents();
  WebContentsKey web_contents_key2 = GetWebContentsKey(web_contents2);
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents1));
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents2));
  EXPECT_THAT(
      data_collectors_map(),
      UnorderedElementsAre(Key(web_contents_key1), Key(web_contents_key2)));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_NE(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_NE(data_collectors_map().find(web_contents_key2),
            data_collectors_map().end());
  EXPECT_NE(nullptr,
            data_collectors_map().at(web_contents_key2).threat_details);
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents2, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_NE(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_NE(data_collectors_map().find(web_contents_key2),
            data_collectors_map().end());
  EXPECT_EQ(nullptr,
            data_collectors_map().at(web_contents_key2).threat_details);
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents1, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_EQ(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_NE(data_collectors_map().find(web_contents_key2),
            data_collectors_map().end());
  EXPECT_EQ(nullptr,
            data_collectors_map().at(web_contents_key2).threat_details);

  // Calling Start twice with the same WebContents is an error, and will return
  // false the second time. But it can still be completed normally.
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents1));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_NE(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_FALSE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents1));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_NE(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents1, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_EQ(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);

  // Calling Finish twice with the same WebContents is an error, and will return
  // false the second time. It's basically a no-op.
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents1));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_NE(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents1, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_EQ(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents1, false));
  EXPECT_NE(data_collectors_map().find(web_contents_key1),
            data_collectors_map().end());
  EXPECT_EQ(nullptr,
            data_collectors_map().at(web_contents_key1).threat_details);
}

TEST_F(TriggerManagerTest, NoDataCollection_Incognito) {
  // Data collection will not begin and no reports will be sent when incognito.
  content::WebContents* web_contents = CreateIncognitoWebContents();
  EXPECT_FALSE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents));
  EXPECT_TRUE(data_collectors_map().empty());
  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents, false));
  EXPECT_TRUE(data_collectors_map().empty());
}

TEST_F(TriggerManagerTest, NoDataCollection_SBEROptInDisallowed) {
  // Data collection will not begin and no reports will be sent when the user is
  // not allowed to opt-in to SBER.
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, false);
  content::WebContents* web_contents = CreateWebContents();
  EXPECT_FALSE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents));
  EXPECT_TRUE(data_collectors_map().empty());
  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents, false));
  EXPECT_TRUE(data_collectors_map().empty());
}

TEST_F(TriggerManagerTest, NoDataCollection_IncognitoAndSBEROptInDisallowed) {
  // Data collection will not begin and no reports will be sent when the user is
  // not allowed to opt-in to SBER and is also incognito.
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, false);
  content::WebContents* web_contents = CreateIncognitoWebContents();
  EXPECT_FALSE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents));
  EXPECT_TRUE(data_collectors_map().empty());
  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents, false));
  EXPECT_TRUE(data_collectors_map().empty());
}

TEST_F(TriggerManagerTest, UserOptedOutOfSBER_DataCollected_NoReportSent) {
  // When the user is opted-out of SBER then data collection will begin but no
  // report will be sent when data collection ends.
  SetPref(prefs::kSafeBrowsingScoutReportingEnabled, false);
  content::WebContents* web_contents = CreateWebContents();
  WebContentsKey web_contents_key = GetWebContentsKey(web_contents);
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents));
  EXPECT_THAT(data_collectors_map(),
              UnorderedElementsAre(Key(web_contents_key)));
  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents, false));
  EXPECT_NE(data_collectors_map().find(web_contents_key),
            data_collectors_map().end());
  EXPECT_EQ(nullptr, data_collectors_map().at(web_contents_key).threat_details);
}

TEST_F(TriggerManagerTest, UserOptsOutOfSBER_DataCollected_NoReportSent) {
  // If the user opts-out of Extended Reporting while data is being collected
  // then no report is sent. Note that the test fixture opts the user into
  // Extended Reporting by default.
  content::WebContents* web_contents = CreateWebContents();
  WebContentsKey web_contents_key = GetWebContentsKey(web_contents);
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents));
  EXPECT_THAT(data_collectors_map(),
              UnorderedElementsAre(Key(web_contents_key)));

  SetPref(prefs::kSafeBrowsingScoutReportingEnabled, false);

  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents, false));
  EXPECT_NE(data_collectors_map().find(web_contents_key),
            data_collectors_map().end());
  EXPECT_EQ(nullptr, data_collectors_map().at(web_contents_key).threat_details);
}

TEST_F(TriggerManagerTest, UserOptsInToSBER_DataCollected_ReportSent) {
  // When the user is opted-out of SBER then data collection will begin. If they
  // opt-in to SBER while data collection is in progress then the report will
  // also be sent.
  SetPref(prefs::kSafeBrowsingScoutReportingEnabled, false);
  content::WebContents* web_contents = CreateWebContents();
  WebContentsKey web_contents_key = GetWebContentsKey(web_contents);
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents));
  EXPECT_THAT(data_collectors_map(),
              UnorderedElementsAre(Key(web_contents_key)));

  SetPref(prefs::kSafeBrowsingScoutReportingEnabled, true);

  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key),
            data_collectors_map().end());
  EXPECT_EQ(nullptr, data_collectors_map().at(web_contents_key).threat_details);
}

TEST_F(TriggerManagerTest,
       SBEROptInBecomesDisallowed_DataCollected_NoReportSent) {
  // If the user loses the ability to opt-in to SBER in the middle of data
  // collection then the report will not be sent.
  content::WebContents* web_contents = CreateWebContents();
  WebContentsKey web_contents_key = GetWebContentsKey(web_contents);
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents));
  EXPECT_THAT(data_collectors_map(),
              UnorderedElementsAre(Key(web_contents_key)));

  // Remove the ability to opt-in to SBER.
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, false);

  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents, false));
  EXPECT_NE(data_collectors_map().find(web_contents_key),
            data_collectors_map().end());
  EXPECT_EQ(nullptr, data_collectors_map().at(web_contents_key).threat_details);
}

TEST_F(TriggerManagerTest, NoCollectionWhenOutOfQuota) {
  // Triggers are not allowed to collect data when they're out of quota, even if
  // all other conditions are as expected.
  content::WebContents* web_contents = CreateWebContents();
  WebContentsKey web_contents_key = GetWebContentsKey(web_contents);

  // Turn on the AD_SAMPLE trigger inside the throttler and confirm that it can
  // fire normally.
  SetTriggerHasQuota(TriggerType::AD_SAMPLE, true);
  EXPECT_TRUE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
  EXPECT_THAT(data_collectors_map(),
              UnorderedElementsAre(Key(web_contents_key)));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::AD_SAMPLE,
                                            web_contents, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key),
            data_collectors_map().end());
  EXPECT_EQ(nullptr, data_collectors_map().at(web_contents_key).threat_details);

  // Turn off the AD_SAMPLE trigger inside the throttler, the trigger should no
  // longer be able to fire.
  SetTriggerHasQuota(TriggerType::AD_SAMPLE, false);
  EXPECT_FALSE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
  EXPECT_NE(data_collectors_map().find(web_contents_key),
            data_collectors_map().end());
  EXPECT_EQ(nullptr, data_collectors_map().at(web_contents_key).threat_details);
}

TEST_F(TriggerManagerTest, NoCollectionWhenSBERDisabledByPolicy) {
  // Confirm that disabling SBER through an enterprise policy does disable
  // triggers.
  content::WebContents* web_contents = CreateWebContents();

  SetManagedPref(prefs::kSafeBrowsingScoutReportingEnabled, false);
  EXPECT_FALSE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
  EXPECT_TRUE(data_collectors_map().empty());
  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::AD_SAMPLE,
                                             web_contents, false));
  EXPECT_TRUE(data_collectors_map().empty());
}

TEST_F(TriggerManagerTest, AdSamplerTrigger) {
  // Check the conditions required for the Ad Sampler trigger to fire. It needs
  // opt-in to start collecting data, scout opt-in, and quota.
  content::WebContents* web_contents = CreateWebContents();
  WebContentsKey web_contents_key = GetWebContentsKey(web_contents);

  // The default setup in this test makes the trigger fire (all prefs enabled,
  // all triggers have quota).
  EXPECT_TRUE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
  EXPECT_THAT(data_collectors_map(),
              UnorderedElementsAre(Key(web_contents_key)));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::AD_SAMPLE,
                                            web_contents, true));
  EXPECT_NE(data_collectors_map().find(web_contents_key),
            data_collectors_map().end());
  EXPECT_EQ(nullptr, data_collectors_map().at(web_contents_key).threat_details);

  // Disabling SBEROptInAllowed disables this trigger.
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, false);
  EXPECT_FALSE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));

  // Confirm it can fire when we re-enable SBEROptInAllowed
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, true);
  EXPECT_TRUE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::AD_SAMPLE,
                                            web_contents, true));

  // Disabling Scout disables this trigger.
  SetPref(prefs::kSafeBrowsingScoutReportingEnabled, false);
  EXPECT_FALSE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));

  // Confirm it can fire when we re-enable Scout and disable legacy SBER.
  SetPref(prefs::kSafeBrowsingScoutReportingEnabled, true);
  EXPECT_TRUE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::AD_SAMPLE,
                                            web_contents, true));

  // Finally, make sure the trigger can't fire if it has no quota.
  SetTriggerHasQuota(TriggerType::AD_SAMPLE, false);
  EXPECT_FALSE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));

  // Confirm it can fire again when quota is available.
  SetTriggerHasQuota(TriggerType::AD_SAMPLE, true);
  EXPECT_TRUE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::AD_SAMPLE,
                                            web_contents, true));
}

TEST_F(TriggerManagerTest, AdSamplerTrigger_Incognito) {
  // Check the conditions required for the Ad Sampler trigger to fire. It needs
  // opt-in to start collecting data, scout opt-in, and quota, and it can't fire
  // in inconito (except when forced on by finch feature).
  content::WebContents* web_contents = CreateIncognitoWebContents();

  // The default setup in this test makes the trigger fire (all prefs enabled,
  // all triggers have quota), but the incognito window prevents it from firing.
  EXPECT_FALSE(
      StartCollectingThreatDetails(TriggerType::AD_SAMPLE, web_contents));
}

TEST_F(TriggerManagerTest,
       CollectionWhenExtendedReportingDeprecationEnabledAllowOptinEnabled) {
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, true);
  scoped_feature_list_.InitAndEnableFeature(
      kExtendedReportingRemovePrefDependency);

  content::WebContents* web_contents = CreateWebContents();
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents, true));
}

TEST_F(TriggerManagerTest,
       CollectionWhenExtendedReportingDeprecationEnabledAllowOptinDisabled) {
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, false);
  scoped_feature_list_.InitAndEnableFeature(
      kExtendedReportingRemovePrefDependency);

  content::WebContents* web_contents = CreateWebContents();
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents, true));
}

TEST_F(TriggerManagerTest,
       CollectionWhenExtendedReportingDeprecationDisabledAllowOptinEnabled) {
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, true);
  scoped_feature_list_.InitAndDisableFeature(
      kExtendedReportingRemovePrefDependency);

  content::WebContents* web_contents = CreateWebContents();
  EXPECT_TRUE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                           web_contents));
  EXPECT_TRUE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents, true));
}

TEST_F(TriggerManagerTest,
       NoCollectionWhenExtendedReportingDeprecationDisabledAllowOptinDisabled) {
  SetPref(prefs::kSafeBrowsingExtendedReportingOptInAllowed, false);
  scoped_feature_list_.InitAndDisableFeature(
      kExtendedReportingRemovePrefDependency);

  content::WebContents* web_contents = CreateWebContents();
  EXPECT_FALSE(StartCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                            web_contents));
  EXPECT_FALSE(FinishCollectingThreatDetails(TriggerType::SECURITY_INTERSTITIAL,
                                             web_contents, false));
}
}  // namespace safe_browsing