blob: f01b25be9934cd48028a3c7251ddf42390fc3d38 [file] [log] [blame]
[email protected]b4d3771d2012-11-14 14:44:101// 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.qian6fcce6a2015-02-25 06:02:095#include "chrome/browser/extensions/warning_badge_service.h"
[email protected]b4d3771d2012-11-14 14:44:106
7#include "chrome/app/chrome_command_ids.h"
[email protected]b4d3771d2012-11-14 14:44:108#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"
skyostil0becb332015-04-27 17:59:3712#include "content/public/test/test_browser_thread_bundle.h"
hanxic7e55202014-08-28 14:13:2113#include "extensions/browser/warning_service.h"
14#include "extensions/browser/warning_set.h"
[email protected]b4d3771d2012-11-14 14:44:1015#include "testing/gtest/include/gtest/gtest.h"
16
17namespace extensions {
18
19namespace {
20
hanxic7e55202014-08-28 14:13:2121class TestExtensionWarningSet : public WarningService {
[email protected]b4d3771d2012-11-14 14:44:1022 public:
zhuoyu.qian6fcce6a2015-02-25 06:02:0923 explicit TestExtensionWarningSet(Profile* profile)
24 : WarningService(profile) {}
dchengae36a4a2014-10-21 12:36:3625 ~TestExtensionWarningSet() override {}
[email protected]b4d3771d2012-11-14 14:44:1026
hanxic7e55202014-08-28 14:13:2127 void AddWarning(const Warning& warning) {
28 WarningSet warnings;
[email protected]b4d3771d2012-11-14 14:44:1029 warnings.insert(warning);
30 AddWarnings(warnings);
31 }
32};
33
zhuoyu.qian6fcce6a2015-02-25 06:02:0934class TestWarningBadgeService : public WarningBadgeService {
[email protected]b4d3771d2012-11-14 14:44:1035 public:
zhuoyu.qian6fcce6a2015-02-25 06:02:0936 TestWarningBadgeService(Profile* profile, WarningService* warning_service)
37 : WarningBadgeService(profile), warning_service_(warning_service) {}
38 ~TestWarningBadgeService() override {}
[email protected]b4d3771d2012-11-14 14:44:1039
dchengae36a4a2014-10-21 12:36:3640 const std::set<Warning>& GetCurrentWarnings() const override {
[email protected]b4d3771d2012-11-14 14:44:1041 return warning_service_->warnings();
42 }
43
44 private:
hanxic7e55202014-08-28 14:13:2145 WarningService* warning_service_;
[email protected]b4d3771d2012-11-14 14:44:1046};
47
48bool HasBadge(Profile* profile) {
49 GlobalErrorService* service =
50 GlobalErrorServiceFactory::GetForProfile(profile);
51 return service->GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_ERRORS) !=
zhuoyu.qian6fcce6a2015-02-25 06:02:0952 NULL;
[email protected]b4d3771d2012-11-14 14:44:1053}
54
thestig4b36dd32014-10-31 20:30:1955const char ext1_id[] = "extension1";
56const char ext2_id[] = "extension2";
[email protected]b4d3771d2012-11-14 14:44:1057
58} // namespace
59
60// Check that no badge appears if it has been suppressed for a specific
61// warning.
zhuoyu.qian6fcce6a2015-02-25 06:02:0962TEST(WarningBadgeServiceTest, SuppressBadgeForCurrentWarnings) {
skyostil0becb332015-04-27 17:59:3763 content::TestBrowserThreadBundle thread_bundle;
[email protected]b4d3771d2012-11-14 14:44:1064 TestingProfile profile;
65 TestExtensionWarningSet warnings(&profile);
zhuoyu.qian6fcce6a2015-02-25 06:02:0966 TestWarningBadgeService badge_service(&profile, &warnings);
[email protected]b4d3771d2012-11-14 14:44:1067 warnings.AddObserver(&badge_service);
68
69 // Insert first warning.
hanxic7e55202014-08-28 14:13:2170 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id));
[email protected]b4d3771d2012-11-14 14:44:1071 EXPECT_TRUE(HasBadge(&profile));
72
73 // Suppress first warning.
74 badge_service.SuppressCurrentWarnings();
75 EXPECT_FALSE(HasBadge(&profile));
76
77 // Simulate deinstallation of extension.
hanxic7e55202014-08-28 14:13:2178 std::set<Warning::WarningType> to_clear =
[email protected]b4d3771d2012-11-14 14:44:1079 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.
hanxic7e55202014-08-28 14:13:2184 warnings.AddWarning(Warning::CreateNetworkDelayWarning(ext1_id));
[email protected]b4d3771d2012-11-14 14:44:1085 EXPECT_FALSE(HasBadge(&profile));
86
87 // Set second warning and verify that it shows a badge.
hanxic7e55202014-08-28 14:13:2188 warnings.AddWarning(Warning::CreateNetworkConflictWarning(ext2_id));
[email protected]b4d3771d2012-11-14 14:44:1089 EXPECT_TRUE(HasBadge(&profile));
90
91 warnings.RemoveObserver(&badge_service);
92}
93
zhuoyu.qian6fcce6a2015-02-25 06:02:0994} // namespace extensions