File: content_protection_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (774 lines) | stat: -rw-r--r-- 28,079 bytes parent folder | download | duplicates (8)
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ui/display/manager/content_protection_manager.h"

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/manager/test/action_logger_util.h"
#include "ui/display/manager/test/fake_display_snapshot.h"
#include "ui/display/manager/test/test_display_layout_manager.h"
#include "ui/display/manager/test/test_native_display_delegate.h"

namespace display::test {

namespace {

constexpr int64_t kDisplayIds[] = {123, 234, 345, 456};
const DisplayMode kDisplayMode({1366, 768}, false, 60.0f);

}  // namespace

using SecurityChanges = base::flat_map<int64_t, bool>;

class TestObserver : public ContentProtectionManager::Observer {
 public:
  explicit TestObserver(ContentProtectionManager* manager) : manager_(manager) {
    manager_->AddObserver(this);
  }

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

  ~TestObserver() override { manager_->RemoveObserver(this); }

  const SecurityChanges& security_changes() const { return security_changes_; }

  void Reset() { security_changes_.clear(); }

 private:
  void OnDisplaySecurityMaybeChanged(int64_t display_id, bool secure) override {
    security_changes_.emplace(display_id, secure);
  }

  const raw_ptr<ContentProtectionManager> manager_;
  SecurityChanges security_changes_;
};

class ContentProtectionManagerTest : public testing::Test {
 public:
  ContentProtectionManagerTest() = default;

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

  ~ContentProtectionManagerTest() override = default;

  void SetUp() override {
    manager_.set_native_display_delegate(&native_display_delegate_);

    DisplayConnectionType conn_types[] = {
        DISPLAY_CONNECTION_TYPE_INTERNAL, DISPLAY_CONNECTION_TYPE_HDMI,
        DISPLAY_CONNECTION_TYPE_VGA, DISPLAY_CONNECTION_TYPE_HDMI};
    for (size_t i = 0; i < std::size(kDisplayIds); ++i) {
      displays_[i] = FakeDisplaySnapshot::Builder()
                         .SetId(kDisplayIds[i])
                         .SetType(conn_types[i])
                         .SetCurrentMode(kDisplayMode.Clone())
                         .Build();
    }

    UpdateDisplays(2);
  }

  void ApplyContentProtectionCallback(bool success) {
    apply_content_protection_success_ = success;
    apply_content_protection_call_count_++;
  }

  void QueryContentProtectionCallback(bool success,
                                      uint32_t connection_mask,
                                      uint32_t protection_mask) {
    query_content_protection_success_ = success;
    query_content_protection_call_count_++;

    connection_mask_ = connection_mask;
    protection_mask_ = protection_mask;
  }

 protected:
  void UpdateDisplays(size_t count) {
    ASSERT_LE(count, std::size(displays_));

    std::vector<std::unique_ptr<DisplaySnapshot>> displays;
    for (size_t i = 0; i < count; ++i)
      displays.push_back(displays_[i]->Clone());

    native_display_delegate_.SetOutputs(std::move(displays));
    layout_manager_.set_displays(native_display_delegate_.GetOutputs());
  }

  void TriggerDisplayConfiguration() {
    manager_.OnDisplayConfigurationChanged(layout_manager_.GetDisplayStates());
  }

  bool TriggerDisplaySecurityTimeout() {
    return manager_.TriggerDisplaySecurityTimeoutForTesting();
  }

  base::test::SingleThreadTaskEnvironment task_environment_;
  TestDisplayLayoutManager layout_manager_{{}, MULTIPLE_DISPLAY_STATE_INVALID};

  ActionLogger log_;
  TestNativeDisplayDelegate native_display_delegate_{&log_};

  ContentProtectionManager manager_{&layout_manager_,
                                    base::BindRepeating([] { return false; })};

  bool apply_content_protection_success_ = false;
  int apply_content_protection_call_count_ = 0;

  bool query_content_protection_success_ = false;
  int query_content_protection_call_count_ = 0;
  uint32_t connection_mask_ = DISPLAY_CONNECTION_TYPE_NONE;
  uint32_t protection_mask_ = CONTENT_PROTECTION_METHOD_NONE;

  std::unique_ptr<DisplaySnapshot> displays_[std::size(kDisplayIds)];
};

TEST_F(ContentProtectionManagerTest, Basic) {
  UpdateDisplays(1);

  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  manager_.QueryContentProtection(
      id, displays_[0]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(1, query_content_protection_call_count_);
  EXPECT_TRUE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_INTERNAL, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_NONE, protection_mask_);
  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());

  UpdateDisplays(2);

