Roll src/third_party/WebKit 63beae6:7fa8deb (svn 201790:201791)
[chromium-blink-merge.git] / extensions / browser / warning_service_unittest.cc
blobabe7851a35c9647d10dfb9435be92aee7c22b736
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 {
14 namespace {
16 class TestWarningService : public WarningService {
17 public:
18 explicit TestWarningService(content::BrowserContext* browser_context)
19 : WarningService(browser_context) {
21 ~TestWarningService() override {}
23 void AddWarning(const Warning& warning) {
24 WarningSet warnings;
25 warnings.insert(warning);
26 AddWarnings(warnings);
30 class MockObserver : public WarningService::Observer {
31 public:
32 virtual ~MockObserver() {}
33 MOCK_METHOD1(ExtensionWarningsChanged, void(const ExtensionIdSet&));
36 typedef ExtensionsTest WarningServiceTest;
38 const char* ext1_id = "extension1";
39 const char* ext2_id = "extension2";
40 const Warning::WarningType warning_1 = Warning::kNetworkDelay;
41 const Warning::WarningType warning_2 = Warning::kNetworkConflict;
43 } // namespace
45 // Check that inserting a warning triggers notifications, whereas inserting
46 // the same warning again is silent.
47 TEST_F(WarningServiceTest, SetWarning) {
48 content::TestBrowserContext browser_context;
49 TestWarningService warning_service(&browser_context);
50 MockObserver observer;
51 warning_service.AddObserver(&observer);
53 ExtensionIdSet affected_extensions;
54 affected_extensions.insert(ext1_id);
55 // Insert warning for the first time.
56 EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions));
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(Warning::CreateNetworkDelayWarning(ext1_id));
63 testing::Mock::VerifyAndClearExpectations(&warning_service);
65 warning_service.RemoveObserver(&observer);
68 // Check that ClearWarnings deletes exactly the specified warnings and
69 // triggers notifications where appropriate.
70 TEST_F(WarningServiceTest, ClearWarnings) {
71 content::TestBrowserContext browser_context;
72 TestWarningService warning_service(&browser_context);
73 MockObserver observer;
74 warning_service.AddObserver(&observer);
76 // Insert two unique warnings in one batch.
77 std::set<std::string> affected_extensions;
78 affected_extensions.insert(ext1_id);
79 affected_extensions.insert(ext2_id);
80 EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions));
81 WarningSet warning_set;
82 warning_set.insert(Warning::CreateNetworkDelayWarning(ext1_id));
83 warning_set.insert(Warning::CreateNetworkConflictWarning(ext2_id));
84 warning_service.AddWarnings(warning_set);
85 testing::Mock::VerifyAndClearExpectations(&warning_service);
87 // Remove one warning and check that the badge remains.
88 affected_extensions.clear();
89 affected_extensions.insert(ext2_id);
90 EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions));
91 std::set<Warning::WarningType> to_clear;
92 to_clear.insert(warning_2);
93 warning_service.ClearWarnings(to_clear);
94 testing::Mock::VerifyAndClearExpectations(&warning_service);
96 // Check that the correct warnings appear in |warnings|.
97 std::set<Warning::WarningType> existing_warnings =
98 warning_service.GetWarningTypesAffectingExtension(ext1_id);
99 EXPECT_EQ(1u, existing_warnings.size());
100 existing_warnings =
101 warning_service.GetWarningTypesAffectingExtension(ext2_id);
102 EXPECT_EQ(0u, existing_warnings.size());
104 // Remove the other one warning.
105 affected_extensions.clear();
106 affected_extensions.insert(ext1_id);
107 EXPECT_CALL(observer, ExtensionWarningsChanged(affected_extensions));
108 to_clear.insert(warning_1);
109 warning_service.ClearWarnings(to_clear);
110 testing::Mock::VerifyAndClearExpectations(&warning_service);
112 // Check that no warnings remain.
113 existing_warnings =
114 warning_service.GetWarningTypesAffectingExtension(ext1_id);
115 EXPECT_EQ(0u, existing_warnings.size());
116 existing_warnings =
117 warning_service.GetWarningTypesAffectingExtension(ext2_id);
118 EXPECT_EQ(0u, existing_warnings.size());
120 warning_service.RemoveObserver(&observer);
123 } // namespace extensions