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_service.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/global_error/global_error_service.h"
9 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace extensions
{
18 class TestExtensionWarningService
: public ExtensionWarningService
{
20 explicit TestExtensionWarningService(Profile
* profile
)
21 : ExtensionWarningService(profile
) {
23 virtual ~TestExtensionWarningService() {}
25 void AddWarning(const ExtensionWarning
& warning
) {
26 ExtensionWarningSet warnings
;
27 warnings
.insert(warning
);
28 AddWarnings(warnings
);
32 class MockObserver
: public ExtensionWarningService::Observer
{
34 virtual ~MockObserver() {}
35 MOCK_METHOD0(ExtensionWarningsChanged
, void());
38 const char* ext1_id
= "extension1";
39 const char* ext2_id
= "extension2";
40 const ExtensionWarning::WarningType warning_1
=
41 ExtensionWarning::kNetworkDelay
;
42 const ExtensionWarning::WarningType warning_2
=
43 ExtensionWarning::kNetworkConflict
;
47 // Check that inserting a warning triggers notifications, whereas inserting
48 // the same warning again is silent.
49 TEST(ExtensionWarningServiceTest
, SetWarning
) {
50 TestingProfile profile
;
51 TestExtensionWarningService
warning_service(&profile
);
52 MockObserver observer
;
53 warning_service
.AddObserver(&observer
);
55 // Insert warning for the first time.
56 EXPECT_CALL(observer
, ExtensionWarningsChanged());
57 warning_service
.AddWarning(
58 ExtensionWarning::CreateNetworkDelayWarning(ext1_id
));
59 testing::Mock::VerifyAndClearExpectations(&warning_service
);
61 // Second insertion of same warning does not trigger anything.
62 warning_service
.AddWarning(
63 ExtensionWarning::CreateNetworkDelayWarning(ext1_id
));
64 testing::Mock::VerifyAndClearExpectations(&warning_service
);
66 warning_service
.RemoveObserver(&observer
);
69 // Check that ClearWarnings deletes exactly the specified warnings and
70 // triggers notifications where appropriate.
71 TEST(ExtensionWarningServiceTest
, ClearWarnings
) {
72 TestingProfile profile
;
73 TestExtensionWarningService
warning_service(&profile
);
74 MockObserver observer
;
75 warning_service
.AddObserver(&observer
);
77 // Insert two unique warnings in one batch.
78 EXPECT_CALL(observer
, ExtensionWarningsChanged());
79 ExtensionWarningSet warning_set
;
80 warning_set
.insert(ExtensionWarning::CreateNetworkDelayWarning(ext1_id
));
81 warning_set
.insert(ExtensionWarning::CreateNetworkConflictWarning(ext2_id
));
82 warning_service
.AddWarnings(warning_set
);
83 testing::Mock::VerifyAndClearExpectations(&warning_service
);
85 // Remove one warning and check that the badge remains.
86 EXPECT_CALL(observer
, ExtensionWarningsChanged());
87 std::set
<ExtensionWarning::WarningType
> to_clear
;
88 to_clear
.insert(warning_2
);
89 warning_service
.ClearWarnings(to_clear
);
90 testing::Mock::VerifyAndClearExpectations(&warning_service
);
92 // Check that the correct warnings appear in |warnings|.
93 std::set
<ExtensionWarning::WarningType
> existing_warnings
=
94 warning_service
.GetWarningTypesAffectingExtension(ext1_id
);
95 EXPECT_EQ(1u, existing_warnings
.size());
97 warning_service
.GetWarningTypesAffectingExtension(ext2_id
);
98 EXPECT_EQ(0u, existing_warnings
.size());
100 // Remove the other one warning.
101 EXPECT_CALL(observer
, ExtensionWarningsChanged());
102 to_clear
.insert(warning_1
);
103 warning_service
.ClearWarnings(to_clear
);
104 testing::Mock::VerifyAndClearExpectations(&warning_service
);
106 // Check that not warnings remain.
108 warning_service
.GetWarningTypesAffectingExtension(ext1_id
);
109 EXPECT_EQ(0u, existing_warnings
.size());
111 warning_service
.GetWarningTypesAffectingExtension(ext2_id
);
112 EXPECT_EQ(0u, existing_warnings
.size());
114 warning_service
.RemoveObserver(&observer
);
117 } // namespace extensions