Chromium Code Reviews| 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_ = reinterpret_cast<ExtensionGlobalError*>(1); | |
| 32 } | |
| 33 void DeactivateBadgeImpl() { | |
| 34 extension_global_error_ = NULL; | |
| 35 } | |
| 36 }; | |
| 37 | |
| 38 const char* ext1_id = "extension1"; | |
| 39 const char* ext2_id = "extension2"; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 // Check that inserting a warning triggers notifications, whereas inserting | |
| 44 // the same warning again is silent. | |
| 45 TEST(ExtensionWarningSet, SetWarning) { | |
| 46 MockExtensionWarningSet warnings; | |
| 47 ExtensionWarning warning(ExtensionWarning::kNetworkDelay, ext1_id); | |
| 48 | |
| 49 // Insert warning for the first time. | |
| 50 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 51 EXPECT_CALL(warnings, ActivateBadge()); | |
| 52 warnings.SetWarning(warning); | |
| 53 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 54 | |
| 55 // Second insertion of same warning does not trigger anything. | |
| 56 warnings.SetWarning(warning); | |
| 57 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 58 } | |
| 59 | |
| 60 // Check that ClearWarnings deletes exaclty the specified warnings and | |
|
Matt Perry
2011/10/07 19:18:38
typo: exactly
battre
2011/10/10 13:16:36
Done.
| |
| 61 // triggers notifications where appropriate. | |
| 62 TEST(ExtensionWarningSet, ClearWarnings) { | |
| 63 MockExtensionWarningSet warnings; | |
| 64 ExtensionWarning warning1(ExtensionWarning::kNetworkDelay, ext1_id); | |
| 65 // TODO(battre): Replace kInvalid with something else once we have additional | |
| 66 // warning types. | |
| 67 ExtensionWarning warning2(ExtensionWarning::kInvalid, ext2_id); | |
| 68 | |
| 69 // Insert two unique warnings. | |
| 70 EXPECT_CALL(warnings, NotifyWarningsChanged()).Times(2); | |
| 71 EXPECT_CALL(warnings, ActivateBadge()).Times(1); | |
| 72 warnings.SetWarning(warning1); | |
| 73 warnings.SetWarning(warning2); | |
| 74 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 75 | |
| 76 // Remove the one warning and check that the badge remains. | |
| 77 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 78 std::set<ExtensionWarning::WarningType> to_clear; | |
| 79 to_clear.insert(ExtensionWarning::kInvalid); | |
| 80 warnings.ClearWarnings(to_clear); | |
| 81 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 82 | |
| 83 // Check that the correct warnings appear in |warnings|. | |
| 84 std::set<ExtensionWarning::WarningType> existing_warnings; | |
| 85 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); | |
| 86 EXPECT_EQ(1u, existing_warnings.size()); | |
| 87 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); | |
| 88 EXPECT_EQ(0u, existing_warnings.size()); | |
| 89 | |
| 90 // Remove the other one warning and check that badge disappears. | |
| 91 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 92 EXPECT_CALL(warnings, DeactivateBadge()); | |
| 93 to_clear.insert(ExtensionWarning::kNetworkDelay); | |
| 94 warnings.ClearWarnings(to_clear); | |
| 95 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 96 | |
| 97 // Check that not warnings remain. | |
| 98 warnings.GetWarningsAffectingExtension(ext1_id, &existing_warnings); | |
| 99 EXPECT_EQ(0u, existing_warnings.size()); | |
| 100 warnings.GetWarningsAffectingExtension(ext2_id, &existing_warnings); | |
| 101 EXPECT_EQ(0u, existing_warnings.size()); | |
| 102 } | |
| 103 | |
| 104 // Check that no badge appears if it has been suppressed for a specific | |
| 105 // warning. | |
| 106 TEST(ExtensionWarningSet, SuppressBadgeForCurrentWarnings) { | |
| 107 MockExtensionWarningSet warnings; | |
| 108 ExtensionWarning warning1(ExtensionWarning::kNetworkDelay, ext1_id); | |
| 109 // TODO(battre): Replace kInvalid with something else once we have additional | |
| 110 // warning types. | |
| 111 ExtensionWarning warning2(ExtensionWarning::kInvalid, ext2_id); | |
| 112 | |
| 113 // Insert first warning. | |
| 114 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 115 EXPECT_CALL(warnings, ActivateBadge()); | |
| 116 warnings.SetWarning(warning1); | |
| 117 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 118 | |
| 119 // Suppress first warning. | |
| 120 EXPECT_CALL(warnings, DeactivateBadge()); | |
| 121 warnings.SuppressBadgeForCurrentWarnings(); | |
| 122 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 123 | |
| 124 // Simulate deinstallation of extension. | |
| 125 std::set<ExtensionWarning::WarningType> to_clear; | |
| 126 warnings.GetWarningsAffectingExtension(ext1_id, &to_clear); | |
| 127 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 128 warnings.ClearWarnings(to_clear); | |
| 129 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 130 | |
| 131 // Set first warning again and verify that not badge is shown this time. | |
| 132 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 133 warnings.SetWarning(warning1); | |
| 134 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 135 | |
| 136 // Set second warning and verify that it shows a badge. | |
| 137 EXPECT_CALL(warnings, NotifyWarningsChanged()); | |
| 138 EXPECT_CALL(warnings, ActivateBadge()); | |
| 139 warnings.SetWarning(warning2); | |
| 140 testing::Mock::VerifyAndClearExpectations(&warnings); | |
| 141 } | |
| OLD | NEW |