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