| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 GET_HEIGHT_FOR_WIDTH, | 24 GET_HEIGHT_FOR_WIDTH, |
| 25 LAYOUT | 25 LAYOUT |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 /* Instrumented/Mock NotificationView subclass ********************************/ | 28 /* Instrumented/Mock NotificationView subclass ********************************/ |
| 29 | 29 |
| 30 class MockNotificationView : public NotificationView { | 30 class MockNotificationView : public NotificationView { |
| 31 public: | 31 public: |
| 32 class Test { | 32 class Test { |
| 33 public: | 33 public: |
| 34 virtual void RegisterCall(int receiver_id, CallType type) = 0; | 34 virtual void RegisterCall(CallType type) = 0; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 explicit MockNotificationView(const Notification& notification, | 37 explicit MockNotificationView(const Notification& notification, |
| 38 MessageCenter* message_center, | 38 MessageCenter* message_center, |
| 39 Test* test, | 39 Test* test); |
| 40 int view_id); | |
| 41 virtual ~MockNotificationView(); | 40 virtual ~MockNotificationView(); |
| 42 | 41 |
| 43 virtual gfx::Size GetPreferredSize() OVERRIDE; | 42 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 44 virtual int GetHeightForWidth(int w) OVERRIDE; | 43 virtual int GetHeightForWidth(int w) OVERRIDE; |
| 45 virtual void Layout() OVERRIDE; | 44 virtual void Layout() OVERRIDE; |
| 46 | 45 |
| 47 int get_id() { return id_; }; | |
| 48 | |
| 49 private: | 46 private: |
| 50 void RegisterCall(CallType type); | |
| 51 | |
| 52 Test* test_; | 47 Test* test_; |
| 53 int id_; | |
| 54 | 48 |
| 55 DISALLOW_COPY_AND_ASSIGN(MockNotificationView); | 49 DISALLOW_COPY_AND_ASSIGN(MockNotificationView); |
| 56 }; | 50 }; |
| 57 | 51 |
| 58 MockNotificationView::MockNotificationView(const Notification& notification, | 52 MockNotificationView::MockNotificationView(const Notification& notification, |
| 59 MessageCenter* message_center, | 53 MessageCenter* message_center, |
| 60 Test* test, | 54 Test* test) |
| 61 int view_id) | |
| 62 : NotificationView(notification, message_center, NULL, true), | 55 : NotificationView(notification, message_center, NULL, true), |
| 63 test_(test), | 56 test_(test) { |
| 64 id_(view_id) { | |
| 65 } | 57 } |
| 66 | 58 |
| 67 MockNotificationView::~MockNotificationView() { | 59 MockNotificationView::~MockNotificationView() { |
| 68 } | 60 } |
| 69 | 61 |
| 70 gfx::Size MockNotificationView::GetPreferredSize() { | 62 gfx::Size MockNotificationView::GetPreferredSize() { |
| 71 RegisterCall(GET_PREFERRED_SIZE); | 63 test_->RegisterCall(GET_PREFERRED_SIZE); |
| 72 return child_count() ? NotificationView::GetPreferredSize() : | 64 DCHECK(child_count() > 0); |
| 73 gfx::Size(id_, id_); | 65 return NotificationView::GetPreferredSize(); |
| 74 } | 66 } |
| 75 | 67 |
| 76 int MockNotificationView::GetHeightForWidth(int width) { | 68 int MockNotificationView::GetHeightForWidth(int width) { |
| 77 RegisterCall(GET_HEIGHT_FOR_WIDTH); | 69 test_->RegisterCall(GET_HEIGHT_FOR_WIDTH); |
| 78 return child_count() ? NotificationView::GetHeightForWidth(width) : (id_); | 70 DCHECK(child_count() > 0); |
| 71 return NotificationView::GetHeightForWidth(width); |
| 79 } | 72 } |
| 80 | 73 |
| 81 void MockNotificationView::Layout() { | 74 void MockNotificationView::Layout() { |
| 82 RegisterCall(LAYOUT); | 75 test_->RegisterCall(LAYOUT); |
| 83 if (child_count()) | 76 DCHECK(child_count() > 0); |
| 84 NotificationView::Layout(); | 77 NotificationView::Layout(); |
| 85 } | |
| 86 | |
| 87 void MockNotificationView::RegisterCall(CallType type) { | |
| 88 if (test_) | |
| 89 test_->RegisterCall(id_, type); | |
| 90 } | 78 } |
| 91 | 79 |
| 92 /* Test fixture ***************************************************************/ | 80 /* Test fixture ***************************************************************/ |
| 93 | 81 |
| 94 class MessageCenterViewTest : public testing::Test, | 82 class MessageCenterViewTest : public testing::Test, |
| 95 public MockNotificationView::Test { | 83 public MockNotificationView::Test { |
| 96 public: | 84 public: |
| 97 MessageCenterViewTest(); | 85 MessageCenterViewTest(); |
| 98 virtual ~MessageCenterViewTest(); | 86 virtual ~MessageCenterViewTest(); |
| 99 | 87 |
| 100 virtual void SetUp() OVERRIDE; | 88 virtual void SetUp() OVERRIDE; |
| 101 virtual void TearDown() OVERRIDE; | 89 virtual void TearDown() OVERRIDE; |
| 102 | 90 |
| 103 MessageCenterView* GetMessageCenterView(); | 91 MessageCenterView* GetMessageCenterView(); |
| 104 int GetNotificationCount(); | 92 int GetNotificationCount(); |
| 105 int GetCallCount(CallType type); | 93 int GetCallCount(CallType type); |
| 106 | 94 |
| 107 virtual void RegisterCall(int receiver_id, CallType type) OVERRIDE; | 95 virtual void RegisterCall(CallType type) OVERRIDE; |
| 108 | 96 |
| 109 void LogBounds(int depth, views::View* view); | 97 void LogBounds(int depth, views::View* view); |
| 110 | 98 |
| 111 private: | 99 private: |
| 112 views::View* MakeParent(views::View* child1, views::View* child2); | 100 views::View* MakeParent(views::View* child1, views::View* child2); |
| 113 | 101 |
| 114 | 102 |
| 115 scoped_ptr<MessageCenterView> message_center_view_; | 103 scoped_ptr<MessageCenterView> message_center_view_; |
| 116 FakeMessageCenter message_center_; | 104 FakeMessageCenter message_center_; |
| 117 std::map<CallType,int> callCounts_; | 105 std::map<CallType,int> callCounts_; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 143 | 131 |
| 144 // Then create a new MessageCenterView with that single notification. | 132 // Then create a new MessageCenterView with that single notification. |
| 145 message_center_view_.reset(new MessageCenterView( | 133 message_center_view_.reset(new MessageCenterView( |
| 146 &message_center_, NULL, 100, false, /*top_down =*/false)); | 134 &message_center_, NULL, 100, false, /*top_down =*/false)); |
| 147 message_center_view_->SetNotifications(notifications); | 135 message_center_view_->SetNotifications(notifications); |
| 148 | 136 |
| 149 // Remove and delete the NotificationView now owned by the MessageCenterView's | 137 // Remove and delete the NotificationView now owned by the MessageCenterView's |
| 150 // MessageListView and replace it with an instrumented MockNotificationView | 138 // MessageListView and replace it with an instrumented MockNotificationView |
| 151 // that will become owned by the MessageListView. | 139 // that will become owned by the MessageListView. |
| 152 MockNotificationView* mock; | 140 MockNotificationView* mock; |
| 153 mock = new MockNotificationView(notification, &message_center_, this, 42); | 141 mock = new MockNotificationView(notification, &message_center_, this); |
| 154 message_center_view_->message_views_.push_back(mock); | 142 message_center_view_->notification_views_[notification.id()] = mock; |
| 155 message_center_view_->SetNotificationViewForTest(mock); | 143 message_center_view_->SetNotificationViewForTest(mock); |
| 156 } | 144 } |
| 157 | 145 |
| 158 void MessageCenterViewTest::TearDown() { | 146 void MessageCenterViewTest::TearDown() { |
| 159 message_center_view_.reset(); | 147 message_center_view_.reset(); |
| 160 } | 148 } |
| 161 | 149 |
| 162 MessageCenterView* MessageCenterViewTest::GetMessageCenterView() { | 150 MessageCenterView* MessageCenterViewTest::GetMessageCenterView() { |
| 163 return message_center_view_.get(); | 151 return message_center_view_.get(); |
| 164 } | 152 } |
| 165 | 153 |
| 166 int MessageCenterViewTest::GetNotificationCount() { | 154 int MessageCenterViewTest::GetNotificationCount() { |
| 167 return 1; | 155 return 1; |
| 168 } | 156 } |
| 169 | 157 |
| 170 int MessageCenterViewTest::GetCallCount(CallType type) { | 158 int MessageCenterViewTest::GetCallCount(CallType type) { |
| 171 return callCounts_[type]; | 159 return callCounts_[type]; |
| 172 } | 160 } |
| 173 | 161 |
| 174 void MessageCenterViewTest::RegisterCall(int receiver_id, CallType type) { | 162 void MessageCenterViewTest::RegisterCall(CallType type) { |
| 175 callCounts_[type] += 1; | 163 callCounts_[type] += 1; |
| 176 } | 164 } |
| 177 | 165 |
| 178 void MessageCenterViewTest::LogBounds(int depth, views::View* view) { | 166 void MessageCenterViewTest::LogBounds(int depth, views::View* view) { |
| 179 string16 inset; | 167 string16 inset; |
| 180 for (int i = 0; i < depth; ++i) | 168 for (int i = 0; i < depth; ++i) |
| 181 inset.append(UTF8ToUTF16(" ")); | 169 inset.append(UTF8ToUTF16(" ")); |
| 182 gfx::Rect bounds = view->bounds(); | 170 gfx::Rect bounds = view->bounds(); |
| 183 DVLOG(0) << inset << bounds.width() << " x " << bounds.height() | 171 DVLOG(0) << inset << bounds.width() << " x " << bounds.height() |
| 184 << " @ " << bounds.x() << ", " << bounds.y(); | 172 << " @ " << bounds.x() << ", " << bounds.y(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 197 // GetHeightForWidth() calls per descendant NotificationView. 20 is a very | 185 // GetHeightForWidth() calls per descendant NotificationView. 20 is a very |
| 198 // large number corresponding to the current reality. That number will be | 186 // large number corresponding to the current reality. That number will be |
| 199 // ratcheted down over time as the code improves. | 187 // ratcheted down over time as the code improves. |
| 200 EXPECT_LE(GetCallCount(LAYOUT), GetNotificationCount() * 2); | 188 EXPECT_LE(GetCallCount(LAYOUT), GetNotificationCount() * 2); |
| 201 EXPECT_LE(GetCallCount(GET_PREFERRED_SIZE) + | 189 EXPECT_LE(GetCallCount(GET_PREFERRED_SIZE) + |
| 202 GetCallCount(GET_HEIGHT_FOR_WIDTH), | 190 GetCallCount(GET_HEIGHT_FOR_WIDTH), |
| 203 GetNotificationCount() * 20); | 191 GetNotificationCount() * 20); |
| 204 } | 192 } |
| 205 | 193 |
| 206 } // namespace message_center | 194 } // namespace message_center |
| OLD | NEW |