[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() {} |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 33 | MOCK_METHOD1(ExtensionWarningsChanged, void(const ExtensionIdSet&)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 34 | }; |
| 35 | |
Lukasz Anforowicz | 58d0dac | 2018-03-23 15:48:10 | [diff] [blame] | 36 | using WarningServiceTest = ExtensionsTest; |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 37 | |
thestig | 041f756e | 2016-10-14 18:26:18 | [diff] [blame] | 38 | const char ext1_id[] = "extension1"; |
| 39 | const char ext2_id[] = "extension2"; |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 40 | const Warning::WarningType warning_1 = Warning::kNetworkDelay; |
karandeepb | a1a134d4 | 2018-07-24 20:33:28 | [diff] [blame] | 41 | const Warning::WarningType warning_2 = Warning::kRepeatedCacheFlushes; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | // Check that inserting a warning triggers notifications, whereas inserting |
| 46 | // the same warning again is silent. |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 47 | TEST_F(WarningServiceTest, SetWarning) { |
| 48 | content::TestBrowserContext browser_context; |
| 49 | TestWarningService warning_service(&browser_context); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 50 | MockObserver observer; |
| 51 | warning_service.AddObserver(&observer); |
| 52 | |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 53 | ExtensionIdSet affected_extensions; |
| 54 | affected_extensions.insert(ext1_id); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 55 | // Insert warning for the first time. |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 56 | EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 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. |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 62 | warning_service.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 63 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 64 | |
| 65 | warning_service.RemoveObserver(&observer); |
| 66 | } |
| 67 | |
| 68 | // Check that ClearWarnings deletes exactly the specified warnings and |
| 69 | // triggers notifications where appropriate. |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 70 | TEST_F(WarningServiceTest, ClearWarnings) { |
| 71 | content::TestBrowserContext browser_context; |
| 72 | TestWarningService warning_service(&browser_context); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 73 | MockObserver observer; |
| 74 | warning_service.AddObserver(&observer); |
| 75 | |
| 76 | // Insert two unique warnings in one batch. |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 77 | std::set<std::string> affected_extensions; |
| 78 | affected_extensions.insert(ext1_id); |
| 79 | affected_extensions.insert(ext2_id); |
| 80 | EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions)); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 81 | WarningSet warning_set; |
| 82 | warning_set.insert(Warning::CreateNetworkDelayWarning(ext1_id)); |
karandeepb | a1a134d4 | 2018-07-24 20:33:28 | [diff] [blame] | 83 | warning_set.insert(Warning::CreateRepeatedCacheFlushesWarning(ext2_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 84 | warning_service.AddWarnings(warning_set); |
| 85 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 86 | |
| 87 | // Remove one warning and check that the badge remains. |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 88 | affected_extensions.clear(); |
| 89 | affected_extensions.insert(ext2_id); |
| 90 | EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions)); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 91 | std::set<Warning::WarningType> to_clear; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 92 | to_clear.insert(warning_2); |
| 93 | warning_service.ClearWarnings(to_clear); |
| 94 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 95 | |
| 96 | // Check that the correct warnings appear in |warnings|. |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 97 | std::set<Warning::WarningType> existing_warnings = |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 98 | warning_service.GetWarningTypesAffectingExtension(ext1_id); |
| 99 | EXPECT_EQ(1u, existing_warnings.size()); |
| 100 | existing_warnings = |
| 101 | warning_service.GetWarningTypesAffectingExtension(ext2_id); |
| 102 | EXPECT_EQ(0u, existing_warnings.size()); |
| 103 | |
| 104 | // Remove the other one warning. |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 105 | affected_extensions.clear(); |
| 106 | affected_extensions.insert(ext1_id); |
| 107 | EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 108 | to_clear.insert(warning_1); |
| 109 | warning_service.ClearWarnings(to_clear); |
| 110 | testing::Mock::VerifyAndClearExpectations(&warning_service); |
| 111 | |
rdevlin.cronin | 7dd05290 | 2015-05-19 22:23:22 | [diff] [blame] | 112 | // Check that no warnings remain. |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 113 | existing_warnings = |
| 114 | warning_service.GetWarningTypesAffectingExtension(ext1_id); |
| 115 | EXPECT_EQ(0u, existing_warnings.size()); |
| 116 | existing_warnings = |
| 117 | warning_service.GetWarningTypesAffectingExtension(ext2_id); |
| 118 | EXPECT_EQ(0u, existing_warnings.size()); |
| 119 | |
| 120 | warning_service.RemoveObserver(&observer); |
| 121 | } |
| 122 | |
| 123 | } // namespace extensions |