1 // Copyright 2013 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 "base/strings/utf_string_conversions.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/message_center/fake_notifier_settings_provider.h"
8 #include "ui/message_center/views/notifier_settings_view.h"
9
10 namespace message_center {
11
12 namespace {
13
NewNotifier(const std::string & id,const std::string & title,bool enabled)14 Notifier* NewNotifier(const std::string& id,
15 const std::string& title,
16 bool enabled) {
17 NotifierId notifier_id(NotifierId::APPLICATION, id);
18 return new Notifier(notifier_id, base::UTF8ToUTF16(title), enabled);
19 }
20
21 // A class used by NotifierSettingsView to integrate with a setting system
22 // for the clients of this module.
23 class TestingNotifierSettingsProvider
24 : public FakeNotifierSettingsProvider {
25 public:
TestingNotifierSettingsProvider(const std::vector<Notifier * > & notifiers,const NotifierId & settings_handler_id)26 TestingNotifierSettingsProvider(const std::vector<Notifier*>& notifiers,
27 const NotifierId& settings_handler_id)
28 : FakeNotifierSettingsProvider(notifiers),
29 settings_handler_id_(settings_handler_id),
30 request_count_(0u) {}
~TestingNotifierSettingsProvider()31 virtual ~TestingNotifierSettingsProvider() {}
32
NotifierHasAdvancedSettings(const NotifierId & notifier_id) const33 virtual bool NotifierHasAdvancedSettings(const NotifierId& notifier_id) const
34 OVERRIDE {
35 return notifier_id == settings_handler_id_;
36 }
37
OnNotifierAdvancedSettingsRequested(const NotifierId & notifier_id,const std::string * notification_id)38 virtual void OnNotifierAdvancedSettingsRequested(
39 const NotifierId& notifier_id,
40 const std::string* notification_id) OVERRIDE {
41 request_count_++;
42 last_notifier_id_settings_requested_.reset(new NotifierId(notifier_id));
43 }
44
request_count() const45 size_t request_count() const { return request_count_; }
last_requested_notifier_id() const46 const NotifierId* last_requested_notifier_id() const {
47 return last_notifier_id_settings_requested_.get();
48 }
49
50 private:
51 NotifierId settings_handler_id_;
52 size_t request_count_;
53 scoped_ptr<NotifierId> last_notifier_id_settings_requested_;
54 };
55
56 } // namespace
57
58 class NotifierSettingsViewTest : public testing::Test {
59 public:
60 NotifierSettingsViewTest();
61 virtual ~NotifierSettingsViewTest();
62
63 virtual void SetUp() OVERRIDE;
64 virtual void TearDown() OVERRIDE;
65
66 NotifierSettingsView* GetView() const;
settings_provider() const67 TestingNotifierSettingsProvider* settings_provider() const {
68 return settings_provider_.get();
69 }
70
71 private:
72 scoped_ptr<TestingNotifierSettingsProvider> settings_provider_;
73 scoped_ptr<NotifierSettingsView> notifier_settings_view_;
74
75 DISALLOW_COPY_AND_ASSIGN(NotifierSettingsViewTest);
76 };
77
NotifierSettingsViewTest()78 NotifierSettingsViewTest::NotifierSettingsViewTest() {}
79
~NotifierSettingsViewTest()80 NotifierSettingsViewTest::~NotifierSettingsViewTest() {}
81
SetUp()82 void NotifierSettingsViewTest::SetUp() {
83 std::vector<Notifier*> notifiers;
84 notifiers.push_back(NewNotifier("id", "title", /*enabled=*/true));
85 notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/false));
86 settings_provider_.reset(new TestingNotifierSettingsProvider(
87 notifiers, NotifierId(NotifierId::APPLICATION, "id")));
88 notifier_settings_view_.reset(
89 new NotifierSettingsView(settings_provider_.get()));
90 }
91
TearDown()92 void NotifierSettingsViewTest::TearDown() {
93 notifier_settings_view_.reset();
94 settings_provider_.reset();
95 }
96
GetView() const97 NotifierSettingsView* NotifierSettingsViewTest::GetView() const {
98 return notifier_settings_view_.get();
99 }
100
TEST_F(NotifierSettingsViewTest,TestLearnMoreButton)101 TEST_F(NotifierSettingsViewTest, TestLearnMoreButton) {
102 const std::set<NotifierSettingsView::NotifierButton*> buttons =
103 GetView()->buttons_;
104 EXPECT_EQ(2u, buttons.size());
105 size_t number_of_settings_buttons = 0;
106 std::set<NotifierSettingsView::NotifierButton*>::iterator iter =
107 buttons.begin();
108 for (; iter != buttons.end(); ++iter) {
109 if ((*iter)->has_learn_more()) {
110 ++number_of_settings_buttons;
111 (*iter)->SendLearnMorePressedForTest();
112 }
113 }
114
115 EXPECT_EQ(1u, number_of_settings_buttons);
116 EXPECT_EQ(1u, settings_provider()->request_count());
117 const NotifierId* last_settings_button_id =
118 settings_provider()->last_requested_notifier_id();
119 ASSERT_FALSE(last_settings_button_id == NULL);
120 EXPECT_EQ(NotifierId(NotifierId::APPLICATION, "id"),
121 *last_settings_button_id);
122 }
123
124 } // namespace message_center
125