File: background_mode_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (1169 lines) | stat: -rw-r--r-- 49,723 bytes parent folder | download | duplicates (4)
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
// Copyright 2011 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/background/extensions/background_mode_manager.h"

#include <stddef.h>

#include <algorithm>
#include <memory>
#include <string>
#include <utility>

#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_simple_task_runner.h"
#include "build/build_config.h"
#include "chrome/browser/background/startup_launch_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/status_icons/status_icon_menu_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/keep_alive_registry/keep_alive_registry.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension_builder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image.h"
#include "ui/message_center/public/cpp/notifier_id.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/login/users/user_manager_delegate_impl.h"
#include "chrome/browser/ash/settings/scoped_cros_settings_test_helper.h"
#include "chrome/browser/browser_process.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager_impl.h"
#endif

using extensions::mojom::ManifestLocation;
using testing::_;
using testing::AtMost;
using testing::Exactly;
using testing::InSequence;
using testing::Mock;
using testing::NiceMock;
using testing::StrictMock;

namespace {

std::unique_ptr<TestingProfileManager> CreateTestingProfileManager() {
  std::unique_ptr<TestingProfileManager> profile_manager(
      new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
  EXPECT_TRUE(profile_manager->SetUp());
  return profile_manager;
}

// Helper class that tracks state transitions in BackgroundModeManager and
// exposes them via getters.
class TestBackgroundModeManager : public StrictMock<BackgroundModeManager> {
 public:
  TestBackgroundModeManager(const base::CommandLine& command_line,
                            ProfileAttributesStorage* storage)
      : StrictMock<BackgroundModeManager>(command_line, storage) {
    ResumeBackgroundMode();
  }
  TestBackgroundModeManager(TestBackgroundModeManager&) = delete;
  TestBackgroundModeManager& operator=(TestBackgroundModeManager&) = delete;
  ~TestBackgroundModeManager() override = default;

  // TODO: Use strict-mocking rather than keeping state through overrides below.
  void DisplayClientInstalledNotification(const std::u16string& name) override {
    has_shown_balloon_ = true;
  }
  void CreateStatusTrayIcon() override { have_status_tray_ = true; }
  void RemoveStatusTrayIcon() override { have_status_tray_ = false; }

  bool HaveStatusTray() const { return have_status_tray_; }
  bool HasShownBalloon() const { return has_shown_balloon_; }
  void SetHasShownBalloon(bool value) { has_shown_balloon_ = value; }

 private:
  // Flags to track whether we have a status tray/have shown the balloon.
  bool have_status_tray_ = false;
  bool has_shown_balloon_ = false;
};

class TestStatusIcon : public StatusIcon {
 public:
  TestStatusIcon() = default;
  TestStatusIcon(TestStatusIcon&) = delete;
  TestStatusIcon& operator=(TestStatusIcon&) = delete;

  void SetImage(const gfx::ImageSkia& image) override {}
  void SetToolTip(const std::u16string& tool_tip) override {}
  void DisplayBalloon(const gfx::ImageSkia& icon,
                      const std::u16string& title,
                      const std::u16string& contents,
                      const message_center::NotifierId& notifier_id) override {}
  void UpdatePlatformContextMenu(StatusIconMenuModel* menu) override {}
};

class TestStartupLaunchManager : public StartupLaunchManager {
 public:
  TestStartupLaunchManager() = default;
  MOCK_METHOD1(UpdateLaunchOnStartup, void(bool should_launch_on_startup));
};

void AssertBackgroundModeActive(const TestBackgroundModeManager& manager) {
  EXPECT_TRUE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
  EXPECT_TRUE(manager.HaveStatusTray());
}

void AssertBackgroundModeInactive(const TestBackgroundModeManager& manager) {
  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
  EXPECT_FALSE(manager.HaveStatusTray());
}

}  // namespace

// More complex test helper that exposes APIs for fine grained control of
// things like the number of background applications. This allows writing
// smaller tests that don't have to install/uninstall extensions.
class AdvancedTestBackgroundModeManager : public TestBackgroundModeManager {
 public:
  AdvancedTestBackgroundModeManager(const base::CommandLine& command_line,
                                    ProfileAttributesStorage* storage,
                                    bool enabled)
      : TestBackgroundModeManager(command_line, storage), enabled_(enabled) {}
  AdvancedTestBackgroundModeManager(AdvancedTestBackgroundModeManager&) =
      delete;
  AdvancedTestBackgroundModeManager& operator=(
      AdvancedTestBackgroundModeManager&) = delete;
  ~AdvancedTestBackgroundModeManager() override = default;

