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/warning_badge_service.h"
7 #include "chrome/app/chrome_command_ids.h"
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"
12 #include "extensions/browser/warning_service.h"
13 #include "extensions/browser/warning_set.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace extensions
{
20 class TestExtensionWarningSet
: public WarningService
{
22 explicit TestExtensionWarningSet(Profile
* profile
)
23 : WarningService(profile
) {}
24 ~TestExtensionWarningSet() override
{}
26 void AddWarning(const Warning
& warning
) {
28 warnings
.insert(warning
);
29 AddWarnings(warnings
);
33 class TestWarningBadgeService
: public WarningBadgeService
{
35 TestWarningBadgeService(Profile
* profile
, WarningService
* warning_service
)
36 : WarningBadgeService(profile
), warning_service_(warning_service
) {}
37 ~TestWarningBadgeService() override
{}
39 const std::set
<Warning
>& GetCurrentWarnings() const override
{
40 return warning_service_
->warnings();
44 WarningService
* warning_service_
;
47 bool HasBadge(Profile
* profile
) {
48 GlobalErrorService
* service
=
49 GlobalErrorServiceFactory::GetForProfile(profile
);
50 return service
->GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_ERRORS
) !=
54 const char ext1_id
[] = "extension1";
55 const char ext2_id
[] = "extension2";
59 // Check that no badge appears if it has been suppressed for a specific
61 TEST(WarningBadgeServiceTest
, SuppressBadgeForCurrentWarnings
) {
62 TestingProfile profile
;
63 TestExtensionWarningSet
warnings(&profile
);
64 TestWarningBadgeService
badge_service(&profile
, &warnings
);
65 warnings
.AddObserver(&badge_service
);
67 // Insert first warning.
68 warnings
.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id
));
69 EXPECT_TRUE(HasBadge(&profile
));
71 // Suppress first warning.
72 badge_service
.SuppressCurrentWarnings();
73 EXPECT_FALSE(HasBadge(&profile
));
75 // Simulate deinstallation of extension.
76 std::set
<Warning::WarningType
> to_clear
=
77 warnings
.GetWarningTypesAffectingExtension(ext1_id
);
78 warnings
.ClearWarnings(to_clear
);
79 EXPECT_FALSE(HasBadge(&profile
));
81 // Set first warning again and verify that not badge is shown this time.
82 warnings
.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id
));
83 EXPECT_FALSE(HasBadge(&profile
));
85 // Set second warning and verify that it shows a badge.
86 warnings
.AddWarning(Warning::CreateNetworkConflictWarning(ext2_id
));
87 EXPECT_TRUE(HasBadge(&profile
));
89 warnings
.RemoveObserver(&badge_service
);
92 } // namespace extensions