  manager_.QueryContentProtection(
      id, displays_[1]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(2, query_content_protection_call_count_);
  EXPECT_TRUE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_HDMI, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_NONE, protection_mask_);
  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());

  manager_.ApplyContentProtection(
      id, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(1, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);
  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_DESIRED,
                                  CONTENT_PROTECTION_METHOD_HDCP),
            log_.GetActionsAndClear());

  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());

  manager_.QueryContentProtection(
      id, displays_[1]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(3, query_content_protection_call_count_);
  EXPECT_TRUE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_HDMI, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_HDCP, protection_mask_);
  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());

  // Requests on invalid display should fail.
  constexpr int64_t kInvalidDisplayId = -999;
  manager_.QueryContentProtection(
      id, kInvalidDisplayId,
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  manager_.ApplyContentProtection(
      id, kInvalidDisplayId, CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(4, query_content_protection_call_count_);
  EXPECT_FALSE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_NONE, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_NONE, protection_mask_);

  EXPECT_EQ(2, apply_content_protection_call_count_);
  EXPECT_FALSE(apply_content_protection_success_);

  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());

  // Protections should be disabled after unregister.
  manager_.UnregisterClient(id);

  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_UNDESIRED,
                                  CONTENT_PROTECTION_METHOD_NONE),
            log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_UNDESIRED, native_display_delegate_.hdcp_state());
}

TEST_F(ContentProtectionManagerTest, BasicAsync) {
  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  native_display_delegate_.set_run_async(true);

  // Asynchronous tasks should be pending.
  constexpr int kTaskCount = 3;
  for (int i = 0; i < kTaskCount; ++i) {
    manager_.ApplyContentProtection(
        id, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
        base::BindOnce(
            &ContentProtectionManagerTest::ApplyContentProtectionCallback,
            base::Unretained(this)));

    manager_.QueryContentProtection(
        id, displays_[1]->display_id(),
        base::BindOnce(
            &ContentProtectionManagerTest::QueryContentProtectionCallback,
            base::Unretained(this)));
  }

  EXPECT_EQ(0, apply_content_protection_call_count_);
  EXPECT_EQ(0, query_content_protection_call_count_);
  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_UNDESIRED, native_display_delegate_.hdcp_state());

  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(kTaskCount, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  EXPECT_EQ(kTaskCount, query_content_protection_call_count_);
  EXPECT_TRUE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_HDMI, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_HDCP, protection_mask_);

  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_DESIRED,
                                  CONTENT_PROTECTION_METHOD_HDCP),
            log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());

  // Pending task should run even if previous task fails.
  native_display_delegate_.set_set_hdcp_state_expectation(false);

  manager_.ApplyContentProtection(
      id, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_NONE,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  manager_.QueryContentProtection(
      id, displays_[1]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(kTaskCount, apply_content_protection_call_count_);
  EXPECT_EQ(kTaskCount, query_content_protection_call_count_);

  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(kTaskCount + 1, apply_content_protection_call_count_);
  EXPECT_FALSE(apply_content_protection_success_);

  EXPECT_EQ(kTaskCount + 1, query_content_protection_call_count_);
  EXPECT_TRUE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_HDMI, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_NONE, protection_mask_);

  // Disabling protection should fail.
  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_UNDESIRED,
                                  CONTENT_PROTECTION_METHOD_NONE),
            log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());
}

