summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/ui/webui/management_ui_handler_unittest.cc
blob: 8541fce2c79e182beacdd7fb1c5017e205f5733f (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <set>
#include <string>

#include "base/strings/utf_string_conversions.h"

#include "chrome/browser/ui/webui/management_ui_handler.h"
#include "chrome/test/base/testing_profile.h"

#include "components/policy/core/common/mock_policy_service.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_constants.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#include "ui/base/l10n/l10n_util.h"

#if defined(OS_CHROMEOS)
#include "ui/chromeos/devicetype_utils.h"
#endif  // defined(OS_CHROMEOS)

using testing::_;
using testing::Return;
using testing::ReturnRef;

struct ContextualManagementSourceUpdate {
  base::string16* extension_reporting_title;
  base::string16* subtitle;
#if defined(OS_CHROMEOS)
  base::string16* management_overview;
#else
  base::string16* browser_management_notice;
#endif  // defined(OS_CHROMEOS)
  bool* managed;
};

class TestManagementUIHandler : public ManagementUIHandler {
 public:
  TestManagementUIHandler() = default;
  explicit TestManagementUIHandler(policy::PolicyService* policy_service)
      : policy_service_(policy_service) {}
  ~TestManagementUIHandler() override = default;

  void EnableCloudReportingExtension(bool enable) {
    cloud_reporting_extension_exists_ = enable;
  }

  base::DictionaryValue GetContextualManagedDataForTesting(Profile* profile) {
    return GetContextualManagedData(profile);
  }

  base::Value GetExtensionReportingInfo() {
    base::Value report_sources(base::Value::Type::LIST);
    AddExtensionReportingInfo(&report_sources);
    return report_sources;
  }

  base::Value GetThreatProtectionInfo(Profile* profile) {
    return ManagementUIHandler::GetThreatProtectionInfo(profile);
  }

  policy::PolicyService* GetPolicyService() const override {
    return policy_service_;
  }

  const extensions::Extension* GetEnabledExtension(
      const std::string& extensionId) const override {
    if (cloud_reporting_extension_exists_)
      return extensions::ExtensionBuilder("dummy").SetID("id").Build().get();
    return nullptr;
  }

#if defined(OS_CHROMEOS)
  const std::string GetDeviceDomain() const override { return device_domain; }
  void SetDeviceDomain(const std::string& domain) { device_domain = domain; }
#endif  // defined(OS_CHROMEOS)

 private:
  bool cloud_reporting_extension_exists_ = false;
  policy::PolicyService* policy_service_ = nullptr;
  std::string device_domain = "devicedomain.com";
};

class ManagementUIHandlerTests : public testing::Test {
 public:
  ManagementUIHandlerTests()
      : handler_(&policy_service_),
        device_domain_(base::UTF8ToUTF16("devicedomain.com")) {
    ON_CALL(policy_service_, GetPolicies(_))
        .WillByDefault(ReturnRef(empty_policy_map_));
  }

  base::string16 device_domain() { return device_domain_; }
  void EnablePolicy(const char* policy_key, policy::PolicyMap& policies) {
    policies.Set(policy_key, policy::POLICY_LEVEL_MANDATORY,
                 policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_CLOUD,
                 std::make_unique<base::Value>(true), nullptr);
  }
  void SetPolicyValue(const char* policy_key,
                      policy::PolicyMap& policies,
                      int value) {
    policies.Set(policy_key, policy::POLICY_LEVEL_MANDATORY,
                 policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_CLOUD,
                 std::make_unique<base::Value>(value), nullptr);
  }
  void SetPolicyValue(const char* policy_key,
                      policy::PolicyMap& policies,
                      bool value) {
    policies.Set(policy_key, policy::POLICY_LEVEL_MANDATORY,
                 policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_CLOUD,
                 std::make_unique<base::Value>(value), nullptr);
  }

  void ExtractContextualSourceUpdate(
      const base::DictionaryValue& data,
      const ContextualManagementSourceUpdate& extracted) {
    data.GetString("extensionReportingTitle",
                   extracted.extension_reporting_title);
    data.GetString("pageSubtitle", extracted.subtitle);
#if defined(OS_CHROMEOS)
    data.GetString("overview", extracted.management_overview);
#else
    data.GetString("browserManagementNotice",
                   extracted.browser_management_notice);
#endif  // defined(OS_CHROMEOS)
    data.GetBoolean("managed", extracted.managed);
  }

 protected:
  TestManagementUIHandler handler_;
  content::BrowserTaskEnvironment task_environment_;
  policy::MockPolicyService policy_service_;
  policy::PolicyMap empty_policy_map_;
  base::string16 device_domain_;
};

#if !defined(OS_CHROMEOS)
TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateUnmanagedNoDomain) {
  auto profile = TestingProfile::Builder().Build();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 browser_management_notice;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &browser_management_notice,
      &managed};

  handler_.SetAccountManagedForTesting(false);
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(data.DictSize(), 4u);
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
  EXPECT_EQ(browser_management_notice,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_NOT_MANAGED_NOTICE,
                base::UTF8ToUTF16(chrome::kManagedUiLearnMoreUrl)));
  EXPECT_EQ(subtitle,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_NOT_MANAGED_SUBTITLE));
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedNoDomain) {
  auto profile = TestingProfile::Builder().Build();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 browser_management_notice;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &browser_management_notice,
      &managed};

  handler_.SetAccountManagedForTesting(true);
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(data.DictSize(), 4u);
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
  EXPECT_EQ(browser_management_notice,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_BROWSER_NOTICE,
                base::UTF8ToUTF16(chrome::kManagedUiLearnMoreUrl)));
  EXPECT_EQ(subtitle, l10n_util::GetStringUTF16(IDS_MANAGEMENT_SUBTITLE));
  EXPECT_TRUE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedConsumerDomain) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@gmail.com");
  auto profile = builder.Build();

  base::string16 extensions_installed;
  base::string16 subtitle;
  base::string16 browser_management_notice;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extensions_installed,
      &subtitle,
      &browser_management_notice,
      &managed};

  handler_.SetAccountManagedForTesting(true);
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(data.DictSize(), 4u);
  EXPECT_EQ(extensions_installed,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
  EXPECT_EQ(browser_management_notice,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_BROWSER_NOTICE,
                base::UTF8ToUTF16(chrome::kManagedUiLearnMoreUrl)));
  EXPECT_EQ(subtitle, l10n_util::GetStringUTF16(IDS_MANAGEMENT_SUBTITLE));
  EXPECT_TRUE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateUnmanagedKnownDomain) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@manager.com");
  auto profile = builder.Build();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 browser_management_notice;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &browser_management_notice,
      &managed};

  handler_.SetAccountManagedForTesting(false);

  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(data.DictSize(), 4u);
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
                                       base::UTF8ToUTF16("manager.com")));
  EXPECT_EQ(browser_management_notice,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_NOT_MANAGED_NOTICE,
                base::UTF8ToUTF16(chrome::kManagedUiLearnMoreUrl)));
  EXPECT_EQ(subtitle,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_NOT_MANAGED_SUBTITLE));
  EXPECT_FALSE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateUnmanagedCustomerDomain) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@googlemail.com");
  auto profile = builder.Build();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 browser_management_notice;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &browser_management_notice,
      &managed};

  handler_.SetAccountManagedForTesting(false);

  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(data.DictSize(), 4u);
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
  EXPECT_EQ(browser_management_notice,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_NOT_MANAGED_NOTICE,
                base::UTF8ToUTF16(chrome::kManagedUiLearnMoreUrl)));
  EXPECT_EQ(subtitle,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_NOT_MANAGED_SUBTITLE));
  EXPECT_FALSE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedKnownDomain) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@manager.com");
  auto profile = builder.Build();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 browser_management_notice;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &browser_management_notice,
      &managed};

  handler_.SetAccountManagedForTesting(true);
  handler_.SetDeviceManagedForTesting(false);
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(data.DictSize(), 4u);
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
                                       base::UTF8ToUTF16("manager.com")));
  EXPECT_EQ(browser_management_notice,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_BROWSER_NOTICE,
                base::UTF8ToUTF16(chrome::kManagedUiLearnMoreUrl)));
  EXPECT_EQ(subtitle,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
                                       base::UTF8ToUTF16("manager.com")));
  EXPECT_TRUE(managed);
}

