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.
5 #include "chrome/browser/extensions/extension_warning_badge_service.h"
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/extensions/extension_warning_set.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/global_error/global_error_service.h"
11 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
19 class TestExtensionWarningSet
: public ExtensionWarningService
{
21 explicit TestExtensionWarningSet(Profile
* profile
)
22 : ExtensionWarningService(profile
) {
24 virtual ~TestExtensionWarningSet() {}
26 void AddWarning(const ExtensionWarning
& warning
) {
27 ExtensionWarningSet warnings
;
28 warnings
.insert(warning
);
29 AddWarnings(warnings
);
33 class TestExtensionWarningBadgeService
: public ExtensionWarningBadgeService
{
35 TestExtensionWarningBadgeService(Profile
* profile
,
36 ExtensionWarningService
* warning_service
)
37 : ExtensionWarningBadgeService(profile
),
38 warning_service_(warning_service
) {}
39 virtual ~TestExtensionWarningBadgeService() {}
41 virtual const std::set
<ExtensionWarning
>&
42 GetCurrentWarnings() const OVERRIDE
{
43 return warning_service_
->warnings();
47 ExtensionWarningService
* warning_service_
;
50 bool HasBadge(Profile
* profile
) {
51 GlobalErrorService
* service
=
52 GlobalErrorServiceFactory::GetForProfile(profile
);
53 return service
->GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_ERRORS
) !=
57 const char* ext1_id
= "extension1";
58 const char* ext2_id
= "extension2";
62 // Check that no badge appears if it has been suppressed for a specific
64 TEST(ExtensionWarningBadgeServiceTest
, SuppressBadgeForCurrentWarnings
) {
65 TestingProfile profile
;
66 TestExtensionWarningSet
warnings(&profile
);
67 TestExtensionWarningBadgeService
badge_service(&profile
, &warnings
);
68 warnings
.AddObserver(&badge_service
);
70 // Insert first warning.
71 warnings
.AddWarning(ExtensionWarning::CreateNetworkDelayWarning(ext1_id
));
72 EXPECT_TRUE(HasBadge(&profile
));
74 // Suppress first warning.
75 badge_service
.SuppressCurrentWarnings();
76 EXPECT_FALSE(HasBadge(&profile
));
78 // Simulate deinstallation of extension.
79 std::set
<ExtensionWarning::WarningType
> to_clear
=
80 warnings
.GetWarningTypesAffectingExtension(ext1_id
);
81 warnings
.ClearWarnings(to_clear
);
82 EXPECT_FALSE(HasBadge(&profile
));
84 // Set first warning again and verify that not badge is shown this time.
85 warnings
.AddWarning(ExtensionWarning::CreateNetworkDelayWarning(ext1_id
));
86 EXPECT_FALSE(HasBadge(&profile
));
88 // Set second warning and verify that it shows a badge.
89 warnings
.AddWarning(ExtensionWarning::CreateNetworkConflictWarning(ext2_id
));
90 EXPECT_TRUE(HasBadge(&profile
));
92 warnings
.RemoveObserver(&badge_service
);
95 } // namespace extensions