| 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/profiles/profile.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 extensions { |
| 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 class MockExtensionWarningSet : public ExtensionWarningSet { | 18 class TestExtensionWarningSet : public ExtensionWarningSet { |
| 17 public: | 19 public: |
| 18 explicit MockExtensionWarningSet(Profile* profile) | 20 explicit TestExtensionWarningSet(Profile* profile) |
| 19 : ExtensionWarningSet(profile) { | 21 : ExtensionWarningSet(profile) { |
| 20 } | 22 } |
| 21 virtual ~MockExtensionWarningSet() {} | 23 virtual ~TestExtensionWarningSet() {} |
| 22 | 24 |
| 23 MOCK_METHOD0(NotifyWarningsChanged, void()); | 25 void AddWarning(const ExtensionWarning& warning) { |
| 26 std::set<ExtensionWarning> warnings; |
| 27 warnings.insert(warning); |
| 28 AddWarnings(warnings); |
| 29 } |
| 24 }; | 30 }; |
| 25 | 31 |
| 26 bool HasBadge(Profile* profile) { | 32 class MockObserver : public ExtensionWarningSet::Observer { |
| 27 GlobalErrorService* service = | 33 public: |
| 28 GlobalErrorServiceFactory::GetForProfile(profile); | 34 virtual ~MockObserver() {} |
| 29 return service->GetGlobalErrorByMenuItemCommandID( | 35 MOCK_METHOD0(ExtensionWarningsChanged, void()); |
| 30 ExtensionGlobalErrorBadge::GetMenuItemCommandID()) != NULL; | 36 }; |
| 31 } | |
| 32 | 37 |
| 33 const char* ext1_id = "extension1"; | 38 const char* ext1_id = "extension1"; |
| 34 const char* ext2_id = "extension2"; | 39 const char* ext2_id = "extension2"; |
| 35 const ExtensionWarningSet::WarningType warning_1 = | 40 const ExtensionWarning::WarningType warning_1 = |
| 36 ExtensionWarningSet::kNetworkDelay; | 41 ExtensionWarning::kNetworkDelay; |
| 37 const ExtensionWarningSet::WarningType warning_2 = | 42 const ExtensionWarning::WarningType warning_2 = |
| 38 ExtensionWarningSet::kNetworkConflict; | 43 ExtensionWarning::kNetworkConflict; |
| 39 | 44 |
| 40 } // namespace | 45 } // namespace |
| 41 | 46 |
| 42 // Check that inserting a warning triggers notifications, whereas inserting | 47 // Check that inserting a warning triggers notifications, whereas inserting |
| 43 // the same warning again is silent. | 48 // the same warning again is silent. |
| 44 TEST(ExtensionWarningSetTest, SetWarning) { | 49 TEST(ExtensionWarningSetTest, SetWarning) { |
| 45 TestingProfile profile; | 50 TestingProfile profile; |
| 46 MockExtensionWarningSet warnings(&profile); | 51 TestExtensionWarningSet warnings(&profile); |
| 52 MockObserver observer; |
| 53 warnings.AddObserver(&observer); |
| 47 | 54 |
| 48 // Insert warning for the first time. | 55 // Insert warning for the first time. |
| 49 EXPECT_CALL(warnings, NotifyWarningsChanged()); | 56 EXPECT_CALL(observer, ExtensionWarningsChanged()); |
| 50 warnings.SetWarning(warning_1, ext1_id); | 57 warnings.AddWarning(ExtensionWarning::CreateNetworkDelayWarning(ext1_id)); |
| 51 testing::Mock::VerifyAndClearExpectations(&warnings); | 58 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 52 EXPECT_TRUE(HasBadge(&profile)); | 59 //EXPECT_TRUE(HasBadge(&profile)); |
| 53 | 60 |
| 54 // Second insertion of same warning does not trigger anything. | 61 // Second insertion of same warning does not trigger anything. |
| 55 warnings.SetWarning(warning_1, ext1_id); | 62 warnings.AddWarning(ExtensionWarning::CreateNetworkDelayWarning(ext1_id)); |
| 56 testing::Mock::VerifyAndClearExpectations(&warnings); | 63 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 64 |
| 65 warnings.RemoveObserver(&observer); |
| 57 } | 66 } |
| 58 | 67 |
| 59 // Check that ClearWarnings deletes exactly the specified warnings and | 68 // Check that ClearWarnings deletes exactly the specified warnings and |
| 60 // triggers notifications where appropriate. | 69 // triggers notifications where appropriate. |
| 61 TEST(ExtensionWarningSetTest, ClearWarnings) { | 70 TEST(ExtensionWarningSetTest, ClearWarnings) { |
| 62 TestingProfile profile; | 71 TestingProfile profile; |
| 63 MockExtensionWarningSet warnings(&profile); | 72 TestExtensionWarningSet warnings(&profile); |
| 73 MockObserver observer; |
| 74 warnings.AddObserver(&observer); |
| 64 | 75 |
| 65 // Insert two unique warnings. | 76 // Insert two unique warnings in one batch. |
| 66 EXPECT_CALL(warnings, NotifyWarningsChanged()).Times(2); | 77 EXPECT_CALL(observer, ExtensionWarningsChanged()).Times(1); |
| 67 warnings.SetWarning(warning_1, ext1_id); | 78 std::set<ExtensionWarning> warning_set; |
| 68 warnings.SetWarning(warning_2, ext2_id); | 79 warning_set.insert(ExtensionWarning::CreateNetworkDelayWarning(ext1_id)); |
| 80 warning_set.insert(ExtensionWarning::CreateNetworkConflictWarning(ext2_id)); |
| 81 warnings.AddWarnings(warning_set); |
| 69 testing::Mock::VerifyAndClearExpectations(&warnings); | 82 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 70 EXPECT_TRUE(HasBadge(&profile)); | |
| 71 | 83 |
| 72 // Remove one warning and check that the badge remains. | 84 // Remove one warning and check that the badge remains. |
| 73 EXPECT_CALL(warnings, NotifyWarningsChanged()); | 85 EXPECT_CALL(observer, ExtensionWarningsChanged()); |
| 74 std::set<ExtensionWarningSet::WarningType> to_clear; | 86 std::set<ExtensionWarning::WarningType> to_clear; |
| 75 to_clear.insert(warning_2); | 87 to_clear.insert(warning_2); |
| 76 warnings.ClearWarnings(to_clear); | 88 warnings.ClearWarnings(to_clear); |
| 77 testing::Mock::VerifyAndClearExpectations(&warnings); | 89 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 78 EXPECT_TRUE(HasBadge(&profile)); | |
| 79 | 90 |
| 80 // Check that the correct warnings appear in |warnings|. | 91 // Check that the correct warnings appear in |warnings|. |
| 81 std::set<ExtensionWarningSet::WarningType> existing_warnings; | 92 std::set<ExtensionWarning::WarningType> existing_warnings; |
| 82 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); | 93 warnings.GetWarningTypesAffectingExtension(ext1_id, &existing_warnings); |
| 83 EXPECT_EQ(1u, existing_warnings.size()); | 94 EXPECT_EQ(1u, existing_warnings.size()); |
| 84 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); | 95 warnings.GetWarningTypesAffectingExtension(ext2_id, &existing_warnings); |
| 85 EXPECT_EQ(0u, existing_warnings.size()); | 96 EXPECT_EQ(0u, existing_warnings.size()); |
| 86 | 97 |
| 87 // Remove the other one warning and check that badge disappears. | 98 // Remove the other one warning. |
| 88 EXPECT_CALL(warnings, NotifyWarningsChanged()); | 99 EXPECT_CALL(observer, ExtensionWarningsChanged()); |
| 89 to_clear.insert(warning_1); | 100 to_clear.insert(warning_1); |
| 90 warnings.ClearWarnings(to_clear); | 101 warnings.ClearWarnings(to_clear); |
| 91 testing::Mock::VerifyAndClearExpectations(&warnings); | 102 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 92 EXPECT_FALSE(HasBadge(&profile)); | |
| 93 | 103 |
| 94 // Check that not warnings remain. | 104 // Check that not warnings remain. |
| 95 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); | 105 warnings.GetWarningTypesAffectingExtension(ext1_id, &existing_warnings); |
| 96 EXPECT_EQ(0u, existing_warnings.size()); | 106 EXPECT_EQ(0u, existing_warnings.size()); |
| 97 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); | 107 warnings.GetWarningTypesAffectingExtension(ext2_id, &existing_warnings); |
| 98 EXPECT_EQ(0u, existing_warnings.size()); | 108 EXPECT_EQ(0u, existing_warnings.size()); |
| 109 |
| 110 warnings.RemoveObserver(&observer); |
| 99 } | 111 } |
| 100 | 112 |
| 101 // Check that no badge appears if it has been suppressed for a specific | 113 } // namespace extensions |
| 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 |