  // TestBackgroundModeManager:
  bool HasPersistentBackgroundClient() const override {
    return std::ranges::any_of(
        profile_app_counts_, [](const auto& profile_count_pair) {
          return profile_count_pair.second.persistent > 0;
        });
  }
  bool HasAnyBackgroundClient() const override {
    return std::ranges::any_of(profile_app_counts_,
                               [](const auto& profile_count_pair) {
                                 return profile_count_pair.second.any > 0;
                               });
  }
  bool HasPersistentBackgroundClientForProfile(
      const Profile* profile) const override {
    auto it = profile_app_counts_.find(profile);
    if (it == profile_app_counts_.end()) {
      ADD_FAILURE();
      return false;
    }
    return it->second.persistent > 0;
  }

  bool IsBackgroundModePrefEnabled() const override { return enabled_; }

  void SetBackgroundClientCountForProfile(const Profile* profile,
                                          size_t count) {
    profile_app_counts_[profile] = {count, count};
  }

  void SetPersistentBackgroundClientCountForProfile(const Profile* profile,
                                                    size_t count) {
    profile_app_counts_[profile].persistent = count;
  }

  void SetEnabled(bool enabled) {
    enabled_ = enabled;
    OnBackgroundModeEnabledPrefChanged();
  }

  using BackgroundModeManager::OnApplicationListChanged;

 private:
  struct AppCounts {
    size_t any = 0;
    size_t persistent = 0;
  };
  bool enabled_;
  std::map<const Profile*, AppCounts> profile_app_counts_;
};

class BackgroundModeManagerTest : public testing::Test {
 public:
  BackgroundModeManagerTest() = default;
  BackgroundModeManagerTest(BackgroundModeManagerTest&) = delete;
  BackgroundModeManagerTest& operator=(BackgroundModeManagerTest&) = delete;
  ~BackgroundModeManagerTest() override = default;

  void SetUp() override {
    command_line_ =
        std::make_unique<base::CommandLine>(base::CommandLine::NO_PROGRAM);

    auto policy_service = std::make_unique<policy::PolicyServiceImpl>(
        std::vector<
            raw_ptr<policy::ConfigurationPolicyProvider, VectorExperimental>>{
            &policy_provider_});
    profile_manager_ = CreateTestingProfileManager();
    profile_ = profile_manager_->CreateTestingProfile(
        "p1", nullptr, u"p1", 0, TestingProfile::TestingFactories(),
        /*is_supervised_profile=*/false, std::nullopt,
        std::move(policy_service));
    startup_launch_manager_ = std::make_unique<TestStartupLaunchManager>();
    StartupLaunchManager::SetInstanceForTesting(startup_launch_manager_.get());
  }

  TestStartupLaunchManager* startup_launch_manager() {
    return startup_launch_manager_.get();
  }

 protected:
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<base::CommandLine> command_line_;
  std::unique_ptr<TestStartupLaunchManager> startup_launch_manager_;
  NiceMock<policy::MockConfigurationPolicyProvider> policy_provider_;

  std::unique_ptr<TestingProfileManager> profile_manager_;
  // Test profile used by all tests - this is owned by profile_manager_.
  raw_ptr<TestingProfile> profile_;
};

class BackgroundModeManagerWithExtensionsTest : public testing::Test {
 public:
  BackgroundModeManagerWithExtensionsTest() = default;
  BackgroundModeManagerWithExtensionsTest(
      BackgroundModeManagerWithExtensionsTest&) = delete;
  BackgroundModeManagerWithExtensionsTest& operator=(
      BackgroundModeManagerWithExtensionsTest&) = delete;
  ~BackgroundModeManagerWithExtensionsTest() override = default;

  void SetUp() override {
    command_line_ =
        std::make_unique<base::CommandLine>(base::CommandLine::NO_PROGRAM);
    profile_manager_ = CreateTestingProfileManager();
    profile_ = profile_manager_->CreateTestingProfile("p1");

    test_keep_alive_ = std::make_unique<ScopedKeepAlive>(
        KeepAliveOrigin::BACKGROUND_MODE_MANAGER,
        KeepAliveRestartOption::DISABLED);

    startup_launch_manager_ = std::make_unique<TestStartupLaunchManager>();
    StartupLaunchManager::SetInstanceForTesting(startup_launch_manager_.get());

    // Create our test BackgroundModeManager.
    manager_ = std::make_unique<TestBackgroundModeManager>(
        *command_line_, profile_manager_->profile_attributes_storage());
    manager_->RegisterProfile(profile_);
  }

  void TearDown() override {
    // Clean up the status icon. If this is not done before profile deletes,
    // the context menu updates will DCHECK with the now deleted profiles.
    manager_->status_icon_ = nullptr;

    // We're getting ready to shutdown the message loop. Clear everything out!
    base::RunLoop().RunUntilIdle();

    // TestBackgroundModeManager has dependencies on the infrastructure.
    // It should get cleared first.
    manager_.reset();

    // Now that the background manager is destroyed, the test KeepAlive can be
    // cleared without having |manager_| attempt to perform optimizations.
    test_keep_alive_.reset();

    profile_ = nullptr;

    // The Profile Manager references the Browser Process.
    // The Browser Process references the Notification UI Manager.
    // The Notification UI Manager references the Message Center.
    // As a result, we have to clear the browser process state here
    // before tearing down the Message Center.
    profile_manager_.reset();

    // Clear the shutdown flag to isolate the remaining effect of this test.
    browser_shutdown::SetTryingToQuit(false);
  }