#endif  // !defined(OS_CHROMEOS)

#if defined(OS_CHROMEOS)
TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedAccountKnownDomain) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@manager.com");
  auto profile = builder.Build();
  const auto device_type = ui::GetChromeOSDeviceTypeResourceId();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 management_overview;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &management_overview,
      &managed};

  handler_.SetAccountManagedForTesting(true);
  handler_.SetDeviceManagedForTesting(false);
  handler_.SetDeviceDomain("");
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
                                       base::UTF8ToUTF16("manager.com")));
  EXPECT_EQ(subtitle,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
                                       l10n_util::GetStringUTF16(device_type),
                                       base::UTF8ToUTF16("manager.com")));
  EXPECT_EQ(management_overview,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_ACCOUNT_MANAGED_BY,
                                       base::UTF8ToUTF16("manager.com")));
  EXPECT_TRUE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedAccountUnknownDomain) {
  TestingProfile::Builder builder;
  auto profile = builder.Build();
  const auto device_type = ui::GetChromeOSDeviceTypeResourceId();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 management_overview;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &management_overview,
      &managed};

  handler_.SetAccountManagedForTesting(true);
  handler_.SetDeviceManagedForTesting(false);
  handler_.SetDeviceDomain("");
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
  EXPECT_EQ(subtitle,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED,
                                       l10n_util::GetStringUTF16(device_type)));
  EXPECT_EQ(management_overview, base::string16());
  EXPECT_TRUE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedDevice) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@manager.com");
  auto profile = builder.Build();
  const auto device_type = ui::GetChromeOSDeviceTypeResourceId();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 management_overview;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &management_overview,
      &managed};

  handler_.SetAccountManagedForTesting(false);
  handler_.SetDeviceManagedForTesting(true);
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(subtitle,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
                                       l10n_util::GetStringUTF16(device_type),
                                       device_domain()));
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
                                       device_domain()));
  EXPECT_EQ(management_overview, base::string16());
  EXPECT_TRUE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedDeviceAndAccount) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@devicedomain.com");
  auto profile = builder.Build();
  const auto device_type = ui::GetChromeOSDeviceTypeResourceId();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 management_overview;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &management_overview,
      &managed};

  handler_.SetAccountManagedForTesting(true);
  handler_.SetDeviceManagedForTesting(true);
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(subtitle,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
                                       l10n_util::GetStringUTF16(device_type),
                                       device_domain()));
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
                                       device_domain()));
  EXPECT_EQ(management_overview,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_DEVICE_AND_ACCOUNT_MANAGED_BY, device_domain()));
  EXPECT_TRUE(managed);
}

