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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// Copyright 2014 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 "ui/message_center/views/notification_view.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/notification_types.h"
#include "ui/message_center/views/message_center_controller.h"
#include "ui/message_center/views/notification_button.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget_delegate.h"
namespace message_center {
/* Test fixture ***************************************************************/
class NotificationViewTest : public views::ViewsTestBase,
public MessageCenterController {
public:
NotificationViewTest();
~NotificationViewTest() override;
void SetUp() override;
void TearDown() override;
views::Widget* widget() { return notification_view_->GetWidget(); }
NotificationView* notification_view() { return notification_view_.get(); }
Notification* notification() { return notification_.get(); }
RichNotificationData* data() { return data_.get(); }
// Overridden from MessageCenterController:
void ClickOnNotification(const std::string& notification_id) override;
void RemoveNotification(const std::string& notification_id,
bool by_user) override;
scoped_ptr<ui::MenuModel> CreateMenuModel(
const NotifierId& notifier_id,
const base::string16& display_source) override;
bool HasClickedListener(const std::string& notification_id) override;
void ClickOnNotificationButton(const std::string& notification_id,
int button_index) override;
protected:
const gfx::Image CreateTestImage(int width, int height) {
return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height));
}
const SkBitmap CreateBitmap(int width, int height) {
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
bitmap.eraseRGB(0, 255, 0);
return bitmap;
}
std::vector<ButtonInfo> CreateButtons(int number) {
ButtonInfo info(base::ASCIIToUTF16("Test button title."));
info.icon = CreateTestImage(4, 4);
return std::vector<ButtonInfo>(number, info);
}
void CheckVerticalOrderInNotification() {
std::vector<views::View*> vertical_order;
vertical_order.push_back(notification_view()->top_view_);
vertical_order.push_back(notification_view()->image_view_);
std::copy(notification_view()->action_buttons_.begin(),
notification_view()->action_buttons_.end(),
std::back_inserter(vertical_order));
std::vector<views::View*>::iterator current = vertical_order.begin();
std::vector<views::View*>::iterator last = current++;
while (current != vertical_order.end()) {
gfx::Point last_point = (*last)->bounds().origin();
views::View::ConvertPointToTarget(
(*last), notification_view(), &last_point);
gfx::Point current_point = (*current)->bounds().origin();
views::View::ConvertPointToTarget(
(*current), notification_view(), ¤t_point);
EXPECT_LT(last_point.y(), current_point.y());
last = current++;
}
}
void UpdateNotificationViews() {
notification_view()->CreateOrUpdateViews(*notification());
notification_view()->Layout();
}
private:
scoped_ptr<RichNotificationData> data_;
scoped_ptr<Notification> notification_;
scoped_ptr<NotificationView> notification_view_;
DISALLOW_COPY_AND_ASSIGN(NotificationViewTest);
};
NotificationViewTest::NotificationViewTest() {
}
NotificationViewTest::~NotificationViewTest() {
}
void NotificationViewTest::SetUp() {
views::ViewsTestBase::SetUp();
// Create a dummy notification.
SkBitmap bitmap;
data_.reset(new RichNotificationData());
notification_.reset(
new Notification(NOTIFICATION_TYPE_BASE_FORMAT,
std::string("notification id"),
base::UTF8ToUTF16("title"),
base::UTF8ToUTF16("message"),
CreateTestImage(80, 80),
base::UTF8ToUTF16("display source"),
NotifierId(NotifierId::APPLICATION, "extension_id"),
*data_,
NULL));
notification_->set_small_image(CreateTestImage(16, 16));
notification_->set_image(CreateTestImage(320, 240));
// Then create a new NotificationView with that single notification.
notification_view_.reset(
NotificationView::Create(this, *notification_, true));
notification_view_->set_owned_by_client();
views::Widget::InitParams init_params(
CreateParams(views::Widget::InitParams::TYPE_POPUP));
views::Widget* widget = new views::Widget();
widget->Init(init_params);
widget->SetContentsView(notification_view_.get());
widget->SetSize(notification_view_->GetPreferredSize());
}
void NotificationViewTest::TearDown() {
widget()->Close();
notification_view_.reset();
views::ViewsTestBase::TearDown();
}
void NotificationViewTest::ClickOnNotification(
const std::string& notification_id) {
// For this test, this method should not be invoked.
NOTREACHED();
}
void NotificationViewTest::RemoveNotification(
const std::string& notification_id,
bool by_user) {
// For this test, this method should not be invoked.
NOTREACHED();
}
scoped_ptr<ui::MenuModel> NotificationViewTest::CreateMenuModel(
const NotifierId& notifier_id,
const base::string16& display_source) {
// For this test, this method should not be invoked.
NOTREACHED();
return scoped_ptr<ui::MenuModel>();
}
bool NotificationViewTest::HasClickedListener(
const std::string& notification_id) {
return true;
}
void NotificationViewTest::ClickOnNotificationButton(
const std::string& notification_id,
int button_index) {
// For this test, this method should not be invoked.
NOTREACHED();
}
/* Unit tests *****************************************************************/
TEST_F(NotificationViewTest, CreateOrUpdateTest) {
EXPECT_TRUE(NULL != notification_view()->title_view_);
EXPECT_TRUE(NULL != notification_view()->message_view_);
EXPECT_TRUE(NULL != notification_view()->icon_view_);
EXPECT_TRUE(NULL != notification_view()->image_view_);
notification()->set_image(gfx::Image());
notification()->set_title(base::ASCIIToUTF16(""));
notification()->set_message(base::ASCIIToUTF16(""));
notification()->set_icon(gfx::Image());
notification_view()->CreateOrUpdateViews(*notification());
EXPECT_TRUE(NULL == notification_view()->title_view_);
EXPECT_TRUE(NULL == notification_view()->message_view_);
EXPECT_TRUE(NULL == notification_view()->image_view_);
// We still expect an icon view for all layouts.
EXPECT_TRUE(NULL != notification_view()->icon_view_);
}
TEST_F(NotificationViewTest, TestLineLimits) {
notification()->set_image(CreateTestImage(0, 0));
notification()->set_context_message(base::ASCIIToUTF16(""));
notification_view()->CreateOrUpdateViews(*notification());
EXPECT_EQ(5, notification_view()->GetMessageLineLimit(0, 360));
EXPECT_EQ(5, notification_view()->GetMessageLineLimit(1, 360));
EXPECT_EQ(3, notification_view()->GetMessageLineLimit(2, 360));
notification()->set_image(CreateTestImage(2, 2));
notification_view()->CreateOrUpdateViews(*notification());
EXPECT_EQ(2, notification_view()->GetMessageLineLimit(0, 360));
EXPECT_EQ(2, notification_view()->GetMessageLineLimit(1, 360));
EXPECT_EQ(1, notification_view()->GetMessageLineLimit(2, 360));
notification()->set_context_message(base::UTF8ToUTF16("foo"));
notification_view()->CreateOrUpdateViews(*notification());
EXPECT_TRUE(notification_view()->context_message_view_ != NULL);
EXPECT_EQ(1, notification_view()->GetMessageLineLimit(0, 360));
EXPECT_EQ(1, notification_view()->GetMessageLineLimit(1, 360));
EXPECT_EQ(0, notification_view()->GetMessageLineLimit(2, 360));
}
TEST_F(NotificationViewTest, UpdateButtonsStateTest) {
notification()->set_buttons(CreateButtons(2));
notification_view()->CreateOrUpdateViews(*notification());
widget()->Show();
EXPECT_EQ(views::CustomButton::STATE_NORMAL,
notification_view()->action_buttons_[0]->state());
// Now construct a mouse move event 1 pixel inside the boundary of the action
// button.
gfx::Point cursor_location(1, 1);
views::View::ConvertPointToWidget(notification_view()->action_buttons_[0],
&cursor_location);
ui::MouseEvent move(ui::ET_MOUSE_MOVED,
cursor_location,
cursor_location,
ui::EF_NONE,
ui::EF_NONE);
widget()->OnMouseEvent(&move);
EXPECT_EQ(views::CustomButton::STATE_HOVERED,
notification_view()->action_buttons_[0]->state());
notification_view()->CreateOrUpdateViews(*notification());
EXPECT_EQ(views::CustomButton::STATE_HOVERED,
notification_view()->action_buttons_[0]->state());
// Now construct a mouse move event 1 pixel outside the boundary of the
// widget.
cursor_location = gfx::Point(-1, -1);
move = ui::MouseEvent(ui::ET_MOUSE_MOVED,
cursor_location,
cursor_location,
ui::EF_NONE,
ui::EF_NONE);
widget()->OnMouseEvent(&move);
EXPECT_EQ(views::CustomButton::STATE_NORMAL,
notification_view()->action_buttons_[0]->state());
}
TEST_F(NotificationViewTest, UpdateButtonCountTest) {
notification()->set_buttons(CreateButtons(2));
notification_view()->CreateOrUpdateViews(*notification());
widget()->Show();
EXPECT_EQ(views::CustomButton::STATE_NORMAL,
notification_view()->action_buttons_[0]->state());
EXPECT_EQ(views::CustomButton::STATE_NORMAL,
notification_view()->action_buttons_[1]->state());
// Now construct a mouse move event 1 pixel inside the boundary of the action
// button.
gfx::Point cursor_location(1, 1);
views::View::ConvertPointToWidget(notification_view()->action_buttons_[0],
&cursor_location);
ui::MouseEvent move(ui::ET_MOUSE_MOVED,
cursor_location,
cursor_location,
ui::EF_NONE,
ui::EF_NONE);
widget()->OnMouseEvent(&move);
EXPECT_EQ(views::CustomButton::STATE_HOVERED,
notification_view()->action_buttons_[0]->state());
EXPECT_EQ(views::CustomButton::STATE_NORMAL,
notification_view()->action_buttons_[1]->state());
notification()->set_buttons(CreateButtons(1));
notification_view()->CreateOrUpdateViews(*notification());
EXPECT_EQ(views::CustomButton::STATE_HOVERED,
notification_view()->action_buttons_[0]->state());
EXPECT_EQ(1u, notification_view()->action_buttons_.size());
// Now construct a mouse move event 1 pixel outside the boundary of the
// widget.
cursor_location = gfx::Point(-1, -1);
move = ui::MouseEvent(ui::ET_MOUSE_MOVED,
cursor_location,
cursor_location,
ui::EF_NONE,
ui::EF_NONE);
widget()->OnMouseEvent(&move);
EXPECT_EQ(views::CustomButton::STATE_NORMAL,
notification_view()->action_buttons_[0]->state());
}
TEST_F(NotificationViewTest, ViewOrderingTest) {
// Tests that views are created in the correct vertical order.
notification()->set_buttons(CreateButtons(2));
// Layout the initial views.
UpdateNotificationViews();
// Double-check that vertical order is correct.
CheckVerticalOrderInNotification();
// Tests that views remain in that order even after an update.
UpdateNotificationViews();
CheckVerticalOrderInNotification();
}
} // namespace message_center
|