File: message_list_view_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (185 lines) | stat: -rw-r--r-- 6,034 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <memory>
#include <utility>

#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/fake_message_center.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/views/message_center_controller.h"
#include "ui/message_center/views/message_list_view.h"
#include "ui/message_center/views/notification_view.h"
#include "ui/views/test/views_test_base.h"

namespace message_center {

static const char* kNotificationId1 = "notification id 1";

namespace {

/* Types **********************************************************************/

enum CallType { GET_PREFERRED_SIZE, GET_HEIGHT_FOR_WIDTH, LAYOUT };

/* Instrumented/Mock NotificationView subclass ********************************/

class MockNotificationView : public NotificationView {
 public:
  class Test {
   public:
    virtual void RegisterCall(CallType type) = 0;
  };

  MockNotificationView(MessageCenterController* controller,
                       const Notification& notification,
                       Test* test);
  ~MockNotificationView() override;

  gfx::Size GetPreferredSize() const override;
  int GetHeightForWidth(int w) const override;
  void Layout() override;

 private:
  Test* test_;

  DISALLOW_COPY_AND_ASSIGN(MockNotificationView);
};

MockNotificationView::MockNotificationView(MessageCenterController* controller,
                                           const Notification& notification,
                                           Test* test)
    : NotificationView(controller, notification), test_(test) {}

MockNotificationView::~MockNotificationView() {}

gfx::Size MockNotificationView::GetPreferredSize() const {
  test_->RegisterCall(GET_PREFERRED_SIZE);
  DCHECK(child_count() > 0);
  return NotificationView::GetPreferredSize();
}

int MockNotificationView::GetHeightForWidth(int width) const {
  test_->RegisterCall(GET_HEIGHT_FOR_WIDTH);
  DCHECK(child_count() > 0);
  return NotificationView::GetHeightForWidth(width);
}

void MockNotificationView::Layout() {
  test_->RegisterCall(LAYOUT);
  DCHECK(child_count() > 0);
  NotificationView::Layout();
}

}  // anonymous namespace

/* Test fixture ***************************************************************/

class MessageListViewTest : public views::ViewsTestBase,
                            public MockNotificationView::Test,
                            public MessageListView::Observer,
                            public MessageCenterController {
 public:
  MessageListViewTest() {}

  ~MessageListViewTest() override {}

  void SetUp() override {
    views::ViewsTestBase::SetUp();

    message_list_view_.reset(new MessageListView());
    message_list_view_->AddObserver(this);
    message_list_view_->set_owned_by_client();

    widget_.reset(new views::Widget());
    views::Widget::InitParams params =
        CreateParams(views::Widget::InitParams::TYPE_POPUP);
    params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
    params.bounds = gfx::Rect(50, 50, 650, 650);
    widget_->Init(params);
    views::View* root = widget_->GetRootView();
    root->AddChildView(message_list_view_.get());
    widget_->Show();
    widget_->Activate();
  }

  void TearDown() override {
    widget_->CloseNow();
    widget_.reset();

    message_list_view_->RemoveObserver(this);
    message_list_view_.reset();

    views::ViewsTestBase::TearDown();
  }

 protected:
  MessageListView* message_list_view() const {
    return message_list_view_.get();
  }

  MockNotificationView* CreateNotificationView(
      const Notification& notification) {
    return new MockNotificationView(this, notification, this);
  }

 private:
  // MockNotificationView::Test override
  void RegisterCall(CallType type) override {}

  // MessageListView::Observer override
  void OnAllNotificationsCleared() override {}

  // MessageCenterController override:
  void ClickOnNotification(const std::string& notification_id) override {}
  void RemoveNotification(const std::string& notification_id,
                          bool by_user) override {}
  std::unique_ptr<ui::MenuModel> CreateMenuModel(
      const NotifierId& notifier_id,
      const base::string16& display_source) override {
    NOTREACHED();
    return nullptr;
  }
  bool HasClickedListener(const std::string& notification_id) override {
    return false;
  }
  void ClickOnNotificationButton(const std::string& notification_id,
                                 int button_index) override {}
  void ClickOnSettingsButton(const std::string& notification_id) override {}

  // Widget to host a MessageListView.
  std::unique_ptr<views::Widget> widget_;
  // MessageListView to be tested.
  std::unique_ptr<MessageListView> message_list_view_;

  DISALLOW_COPY_AND_ASSIGN(MessageListViewTest);
};

/* Unit tests *****************************************************************/

TEST_F(MessageListViewTest, AddNotification) {
  // Create a dummy notification.
  auto notification_view = CreateNotificationView(
      Notification(NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
                   base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message1"),
                   gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
                   NotifierId(NotifierId::APPLICATION, "extension_id"),
                   message_center::RichNotificationData(), nullptr));

  EXPECT_EQ(0, message_list_view()->child_count());
  EXPECT_FALSE(message_list_view()->Contains(notification_view));

  // Add a notification.
  message_list_view()->AddNotificationAt(notification_view, 0);

  EXPECT_EQ(1, message_list_view()->child_count());
  EXPECT_TRUE(message_list_view()->Contains(notification_view));
}

}  // namespace