TEST_F(ManagementUIHandlerTests,
       ManagementContextualSourceUpdateManagedDeviceAndAccountMultipleDomains) {
  TestingProfile::Builder builder;
  builder.SetProfileName("managed@manager.com");
  auto profile = builder.Build();
  const auto device_type = ui::GetChromeOSDeviceTypeResourceId();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 management_overview;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &management_overview,
      &managed};

  handler_.SetAccountManagedForTesting(true);
  handler_.SetDeviceManagedForTesting(true);
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);

  EXPECT_EQ(subtitle,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
                                       l10n_util::GetStringUTF16(device_type),
                                       device_domain()));
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
                                       device_domain()));
  EXPECT_EQ(management_overview,
            l10n_util::GetStringFUTF16(
                IDS_MANAGEMENT_DEVICE_MANAGED_BY_ACCOUNT_MANAGED_BY,
                device_domain(), base::UTF8ToUTF16("manager.com")));
  EXPECT_TRUE(managed);
}

TEST_F(ManagementUIHandlerTests, ManagementContextualSourceUpdateUnmanaged) {
  auto profile = TestingProfile::Builder().Build();
  const auto device_type = ui::GetChromeOSDeviceTypeResourceId();

  base::string16 extension_reporting_title;
  base::string16 subtitle;
  base::string16 management_overview;
  bool managed;
  ContextualManagementSourceUpdate extracted{
      &extension_reporting_title,
      &subtitle,
      &management_overview,
      &managed};

  handler_.SetAccountManagedForTesting(false);
  handler_.SetDeviceDomain("");
  auto data = handler_.GetContextualManagedDataForTesting(profile.get());
  ExtractContextualSourceUpdate(data, extracted);
  EXPECT_EQ(subtitle,
            l10n_util::GetStringFUTF16(IDS_MANAGEMENT_NOT_MANAGED_SUBTITLE,
                                       l10n_util::GetStringUTF16(device_type)));
  EXPECT_EQ(extension_reporting_title,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
  EXPECT_EQ(management_overview,
            l10n_util::GetStringUTF16(IDS_MANAGEMENT_DEVICE_NOT_MANAGED));
  EXPECT_FALSE(managed);
}
#endif

TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoNoPolicySetNoMessage) {
  handler_.EnableCloudReportingExtension(false);
  auto reporting_info = handler_.GetExtensionReportingInfo();
  EXPECT_EQ(reporting_info.GetList().size(), 0u);
}

