app_restore_info_unittest.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/app_restore/app_restore_info.h"
  5. #include <map>
  6. #include <set>
  7. #include "components/account_id/account_id.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace app_restore {
  10. class FakeAppRestoreInfoObserver : public AppRestoreInfo::Observer {
  11. public:
  12. void OnRestorePrefChanged(const AccountId& account_id,
  13. bool could_restore) override {
  14. if (could_restore)
  15. restore_prefs_.insert(account_id);
  16. else
  17. restore_prefs_.erase(account_id);
  18. restore_pref_changed_count_[account_id]++;
  19. }
  20. bool CanPerformRestore(const AccountId& account_id) const {
  21. return restore_prefs_.find(account_id) != restore_prefs_.end();
  22. }
  23. int RestorePrefChangedCount(const AccountId& account_id) const {
  24. auto it = restore_pref_changed_count_.find(account_id);
  25. return it != restore_pref_changed_count_.end() ? it->second : 0;
  26. }
  27. private:
  28. std::set<AccountId> restore_prefs_;
  29. std::map<AccountId, int> restore_pref_changed_count_;
  30. };
  31. using AppRestoreInfoTest = testing::Test;
  32. // Test the OnRestorePrefChanged callback when the restore pref is reset.
  33. TEST_F(AppRestoreInfoTest, RestorePref) {
  34. AccountId account_id1 = AccountId::FromUserEmail("aaa@gmail.com");
  35. AccountId account_id2 = AccountId::FromUserEmail("bbb@gmail.com");
  36. AppRestoreInfo::GetInstance()->SetRestorePref(account_id1, true);
  37. FakeAppRestoreInfoObserver observer;
  38. AppRestoreInfo::GetInstance()->AddObserver(&observer);
  39. // Not change the restore flag.
  40. AppRestoreInfo::GetInstance()->SetRestorePref(account_id1, true);
  41. AppRestoreInfo::GetInstance()->SetRestorePref(account_id2, false);
  42. EXPECT_EQ(0, observer.RestorePrefChangedCount(account_id1));
  43. EXPECT_EQ(0, observer.RestorePrefChangedCount(account_id2));
  44. EXPECT_TRUE(AppRestoreInfo::GetInstance()->CanPerformRestore(account_id1));
  45. EXPECT_FALSE(AppRestoreInfo::GetInstance()->CanPerformRestore(account_id2));
  46. // Change the restore flag.
  47. AppRestoreInfo::GetInstance()->SetRestorePref(account_id1, false);
  48. AppRestoreInfo::GetInstance()->SetRestorePref(account_id2, true);
  49. EXPECT_EQ(1, observer.RestorePrefChangedCount(account_id1));
  50. EXPECT_EQ(1, observer.RestorePrefChangedCount(account_id2));
  51. EXPECT_FALSE(observer.CanPerformRestore(account_id1));
  52. EXPECT_TRUE(observer.CanPerformRestore(account_id2));
  53. EXPECT_FALSE(AppRestoreInfo::GetInstance()->CanPerformRestore(account_id1));
  54. EXPECT_TRUE(AppRestoreInfo::GetInstance()->CanPerformRestore(account_id2));
  55. AppRestoreInfo::GetInstance()->RemoveObserver(&observer);
  56. }
  57. } // namespace app_restore