TEST_F(ContentProtectionManagerTest, TwoClients) {
  auto client1 = manager_.RegisterClient();
  auto client2 = manager_.RegisterClient();
  EXPECT_NE(client1, client2);

  // Clients should not be aware of requests from other clients.
  manager_.ApplyContentProtection(
      client1, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(1, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_DESIRED,
                                  CONTENT_PROTECTION_METHOD_HDCP),
            log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());

  manager_.QueryContentProtection(
      client1, displays_[1]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(1, query_content_protection_call_count_);
  EXPECT_TRUE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_HDMI, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_HDCP, protection_mask_);

  manager_.QueryContentProtection(
      client2, displays_[1]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(2, query_content_protection_call_count_);
  EXPECT_TRUE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_HDMI, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_NONE, protection_mask_);

  // Protection should be disabled if there are no client requests.
  manager_.ApplyContentProtection(
      client2, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_NONE,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(2, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());

  manager_.ApplyContentProtection(
      client1, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_NONE,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(3, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_UNDESIRED,
                                  CONTENT_PROTECTION_METHOD_NONE),
            log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_UNDESIRED, native_display_delegate_.hdcp_state());
}

TEST_F(ContentProtectionManagerTest, TwoClientsEnable) {
  auto client1 = manager_.RegisterClient();
  auto client2 = manager_.RegisterClient();
  EXPECT_NE(client1, client2);

  // Multiple requests should result in one update.
  manager_.ApplyContentProtection(
      client1, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(1, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  manager_.ApplyContentProtection(
      client2, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(2, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_DESIRED,
                                  CONTENT_PROTECTION_METHOD_HDCP),
            log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());

  // Requests should not result in updates if protection is already active.
  manager_.ApplyContentProtection(
      client1, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(3, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  manager_.ApplyContentProtection(
      client2, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(4, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());
}

TEST_F(ContentProtectionManagerTest, RequestRetention) {
  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  native_display_delegate_.set_run_async(true);

  // Enable protection on external display.
  manager_.ApplyContentProtection(
      id, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  // Disable protection on internal display.
  manager_.ApplyContentProtection(
      id, displays_[0]->display_id(), CONTENT_PROTECTION_METHOD_NONE,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  EXPECT_EQ(0, apply_content_protection_call_count_);
  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_UNDESIRED, native_display_delegate_.hdcp_state());

  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(2, apply_content_protection_call_count_);
  EXPECT_TRUE(apply_content_protection_success_);

  // Protection on external display should be retained.
  EXPECT_EQ(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_DESIRED,
                                  CONTENT_PROTECTION_METHOD_HDCP)
                .c_str(),
            log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_ENABLED, native_display_delegate_.hdcp_state());
}

TEST_F(ContentProtectionManagerTest, ClientRegistration) {
  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  native_display_delegate_.set_run_async(true);

  manager_.ApplyContentProtection(
      id, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  manager_.QueryContentProtection(
      id, displays_[1]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  manager_.UnregisterClient(id);

  base::RunLoop().RunUntilIdle();

  // Pending callbacks should not run if client was unregistered.
  EXPECT_EQ(0, apply_content_protection_call_count_);
  EXPECT_EQ(0, query_content_protection_call_count_);

  // Unregistration should disable protection.
  EXPECT_EQ(
      JoinActions(GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_DESIRED,
                                        CONTENT_PROTECTION_METHOD_HDCP)
                      .c_str(),
                  GetSetHDCPStateAction(kDisplayIds[1], HDCP_STATE_UNDESIRED,
                                        CONTENT_PROTECTION_METHOD_NONE)
                      .c_str(),
                  nullptr),
      log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_UNDESIRED, native_display_delegate_.hdcp_state());
}

TEST_F(ContentProtectionManagerTest, TasksKilledOnConfigure) {
  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  native_display_delegate_.set_run_async(true);

  manager_.ApplyContentProtection(
      id, displays_[1]->display_id(), CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  manager_.QueryContentProtection(
      id, displays_[1]->display_id(),
      base::BindOnce(
          &ContentProtectionManagerTest::QueryContentProtectionCallback,
          base::Unretained(this)));

  TriggerDisplayConfiguration();
  base::RunLoop().RunUntilIdle();

  // Configuration change should kill tasks and trigger failure callbacks.
  EXPECT_EQ(1, apply_content_protection_call_count_);
  EXPECT_FALSE(apply_content_protection_success_);

  EXPECT_EQ(1, query_content_protection_call_count_);
  EXPECT_FALSE(query_content_protection_success_);
  EXPECT_EQ(DISPLAY_CONNECTION_TYPE_NONE, connection_mask_);
  EXPECT_EQ(CONTENT_PROTECTION_METHOD_NONE, protection_mask_);

  // Pending task to enable protection should have been killed.
  EXPECT_EQ(kNoActions, log_.GetActionsAndClear());
  EXPECT_EQ(HDCP_STATE_UNDESIRED, native_display_delegate_.hdcp_state());
}

TEST_F(ContentProtectionManagerTest, DisplaySecurityObserver) {
  TestObserver observer(&manager_);

  // Internal display is secure if not mirroring.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}, {kDisplayIds[1], false}}),
            observer.security_changes());
  observer.Reset();

  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  native_display_delegate_.set_run_async(true);

  manager_.ApplyContentProtection(
      id, kDisplayIds[1], CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(observer.security_changes().empty());

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Observer should be notified when client applies protection.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}, {kDisplayIds[1], true}}),
            observer.security_changes());
  observer.Reset();

  layout_manager_.set_display_state(MULTIPLE_DISPLAY_STATE_MULTI_MIRROR);
  TriggerDisplayConfiguration();
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Observer should be notified on configuration change.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}, {kDisplayIds[1], true}}),
            observer.security_changes());
  observer.Reset();

  manager_.ApplyContentProtection(
      id, kDisplayIds[1], CONTENT_PROTECTION_METHOD_NONE,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  // Timer should be stopped when no client requests protection.
  EXPECT_FALSE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Internal display is not secure if mirrored to an unprotected display.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], false}, {kDisplayIds[1], false}}),
            observer.security_changes());
  observer.Reset();

  native_display_delegate_.set_set_hdcp_state_expectation(false);

  manager_.ApplyContentProtection(
      id, kDisplayIds[1], CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(observer.security_changes().empty());

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Internal display is not secure if mirrored to an unprotected display.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], false}, {kDisplayIds[1], false}}),
            observer.security_changes());
  observer.Reset();

  native_display_delegate_.set_hdcp_state(HDCP_STATE_UNDESIRED);
  native_display_delegate_.set_set_hdcp_state_expectation(true);

  manager_.ApplyContentProtection(
      id, kDisplayIds[1], CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(observer.security_changes().empty());

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Internal display is secure if mirrored to a protected display.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}, {kDisplayIds[1], true}}),
            observer.security_changes());
  observer.Reset();

  layout_manager_.set_display_state(MULTIPLE_DISPLAY_STATE_MULTI_EXTENDED);
  TriggerDisplayConfiguration();
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Observer should be notified on configuration change.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}, {kDisplayIds[1], true}}),
            observer.security_changes());
  observer.Reset();

  manager_.UnregisterClient(id);

  // Timer should be stopped when no client requests protection.
  EXPECT_FALSE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Observer should be notified when client unregisters.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}, {kDisplayIds[1], false}}),
            observer.security_changes());
}

