Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "base/command_line.h" | |
| 6 #include "chrome/browser/ui/browser.h" | |
| 7 #include "chrome/browser/ui/browser_tabstrip.h" | |
| 8 #include "chrome/browser/ui/search/search_delegate.h" | |
| 9 #include "chrome/browser/ui/search/search_model.h" | |
| 10 #include "chrome/browser/ui/search/search_tab_helper.h" | |
| 11 #include "chrome/browser/ui/search/toolbar_search_animator.h" | |
| 12 #include "chrome/browser/ui/search/toolbar_search_animator_observer.h" | |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 14 #include "chrome/common/chrome_switches.h" | |
| 15 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 #include "ui/base/animation/multi_animation.h" | |
| 19 #include "ui/base/animation/slide_animation.h" | |
| 20 | |
| 21 using ::testing::AtLeast; | |
| 22 | |
| 23 namespace chrome { | |
| 24 namespace search { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 class MockObserver : public ToolbarSearchAnimatorObserver { | |
| 29 public: | |
| 30 MockObserver() {} | |
| 31 | |
| 32 MOCK_METHOD0(OnToolbarBackgroundAnimatorProgressed, void()); | |
| 33 MOCK_METHOD1(OnToolbarBackgroundAnimatorCanceled, void(TabContents*)); | |
| 34 MOCK_METHOD0(OnToolbarSeparatorAnimatorProgressed, void()); | |
| 35 MOCK_METHOD0(OnToolbarSeparatorAnimatorCanceled, void()); | |
| 36 | |
| 37 private: | |
| 38 DISALLOW_COPY_AND_ASSIGN(MockObserver); | |
| 39 }; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 class ToolbarSearchAnimatorTestObserver : public ToolbarSearchAnimatorObserver { | |
|
dhollowa
2012/08/01 22:26:23
Please include this in the anonymous namespace.
kuan
2012/08/02 21:54:03
if i do that, i can't friend this and ToolbarSearc
| |
| 44 public: | |
| 45 explicit ToolbarSearchAnimatorTestObserver(ToolbarSearchAnimator* animator) | |
| 46 : animator_(animator), | |
| 47 cancel_halfway_(false) { | |
| 48 } | |
| 49 | |
| 50 virtual void OnToolbarBackgroundAnimatorProgressed() OVERRIDE { | |
| 51 if (cancel_halfway_ && | |
| 52 animator_->background_animation_->GetCurrentValue() > 0.5) { | |
| 53 MessageLoop::current()->Quit(); | |
| 54 return; | |
| 55 } | |
| 56 QuitMessageLoopIfDone(); | |
| 57 } | |
| 58 | |
| 59 virtual void OnToolbarBackgroundAnimatorCanceled( | |
| 60 TabContents* tab_contents) OVERRIDE { | |
| 61 if (!cancel_halfway_) | |
| 62 QuitMessageLoopIfDone(); | |
| 63 } | |
| 64 | |
| 65 virtual void OnToolbarSeparatorAnimatorProgressed() OVERRIDE { | |
| 66 if (cancel_halfway_ && | |
| 67 ((animator_->separator_animation_->IsShowing() && | |
| 68 animator_->separator_animation_->GetCurrentValue() > 0.5) || | |
| 69 (animator_->separator_animation_->IsClosing() && | |
| 70 animator_->separator_animation_->GetCurrentValue() < 0.5))) { | |
| 71 MessageLoop::current()->Quit(); | |
| 72 return; | |
| 73 } | |
| 74 QuitMessageLoopIfDone(); | |
| 75 } | |
| 76 | |
| 77 virtual void OnToolbarSeparatorAnimatorCanceled() OVERRIDE { | |
| 78 if (!cancel_halfway_) | |
| 79 QuitMessageLoopIfDone(); | |
| 80 } | |
| 81 | |
| 82 void set_cancel_halfway(bool cancel) { | |
| 83 cancel_halfway_ = cancel; | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 void QuitMessageLoopIfDone() { | |
| 88 if (!animator_->background_animation_->is_animating() && | |
| 89 !animator_->separator_animation_->is_animating()) { | |
| 90 MessageLoop::current()->Quit(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 ToolbarSearchAnimator* animator_; | |
| 95 | |
| 96 bool cancel_halfway_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTestObserver); | |
| 99 }; | |
| 100 | |
| 101 class ToolbarSearchAnimatorTest : public BrowserWithTestWindowTest { | |
|
dhollowa
2012/08/01 22:41:59
I noticed that the tests take 4+s to run:
[-------
kuan
2012/08/02 21:54:03
Done.
| |
| 102 protected: | |
| 103 ToolbarSearchAnimatorTest() {} | |
| 104 | |
| 105 virtual void SetUp() OVERRIDE { | |
| 106 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 107 command_line->AppendSwitch(switches::kEnableInstantExtendedAPI); | |
| 108 | |
| 109 BrowserWithTestWindowTest::SetUp(); | |
| 110 | |
| 111 AddTab(browser(), GURL("http://foo/0")); | |
| 112 default_observer_.reset(new ToolbarSearchAnimatorTestObserver(&animator())); | |
| 113 animator().AddObserver(default_observer_.get()); | |
| 114 } | |
| 115 | |
| 116 virtual void TearDown() OVERRIDE { | |
| 117 RemoveDefaultObserver(); | |
| 118 BrowserWithTestWindowTest::TearDown(); | |
| 119 } | |
| 120 | |
| 121 void RemoveDefaultObserver() { | |
| 122 if (default_observer_.get()) { | |
| 123 animator().RemoveObserver(default_observer_.get()); | |
| 124 default_observer_.reset(NULL); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void SetMode(const Mode::Type mode, bool animate) { | |
| 129 TabContents* contents = chrome::GetTabContentsAt(browser(), 0); | |
| 130 contents->search_tab_helper()->model()->SetMode(Mode(mode, animate)); | |
| 131 } | |
| 132 | |
| 133 void RunMessageLoop(bool cancel_halfway) { | |
| 134 // Run the message loop. ToolbarSearchAnimatorTestObserver quits the | |
| 135 // message loop when all animations have stopped. | |
| 136 if (default_observer_.get()) | |
| 137 default_observer_->set_cancel_halfway(cancel_halfway); | |
| 138 message_loop()->Run(); | |
| 139 } | |
| 140 | |
| 141 ToolbarSearchAnimator& animator() { | |
| 142 return browser()->search_delegate()->toolbar_search_animator(); | |
| 143 } | |
| 144 | |
| 145 ui::MultiAnimation* background_animation() { | |
| 146 return animator().background_animation_.get(); | |
| 147 } | |
| 148 | |
| 149 ui::SlideAnimation* separator_animation() { | |
| 150 return animator().separator_animation_.get(); | |
| 151 } | |
| 152 | |
| 153 double GetGradientOpacity() const { | |
| 154 return browser()->search_delegate()->toolbar_search_animator(). | |
| 155 GetGradientOpacity(); | |
| 156 } | |
| 157 | |
| 158 double GetSeparatorOpacity() const { | |
| 159 return browser()->search_delegate()->toolbar_search_animator(). | |
| 160 GetSeparatorOpacity(); | |
| 161 } | |
| 162 | |
| 163 bool IsBackgroundChanging() { | |
| 164 return animator().background_animation_->is_animating(); | |
| 165 } | |
| 166 | |
| 167 bool IsSeparatorFading() { | |
| 168 return animator().separator_animation_->is_animating(); | |
| 169 } | |
| 170 | |
| 171 bool IsSeparatorShowing() { | |
| 172 return animator().separator_animation_->IsShowing(); | |
| 173 } | |
| 174 | |
| 175 bool IsSeparatorHiding() { | |
| 176 return animator().separator_animation_->IsClosing(); | |
| 177 } | |
| 178 | |
| 179 private: | |
| 180 scoped_ptr<ToolbarSearchAnimatorTestObserver> default_observer_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTest); | |
| 183 }; | |
| 184 | |
| 185 TEST_F(ToolbarSearchAnimatorTest, StateWithoutAnimation) { | |
| 186 SetMode(Mode::MODE_NTP, false); | |
| 187 EXPECT_FALSE(IsBackgroundChanging()); | |
| 188 EXPECT_EQ(0.0f, GetGradientOpacity()); | |
| 189 EXPECT_FALSE(IsSeparatorFading()); | |
| 190 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 191 | |
| 192 SetMode(Mode::MODE_SEARCH, false); | |
| 193 EXPECT_FALSE(IsBackgroundChanging()); | |
| 194 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 195 EXPECT_FALSE(IsSeparatorFading()); | |
| 196 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 197 | |
| 198 SetMode(Mode::MODE_DEFAULT, false); | |
| 199 EXPECT_FALSE(IsBackgroundChanging()); | |
| 200 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 201 EXPECT_FALSE(IsSeparatorFading()); | |
| 202 EXPECT_EQ(1.0f, GetSeparatorOpacity()); | |
| 203 | |
| 204 SetMode(Mode::MODE_SEARCH, false); | |
| 205 EXPECT_FALSE(IsBackgroundChanging()); | |
| 206 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 207 EXPECT_FALSE(IsSeparatorFading()); | |
| 208 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 209 | |
| 210 SetMode(Mode::MODE_NTP, false); | |
| 211 EXPECT_FALSE(IsBackgroundChanging()); | |
| 212 EXPECT_EQ(0.0f, GetGradientOpacity()); | |
| 213 EXPECT_FALSE(IsSeparatorFading()); | |
| 214 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 215 } | |
| 216 | |
| 217 TEST_F(ToolbarSearchAnimatorTest, NTPToSearch) { | |
| 218 SetMode(Mode::MODE_NTP, false); | |
| 219 | |
| 220 // Set mode to |SEARCH| to start background change animation. | |
| 221 SetMode(Mode::MODE_SEARCH, true); | |
| 222 | |
| 223 // Verify the opacities before letting animation run in message loop. | |
| 224 EXPECT_EQ(0.0f, GetGradientOpacity()); | |
| 225 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 226 | |
| 227 EXPECT_TRUE(IsBackgroundChanging()); | |
| 228 EXPECT_FALSE(IsSeparatorFading()); | |
| 229 | |
| 230 RunMessageLoop(false); | |
| 231 | |
| 232 EXPECT_FALSE(IsBackgroundChanging()); | |
| 233 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 234 EXPECT_FALSE(IsSeparatorFading()); | |
| 235 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 236 } | |
| 237 | |
| 238 TEST_F(ToolbarSearchAnimatorTest, SearchToNTP) { | |
| 239 SetMode(Mode::MODE_SEARCH, false); | |
| 240 // Set mode to |NTP|. | |
| 241 SetMode(Mode::MODE_NTP, true); | |
| 242 | |
| 243 // TODO(kuan): check with UX folks if we should animate from gradient to flat | |
| 244 // background. | |
| 245 EXPECT_FALSE(IsBackgroundChanging()); | |
| 246 EXPECT_FALSE(IsSeparatorFading()); | |
| 247 EXPECT_EQ(0.0f, GetGradientOpacity()); | |
| 248 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 249 } | |
| 250 | |
| 251 TEST_F(ToolbarSearchAnimatorTest, NTPToSearchToDefault) { | |
| 252 SetMode(Mode::MODE_NTP, false); | |
| 253 // Set mode to |SEARCH| to start background change animation. | |
| 254 SetMode(Mode::MODE_SEARCH, true); | |
| 255 // Set mode to |DEFAULT| to start separator fade in animation. | |
| 256 SetMode(Mode::MODE_DEFAULT, true); | |
| 257 | |
| 258 // Verify the opacities before letting animation run in message loop. | |
| 259 EXPECT_EQ(0.0f, GetGradientOpacity()); | |
| 260 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 261 | |
| 262 EXPECT_TRUE(IsBackgroundChanging()); | |
| 263 EXPECT_TRUE(IsSeparatorFading()); | |
| 264 EXPECT_TRUE(IsSeparatorShowing()); | |
| 265 | |
| 266 RunMessageLoop(false); | |
| 267 | |
| 268 EXPECT_FALSE(IsBackgroundChanging()); | |
| 269 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 270 EXPECT_FALSE(IsSeparatorFading()); | |
| 271 EXPECT_EQ(1.0f, GetSeparatorOpacity()); | |
| 272 } | |
| 273 | |
| 274 TEST_F(ToolbarSearchAnimatorTest, SearchToDefault) { | |
| 275 SetMode(Mode::MODE_SEARCH, false); | |
| 276 // Set mode to |DEFAULT| to start separator fade in animation. | |
| 277 SetMode(Mode::MODE_DEFAULT, true); | |
| 278 | |
| 279 // Verify the opacities before letting animation run in message loop. | |
| 280 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 281 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 282 | |
| 283 EXPECT_FALSE(IsBackgroundChanging()); | |
| 284 EXPECT_TRUE(IsSeparatorFading()); | |
| 285 EXPECT_TRUE(IsSeparatorShowing()); | |
| 286 | |
| 287 RunMessageLoop(false); | |
| 288 | |
| 289 EXPECT_FALSE(IsBackgroundChanging()); | |
| 290 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 291 EXPECT_FALSE(IsSeparatorFading()); | |
| 292 EXPECT_TRUE(IsSeparatorShowing()); | |
| 293 EXPECT_EQ(1.0f, GetSeparatorOpacity()); | |
| 294 } | |
| 295 | |
| 296 TEST_F(ToolbarSearchAnimatorTest, DefaultToSearch) { | |
| 297 // chrome::search::Mode is initialized to |DEFAULT|. | |
| 298 // Set mode to |SEARCH| to start separator fade out animation. | |
| 299 SetMode(Mode::MODE_SEARCH, true); | |
| 300 | |
| 301 // Verify the opacities before letting animation run in message loop. | |
| 302 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 303 EXPECT_EQ(1.0f, GetSeparatorOpacity()); | |
| 304 | |
| 305 EXPECT_FALSE(IsBackgroundChanging()); | |
| 306 EXPECT_TRUE(IsSeparatorFading()); | |
| 307 EXPECT_TRUE(IsSeparatorHiding()); | |
| 308 | |
| 309 RunMessageLoop(false); | |
| 310 | |
| 311 EXPECT_FALSE(IsBackgroundChanging()); | |
| 312 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 313 EXPECT_FALSE(IsSeparatorFading()); | |
| 314 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 315 } | |
| 316 | |
| 317 TEST_F(ToolbarSearchAnimatorTest, NTPToDefault) { | |
| 318 SetMode(Mode::MODE_NTP, false); | |
| 319 // Set mode to |DEFAULT| to start separator fade in animation. | |
| 320 SetMode(Mode::MODE_DEFAULT, true); | |
| 321 | |
| 322 // Verify the opacities before letting animation run in message loop. | |
| 323 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 324 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 325 | |
| 326 // TODO(kuan): check with UX folks if we should animate from flat to | |
| 327 // gradient background. | |
| 328 EXPECT_FALSE(IsBackgroundChanging()); | |
| 329 EXPECT_TRUE(IsSeparatorFading()); | |
| 330 EXPECT_TRUE(IsSeparatorShowing()); | |
| 331 | |
| 332 RunMessageLoop(false); | |
| 333 | |
| 334 EXPECT_FALSE(IsBackgroundChanging()); | |
| 335 EXPECT_EQ(1.0f, GetGradientOpacity()); | |
| 336 EXPECT_FALSE(IsSeparatorFading()); | |
| 337 EXPECT_EQ(1.0f, GetSeparatorOpacity()); | |
| 338 } | |
| 339 | |
| 340 TEST_F(ToolbarSearchAnimatorTest, DefaultToNTP) { | |
| 341 // chrome::search::Mode is initialized to |DEFAULT|. | |
| 342 // Set mode to |NTP| to start separator fade out animation. | |
| 343 SetMode(Mode::MODE_NTP, true); | |
| 344 | |
| 345 // Verify the opacities before letting animation run in message loop. | |
| 346 EXPECT_EQ(0.0f, GetGradientOpacity()); | |
| 347 EXPECT_EQ(1.0f, GetSeparatorOpacity()); | |
| 348 | |
| 349 // TODO(kuan): check with UX folks if we should animate from gradient to flat | |
| 350 // background. | |
| 351 EXPECT_FALSE(IsBackgroundChanging()); | |
| 352 EXPECT_TRUE(IsSeparatorFading()); | |
| 353 EXPECT_TRUE(IsSeparatorHiding()); | |
| 354 | |
| 355 RunMessageLoop(false); | |
| 356 | |
| 357 EXPECT_FALSE(IsBackgroundChanging()); | |
| 358 EXPECT_EQ(0.0f, GetGradientOpacity()); | |
| 359 EXPECT_FALSE(IsSeparatorFading()); | |
| 360 EXPECT_EQ(0.0f, GetSeparatorOpacity()); | |
| 361 } | |
| 362 | |
| 363 TEST_F(ToolbarSearchAnimatorTest, ObserverForFinish) { | |
| 364 MockObserver observer; | |
| 365 animator().AddObserver(&observer); | |
| 366 | |
| 367 // OnToolbar*AnimatorProgressed are expected to be called at least 3 times - | |
| 368 // when animation starts, when animation ends, and at least once when | |
| 369 // animation progresses. | |
| 370 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorProgressed()). | |
| 371 Times(AtLeast(3)); | |
|
dhollowa
2012/08/01 22:41:59
Are these |AtLeast(...)| calls dependent on the an
kuan
2012/08/02 21:54:03
Done.
| |
| 372 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorCanceled(NULL)).Times(0); | |
| 373 EXPECT_CALL(observer, OnToolbarSeparatorAnimatorProgressed()). | |
| 374 Times(AtLeast(3)); | |
| 375 EXPECT_CALL(observer, OnToolbarSeparatorAnimatorCanceled()).Times(0); | |
| 376 | |
| 377 SetMode(Mode::MODE_NTP, false); | |
| 378 // Set mode to |SEARCH| to start background change animation. | |
| 379 SetMode(Mode::MODE_SEARCH, true); | |
| 380 // Set mode to |DEFAULT| to start separator fade in animation. | |
| 381 SetMode(Mode::MODE_DEFAULT, true); | |
| 382 | |
| 383 RunMessageLoop(false); | |
| 384 | |
| 385 animator().RemoveObserver(&observer); | |
| 386 } | |
| 387 | |
| 388 TEST_F(ToolbarSearchAnimatorTest, ObserverForCancelBackground) { | |
| 389 MockObserver observer; | |
| 390 animator().AddObserver(&observer); | |
| 391 | |
| 392 // OnToolbarBackgroundAnimatorProgressed is expected to be called at least 2 | |
| 393 // times - when animation starts and at least once when animation progresses. | |
| 394 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorProgressed()). | |
| 395 Times(AtLeast(2)); | |
| 396 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorCanceled( | |
| 397 chrome::GetTabContentsAt(browser(), 0))).Times(1); | |
| 398 EXPECT_CALL(observer, OnToolbarSeparatorAnimatorProgressed()). | |
| 399 Times(0); | |
| 400 EXPECT_CALL(observer, OnToolbarSeparatorAnimatorCanceled()).Times(0); | |
| 401 | |
| 402 SetMode(Mode::MODE_NTP, false); | |
| 403 // Set mode to |SEARCH| to start background change animation. | |
| 404 SetMode(Mode::MODE_SEARCH, true); | |
| 405 | |
| 406 RunMessageLoop(true); | |
| 407 | |
| 408 // Add second tab, make it active, and set its mode to |NTP|. | |
| 409 AddTab(browser(), GURL("http://foo/1")); | |
| 410 chrome::ActivateTabAt(browser(), 1, true); | |
| 411 chrome::GetTabContentsAt(browser(), 1)->search_tab_helper()->model()->SetMode( | |
| 412 Mode(Mode::MODE_NTP, false)); | |
| 413 | |
| 414 animator().RemoveObserver(&observer); | |
| 415 } | |
| 416 | |
| 417 TEST_F(ToolbarSearchAnimatorTest, ObserverForCancelSeparator) { | |
| 418 MockObserver observer; | |
| 419 animator().AddObserver(&observer); | |
| 420 | |
| 421 // OnToolbarSeparatorAnimatorProgressed is expected to be called at least 2 | |
| 422 // times - when animation starts and at least once when animation progresses. | |
| 423 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorProgressed()). | |
| 424 Times(0); | |
| 425 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorCanceled( | |
| 426 chrome::GetTabContentsAt(browser(), 0))).Times(0); | |
| 427 EXPECT_CALL(observer, OnToolbarSeparatorAnimatorProgressed()). | |
| 428 Times(AtLeast(2)); | |
| 429 EXPECT_CALL(observer, OnToolbarSeparatorAnimatorCanceled()).Times(1); | |
| 430 | |
| 431 // chrome::search::Mode is initialized to |DEFAULT|. | |
| 432 // Set mode to |SEARCH| to start separator fade out animation. | |
| 433 SetMode(Mode::MODE_SEARCH, true); | |
| 434 | |
| 435 RunMessageLoop(true); | |
| 436 | |
| 437 // Add second tab, make it active, and set its mode to |NTP|. | |
| 438 AddTab(browser(), GURL("http://foo/1")); | |
| 439 chrome::ActivateTabAt(browser(), 1, true); | |
| 440 chrome::GetTabContentsAt(browser(), 1)->search_tab_helper()->model()->SetMode( | |
| 441 Mode(Mode::MODE_NTP, false)); | |
| 442 | |
| 443 animator().RemoveObserver(&observer); | |
| 444 } | |
| 445 | |
| 446 } // namespace search | |
| 447 } // namespace chrome | |
| OLD | NEW |