Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / warning_badge_service_unittest.cc
blobf01b25be9934cd48028a3c7251ddf42390fc3d38
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 "content/public/test/test_browser_thread_bundle.h"
13 #include "extensions/browser/warning_service.h"
14 #include "extensions/browser/warning_set.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace extensions {
19 namespace {
21 class TestExtensionWarningSet : public WarningService {
22 public:
23 explicit TestExtensionWarningSet(Profile* profile)
24 : WarningService(profile) {}
25 ~TestExtensionWarningSet() override {}
27 void AddWarning(const Warning& warning) {
28 WarningSet warnings;
29 warnings.insert(warning);
30 AddWarnings(warnings);
34 class TestWarningBadgeService : public WarningBadgeService {
35 public:
36 TestWarningBadgeService(Profile* profile, WarningService* warning_service)
37 : WarningBadgeService(profile), warning_service_(warning_service) {}
38 ~TestWarningBadgeService() override {}
40 const std::set<Warning>& GetCurrentWarnings() const override {
41 return warning_service_->warnings();
44 private:
45 WarningService* warning_service_;
48 bool HasBadge(Profile* profile) {
49 GlobalErrorService* service =
50 GlobalErrorServiceFactory::GetForProfile(profile);
51 return service->GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_ERRORS) !=
52 NULL;
55 const char ext1_id[] = "extension1";
56 const char ext2_id[] = "extension2";
58 } // namespace
60 // Check that no badge appears if it has been suppressed for a specific
61 // warning.
62 TEST(WarningBadgeServiceTest, SuppressBadgeForCurrentWarnings) {
63 content::TestBrowserThreadBundle thread_bundle;
64 TestingProfile profile;
65 TestExtensionWarningSet warnings(&profile);
66 TestWarningBadgeService badge_service(&profile, &warnings);
67 warnings.AddObserver(&badge_service);
69 // Insert first warning.
70 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id));
71 EXPECT_TRUE(HasBadge(&profile));
73 // Suppress first warning.
74 badge_service.SuppressCurrentWarnings();
75 EXPECT_FALSE(HasBadge(&profile));
77 // Simulate deinstallation of extension.
78 std::set<Warning::WarningType> to_clear =
79 warnings.GetWarningTypesAffectingExtension(ext1_id);
80 warnings.ClearWarnings(to_clear);
81 EXPECT_FALSE(HasBadge(&profile));
83 // Set first warning again and verify that not badge is shown this time.
84 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id));
85 EXPECT_FALSE(HasBadge(&profile));
87 // Set second warning and verify that it shows a badge.
88 warnings.AddWarning(Warning::CreateNetworkConflictWarning(ext2_id));
89 EXPECT_TRUE(HasBadge(&profile));
91 warnings.RemoveObserver(&badge_service);
94 } // namespace extensions