TEST_F(ManagementUIHandlerTests,
       ExtensionReportingInfoCloudExtensionAddsDefaultPolicies) {
  handler_.EnableCloudReportingExtension(true);

  std::set<std::string> expected_messages = {
      kManagementExtensionReportMachineName, kManagementExtensionReportUsername,
      kManagementExtensionReportVersion,
      kManagementExtensionReportExtensionsPlugin,
      kManagementExtensionReportSafeBrowsingWarnings};

  auto reporting_info = handler_.GetExtensionReportingInfo();
  const auto& reporting_info_list = reporting_info.GetList();

  for (const base::Value& info : reporting_info_list) {
    const std::string* messageId = info.FindStringKey("messageId");
    EXPECT_TRUE(expected_messages.find(*messageId) != expected_messages.end());
  }
  EXPECT_EQ(reporting_info.GetList().size(), expected_messages.size());
}

TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoPoliciesMerge) {
  policy::PolicyMap on_prem_reporting_extension_beta_policies;
  policy::PolicyMap on_prem_reporting_extension_stable_policies;

  EnablePolicy(kPolicyKeyReportUserIdData,
               on_prem_reporting_extension_beta_policies);
  EnablePolicy(kManagementExtensionReportVersion,
               on_prem_reporting_extension_beta_policies);
  EnablePolicy(kPolicyKeyReportUserIdData,
               on_prem_reporting_extension_beta_policies);
  EnablePolicy(kPolicyKeyReportPolicyData,
               on_prem_reporting_extension_stable_policies);

  EnablePolicy(kPolicyKeyReportMachineIdData,
               on_prem_reporting_extension_stable_policies);
  EnablePolicy(kPolicyKeyReportSafeBrowsingData,
               on_prem_reporting_extension_stable_policies);
  EnablePolicy(kPolicyKeyReportSystemTelemetryData,
               on_prem_reporting_extension_stable_policies);
  EnablePolicy(kPolicyKeyReportUserBrowsingData,
               on_prem_reporting_extension_stable_policies);

  const policy::PolicyNamespace
      on_prem_reporting_extension_stable_policy_namespace =
          policy::PolicyNamespace(policy::POLICY_DOMAIN_EXTENSIONS,
                                  kOnPremReportingExtensionStableId);
  const policy::PolicyNamespace
      on_prem_reporting_extension_beta_policy_namespace =
          policy::PolicyNamespace(policy::POLICY_DOMAIN_EXTENSIONS,
                                  kOnPremReportingExtensionBetaId);

  EXPECT_CALL(policy_service_,
              GetPolicies(on_prem_reporting_extension_stable_policy_namespace))
      .WillOnce(ReturnRef(on_prem_reporting_extension_stable_policies));

  EXPECT_CALL(policy_service_,
              GetPolicies(on_prem_reporting_extension_beta_policy_namespace))
      .WillOnce(ReturnRef(on_prem_reporting_extension_beta_policies));

  handler_.EnableCloudReportingExtension(true);

  std::set<std::string> expected_messages = {
      kManagementExtensionReportMachineNameAddress,
      kManagementExtensionReportUsername,
      kManagementExtensionReportVersion,
      kManagementExtensionReportExtensionsPlugin,
      kManagementExtensionReportSafeBrowsingWarnings,
      kManagementExtensionReportUserBrowsingData,
      kManagementExtensionReportPerfCrash};

  auto reporting_info = handler_.GetExtensionReportingInfo();
  const auto& reporting_info_list = reporting_info.GetList();

  for (const base::Value& info : reporting_info_list) {
    const std::string* message_id = info.FindStringKey("messageId");
    ASSERT_TRUE(message_id != nullptr);
    EXPECT_TRUE(expected_messages.find(*message_id) != expected_messages.end());
  }
  EXPECT_EQ(reporting_info.GetList().size(), expected_messages.size());
}

