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 "extensions/browser/warning_service.h"
7 #include "content/public/test/test_browser_context.h"
8 #include "extensions/browser/extensions_test.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions
{
16 class TestWarningService
: public WarningService
{
18 explicit TestWarningService(content::BrowserContext
* browser_context
)
19 : WarningService(browser_context
) {
21 ~TestWarningService() override
{}
23 void AddWarning(const Warning
& warning
) {
25 warnings
.insert(warning
);
26 AddWarnings(warnings
);
30 class MockObserver
: public WarningService::Observer
{
32 virtual ~MockObserver() {}
33 MOCK_METHOD0(ExtensionWarningsChanged
, void());
36 typedef ExtensionsTest WarningServiceTest
;
38 const char* ext1_id
= "extension1";
39 const char* ext2_id
= "extension2";
40 const Warning::WarningType warning_1
=
41 Warning::kNetworkDelay
;
42 const Warning::WarningType warning_2
=
43 Warning::kNetworkConflict
;
47 // Check that inserting a warning triggers notifications, whereas inserting
48 // the same warning again is silent.
49 TEST_F(WarningServiceTest
, SetWarning
) {
50 content::TestBrowserContext browser_context
;
51 TestWarningService
warning_service(&browser_context
);
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 Warning::CreateNetworkDelayWarning(ext1_id
));
59 testing::Mock::VerifyAndClearExpectations(&warning_service
);
61 // Second insertion of same warning does not trigger anything.
62 warning_service
.AddWarning(
63 Warning::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_F(WarningServiceTest
, ClearWarnings
) {
72 content::TestBrowserContext browser_context
;
73 TestWarningService
warning_service(&browser_context
);
74 MockObserver observer
;
75 warning_service
.AddObserver(&observer
);
77 // Insert two unique warnings in one batch.
78 EXPECT_CALL(observer
, ExtensionWarningsChanged());
79 WarningSet warning_set
;
80 warning_set
.insert(Warning::CreateNetworkDelayWarning(ext1_id
));
81 warning_set
.insert(Warning::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
<Warning::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
<Warning::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