  TestStartupLaunchManager* startup_launch_manager() {
    return startup_launch_manager_.get();
  }

 protected:
  // From views::MenuModelAdapter::IsCommandEnabled with modification.
  bool IsCommandEnabled(ui::MenuModel* model, int id) const {
    size_t index = 0;
    return ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index) &&
           model->IsEnabledAt(index);
  }

  std::unique_ptr<TestBackgroundModeManager> manager_;

  std::unique_ptr<base::CommandLine> command_line_;

  std::unique_ptr<TestingProfileManager> profile_manager_;
  // Test profile used by all tests - this is owned by profile_manager_.
  raw_ptr<TestingProfile> profile_;

 private:
  // Required for extension service.
  content::BrowserTaskEnvironment task_environment_;

  // BackgroundModeManager actually affects Chrome start/stop state,
  // tearing down our thread bundle before we've had chance to clean
  // everything up. Keeping Chrome alive prevents this.
  // We aren't interested in if the keep alive works correctly in this test.
  std::unique_ptr<ScopedKeepAlive> test_keep_alive_;

  std::unique_ptr<TestStartupLaunchManager> startup_launch_manager_;

#if BUILDFLAG(IS_CHROMEOS)
  // ChromeOS needs extra services to run in the following order.
  ash::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
  user_manager::ScopedUserManager user_manager_{
      std::make_unique<user_manager::UserManagerImpl>(
          std::make_unique<UserManagerDelegateImpl>(),
          g_browser_process->local_state(),
          CrosSettings::Get())};
#endif
};

TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) {
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();

  // Mimic app load.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));

  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);

  manager.SuspendBackgroundMode();
  AssertBackgroundModeInactive(manager);
  manager.ResumeBackgroundMode();

  // Mimic app unload.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeInactive(manager);

  manager.SuspendBackgroundMode();
  AssertBackgroundModeInactive(manager);

  // Mimic app load while suspended, e.g. from sync. This should enable and
  // resume background mode.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
}

// Apps installed while background mode is disabled should cause activation
// after it is enabled - crbug.com/527023.
TEST_F(BackgroundModeManagerTest, DISABLED_BackgroundAppInstallWhileDisabled) {
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);

  // Turn off background mode (shouldn't explicitly disable launch-on-startup as
  // the app-count is zero and launch-on-startup shouldn't be considered on).
  manager.SetEnabled(false);
  AssertBackgroundModeInactive(manager);

  // When a new client is installed, status tray icons will not be created,
  // launch on startup status will not be modified.
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  AssertBackgroundModeInactive(manager);

  // Turn back on background mode, should show status tray icon as there is now
  // an app installed.
  manager.SetEnabled(true);
  AssertBackgroundModeActive(manager);
}

// Apps installed and uninstalled while background mode is disabled should do
// nothing.
TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) {
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);

  // Turn off background mode (should explicitly disable launch-on-startup as
  // the app-count is zero and launch-on-startup hasn't been initialized yet).
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetEnabled(false);
  AssertBackgroundModeInactive(manager);
  Mock::VerifyAndClearExpectations(launch_manager);

  // When a new client is installed, status tray icons will not be created,
  // launch on startup status will not be modified.
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  AssertBackgroundModeInactive(manager);

  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  AssertBackgroundModeInactive(manager);

  // Re-enable background mode (shouldn't actually enable launch-on-startup as
  // the app-count is zero).
  manager.SetEnabled(true);
  AssertBackgroundModeInactive(manager);
}

// Apps installed before background mode is disabled cause the icon to show up
// again when it is enabled.
TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) {
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);

  // Install app, should show status tray icon.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.OnBackgroundClientInstalled(u"name");
  // OnBackgroundClientInstalled does not actually add an app to the
  // BackgroundApplicationListModel which would result in another
  // call to CreateStatusTray.
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  AssertBackgroundModeActive(manager);
  Mock::VerifyAndClearExpectations(launch_manager);

  // Turn off background mode - should hide status tray icon.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetEnabled(false);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeInactive(manager);

  // Turn back on background mode, should show status tray icon again as there
  // was already an app installed before background mode was disabled.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.SetEnabled(true);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);

  // Uninstall app, should hide status tray icon again.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeInactive(manager);
}