TEST_F(ManagementUIHandlerTests, ThreatReportingInfo) {
  policy::PolicyMap chrome_policies;
  const policy::PolicyNamespace chrome_policies_namespace =
      policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string());

  TestingProfile::Builder builder_no_domain;
  auto profile_no_domain = builder_no_domain.Build();

  TestingProfile::Builder builder_known_domain;
  builder_known_domain.SetProfileName("managed@manager.com");
  auto profile_known_domain = builder_known_domain.Build();

#if defined(OS_CHROMEOS)
  handler_.SetDeviceDomain("");
#endif  // !defined(OS_CHROMEOS)

  EXPECT_CALL(policy_service_, GetPolicies(chrome_policies_namespace))
      .WillRepeatedly(ReturnRef(chrome_policies));

  base::DictionaryValue* threat_protection_info = nullptr;

  // When no policies are set, nothing to report.
  auto info = handler_.GetThreatProtectionInfo(profile_no_domain.get());
  info.GetAsDictionary(&threat_protection_info);
  EXPECT_TRUE(threat_protection_info->FindListKey("info")->GetList().empty());
  EXPECT_EQ(
      l10n_util::GetStringUTF16(IDS_MANAGEMENT_THREAT_PROTECTION_DESCRIPTION),
      base::UTF8ToUTF16(*threat_protection_info->FindStringKey("description")));

  // When policies are set to uninteresting values, nothing to report.
  SetPolicyValue(policy::key::kCheckContentCompliance, chrome_policies, 0);
  SetPolicyValue(policy::key::kSendFilesForMalwareCheck, chrome_policies, 0);
  SetPolicyValue(policy::key::kUnsafeEventsReportingEnabled, chrome_policies,
                 false);
  info = handler_.GetThreatProtectionInfo(profile_known_domain.get());
  info.GetAsDictionary(&threat_protection_info);
  EXPECT_TRUE(threat_protection_info->FindListKey("info")->GetList().empty());
  EXPECT_EQ(
      l10n_util::GetStringFUTF16(
          IDS_MANAGEMENT_THREAT_PROTECTION_DESCRIPTION_BY,
          base::UTF8ToUTF16("manager.com")),
      base::UTF8ToUTF16(*threat_protection_info->FindStringKey("description")));

  // When policies are set to values that enable the feature, report it.
  SetPolicyValue(policy::key::kCheckContentCompliance, chrome_policies, 1);
  SetPolicyValue(policy::key::kSendFilesForMalwareCheck, chrome_policies, 2);
  SetPolicyValue(policy::key::kUnsafeEventsReportingEnabled, chrome_policies,
                 true);
  info = handler_.GetThreatProtectionInfo(profile_no_domain.get());
  info.GetAsDictionary(&threat_protection_info);
  EXPECT_EQ(3u, threat_protection_info->FindListKey("info")->GetList().size());
  EXPECT_EQ(
      l10n_util::GetStringUTF16(IDS_MANAGEMENT_THREAT_PROTECTION_DESCRIPTION),
      base::UTF8ToUTF16(*threat_protection_info->FindStringKey("description")));

  base::Value expected_info(base::Value::Type::LIST);
  {
    base::Value value(base::Value::Type::DICTIONARY);
    value.SetStringKey("title", kManagementDataLossPreventionName);
    value.SetStringKey("permission", kManagementDataLossPreventionPermissions);
    expected_info.GetList().push_back(std::move(value));
  }
  {
    base::Value value(base::Value::Type::DICTIONARY);
    value.SetStringKey("title", kManagementMalwareScanningName);
    value.SetStringKey("permission", kManagementMalwareScanningPermissions);
    expected_info.GetList().push_back(std::move(value));
  }
  {
    base::Value value(base::Value::Type::DICTIONARY);
    value.SetStringKey("title", kManagementEnterpriseReportingName);
    value.SetStringKey("permission", kManagementEnterpriseReportingPermissions);
    expected_info.GetList().push_back(std::move(value));
  }

  EXPECT_EQ(expected_info, *threat_protection_info->FindListKey("info"));
}