[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 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 5 | #include "chrome/browser/extensions/warning_badge_service.h" |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 6 | |
7 | #include "chrome/app/chrome_command_ids.h" | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 8 | #include "chrome/browser/profiles/profile.h" |
9 | #include "chrome/browser/ui/global_error/global_error_service.h" | ||||
10 | #include "chrome/browser/ui/global_error/global_error_service_factory.h" | ||||
11 | #include "chrome/test/base/testing_profile.h" | ||||
skyostil | 0becb33 | 2015-04-27 17:59:37 | [diff] [blame] | 12 | #include "content/public/test/test_browser_thread_bundle.h" |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 13 | #include "extensions/browser/warning_service.h" |
14 | #include "extensions/browser/warning_set.h" | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
16 | |||||
17 | namespace extensions { | ||||
18 | |||||
19 | namespace { | ||||
20 | |||||
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 21 | class TestExtensionWarningSet : public WarningService { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 22 | public: |
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 23 | explicit TestExtensionWarningSet(Profile* profile) |
24 | : WarningService(profile) {} | ||||
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 25 | ~TestExtensionWarningSet() override {} |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 26 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 27 | void AddWarning(const Warning& warning) { |
28 | WarningSet warnings; | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 29 | warnings.insert(warning); |
30 | AddWarnings(warnings); | ||||
31 | } | ||||
32 | }; | ||||
33 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 34 | class TestWarningBadgeService : public WarningBadgeService { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 35 | public: |
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 36 | TestWarningBadgeService(Profile* profile, WarningService* warning_service) |
37 | : WarningBadgeService(profile), warning_service_(warning_service) {} | ||||
38 | ~TestWarningBadgeService() override {} | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 39 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 40 | const std::set<Warning>& GetCurrentWarnings() const override { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 41 | return warning_service_->warnings(); |
42 | } | ||||
43 | |||||
44 | private: | ||||
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 45 | WarningService* warning_service_; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 46 | }; |
47 | |||||
48 | bool HasBadge(Profile* profile) { | ||||
49 | GlobalErrorService* service = | ||||
50 | GlobalErrorServiceFactory::GetForProfile(profile); | ||||
51 | return service->GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_ERRORS) != | ||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 52 | NULL; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 53 | } |
54 | |||||
thestig | 4b36dd3 | 2014-10-31 20:30:19 | [diff] [blame] | 55 | const char ext1_id[] = "extension1"; |
56 | const char ext2_id[] = "extension2"; | ||||
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 57 | |
58 | } // namespace | ||||
59 | |||||
60 | // Check that no badge appears if it has been suppressed for a specific | ||||
61 | // warning. | ||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 62 | TEST(WarningBadgeServiceTest, SuppressBadgeForCurrentWarnings) { |
skyostil | 0becb33 | 2015-04-27 17:59:37 | [diff] [blame] | 63 | content::TestBrowserThreadBundle thread_bundle; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 64 | TestingProfile profile; |
65 | TestExtensionWarningSet warnings(&profile); | ||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 66 | TestWarningBadgeService badge_service(&profile, &warnings); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 67 | warnings.AddObserver(&badge_service); |
68 | |||||
69 | // Insert first warning. | ||||
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 70 | warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 71 | EXPECT_TRUE(HasBadge(&profile)); |
72 | |||||
73 | // Suppress first warning. | ||||
74 | badge_service.SuppressCurrentWarnings(); | ||||
75 | EXPECT_FALSE(HasBadge(&profile)); | ||||
76 | |||||
77 | // Simulate deinstallation of extension. | ||||
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 78 | std::set<Warning::WarningType> to_clear = |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 79 | warnings.GetWarningTypesAffectingExtension(ext1_id); |
80 | warnings.ClearWarnings(to_clear); | ||||
81 | EXPECT_FALSE(HasBadge(&profile)); | ||||
82 | |||||
83 | // Set first warning again and verify that not badge is shown this time. | ||||
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 84 | warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 85 | EXPECT_FALSE(HasBadge(&profile)); |
86 | |||||
87 | // Set second warning and verify that it shows a badge. | ||||
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 88 | warnings.AddWarning(Warning::CreateNetworkConflictWarning(ext2_id)); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 89 | EXPECT_TRUE(HasBadge(&profile)); |
90 | |||||
91 | warnings.RemoveObserver(&badge_service); | ||||
92 | } | ||||
93 | |||||
zhuoyu.qian | 6fcce6a | 2015-02-25 06:02:09 | [diff] [blame] | 94 | } // namespace extensions |