TEST_F(BackgroundModeManagerTest, MultiProfile) {
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  TestingProfile* profile2 = profile_manager_->CreateTestingProfile("p2");
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  manager.RegisterProfile(profile2);
  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());

  // Install app, should show status tray icon.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);

  // Install app for other profile, should show other status tray icon.
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile2, 2);
  manager.OnApplicationListChanged(profile2);
  AssertBackgroundModeActive(manager);

  // Should hide both status tray icons.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetEnabled(false);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeInactive(manager);

  // Turn back on background mode - should show both status tray icons.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.SetEnabled(true);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);

  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  manager.SetBackgroundClientCountForProfile(profile2, 1);
  manager.OnApplicationListChanged(profile2);
  // There is still one background app alive
  AssertBackgroundModeActive(manager);
  // Verify the implicit expectations of no calls on this StrictMock.
  Mock::VerifyAndClearExpectations(launch_manager);

  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetBackgroundClientCountForProfile(profile2, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeInactive(manager);
}

TEST_F(BackgroundModeManagerTest, ProfileAttributesStorage) {
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  TestingProfile* profile2 = profile_manager_->CreateTestingProfile("p2");
  ProfileAttributesStorage* storage =
      profile_manager_->profile_attributes_storage();
  AdvancedTestBackgroundModeManager manager(*command_line_, storage, true);
  manager.RegisterProfile(profile_);
  manager.RegisterProfile(profile2);
  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
  EXPECT_EQ(2u, storage->GetNumberOfProfiles());

  ProfileAttributesEntry* entry1 =
      storage->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry1, nullptr);
  ProfileAttributesEntry* entry2 =
      storage->GetProfileAttributesWithPath(profile2->GetPath());
  ASSERT_NE(entry2, nullptr);

  EXPECT_FALSE(entry1->GetBackgroundStatus());
  EXPECT_FALSE(entry2->GetBackgroundStatus());

  // Install app, should show status tray icon.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);

  // Install app for other profile.
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile2, 1);
  manager.OnApplicationListChanged(profile2);

  EXPECT_TRUE(entry1->GetBackgroundStatus());
  EXPECT_TRUE(entry2->GetBackgroundStatus());

  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);

  EXPECT_FALSE(entry1->GetBackgroundStatus());

  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetBackgroundClientCountForProfile(profile2, 0);
  manager.OnApplicationListChanged(profile2);
  Mock::VerifyAndClearExpectations(launch_manager);

  EXPECT_FALSE(entry2->GetBackgroundStatus());

  // Even though neither has background status on, there should still be two
  // profiles in the ProfileAttributesStorage.
  EXPECT_EQ(2u, storage->GetNumberOfProfiles());
}

TEST_F(BackgroundModeManagerTest, ProfileAttributesStorageObserver) {
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());

  // Install app, should show status tray icon.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);

  // Background mode should remain active for the remainder of this test.

  manager.OnProfileNameChanged(profile_->GetPath(),
                               manager.GetBackgroundModeData(profile_)->name());

  EXPECT_EQ(u"p1", manager.GetBackgroundModeData(profile_)->name());

  EXPECT_TRUE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
  TestingProfile* profile2 = profile_manager_->CreateTestingProfile("p2");
  manager.RegisterProfile(profile2);
  EXPECT_EQ(2U, manager.NumberOfBackgroundModeData());

  manager.OnProfileAdded(profile2->GetPath());
  EXPECT_EQ(u"p2", manager.GetBackgroundModeData(profile2)->name());

  manager.OnProfileWillBeRemoved(profile2->GetPath());
  // Should still be in background mode after deleting profile.
  EXPECT_TRUE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
  EXPECT_EQ(1U, manager.NumberOfBackgroundModeData());

  // Check that the background mode data we think is in the map actually is.
  EXPECT_EQ(u"p1", manager.GetBackgroundModeData(profile_)->name());
}

TEST_F(BackgroundModeManagerTest, DeleteBackgroundProfile) {
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  // Tests whether deleting the only profile when it is a BG profile works
  // or not (http://crbug.com/346214).
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());

  // Install app, should show status tray icon.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  manager.OnBackgroundClientInstalled(u"name");
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);

  manager.OnProfileNameChanged(profile_->GetPath(),
                               manager.GetBackgroundModeData(profile_)->name());

  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  EXPECT_TRUE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnProfileWillBeRemoved(profile_->GetPath());
  Mock::VerifyAndClearExpectations(launch_manager);
  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());
}

TEST_F(BackgroundModeManagerTest, DisableBackgroundModeUnderTestFlag) {
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  command_line_->AppendSwitch(switches::kKeepAliveForTest);
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  EXPECT_TRUE(manager.ShouldBeInBackgroundMode());

  // No enable-launch-on-startup calls expected yet.
  Mock::VerifyAndClearExpectations(launch_manager);
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetEnabled(false);
  EXPECT_FALSE(manager.ShouldBeInBackgroundMode());
}

TEST_F(BackgroundModeManagerTest,
       BackgroundModeDisabledPreventsKeepAliveOnStartup) {
  command_line_->AppendSwitch(switches::kKeepAliveForTest);
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), false);
  manager.RegisterProfile(profile_);
  EXPECT_FALSE(manager.ShouldBeInBackgroundMode());
}

