OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 <vector> |
| 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "base/stl_util-inl.h" |
| 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/browser_thread.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/browser/sidebar/sidebar_container.h" |
| 14 #include "chrome/browser/sidebar/sidebar_model.h" |
| 15 #include "chrome/browser/tab_contents/tab_contents.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 17 #include "chrome/browser/ui/views/sidebar/browser_sidebar_tab_strip_controller.h
" |
| 18 #include "chrome/browser/ui/views/sidebar/sidebar_base_tab_strip.h" |
| 19 #include "chrome/browser/ui/views/sidebar/sidebar_tab_strip_controller.h" |
| 20 #include "chrome/browser/ui/views/sidebar/sidebar_tab_test_helper.h" |
| 21 #include "chrome/test/testing_profile.h" |
| 22 #include "ipc/ipc_message.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 using testing::_; |
| 27 using testing::AnyNumber; |
| 28 using testing::Exactly; |
| 29 using testing::InSequence; |
| 30 using testing::Return; |
| 31 |
| 32 namespace { |
| 33 |
| 34 // Fake content ids. |
| 35 const char kContentId1[] = "1"; |
| 36 const char kContentId2[] = "12"; |
| 37 const char kContentId3[] = "13"; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 // Test harness for BrowserSidebarTabStripController tests. |
| 42 // Creates all necessary mocks and real objects and binds them together |
| 43 // to create a simulated environment for the controller under test. |
| 44 class BrowserSidebarTabStripControllerTest : public testing::Test { |
| 45 public: |
| 46 BrowserSidebarTabStripControllerTest() |
| 47 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 48 } |
| 49 |
| 50 virtual void SetUp() { |
| 51 profile_.reset(new TestingProfile()); |
| 52 tabs_.push_back(new TabContentsWrapper( |
| 53 new TabContents(profile_.get(), NULL, MSG_ROUTING_NONE, NULL, NULL))); |
| 54 tabs_.push_back(new TabContentsWrapper( |
| 55 new TabContents(profile_.get(), NULL, MSG_ROUTING_NONE, NULL, NULL))); |
| 56 tabs_.push_back(new TabContentsWrapper( |
| 57 new TabContents(profile_.get(), NULL, MSG_ROUTING_NONE, NULL, NULL))); |
| 58 |
| 59 // Create all mocks and build the ownership graph. |
| 60 model_.reset(new MockSidebarModel()); |
| 61 delegate_.reset(new MockSidebarTabStripControllerDelegate()); |
| 62 |
| 63 controller_ = |
| 64 new BrowserSidebarTabStripController(model_.get(), delegate_.get()); |
| 65 |
| 66 tabstrip_.reset(new MockSidebarBaseTabStrip(controller_)); |
| 67 |
| 68 controller_->set_tab_strip(tabstrip_.get()); |
| 69 } |
| 70 |
| 71 virtual void TearDown() { |
| 72 tabstrip_.reset(); |
| 73 delegate_.reset(); |
| 74 model_.reset(); |
| 75 |
| 76 STLDeleteContainerPointers(tabs_.begin(), tabs_.end()); |
| 77 profile_.reset(); |
| 78 } |
| 79 |
| 80 protected: |
| 81 // Accessors for private members. |
| 82 size_t GetControllerModelCount() const { |
| 83 return controller_->model_index_.size(); |
| 84 } |
| 85 |
| 86 // Mocks accessible from tests. |
| 87 scoped_ptr<MockSidebarModel> model_; |
| 88 scoped_ptr<MockSidebarBaseTabStrip> tabstrip_; |
| 89 // Owned by |tabstrip_|. |
| 90 BrowserSidebarTabStripController* controller_; |
| 91 // Almost real tabs accessible from tests. |
| 92 std::vector<TabContentsWrapper*> tabs_; |
| 93 |
| 94 private: |
| 95 scoped_ptr<Profile> profile_; |
| 96 scoped_ptr<MockSidebarTabStripControllerDelegate> delegate_; |
| 97 |
| 98 MessageLoopForUI message_loop_; |
| 99 BrowserThread ui_thread_; |
| 100 }; |
| 101 |
| 102 TEST_F(BrowserSidebarTabStripControllerTest, EmptyModel) { |
| 103 // Check that nothing extra happens when model is empty, these are the only |
| 104 // calls we expect to receive. |
| 105 EXPECT_CALL(*model_, GetActiveSidebarContainerFor(_)) |
| 106 .Times(AnyNumber()) |
| 107 .WillRepeatedly(Return(static_cast<SidebarContainer*>(NULL))); |
| 108 EXPECT_CALL(*tabstrip_, SetToIdealBounds()) |
| 109 .Times(AnyNumber()); |
| 110 |
| 111 // Check preconditions. |
| 112 EXPECT_EQ(0, tabstrip_->tab_count()); |
| 113 EXPECT_EQ(0, GetControllerModelCount()); |
| 114 |
| 115 // Emulate tab switches. |
| 116 TabContentsWrapper* tab_switch_sequence[] = { |
| 117 tabs_[0], tabs_[1], NULL |
| 118 }; |
| 119 for (int i = 0; i < arraysize(tab_switch_sequence); ++i) { |
| 120 controller_->UpdateTabs(tab_switch_sequence[i]); |
| 121 // Nothing should change. |
| 122 EXPECT_EQ(0, GetControllerModelCount()); |
| 123 } |
| 124 |
| 125 // Check postconditions. |
| 126 EXPECT_EQ(0, tabstrip_->tab_count()); |
| 127 EXPECT_EQ(0, GetControllerModelCount()); |
| 128 } |
| 129 |
| 130 TEST_F(BrowserSidebarTabStripControllerTest, UpdateTabsOnTabChange) { |
| 131 // Set up sidebars for tabs. |
| 132 model_->AddSidebarContainer( |
| 133 new SidebarContainer(tabs_[0], kContentId1, NULL)); |
| 134 model_->AddSidebarContainer( |
| 135 new SidebarContainer(tabs_[1], kContentId1, NULL)); |
| 136 model_->AddSidebarContainer( |
| 137 new SidebarContainer(tabs_[1], kContentId2, NULL)); |
| 138 model_->AddSidebarContainer( |
| 139 new SidebarContainer(tabs_[1], kContentId3, NULL)); |
| 140 model_->AddSidebarContainer( |
| 141 new SidebarContainer(tabs_[2], kContentId2, NULL)); |
| 142 |
| 143 // Set up expectations. |
| 144 EXPECT_CALL(*model_, GetActiveSidebarContainerFor(_)) |
| 145 .Times(AnyNumber()) |
| 146 .WillRepeatedly(Return(static_cast<SidebarContainer*>(NULL))); |
| 147 EXPECT_CALL(*tabstrip_, SetToIdealBounds()) |
| 148 .Times(AnyNumber()); |
| 149 |
| 150 // The following calls should happen in this order to make sure |
| 151 // tab views are created and removed only when necessary (as opposed |
| 152 // to remove all views and recreate them on every UpdateTabs call). |
| 153 { |
| 154 InSequence sequence; |
| 155 |
| 156 // No animation is supposed to be requested on tab switch, hence |
| 157 // false as a second parameter for all calls. |
| 158 // The only tab for tabs_[0] gets added. |
| 159 EXPECT_CALL(*tabstrip_, AddTabAt(0, false, _)); |
| 160 // Two tabs for tabs_[1] are added, the first one is reused. |
| 161 EXPECT_CALL(*tabstrip_, AddTabAt(1, false, _)); |
| 162 EXPECT_CALL(*tabstrip_, AddTabAt(2, false, _)); |
| 163 // The first and third tabs are removed. |
| 164 EXPECT_CALL(*tabstrip_, RemoveTabAt(0, false)); |
| 165 EXPECT_CALL(*tabstrip_, RemoveTabAt(1, false)); |
| 166 // The first tab is inserted, second one is removed |
| 167 // on tabs_[2] -> tabs_[0] switch. |
| 168 EXPECT_CALL(*tabstrip_, AddTabAt(0, false, _)); |
| 169 EXPECT_CALL(*tabstrip_, RemoveTabAt(1, false)); |
| 170 // The last tab is removed on closing all tabs (switching to NULL). |
| 171 EXPECT_CALL(*tabstrip_, RemoveTabAt(0, false)); |
| 172 } |
| 173 |
| 174 // Emulate tab switches, check that a number of sidebars defined |
| 175 // for the tab match a number of sidebar mini tabs in the controller's model. |
| 176 TabContentsWrapper* tab_switch_sequence[] = { |
| 177 tabs_[0], // kContentId1 |
| 178 tabs_[1], // kContentId1 | kContentId2 | kContentId3 |
| 179 tabs_[2], // kContentId2 |
| 180 tabs_[0], // kContentId1 |
| 181 NULL |
| 182 }; |
| 183 for (int i = 0; i < arraysize(tab_switch_sequence); ++i) { |
| 184 controller_->UpdateTabs(tab_switch_sequence[i]); |
| 185 |
| 186 EXPECT_EQ(model_->GetAllSidebarsFor(tab_switch_sequence[i]).size(), |
| 187 GetControllerModelCount()); |
| 188 } |
| 189 |
| 190 // Check postconditions. |
| 191 EXPECT_EQ(0, tabstrip_->tab_count()); |
| 192 EXPECT_EQ(0, GetControllerModelCount()); |
| 193 } |
| 194 |
| 195 TEST_F(BrowserSidebarTabStripControllerTest, UpdateState) { |
| 196 // Set up sidebars for tabs. |
| 197 model_->AddSidebarContainer( |
| 198 new SidebarContainer(tabs_[0], kContentId1, NULL)); |
| 199 model_->AddSidebarContainer( |
| 200 new SidebarContainer(tabs_[0], kContentId2, NULL)); |
| 201 model_->AddSidebarContainer( |
| 202 new SidebarContainer(tabs_[0], kContentId3, NULL)); |
| 203 |
| 204 // Set up expectations. |
| 205 EXPECT_CALL(*model_, GetActiveSidebarContainerFor(_)) |
| 206 .Times(AnyNumber()) |
| 207 .WillRepeatedly(Return(static_cast<SidebarContainer*>(NULL))); |
| 208 EXPECT_CALL(*tabstrip_, SetToIdealBounds()) |
| 209 .Times(AnyNumber()); |
| 210 { |
| 211 InSequence sequence; |
| 212 |
| 213 EXPECT_CALL(*tabstrip_, AddTabAt(0, false, _)); |
| 214 EXPECT_CALL(*tabstrip_, AddTabAt(1, false, _)); |
| 215 EXPECT_CALL(*tabstrip_, AddTabAt(2, false, _)); |
| 216 |
| 217 // Updating tab with model_index 1. |
| 218 EXPECT_CALL(*tabstrip_, SetTabData(1, _)); |
| 219 } |
| 220 |
| 221 // Create tabs. |
| 222 controller_->UpdateTabs(tabs_[0]); |
| 223 |
| 224 // Update second tab state. |
| 225 controller_->UpdateState( |
| 226 model_->GetSidebarContainerFor(tabs_[0], kContentId2)); |
| 227 } |
| 228 |
| 229 TEST_F(BrowserSidebarTabStripControllerTest, IsTabSelected) { |
| 230 // Set up sidebars for tabs. |
| 231 model_->AddSidebarContainer( |
| 232 new SidebarContainer(tabs_[0], kContentId1, NULL)); |
| 233 model_->AddSidebarContainer( |
| 234 new SidebarContainer(tabs_[0], kContentId2, NULL)); |
| 235 |
| 236 // Set up expectations. |
| 237 EXPECT_CALL(*tabstrip_, SetToIdealBounds()) |
| 238 .Times(AnyNumber()); |
| 239 { |
| 240 InSequence sequence; |
| 241 |
| 242 EXPECT_CALL(*model_, GetActiveSidebarContainerFor(_)) |
| 243 .WillOnce(Return(static_cast<SidebarContainer*>(NULL))); |
| 244 EXPECT_CALL(*tabstrip_, AddTabAt(0, false, _)); |
| 245 EXPECT_CALL(*tabstrip_, AddTabAt(1, false, _)); |
| 246 |
| 247 // No active sidebar during first two checks. |
| 248 EXPECT_CALL(*model_, GetActiveSidebarContainerFor(_)) |
| 249 .Times(2) |
| 250 .WillRepeatedly(Return(static_cast<SidebarContainer*>(NULL))); |
| 251 // Second sidebar is active now. |
| 252 EXPECT_CALL(*model_, GetActiveSidebarContainerFor(_)) |
| 253 .Times(2) |
| 254 .WillRepeatedly(Return( |
| 255 model_->GetSidebarContainerFor(tabs_[0], kContentId2))); |
| 256 } |
| 257 |
| 258 // Create tabs. |
| 259 controller_->UpdateTabs(tabs_[0]); |
| 260 |
| 261 // Verify that no tab is selected. |
| 262 EXPECT_FALSE(controller_->IsTabSelected(0)); |
| 263 EXPECT_FALSE(controller_->IsTabSelected(1)); |
| 264 |
| 265 // Now, according to the expectations set earlier (see above), |
| 266 // the second tab is selected, check again. |
| 267 EXPECT_FALSE(controller_->IsTabSelected(0)); |
| 268 EXPECT_TRUE(controller_->IsTabSelected(1)); |
| 269 } |
| 270 |
| 271 TEST_F(BrowserSidebarTabStripControllerTest, SelectTab) { |
| 272 // Set up sidebars for tabs. |
| 273 model_->AddSidebarContainer( |
| 274 new SidebarContainer(tabs_[0], kContentId1, NULL)); |
| 275 model_->AddSidebarContainer( |
| 276 new SidebarContainer(tabs_[0], kContentId2, NULL)); |
| 277 |
| 278 // Set up expectations. |
| 279 EXPECT_CALL(*model_, GetActiveSidebarContainerFor(_)) |
| 280 .Times(AnyNumber()) |
| 281 .WillRepeatedly(Return(static_cast<SidebarContainer*>(NULL))); |
| 282 EXPECT_CALL(*tabstrip_, SetToIdealBounds()) |
| 283 .Times(AnyNumber()); |
| 284 { |
| 285 InSequence sequence; |
| 286 |
| 287 EXPECT_CALL(*tabstrip_, AddTabAt(0, false, _)); |
| 288 EXPECT_CALL(*tabstrip_, AddTabAt(1, false, _)); |
| 289 |
| 290 EXPECT_CALL(*model_, ToggleSidebar(tabs_[0], kContentId2)); |
| 291 EXPECT_CALL(*model_, ToggleSidebar(tabs_[0], kContentId1)); |
| 292 } |
| 293 |
| 294 // Create tabs and emulate tab clicks. |
| 295 controller_->UpdateTabs(tabs_[0]); |
| 296 |
| 297 controller_->SelectTab(1); |
| 298 controller_->SelectTab(0); |
| 299 } |
| 300 |
OLD | NEW |