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.
4
5#include "extensions/browser/warning_service.h"
6
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"
11
12namespace extensions {
13
14namespace {
15
16class TestWarningService : public WarningService {
17 public:
18  explicit TestWarningService(content::BrowserContext* browser_context)
19      : WarningService(browser_context) {
20  }
21  virtual ~TestWarningService() {}
22
23  void AddWarning(const Warning& warning) {
24    WarningSet warnings;
25    warnings.insert(warning);
26    AddWarnings(warnings);
27  }
28};
29
30class MockObserver : public WarningService::Observer {
31 public:
32  virtual ~MockObserver() {}
33  MOCK_METHOD0(ExtensionWarningsChanged, void());
34};
35
36typedef ExtensionsTest WarningServiceTest;
37
38const char* ext1_id = "extension1";
39const char* ext2_id = "extension2";
40const Warning::WarningType warning_1 =
41    Warning::kNetworkDelay;
42const Warning::WarningType warning_2 =
43    Warning::kNetworkConflict;
44
45}  // namespace
46
47// Check that inserting a warning triggers notifications, whereas inserting
48// the same warning again is silent.
49TEST_F(WarningServiceTest, SetWarning) {
50  content::TestBrowserContext browser_context;
51  TestWarningService warning_service(&browser_context);
52  MockObserver observer;
53  warning_service.AddObserver(&observer);
54
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);
60
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);
65
66  warning_service.RemoveObserver(&observer);
67}
68
69// Check that ClearWarnings deletes exactly the specified warnings and
70// triggers notifications where appropriate.
71TEST_F(WarningServiceTest, ClearWarnings) {
72  content::TestBrowserContext browser_context;
73  TestWarningService warning_service(&browser_context);
74  MockObserver observer;
75  warning_service.AddObserver(&observer);
76
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);
84
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);
91
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());
96  existing_warnings =
97      warning_service.GetWarningTypesAffectingExtension(ext2_id);
98  EXPECT_EQ(0u, existing_warnings.size());
99
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);
105
106  // Check that not warnings remain.
107  existing_warnings =
108      warning_service.GetWarningTypesAffectingExtension(ext1_id);
109  EXPECT_EQ(0u, existing_warnings.size());
110  existing_warnings =
111      warning_service.GetWarningTypesAffectingExtension(ext2_id);
112  EXPECT_EQ(0u, existing_warnings.size());
113
114  warning_service.RemoveObserver(&observer);
115}
116
117}  // namespace extensions
118