[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 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 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 5 | #include "extensions/browser/warning_service.h" |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 6 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 7 | #include "content/public/test/test_browser_context.h" |
| 8 | #include "extensions/browser/extensions_test.h" |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 9 | #include "testing/gmock/include/gmock/gmock.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace extensions { |
| 13 | |
| 14 | namespace { |
| 15 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 16 | class TestWarningService : public WarningService { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 17 | public: |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 18 | explicit TestWarningService(content::BrowserContext* browser_context) |
| 19 | : WarningService(browser_context) { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 20 | } |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame^] | 21 | ~TestWarningService() override {} |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 22 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 23 | void AddWarning(const Warning& warning) { |
| 24 | WarningSet warnings; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 25 | warnings.insert(warning); |
| 26 | AddWarnings(warnings); |
| 27 | } |
| 28 | }; |
| 29 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 30 | class MockObserver : public WarningService::Observer { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 31 | public: |
| 32 | virtual ~MockObserver() {} |
| 33 | MOCK_METHOD0(ExtensionWarningsChanged, void()); |
| 34 | }; |
| 35 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 36 | typedef ExtensionsTest WarningServiceTest; |
| 37 | |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 38 | const char* ext1_id = "extension1"; |
| 39 | const char* ext2_id = "extension2"; |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 40 | const Warning::WarningType warning_1 = |
| 41 | Warning::kNetworkDelay; |
| 42 | const Warning::WarningType warning_2 = |
| 43 | Warning::kNetworkConflict; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | // Check that inserting a warning triggers notifications, whereas inserting |
| 48 | // the same warning again is silent. |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 49 | TEST_F(WarningServiceTest, SetWarning) { |
| 50 | content::TestBrowserContext browser_context; |
| 51 | TestWarningService warning_service(&browser_context); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 52 | MockObserver observer; |
| 53 | warning_service.AddObserver(&observer); |
| 54 | |
| 55 | // Insert warning for the first time. |
| 56 | EXPECT_CALL(observer, ExtensionWarningsChanged()); |
| 57 | warning_service.AddWarning( |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 58 | Warning::CreateNetworkDelayWarning(ext1_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 59 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 60 | |
| 61 | // Second insertion of same warning does not trigger anything. |
| 62 | warning_service.AddWarning( |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 63 | Warning::CreateNetworkDelayWarning(ext1_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 64 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 65 | |
| 66 | warning_service.RemoveObserver(&observer); |
| 67 | } |
| 68 | |
| 69 | // Check that ClearWarnings deletes exactly the specified warnings and |
| 70 | // triggers notifications where appropriate. |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 71 | TEST_F(WarningServiceTest, ClearWarnings) { |
| 72 | content::TestBrowserContext browser_context; |
| 73 | TestWarningService warning_service(&browser_context); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 74 | MockObserver observer; |
| 75 | warning_service.AddObserver(&observer); |
| 76 | |
| 77 | // Insert two unique warnings in one batch. |
| 78 | EXPECT_CALL(observer, ExtensionWarningsChanged()); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 79 | WarningSet warning_set; |
| 80 | warning_set.insert(Warning::CreateNetworkDelayWarning(ext1_id)); |
| 81 | warning_set.insert(Warning::CreateNetworkConflictWarning(ext2_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 82 | warning_service.AddWarnings(warning_set); |
| 83 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 84 | |
| 85 | // Remove one warning and check that the badge remains. |
| 86 | EXPECT_CALL(observer, ExtensionWarningsChanged()); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 87 | std::set<Warning::WarningType> to_clear; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 88 | to_clear.insert(warning_2); |
| 89 | warning_service.ClearWarnings(to_clear); |
| 90 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 91 | |
| 92 | // Check that the correct warnings appear in |warnings|. |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 93 | std::set<Warning::WarningType> existing_warnings = |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 94 | warning_service.GetWarningTypesAffectingExtension(ext1_id); |
| 95 | EXPECT_EQ(1u, existing_warnings.size()); |
| 96 | existing_warnings = |
| 97 | warning_service.GetWarningTypesAffectingExtension(ext2_id); |
| 98 | EXPECT_EQ(0u, existing_warnings.size()); |
| 99 | |
| 100 | // Remove the other one warning. |
| 101 | EXPECT_CALL(observer, ExtensionWarningsChanged()); |
| 102 | to_clear.insert(warning_1); |
| 103 | warning_service.ClearWarnings(to_clear); |
| 104 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 105 | |
| 106 | // Check that not warnings remain. |
| 107 | existing_warnings = |
| 108 | warning_service.GetWarningTypesAffectingExtension(ext1_id); |
| 109 | EXPECT_EQ(0u, existing_warnings.size()); |
| 110 | existing_warnings = |
| 111 | warning_service.GetWarningTypesAffectingExtension(ext2_id); |
| 112 | EXPECT_EQ(0u, existing_warnings.size()); |
| 113 | |
| 114 | warning_service.RemoveObserver(&observer); |
| 115 | } |
| 116 | |
| 117 | } // namespace extensions |