TEST_F(BackgroundModeManagerWithExtensionsTest, BackgroundMenuGeneration) {
  scoped_refptr<const extensions::Extension> component_extension =
      extensions::ExtensionBuilder("Component Extension")
          .SetLocation(ManifestLocation::kComponent)
          .AddAPIPermission("background")
          .Build();

  scoped_refptr<const extensions::Extension> component_extension_with_options =
      extensions::ExtensionBuilder("Component Extension with Options")
          .SetLocation(ManifestLocation::kComponent)
          .AddAPIPermission("background")
          .SetManifestKey("options_page", "test.html")
          .Build();

  scoped_refptr<const extensions::Extension> regular_extension =
      extensions::ExtensionBuilder("Regular Extension")
          .SetLocation(ManifestLocation::kCommandLine)
          .AddAPIPermission("background")
          .Build();

  scoped_refptr<const extensions::Extension> regular_extension_with_options =
      extensions::ExtensionBuilder("Regular Extension with Options")
          .SetLocation(ManifestLocation::kCommandLine)
          .AddAPIPermission("background")
          .SetManifestKey("options_page", "test.html")
          .Build();

  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile_))
      ->Init();
  extensions::ExtensionService* service =
      extensions::ExtensionSystem::Get(profile_)->extension_service();
  service->Init();
  // ExtensionSystem::ready() is dispatched using PostTask to UI Thread. Wait
  // until idle so that BackgroundApplicationListModel::OnExtensionSystemReady
  // called.
  base::RunLoop().RunUntilIdle();
  extensions::ExtensionRegistrar* registrar =
      extensions::ExtensionRegistrar::Get(profile_);
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();

  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  registrar->AddComponentExtension(component_extension.get());
  registrar->AddComponentExtension(component_extension_with_options.get());
  registrar->AddExtension(regular_extension.get());
  registrar->AddExtension(regular_extension_with_options.get());
  Mock::VerifyAndClearExpectations(launch_manager);

  auto menu = std::make_unique<StatusIconMenuModel>(nullptr);
  auto submenu = std::make_unique<StatusIconMenuModel>(nullptr);
  BackgroundModeManager::BackgroundModeData* bmd =
      manager_->GetBackgroundModeData(profile_);
  bmd->BuildProfileMenu(submenu.get(), menu.get());
  EXPECT_EQ(submenu->GetLabelAt(0), u"Component Extension");
  EXPECT_FALSE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(0)));
  EXPECT_EQ(submenu->GetLabelAt(1), u"Component Extension with Options");
  EXPECT_TRUE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(1)));
  EXPECT_EQ(submenu->GetLabelAt(2), u"Regular Extension");
  EXPECT_TRUE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(2)));
  EXPECT_EQ(submenu->GetLabelAt(3), u"Regular Extension with Options");
  EXPECT_TRUE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(3)));
}

