| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "testing/gmock/include/gmock/gmock.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 class ExtensionglobalError; |
| 11 |
| 12 namespace { |
| 13 |
| 14 class MockExtensionWarningSet : public ExtensionWarningSet { |
| 15 public: |
| 16 MockExtensionWarningSet() : ExtensionWarningSet(NULL) { |
| 17 ON_CALL(*this, ActivateBadge()).WillByDefault( |
| 18 testing::Invoke(this, &MockExtensionWarningSet::ActivateBadgeImpl)); |
| 19 ON_CALL(*this, DeactivateBadge()).WillByDefault( |
| 20 testing::Invoke(this, &MockExtensionWarningSet::DeactivateBadgeImpl)); |
| 21 } |
| 22 virtual ~MockExtensionWarningSet() {} |
| 23 |
| 24 MOCK_METHOD0(NotifyWarningsChanged, void()); |
| 25 MOCK_METHOD0(ActivateBadge, void()); |
| 26 MOCK_METHOD0(DeactivateBadge, void()); |
| 27 |
| 28 void ActivateBadgeImpl() { |
| 29 // Just fill the value so that we see that something would be there |
| 30 // in a non-mocked execution. |
| 31 extension_global_error_badge_ = |
| 32 reinterpret_cast<ExtensionGlobalErrorBadge*>(1); |
| 33 } |
| 34 void DeactivateBadgeImpl() { |
| 35 extension_global_error_badge_ = NULL; |
| 36 } |
| 37 }; |
| 38 |
| 39 const char* ext1_id = "extension1"; |
| 40 const char* ext2_id = "extension2"; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 // Check that inserting a warning triggers notifications, whereas inserting |
| 45 // the same warning again is silent. |
| 46 TEST(ExtensionWarningSet, SetWarning) { |
| 47 MockExtensionWarningSet warnings; |
| 48 |
| 49 // Insert warning for the first time. |
| 50 EXPECT_CALL(warnings, NotifyWarningsChanged()); |
| 51 EXPECT_CALL(warnings, ActivateBadge()); |
| 52 warnings.SetWarning(ExtensionWarningSet::kNetworkDelay, ext1_id); |
| 53 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 54 |
| 55 // Second insertion of same warning does not trigger anything. |
| 56 warnings.SetWarning(ExtensionWarningSet::kNetworkDelay, ext1_id); |
| 57 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 58 } |
| 59 |
| 60 // Check that ClearWarnings deletes exactly the specified warnings and |
| 61 // triggers notifications where appropriate. |
| 62 TEST(ExtensionWarningSet, ClearWarnings) { |
| 63 MockExtensionWarningSet warnings; |
| 64 |
| 65 // TODO(battre): Replace kInvalid with something else once we have additional |
| 66 // warning types. |
| 67 ExtensionWarningSet::WarningType type1 = ExtensionWarningSet::kNetworkDelay; |
| 68 ExtensionWarningSet::WarningType type2 = ExtensionWarningSet::kInvalid; |
| 69 |
| 70 // Insert two unique warnings. |
| 71 EXPECT_CALL(warnings, NotifyWarningsChanged()).Times(2); |
| 72 EXPECT_CALL(warnings, ActivateBadge()).Times(1); |
| 73 warnings.SetWarning(type1, ext1_id); |
| 74 warnings.SetWarning(type2, ext2_id); |
| 75 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 76 |
| 77 // Remove the one warning and check that the badge remains. |
| 78 EXPECT_CALL(warnings, NotifyWarningsChanged()); |
| 79 std::set<ExtensionWarningSet::WarningType> to_clear; |
| 80 to_clear.insert(ExtensionWarningSet::kInvalid); |
| 81 warnings.ClearWarnings(to_clear); |
| 82 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 83 |
| 84 // Check that the correct warnings appear in |warnings|. |
| 85 std::set<ExtensionWarningSet::WarningType> existing_warnings; |
| 86 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); |
| 87 EXPECT_EQ(1u, existing_warnings.size()); |
| 88 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); |
| 89 EXPECT_EQ(0u, existing_warnings.size()); |
| 90 |
| 91 // Remove the other one warning and check that badge disappears. |
| 92 EXPECT_CALL(warnings, NotifyWarningsChanged()); |
| 93 EXPECT_CALL(warnings, DeactivateBadge()); |
| 94 to_clear.insert(ExtensionWarningSet::kNetworkDelay); |
| 95 warnings.ClearWarnings(to_clear); |
| 96 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 97 |
| 98 // Check that not warnings remain. |
| 99 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); |
| 100 EXPECT_EQ(0u, existing_warnings.size()); |
| 101 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); |
| 102 EXPECT_EQ(0u, existing_warnings.size()); |
| 103 } |
| 104 |
| 105 // Check that no badge appears if it has been suppressed for a specific |
| 106 // warning. |
| 107 TEST(ExtensionWarningSet, SuppressBadgeForCurrentWarnings) { |
| 108 MockExtensionWarningSet warnings; |
| 109 // TODO(battre): Replace kInvalid with something else once we have additional |
| 110 // warning types. |
| 111 ExtensionWarningSet::WarningType type1 = ExtensionWarningSet::kNetworkDelay; |
| 112 ExtensionWarningSet::WarningType type2 = ExtensionWarningSet::kInvalid; |
| 113 |
| 114 // Insert first warning. |
| 115 EXPECT_CALL(warnings, NotifyWarningsChanged()); |
| 116 EXPECT_CALL(warnings, ActivateBadge()); |
| 117 warnings.SetWarning(type1, ext1_id); |
| 118 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 119 |
| 120 // Suppress first warning. |
| 121 EXPECT_CALL(warnings, DeactivateBadge()); |
| 122 warnings.SuppressBadgeForCurrentWarnings(); |
| 123 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 124 |
| 125 // Simulate deinstallation of extension. |
| 126 std::set<ExtensionWarningSet::WarningType> to_clear; |
| 127 warnings.GetWarningsAffectingExtension(ext1_id, &to_clear); |
| 128 EXPECT_CALL(warnings, NotifyWarningsChanged()); |
| 129 warnings.ClearWarnings(to_clear); |
| 130 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 131 |
| 132 // Set first warning again and verify that not badge is shown this time. |
| 133 EXPECT_CALL(warnings, NotifyWarningsChanged()); |
| 134 warnings.SetWarning(type1, ext1_id); |
| 135 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 136 |
| 137 // Set second warning and verify that it shows a badge. |
| 138 EXPECT_CALL(warnings, NotifyWarningsChanged()); |
| 139 EXPECT_CALL(warnings, ActivateBadge()); |
| 140 warnings.SetWarning(type2, ext2_id); |
| 141 testing::Mock::VerifyAndClearExpectations(&warnings); |
| 142 } |
| OLD | NEW |