| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_warning_badge_service.h" | 5 #include "chrome/browser/extensions/warning_badge_service.h" |
| 6 | 6 |
| 7 #include "chrome/app/chrome_command_ids.h" | 7 #include "chrome/app/chrome_command_ids.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/global_error/global_error_service.h" | 9 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 10 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 10 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "extensions/browser/warning_service.h" | 12 #include "extensions/browser/warning_service.h" |
| 13 #include "extensions/browser/warning_set.h" | 13 #include "extensions/browser/warning_set.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class TestExtensionWarningSet : public WarningService { | 20 class TestExtensionWarningSet : public WarningService { |
| 21 public: | 21 public: |
| 22 explicit TestExtensionWarningSet(Profile* profile) : WarningService(profile) { | 22 explicit TestExtensionWarningSet(Profile* profile) |
| 23 } | 23 : WarningService(profile) {} |
| 24 ~TestExtensionWarningSet() override {} | 24 ~TestExtensionWarningSet() override {} |
| 25 | 25 |
| 26 void AddWarning(const Warning& warning) { | 26 void AddWarning(const Warning& warning) { |
| 27 WarningSet warnings; | 27 WarningSet warnings; |
| 28 warnings.insert(warning); | 28 warnings.insert(warning); |
| 29 AddWarnings(warnings); | 29 AddWarnings(warnings); |
| 30 } | 30 } |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 class TestExtensionWarningBadgeService : public ExtensionWarningBadgeService { | 33 class TestWarningBadgeService : public WarningBadgeService { |
| 34 public: | 34 public: |
| 35 TestExtensionWarningBadgeService(Profile* profile, | 35 TestWarningBadgeService(Profile* profile, WarningService* warning_service) |
| 36 WarningService* warning_service) | 36 : WarningBadgeService(profile), warning_service_(warning_service) {} |
| 37 : ExtensionWarningBadgeService(profile), | 37 ~TestWarningBadgeService() override {} |
| 38 warning_service_(warning_service) {} | |
| 39 ~TestExtensionWarningBadgeService() override {} | |
| 40 | 38 |
| 41 const std::set<Warning>& GetCurrentWarnings() const override { | 39 const std::set<Warning>& GetCurrentWarnings() const override { |
| 42 return warning_service_->warnings(); | 40 return warning_service_->warnings(); |
| 43 } | 41 } |
| 44 | 42 |
| 45 private: | 43 private: |
| 46 WarningService* warning_service_; | 44 WarningService* warning_service_; |
| 47 }; | 45 }; |
| 48 | 46 |
| 49 bool HasBadge(Profile* profile) { | 47 bool HasBadge(Profile* profile) { |
| 50 GlobalErrorService* service = | 48 GlobalErrorService* service = |
| 51 GlobalErrorServiceFactory::GetForProfile(profile); | 49 GlobalErrorServiceFactory::GetForProfile(profile); |
| 52 return service->GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_ERRORS) != | 50 return service->GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_ERRORS) != |
| 53 NULL; | 51 NULL; |
| 54 } | 52 } |
| 55 | 53 |
| 56 const char ext1_id[] = "extension1"; | 54 const char ext1_id[] = "extension1"; |
| 57 const char ext2_id[] = "extension2"; | 55 const char ext2_id[] = "extension2"; |
| 58 | 56 |
| 59 } // namespace | 57 } // namespace |
| 60 | 58 |
| 61 // Check that no badge appears if it has been suppressed for a specific | 59 // Check that no badge appears if it has been suppressed for a specific |
| 62 // warning. | 60 // warning. |
| 63 TEST(ExtensionWarningBadgeServiceTest, SuppressBadgeForCurrentWarnings) { | 61 TEST(WarningBadgeServiceTest, SuppressBadgeForCurrentWarnings) { |
| 64 TestingProfile profile; | 62 TestingProfile profile; |
| 65 TestExtensionWarningSet warnings(&profile); | 63 TestExtensionWarningSet warnings(&profile); |
| 66 TestExtensionWarningBadgeService badge_service(&profile, &warnings); | 64 TestWarningBadgeService badge_service(&profile, &warnings); |
| 67 warnings.AddObserver(&badge_service); | 65 warnings.AddObserver(&badge_service); |
| 68 | 66 |
| 69 // Insert first warning. | 67 // Insert first warning. |
| 70 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id)); | 68 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id)); |
| 71 EXPECT_TRUE(HasBadge(&profile)); | 69 EXPECT_TRUE(HasBadge(&profile)); |
| 72 | 70 |
| 73 // Suppress first warning. | 71 // Suppress first warning. |
| 74 badge_service.SuppressCurrentWarnings(); | 72 badge_service.SuppressCurrentWarnings(); |
| 75 EXPECT_FALSE(HasBadge(&profile)); | 73 EXPECT_FALSE(HasBadge(&profile)); |
| 76 | 74 |
| 77 // Simulate deinstallation of extension. | 75 // Simulate deinstallation of extension. |
| 78 std::set<Warning::WarningType> to_clear = | 76 std::set<Warning::WarningType> to_clear = |
| 79 warnings.GetWarningTypesAffectingExtension(ext1_id); | 77 warnings.GetWarningTypesAffectingExtension(ext1_id); |
| 80 warnings.ClearWarnings(to_clear); | 78 warnings.ClearWarnings(to_clear); |
| 81 EXPECT_FALSE(HasBadge(&profile)); | 79 EXPECT_FALSE(HasBadge(&profile)); |
| 82 | 80 |
| 83 // Set first warning again and verify that not badge is shown this time. | 81 // Set first warning again and verify that not badge is shown this time. |
| 84 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id)); | 82 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id)); |
| 85 EXPECT_FALSE(HasBadge(&profile)); | 83 EXPECT_FALSE(HasBadge(&profile)); |
| 86 | 84 |
| 87 // Set second warning and verify that it shows a badge. | 85 // Set second warning and verify that it shows a badge. |
| 88 warnings.AddWarning(Warning::CreateNetworkConflictWarning(ext2_id)); | 86 warnings.AddWarning(Warning::CreateNetworkConflictWarning(ext2_id)); |
| 89 EXPECT_TRUE(HasBadge(&profile)); | 87 EXPECT_TRUE(HasBadge(&profile)); |
| 90 | 88 |
| 91 warnings.RemoveObserver(&badge_service); | 89 warnings.RemoveObserver(&badge_service); |
| 92 } | 90 } |
| 93 | 91 |
| 94 } // namespace extensions | 92 } // namespace extensions |
| OLD | NEW |