TEST_F(BackgroundModeManagerWithExtensionsTest,
       BackgroundMenuGenerationMultipleProfile) {
  // Helper methods to build extensions; we build new instances so that each
  // Extension object is only used in a single profile.
  auto build_component_extension = []() {
    return extensions::ExtensionBuilder("Component Extension")
        .SetLocation(ManifestLocation::kComponent)
        .AddAPIPermission("background")
        .Build();
  };
  auto build_component_extension_with_options = []() {
    return extensions::ExtensionBuilder("Component Extension with Options")
        .SetLocation(ManifestLocation::kComponent)
        .AddAPIPermission("background")
        .SetManifestKey("options_page", "test.html")
        .Build();
  };
  auto build_regular_extension = []() {
    return extensions::ExtensionBuilder("Regular Extension")
        .SetLocation(ManifestLocation::kCommandLine)
        .AddAPIPermission("background")
        .Build();
  };
  auto build_regular_extension_with_options = []() {
    return extensions::ExtensionBuilder("Regular Extension with Options")
        .SetLocation(ManifestLocation::kCommandLine)
        .AddAPIPermission("background")
        .SetManifestKey("options_page", "test.html")
        .Build();
  };

  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile_))
      ->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
                               base::FilePath(), false);
  extensions::ExtensionService* service1 =
      extensions::ExtensionSystem::Get(profile_)->extension_service();
  service1->Init();
  base::RunLoop().RunUntilIdle();
  extensions::ExtensionRegistrar* registrar1 =
      extensions::ExtensionRegistrar::Get(profile_);
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();

  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  registrar1->AddComponentExtension(build_component_extension().get());
  registrar1->AddComponentExtension(
      build_component_extension_with_options().get());
  registrar1->AddExtension(build_regular_extension().get());
  registrar1->AddExtension(build_regular_extension_with_options().get());
  Mock::VerifyAndClearExpectations(launch_manager);

  TestingProfile* profile2 = profile_manager_->CreateTestingProfile("p2");
  manager_->RegisterProfile(profile2);

  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile2))
      ->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
                               base::FilePath(), false);
  extensions::ExtensionService* service2 =
      extensions::ExtensionSystem::Get(profile2)->extension_service();
  service2->Init();
  base::RunLoop().RunUntilIdle();
  extensions::ExtensionRegistrar* registrar2 =
      extensions::ExtensionRegistrar::Get(profile2);

  registrar2->AddComponentExtension(build_component_extension().get());
  registrar2->AddExtension(build_regular_extension().get());
  registrar2->AddExtension(build_regular_extension_with_options().get());

  std::unique_ptr<StatusIcon> test_status_icon =
      std::make_unique<TestStatusIcon>();
  manager_->status_icon_ = test_status_icon.get();
  manager_->UpdateStatusTrayIconContextMenu();
  StatusIconMenuModel* context_menu = manager_->context_menu_;
  EXPECT_TRUE(context_menu);

  // Background Profile Enable Checks
  EXPECT_EQ(context_menu->GetLabelAt(3), u"p1");
  EXPECT_TRUE(
      context_menu->IsCommandIdEnabled(context_menu->GetCommandIdAt(3)));
  EXPECT_EQ(context_menu->GetCommandIdAt(3), 4);

  EXPECT_EQ(context_menu->GetLabelAt(4), u"p2");
  EXPECT_TRUE(
      context_menu->IsCommandIdEnabled(context_menu->GetCommandIdAt(4)));
  EXPECT_EQ(context_menu->GetCommandIdAt(4), 8);

  // Profile 1 Submenu Checks
  StatusIconMenuModel* profile1_submenu =
      static_cast<StatusIconMenuModel*>(context_menu->GetSubmenuModelAt(3));
  EXPECT_EQ(profile1_submenu->GetLabelAt(0), u"Component Extension");
  EXPECT_FALSE(profile1_submenu->IsCommandIdEnabled(
      profile1_submenu->GetCommandIdAt(0)));
  EXPECT_EQ(profile1_submenu->GetCommandIdAt(0), 0);
  EXPECT_EQ(profile1_submenu->GetLabelAt(1),
            u"Component Extension with Options");
  EXPECT_TRUE(profile1_submenu->IsCommandIdEnabled(
      profile1_submenu->GetCommandIdAt(1)));
  EXPECT_EQ(profile1_submenu->GetCommandIdAt(1), 1);
  EXPECT_EQ(profile1_submenu->GetLabelAt(2), u"Regular Extension");
  EXPECT_TRUE(profile1_submenu->IsCommandIdEnabled(
      profile1_submenu->GetCommandIdAt(2)));
  EXPECT_EQ(profile1_submenu->GetCommandIdAt(2), 2);
  EXPECT_EQ(profile1_submenu->GetLabelAt(3), u"Regular Extension with Options");
  EXPECT_TRUE(profile1_submenu->IsCommandIdEnabled(
      profile1_submenu->GetCommandIdAt(3)));
  EXPECT_EQ(profile1_submenu->GetCommandIdAt(3), 3);

  // Profile 2 Submenu Checks
  StatusIconMenuModel* profile2_submenu =
      static_cast<StatusIconMenuModel*>(context_menu->GetSubmenuModelAt(4));
  EXPECT_EQ(profile2_submenu->GetLabelAt(0), u"Component Extension");
  EXPECT_FALSE(profile2_submenu->IsCommandIdEnabled(
      profile2_submenu->GetCommandIdAt(0)));
  EXPECT_EQ(profile2_submenu->GetCommandIdAt(0), 5);
  EXPECT_EQ(profile2_submenu->GetLabelAt(1), u"Regular Extension");
  EXPECT_TRUE(profile2_submenu->IsCommandIdEnabled(
      profile2_submenu->GetCommandIdAt(1)));
  EXPECT_EQ(profile2_submenu->GetCommandIdAt(1), 6);
  EXPECT_EQ(profile2_submenu->GetLabelAt(2), u"Regular Extension with Options");
  EXPECT_TRUE(profile2_submenu->IsCommandIdEnabled(
      profile2_submenu->GetCommandIdAt(2)));
  EXPECT_EQ(profile2_submenu->GetCommandIdAt(2), 7);

  // Model Adapter Checks for crbug.com/315164
  // P1: Profile 1 Menu Item
  // P2: Profile 2 Menu Item
  // CE: Component Extension Menu Item
  // CEO: Component Extenison with Options Menu Item
  // RE: Regular Extension Menu Item
  // REO: Regular Extension with Options Menu Item
  EXPECT_FALSE(IsCommandEnabled(context_menu, 0));  // P1 - CE
  EXPECT_TRUE(IsCommandEnabled(context_menu, 1));   // P1 - CEO
  EXPECT_TRUE(IsCommandEnabled(context_menu, 2));   // P1 - RE
  EXPECT_TRUE(IsCommandEnabled(context_menu, 3));   // P1 - REO
  EXPECT_TRUE(IsCommandEnabled(context_menu, 4));   // P1
  EXPECT_FALSE(IsCommandEnabled(context_menu, 5));  // P2 - CE
  EXPECT_TRUE(IsCommandEnabled(context_menu, 6));   // P2 - RE
  EXPECT_TRUE(IsCommandEnabled(context_menu, 7));   // P2 - REO
  EXPECT_TRUE(IsCommandEnabled(context_menu, 8));   // P2

  manager_->context_menu_ = nullptr;
  manager_->status_icon_ = nullptr;
}

