| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/extension_warning_set.h" | |
| 6 | |
| 7 #include "chrome/browser/extensions/extension_global_error_badge.h" | |
| 8 #include "chrome/browser/ui/global_error/global_error_service.h" | |
| 9 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | |
| 10 #include "chrome/test/base/testing_profile.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 class MockExtensionWarningSet : public ExtensionWarningSet { | |
| 17 public: | |
| 18 explicit MockExtensionWarningSet(Profile* profile) | |
| 19 : ExtensionWarningSet(profile) { | |
| 20 } | |
| 21 virtual ~MockExtensionWarningSet() {} | |
| 22 | |
| 23 MOCK_METHOD0(NotifyWarningsChanged, void()); | |
| 24 }; | |
| 25 | |
| 26 bool HasBadge(Profile* profile) { | |
| 27 GlobalErrorService* service = | |
| 28 GlobalErrorServiceFactory::GetForProfile(profile); | |
| 29 return service->GetGlobalErrorByMenuItemCommandID( | |
| 30 ExtensionGlobalErrorBadge::GetMenuItemCommandID()) != NULL; | |
| 31 } | |
| 32 | |
| 33 const char* ext1_id = "extension1"; | |
| 34 const char* ext2_id = "extension2"; | |
| 35 const ExtensionWarningSet::WarningType warning_1 = | |
| 36 ExtensionWarningSet::kNetworkDelay; | |
| 37 const ExtensionWarningSet::WarningType warning_2 = | |
| 38 ExtensionWarningSet::kNetworkConflict; | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 // Check that inserting a warning triggers notifications, whereas inserting | |
| 43 // the same warning again is silent. | |
| 44 TEST(ExtensionWarningSetTest, SetWarning) { | |
| 45 TestingProfile profile; | |
| 46 MockExtensionWarningSet warnings(&profile); | |
| 47 | |
| 48 // Insert warning for the first time. | |
| 49 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 50 warnings.SetWarning(warning_1, ext1_id); | |
| 51 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 52 EXPECT_TRUE(HasBadge(&profile)); | |
| 53 | |
| 54 // Second insertion of same warning does not trigger anything. | |
| 55 warnings.SetWarning(warning_1, ext1_id); | |
| 56 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 57 } | |
| 58 | |
| 59 // Check that ClearWarnings deletes exactly the specified warnings and | |
| 60 // triggers notifications where appropriate. | |
| 61 TEST(ExtensionWarningSetTest, ClearWarnings) { | |
| 62 TestingProfile profile; | |
| 63 MockExtensionWarningSet warnings(&profile); | |
| 64 | |
| 65 // Insert two unique warnings. | |
| 66 EXPECT_CALL(warnings, NotifyWarningsChanged()).Times(2); | |
| 67 warnings.SetWarning(warning_1, ext1_id); | |
| 68 warnings.SetWarning(warning_2, ext2_id); | |
| 69 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 70 EXPECT_TRUE(HasBadge(&profile)); | |
| 71 | |
| 72 // Remove one warning and check that the badge remains. | |
| 73 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 74 std::set<ExtensionWarningSet::WarningType> to_clear; | |
| 75 to_clear.insert(warning_2); | |
| 76 warnings.ClearWarnings(to_clear); | |
| 77 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 78 EXPECT_TRUE(HasBadge(&profile)); | |
| 79 | |
| 80 // Check that the correct warnings appear in |warnings|. | |
| 81 std::set<ExtensionWarningSet::WarningType> existing_warnings; | |
| 82 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); | |
| 83 EXPECT_EQ(1u, existing_warnings.size()); | |
| 84 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); | |
| 85 EXPECT_EQ(0u, existing_warnings.size()); | |
| 86 | |
| 87 // Remove the other one warning and check that badge disappears. | |
| 88 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 89 to_clear.insert(warning_1); | |
| 90 warnings.ClearWarnings(to_clear); | |
| 91 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 92 EXPECT_FALSE(HasBadge(&profile)); | |
| 93 | |
| 94 // Check that not warnings remain. | |
| 95 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); | |
| 96 EXPECT_EQ(0u, existing_warnings.size()); | |
| 97 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); | |
| 98 EXPECT_EQ(0u, existing_warnings.size()); | |
| 99 } | |
| 100 | |
| 101 // Check that no badge appears if it has been suppressed for a specific | |
| 102 // warning. | |
| 103 TEST(ExtensionWarningSetTest, SuppressBadgeForCurrentWarnings) { | |
| 104 TestingProfile profile; | |
| 105 MockExtensionWarningSet warnings(&profile); | |
| 106 | |
| 107 // Insert first warning. | |
| 108 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 109 warnings.SetWarning(warning_1, ext1_id); | |
| 110 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 111 EXPECT_TRUE(HasBadge(&profile)); | |
| 112 | |
| 113 // Suppress first warning. | |
| 114 warnings.SuppressBadgeForCurrentWarnings(); | |
| 115 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 116 EXPECT_FALSE(HasBadge(&profile)); | |
| 117 | |
| 118 // Simulate deinstallation of extension. | |
| 119 std::set<ExtensionWarningSet::WarningType> to_clear; | |
| 120 warnings.GetWarningsAffectingExtension(ext1_id, &to_clear); | |
| 121 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 122 warnings.ClearWarnings(to_clear); | |
| 123 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 124 | |
| 125 // Set first warning again and verify that not badge is shown this time. | |
| 126 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 127 warnings.SetWarning(warning_1, ext1_id); | |
| 128 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 129 EXPECT_FALSE(HasBadge(&profile)); | |
| 130 | |
| 131 // Set second warning and verify that it shows a badge. | |
| 132 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 133 warnings.SetWarning(warning_2, ext2_id); | |
| 134 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 135 EXPECT_TRUE(HasBadge(&profile)); | |
| 136 } | |
| OLD | NEW |