TEST_F(ContentProtectionManagerTest, NoSecurityPollingIfInternalDisplayOnly) {
  UpdateDisplays(1);
  TestObserver observer(&manager_);

  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}}),
            observer.security_changes());
  observer.Reset();

  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  native_display_delegate_.set_run_async(true);

  manager_.ApplyContentProtection(
      id, kDisplayIds[0], CONTENT_PROTECTION_METHOD_HDCP,
      base::BindOnce(
          &ContentProtectionManagerTest::ApplyContentProtectionCallback,
          base::Unretained(this)));

  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}}),
            observer.security_changes());
  observer.Reset();

  // Timer should not be running unless there are external displays.
  EXPECT_FALSE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(SecurityChanges(), observer.security_changes());

  manager_.UnregisterClient(id);

  EXPECT_FALSE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true}}),
            observer.security_changes());
}

TEST_F(ContentProtectionManagerTest, AnalogDisplaySecurity) {
  UpdateDisplays(3);
  TestObserver observer(&manager_);

  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true},
                             {kDisplayIds[1], false},
                             {kDisplayIds[2], false}}),
            observer.security_changes());
  observer.Reset();

  auto id = manager_.RegisterClient();
  EXPECT_TRUE(id);

  native_display_delegate_.set_run_async(true);

  for (int64_t display_id : kDisplayIds) {
    manager_.ApplyContentProtection(
        id, display_id, CONTENT_PROTECTION_METHOD_HDCP,
        base::BindOnce(
            &ContentProtectionManagerTest::ApplyContentProtectionCallback,
            base::Unretained(this)));
  }

  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(observer.security_changes().empty());

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Analog display is never secure.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true},
                             {kDisplayIds[1], true},
                             {kDisplayIds[2], false}}),
            observer.security_changes());
  observer.Reset();

  layout_manager_.set_display_state(MULTIPLE_DISPLAY_STATE_MULTI_MIRROR);
  TriggerDisplayConfiguration();
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Internal display is not secure if mirrored to an analog display.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], false},
                             {kDisplayIds[1], false},
                             {kDisplayIds[2], false}}),
            observer.security_changes());
  observer.Reset();

  layout_manager_.set_display_state(MULTIPLE_DISPLAY_STATE_MULTI_EXTENDED);
  TriggerDisplayConfiguration();
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true},
                             {kDisplayIds[1], true},
                             {kDisplayIds[2], false}}),
            observer.security_changes());
  observer.Reset();

  manager_.UnregisterClient(id);

  // Timer should be stopped when no client requests protection.
  EXPECT_FALSE(TriggerDisplaySecurityTimeout());
  base::RunLoop().RunUntilIdle();

  // Observer should be notified when client unregisters.
  EXPECT_EQ(SecurityChanges({{kDisplayIds[0], true},
                             {kDisplayIds[1], false},
                             {kDisplayIds[2], false}}),
            observer.security_changes());
}

}  // namespace display::test