TEST_F(BackgroundModeManagerWithExtensionsTest, BalloonDisplay) {
  scoped_refptr<const extensions::Extension> bg_ext =
      extensions::ExtensionBuilder("Background Extension")
          .SetVersion("1.0")
          .SetLocation(ManifestLocation::kCommandLine)
          .AddAPIPermission("background")
          .Build();

  scoped_refptr<const extensions::Extension> upgraded_bg_ext =
      extensions::ExtensionBuilder("Background Extension")
          .SetVersion("2.0")
          .SetLocation(ManifestLocation::kCommandLine)
          .AddAPIPermission("background")
          .Build();

  scoped_refptr<const extensions::Extension> no_bg_ext =
      extensions::ExtensionBuilder("Regular Extension")
          .SetVersion("1.0")
          .SetLocation(ManifestLocation::kCommandLine)
          .Build();

  scoped_refptr<const extensions::Extension> upgraded_no_bg_ext_has_bg =
      extensions::ExtensionBuilder("Regular Extension")
          .SetVersion("1.0")
          .SetLocation(ManifestLocation::kCommandLine)
          .AddAPIPermission("background")
          .Build();

  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile_))
      ->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
                               base::FilePath(), false);

  extensions::ExtensionSystem* system =
      extensions::ExtensionSystem::Get(profile_);
  ASSERT_FALSE(system->is_ready());
  extensions::ExtensionService* service = system->extension_service();
  service->Init();
  base::RunLoop run_loop;
  system->ready().Post(FROM_HERE, run_loop.QuitClosure());
  run_loop.Run();
  extensions::ExtensionRegistrar* registrar =
      extensions::ExtensionRegistrar::Get(profile_);

  ASSERT_TRUE(system->is_ready());
  std::unique_ptr<StatusIcon> test_status_icon =
      std::make_unique<TestStatusIcon>();
  manager_->status_icon_ = test_status_icon.get();
  manager_->UpdateStatusTrayIconContextMenu();

  // Adding a background extension should show the balloon.
  EXPECT_FALSE(manager_->HasShownBalloon());
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  registrar->AddExtension(bg_ext.get());
  Mock::VerifyAndClearExpectations(launch_manager);
  EXPECT_TRUE(manager_->HasShownBalloon());

  // Adding an extension without background should not show the balloon.
  manager_->SetHasShownBalloon(false);
  registrar->AddExtension(no_bg_ext.get());
  EXPECT_FALSE(manager_->HasShownBalloon());

  // Upgrading an extension that has background should not reshow the balloon.
  {
    // TODO(crbug.com/41145854): Fix crbug.com/438376 and remove these checks.
    InSequence expected_call_sequence;
    EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false))
        .Times(Exactly(1));
    EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(Exactly(1));
  }
  registrar->AddExtension(upgraded_bg_ext.get());
  Mock::VerifyAndClearExpectations(launch_manager);
  EXPECT_FALSE(manager_->HasShownBalloon());

  // Upgrading an extension that didn't have background to one that does should
  // show the balloon.
  registrar->AddExtension(upgraded_no_bg_ext_has_bg.get());
  EXPECT_TRUE(manager_->HasShownBalloon());

  manager_->context_menu_ = nullptr;
  manager_->status_icon_ = nullptr;
}

TEST_F(BackgroundModeManagerTest, TransientBackgroundApp) {
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  ProfileAttributesEntry* entry =
      profile_manager_->profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);
  EXPECT_FALSE(entry->GetBackgroundStatus());

  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());

  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);

  // Mimic transient app launch.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(_)).Times(0);
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.SetPersistentBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
  EXPECT_FALSE(entry->GetBackgroundStatus());

  manager.SuspendBackgroundMode();
  AssertBackgroundModeInactive(manager);
  EXPECT_FALSE(entry->GetBackgroundStatus());
  manager.ResumeBackgroundMode();

  // Mimic transient app shutdown.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(_)).Times(0);
  manager.SetBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeInactive(manager);
  EXPECT_FALSE(entry->GetBackgroundStatus());
}

TEST_F(BackgroundModeManagerTest, TransientBackgroundAppWithPersistent) {
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  ProfileAttributesEntry* entry =
      profile_manager_->profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);
  EXPECT_FALSE(entry->GetBackgroundStatus());

  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());

  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(1);
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
  EXPECT_TRUE(entry->GetBackgroundStatus());

  // Mimic transient app launch.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(_)).Times(0);
  manager.SetBackgroundClientCountForProfile(profile_, 2);
  manager.SetPersistentBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
  EXPECT_TRUE(entry->GetBackgroundStatus());

  manager.SuspendBackgroundMode();
  AssertBackgroundModeInactive(manager);
  EXPECT_TRUE(entry->GetBackgroundStatus());
  manager.ResumeBackgroundMode();

  // Mimic transient app shutdown.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(_)).Times(0);
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
  EXPECT_TRUE(entry->GetBackgroundStatus());
}

