OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/message_center/views/message_popup_collection.h" | 5 #include "ui/message_center/views/message_popup_collection.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
12 #include "ui/message_center/fake_message_center.h" | 14 #include "ui/message_center/fake_message_center.h" |
13 #include "ui/views/test/views_test_base.h" | 15 #include "ui/views/test/views_test_base.h" |
14 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
15 | 17 |
16 namespace message_center { | 18 namespace message_center { |
17 namespace test { | 19 namespace test { |
18 | 20 |
19 class MessagePopupCollectionTest : public views::ViewsTestBase { | 21 class MessagePopupCollectionTest : public views::ViewsTestBase { |
20 public: | 22 public: |
21 virtual void SetUp() OVERRIDE { | 23 virtual void SetUp() OVERRIDE { |
22 views::ViewsTestBase::SetUp(); | 24 views::ViewsTestBase::SetUp(); |
| 25 MessageCenter::Initialize(); |
| 26 collection_.reset( |
| 27 new MessagePopupCollection(GetContext(), MessageCenter::Get())); |
| 28 collection_->SetWorkAreaForTest(gfx::Rect(0, 0, 1280, 1024)); |
| 29 id_ = 0; |
| 30 } |
| 31 |
| 32 virtual void TearDown() OVERRIDE { |
| 33 collection_->CloseAllWidgets(); |
| 34 collection_.reset(); |
| 35 MessageCenter::Shutdown(); |
| 36 views::ViewsTestBase::TearDown(); |
| 37 } |
| 38 |
| 39 protected: |
| 40 size_t GetToastCounts() { |
| 41 return collection_->toasts_.size(); |
| 42 } |
| 43 |
| 44 bool IsToastShown(const std::string& id) { |
| 45 views::Widget* widget = collection_->GetWidgetForId(id); |
| 46 return widget && widget->IsVisible(); |
| 47 } |
| 48 |
| 49 std::string AddNotification() { |
| 50 std::string id = base::IntToString(id_++); |
| 51 MessageCenter::Get()->AddNotification( |
| 52 NOTIFICATION_TYPE_BASE_FORMAT, id, UTF8ToUTF16("test title"), |
| 53 UTF8ToUTF16("test message"), string16() /* display_source */, |
| 54 "" /* extension_id */, NULL); |
| 55 return id; |
| 56 } |
| 57 |
| 58 private: |
| 59 scoped_ptr<MessagePopupCollection> collection_; |
| 60 int id_; |
| 61 }; |
| 62 |
| 63 class MessagePopupCollectionWidgetsTest : public views::ViewsTestBase { |
| 64 public: |
| 65 virtual void SetUp() OVERRIDE { |
| 66 views::ViewsTestBase::SetUp(); |
23 collection_.reset(new MessagePopupCollection(NULL, &message_center_)); | 67 collection_.reset(new MessagePopupCollection(NULL, &message_center_)); |
24 collection_->SetWorkAreaForTest(gfx::Rect(0, 0, 200, 200)); | 68 collection_->SetWorkAreaForTest(gfx::Rect(0, 0, 200, 200)); |
25 } | 69 } |
26 | 70 |
27 virtual void TearDown() OVERRIDE { | 71 virtual void TearDown() OVERRIDE { |
28 collection_->CloseAllWidgets(); | 72 collection_->CloseAllWidgets(); |
29 views::ViewsTestBase::TearDown(); | 73 views::ViewsTestBase::TearDown(); |
30 } | 74 } |
31 | 75 |
32 protected: | 76 protected: |
(...skipping 29 matching lines...) Expand all Loading... |
62 return (*iter)->GetWindowBoundsInScreen(); | 106 return (*iter)->GetWindowBoundsInScreen(); |
63 } | 107 } |
64 return gfx::Rect(); | 108 return gfx::Rect(); |
65 } | 109 } |
66 | 110 |
67 private: | 111 private: |
68 FakeMessageCenter message_center_; | 112 FakeMessageCenter message_center_; |
69 scoped_ptr<MessagePopupCollection> collection_; | 113 scoped_ptr<MessagePopupCollection> collection_; |
70 }; | 114 }; |
71 | 115 |
72 TEST_F(MessagePopupCollectionTest, RepositionWidgets) { | 116 TEST_F(MessagePopupCollectionTest, DismissOnClick) { |
| 117 std::string id1 = AddNotification(); |
| 118 std::string id2 = AddNotification(); |
| 119 EXPECT_EQ(2u, GetToastCounts()); |
| 120 EXPECT_TRUE(IsToastShown(id1)); |
| 121 EXPECT_TRUE(IsToastShown(id2)); |
| 122 |
| 123 MessageCenter::Get()->ClickOnNotification(id2); |
| 124 RunPendingMessages(); |
| 125 EXPECT_EQ(1u, GetToastCounts()); |
| 126 EXPECT_TRUE(IsToastShown(id1)); |
| 127 EXPECT_FALSE(IsToastShown(id2)); |
| 128 |
| 129 MessageCenter::Get()->ClickOnNotificationButton(id1, 0); |
| 130 RunPendingMessages(); |
| 131 EXPECT_EQ(0u, GetToastCounts()); |
| 132 EXPECT_FALSE(IsToastShown(id1)); |
| 133 EXPECT_FALSE(IsToastShown(id2)); |
| 134 } |
| 135 |
| 136 TEST_F(MessagePopupCollectionWidgetsTest, RepositionWidgets) { |
73 std::vector<gfx::Rect> rects; | 137 std::vector<gfx::Rect> rects; |
74 std::list<views::Widget*> widgets; | 138 std::list<views::Widget*> widgets; |
75 rects.push_back(gfx::Rect(0, 0, 10, 10)); | 139 rects.push_back(gfx::Rect(0, 0, 10, 10)); |
76 rects.push_back(gfx::Rect(0, 0, 10, 20)); | 140 rects.push_back(gfx::Rect(0, 0, 10, 20)); |
77 rects.push_back(gfx::Rect(0, 0, 10, 30)); | 141 rects.push_back(gfx::Rect(0, 0, 10, 30)); |
78 rects.push_back(gfx::Rect(0, 0, 10, 40)); | 142 rects.push_back(gfx::Rect(0, 0, 10, 40)); |
79 CreateWidgets(rects); | 143 CreateWidgets(rects); |
80 RepositionWidgets(); | 144 RepositionWidgets(); |
81 | 145 |
82 EXPECT_EQ("0,180 10x10", GetWidgetRectAt(0).ToString()); | 146 EXPECT_EQ("0,180 10x10", GetWidgetRectAt(0).ToString()); |
83 EXPECT_EQ("0,150 10x20", GetWidgetRectAt(1).ToString()); | 147 EXPECT_EQ("0,150 10x20", GetWidgetRectAt(1).ToString()); |
84 EXPECT_EQ("0,110 10x30", GetWidgetRectAt(2).ToString()); | 148 EXPECT_EQ("0,110 10x30", GetWidgetRectAt(2).ToString()); |
85 EXPECT_EQ("0,60 10x40", GetWidgetRectAt(3).ToString()); | 149 EXPECT_EQ("0,60 10x40", GetWidgetRectAt(3).ToString()); |
86 } | 150 } |
87 | 151 |
88 TEST_F(MessagePopupCollectionTest, RepositionWidgetsWithTargetDown) { | 152 TEST_F(MessagePopupCollectionWidgetsTest, RepositionWidgetsWithTargetDown) { |
89 std::vector<gfx::Rect> rects; | 153 std::vector<gfx::Rect> rects; |
90 std::list<views::Widget*> widgets; | 154 std::list<views::Widget*> widgets; |
91 rects.push_back(gfx::Rect(0, 180, 10, 10)); | 155 rects.push_back(gfx::Rect(0, 180, 10, 10)); |
92 rects.push_back(gfx::Rect(0, 150, 10, 20)); | 156 rects.push_back(gfx::Rect(0, 150, 10, 20)); |
93 rects.push_back(gfx::Rect(0, 60, 10, 40)); | 157 rects.push_back(gfx::Rect(0, 60, 10, 40)); |
94 CreateWidgets(rects); | 158 CreateWidgets(rects); |
95 RepositionWidgetsWithTarget(gfx::Rect(0, 110, 10, 30)); | 159 RepositionWidgetsWithTarget(gfx::Rect(0, 110, 10, 30)); |
96 | 160 |
97 EXPECT_EQ("0,180 10x10", GetWidgetRectAt(0).ToString()); | 161 EXPECT_EQ("0,180 10x10", GetWidgetRectAt(0).ToString()); |
98 EXPECT_EQ("0,150 10x20", GetWidgetRectAt(1).ToString()); | 162 EXPECT_EQ("0,150 10x20", GetWidgetRectAt(1).ToString()); |
99 EXPECT_EQ("0,110 10x40", GetWidgetRectAt(2).ToString()); | 163 EXPECT_EQ("0,110 10x40", GetWidgetRectAt(2).ToString()); |
100 } | 164 } |
101 | 165 |
102 TEST_F(MessagePopupCollectionTest, RepositionWidgetsWithTargetDownAll) { | 166 TEST_F(MessagePopupCollectionWidgetsTest, RepositionWidgetsWithTargetDownAll) { |
103 std::vector<gfx::Rect> rects; | 167 std::vector<gfx::Rect> rects; |
104 std::list<views::Widget*> widgets; | 168 std::list<views::Widget*> widgets; |
105 rects.push_back(gfx::Rect(0, 150, 10, 20)); | 169 rects.push_back(gfx::Rect(0, 150, 10, 20)); |
106 rects.push_back(gfx::Rect(0, 110, 10, 30)); | 170 rects.push_back(gfx::Rect(0, 110, 10, 30)); |
107 rects.push_back(gfx::Rect(0, 60, 10, 40)); | 171 rects.push_back(gfx::Rect(0, 60, 10, 40)); |
108 CreateWidgets(rects); | 172 CreateWidgets(rects); |
109 RepositionWidgetsWithTarget(gfx::Rect(0, 180, 10, 10)); | 173 RepositionWidgetsWithTarget(gfx::Rect(0, 180, 10, 10)); |
110 | 174 |
111 EXPECT_EQ("0,180 10x20", GetWidgetRectAt(0).ToString()); | 175 EXPECT_EQ("0,180 10x20", GetWidgetRectAt(0).ToString()); |
112 EXPECT_EQ("0,140 10x30", GetWidgetRectAt(1).ToString()); | 176 EXPECT_EQ("0,140 10x30", GetWidgetRectAt(1).ToString()); |
113 EXPECT_EQ("0,90 10x40", GetWidgetRectAt(2).ToString()); | 177 EXPECT_EQ("0,90 10x40", GetWidgetRectAt(2).ToString()); |
114 } | 178 } |
115 | 179 |
116 TEST_F(MessagePopupCollectionTest, RepositionWidgetsWithTargetUp) { | 180 TEST_F(MessagePopupCollectionWidgetsTest, RepositionWidgetsWithTargetUp) { |
117 std::vector<gfx::Rect> rects; | 181 std::vector<gfx::Rect> rects; |
118 std::list<views::Widget*> widgets; | 182 std::list<views::Widget*> widgets; |
119 rects.push_back(gfx::Rect(0, 180, 10, 10)); | 183 rects.push_back(gfx::Rect(0, 180, 10, 10)); |
120 rects.push_back(gfx::Rect(0, 150, 10, 20)); | 184 rects.push_back(gfx::Rect(0, 150, 10, 20)); |
121 rects.push_back(gfx::Rect(0, 110, 10, 30)); | 185 rects.push_back(gfx::Rect(0, 110, 10, 30)); |
122 CreateWidgets(rects); | 186 CreateWidgets(rects); |
123 RepositionWidgetsWithTarget(gfx::Rect(0, 60, 10, 40)); | 187 RepositionWidgetsWithTarget(gfx::Rect(0, 60, 10, 40)); |
124 | 188 |
125 EXPECT_EQ("0,130 10x10", GetWidgetRectAt(0).ToString()); | 189 EXPECT_EQ("0,130 10x10", GetWidgetRectAt(0).ToString()); |
126 EXPECT_EQ("0,100 10x20", GetWidgetRectAt(1).ToString()); | 190 EXPECT_EQ("0,100 10x20", GetWidgetRectAt(1).ToString()); |
127 EXPECT_EQ("0,60 10x30", GetWidgetRectAt(2).ToString()); | 191 EXPECT_EQ("0,60 10x30", GetWidgetRectAt(2).ToString()); |
128 } | 192 } |
129 | 193 |
130 } // namespace test | 194 } // namespace test |
131 } // namespace message_center | 195 } // namespace message_center |
OLD | NEW |