TEST_F(BackgroundModeManagerTest,
       BackgroundPersistentAppWhileTransientRunning) {
  AdvancedTestBackgroundModeManager manager(
      *command_line_, profile_manager_->profile_attributes_storage(), true);
  manager.RegisterProfile(profile_);
  ProfileAttributesEntry* entry =
      profile_manager_->profile_attributes_storage()
          ->GetProfileAttributesWithPath(profile_->GetPath());
  ASSERT_NE(entry, nullptr);
  EXPECT_FALSE(entry->GetBackgroundStatus());

  EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsKeepingAlive());

  // Mimic transient app launch.
  TestStartupLaunchManager* const launch_manager = startup_launch_manager();
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.SetPersistentBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
  EXPECT_FALSE(entry->GetBackgroundStatus());

  // Mimic persistent app install.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(true)).Times(1);
  manager.SetBackgroundClientCountForProfile(profile_, 2);
  manager.SetPersistentBackgroundClientCountForProfile(profile_, 1);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
  EXPECT_TRUE(entry->GetBackgroundStatus());

  manager.SuspendBackgroundMode();
  AssertBackgroundModeInactive(manager);
  EXPECT_TRUE(entry->GetBackgroundStatus());
  manager.ResumeBackgroundMode();

  // Mimic persistent app uninstall.
  EXPECT_CALL(*launch_manager, UpdateLaunchOnStartup(false)).Times(Exactly(1));
  manager.SetBackgroundClientCountForProfile(profile_, 1);
  manager.SetPersistentBackgroundClientCountForProfile(profile_, 0);
  manager.OnApplicationListChanged(profile_);
  Mock::VerifyAndClearExpectations(launch_manager);
  AssertBackgroundModeActive(manager);
  EXPECT_FALSE(entry->GetBackgroundStatus());
}

TEST_F(BackgroundModeManagerTest, ForceInstalledExtensionsKeepAlive) {
  const auto* keep_alive_registry = KeepAliveRegistry::GetInstance();
  EXPECT_FALSE(keep_alive_registry->IsKeepingAlive());

  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  command_line.AppendSwitch(switches::kNoStartupWindow);
  TestBackgroundModeManager manager(
      command_line, profile_manager_->profile_attributes_storage());

  EXPECT_TRUE(keep_alive_registry->IsKeepingAlive());
  EXPECT_TRUE(keep_alive_registry->WouldRestartWithout({
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP,
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_FORCE_INSTALLED_EXTENSIONS,
  }));

  manager.RegisterProfile(profile_);
  EXPECT_TRUE(keep_alive_registry->IsKeepingAlive());
  EXPECT_TRUE(keep_alive_registry->WouldRestartWithout({
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP,
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_FORCE_INSTALLED_EXTENSIONS,
  }));

  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile_))
      ->CreateExtensionService(&command_line, base::FilePath(), false);
  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile_))
      ->SetReady();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(keep_alive_registry->IsKeepingAlive());
  EXPECT_TRUE(keep_alive_registry->WouldRestartWithout({
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_FORCE_INSTALLED_EXTENSIONS,
  }));

  manager.GetBackgroundModeData(profile_)->OnForceInstalledExtensionsReady();
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(keep_alive_registry->IsKeepingAlive());
}

TEST_F(BackgroundModeManagerTest,
       ForceInstalledExtensionsKeepAliveReleasedOnAppTerminating) {
  const auto* keep_alive_registry = KeepAliveRegistry::GetInstance();
  EXPECT_FALSE(keep_alive_registry->IsKeepingAlive());

  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  command_line.AppendSwitch(switches::kNoStartupWindow);
  TestBackgroundModeManager manager(
      command_line, profile_manager_->profile_attributes_storage());

  manager.RegisterProfile(profile_);
  EXPECT_TRUE(keep_alive_registry->IsKeepingAlive());
  EXPECT_TRUE(keep_alive_registry->WouldRestartWithout({
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP,
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_FORCE_INSTALLED_EXTENSIONS,
  }));

  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile_))
      ->CreateExtensionService(&command_line, base::FilePath(), false);
  static_cast<extensions::TestExtensionSystem*>(
      extensions::ExtensionSystem::Get(profile_))
      ->SetReady();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(keep_alive_registry->IsKeepingAlive());
  EXPECT_TRUE(keep_alive_registry->WouldRestartWithout({
      KeepAliveOrigin::BACKGROUND_MODE_MANAGER_FORCE_INSTALLED_EXTENSIONS,
  }));

  manager.OnAppTerminating();
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(keep_alive_registry->IsKeepingAlive());
}