Chromium Code Reviews| Index: chrome/browser/ui/search/toolbar_search_animator_unittest.cc |
| diff --git a/chrome/browser/ui/search/toolbar_search_animator_unittest.cc b/chrome/browser/ui/search/toolbar_search_animator_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eacb2ecaf1f025e271021bf931c5354c4f00536b |
| --- /dev/null |
| +++ b/chrome/browser/ui/search/toolbar_search_animator_unittest.cc |
| @@ -0,0 +1,447 @@ |
| +// Copyright (c) 2012 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 "base/command_line.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_tabstrip.h" |
| +#include "chrome/browser/ui/search/search_delegate.h" |
| +#include "chrome/browser/ui/search/search_model.h" |
| +#include "chrome/browser/ui/search/search_tab_helper.h" |
| +#include "chrome/browser/ui/search/toolbar_search_animator.h" |
| +#include "chrome/browser/ui/search/toolbar_search_animator_observer.h" |
| +#include "chrome/browser/ui/tab_contents/tab_contents.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/test/base/browser_with_test_window_test.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/base/animation/multi_animation.h" |
| +#include "ui/base/animation/slide_animation.h" |
| + |
| +using ::testing::AtLeast; |
| + |
| +namespace chrome { |
| +namespace search { |
| + |
| +namespace { |
| + |
| +class MockObserver : public ToolbarSearchAnimatorObserver { |
| + public: |
| + MockObserver() {} |
| + |
| + MOCK_METHOD0(OnToolbarBackgroundAnimatorProgressed, void()); |
| + MOCK_METHOD1(OnToolbarBackgroundAnimatorCanceled, void(TabContents*)); |
| + MOCK_METHOD0(OnToolbarSeparatorAnimatorProgressed, void()); |
| + MOCK_METHOD0(OnToolbarSeparatorAnimatorCanceled, void()); |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MockObserver); |
| +}; |
| + |
| +} // namespace |
| + |
| +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
|
| + public: |
| + explicit ToolbarSearchAnimatorTestObserver(ToolbarSearchAnimator* animator) |
| + : animator_(animator), |
| + cancel_halfway_(false) { |
| + } |
| + |
| + virtual void OnToolbarBackgroundAnimatorProgressed() OVERRIDE { |
| + if (cancel_halfway_ && |
| + animator_->background_animation_->GetCurrentValue() > 0.5) { |
| + MessageLoop::current()->Quit(); |
| + return; |
| + } |
| + QuitMessageLoopIfDone(); |
| + } |
| + |
| + virtual void OnToolbarBackgroundAnimatorCanceled( |
| + TabContents* tab_contents) OVERRIDE { |
| + if (!cancel_halfway_) |
| + QuitMessageLoopIfDone(); |
| + } |
| + |
| + virtual void OnToolbarSeparatorAnimatorProgressed() OVERRIDE { |
| + if (cancel_halfway_ && |
| + ((animator_->separator_animation_->IsShowing() && |
| + animator_->separator_animation_->GetCurrentValue() > 0.5) || |
| + (animator_->separator_animation_->IsClosing() && |
| + animator_->separator_animation_->GetCurrentValue() < 0.5))) { |
| + MessageLoop::current()->Quit(); |
| + return; |
| + } |
| + QuitMessageLoopIfDone(); |
| + } |
| + |
| + virtual void OnToolbarSeparatorAnimatorCanceled() OVERRIDE { |
| + if (!cancel_halfway_) |
| + QuitMessageLoopIfDone(); |
| + } |
| + |
| + void set_cancel_halfway(bool cancel) { |
| + cancel_halfway_ = cancel; |
| + } |
| + |
| + private: |
| + void QuitMessageLoopIfDone() { |
| + if (!animator_->background_animation_->is_animating() && |
| + !animator_->separator_animation_->is_animating()) { |
| + MessageLoop::current()->Quit(); |
| + } |
| + } |
| + |
| + ToolbarSearchAnimator* animator_; |
| + |
| + bool cancel_halfway_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTestObserver); |
| +}; |
| + |
| +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.
|
| + protected: |
| + ToolbarSearchAnimatorTest() {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| + command_line->AppendSwitch(switches::kEnableInstantExtendedAPI); |
| + |
| + BrowserWithTestWindowTest::SetUp(); |
| + |
| + AddTab(browser(), GURL("http://foo/0")); |
| + default_observer_.reset(new ToolbarSearchAnimatorTestObserver(&animator())); |
| + animator().AddObserver(default_observer_.get()); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + RemoveDefaultObserver(); |
| + BrowserWithTestWindowTest::TearDown(); |
| + } |
| + |
| + void RemoveDefaultObserver() { |
| + if (default_observer_.get()) { |
| + animator().RemoveObserver(default_observer_.get()); |
| + default_observer_.reset(NULL); |
| + } |
| + } |
| + |
| + void SetMode(const Mode::Type mode, bool animate) { |
| + TabContents* contents = chrome::GetTabContentsAt(browser(), 0); |
| + contents->search_tab_helper()->model()->SetMode(Mode(mode, animate)); |
| + } |
| + |
| + void RunMessageLoop(bool cancel_halfway) { |
| + // Run the message loop. ToolbarSearchAnimatorTestObserver quits the |
| + // message loop when all animations have stopped. |
| + if (default_observer_.get()) |
| + default_observer_->set_cancel_halfway(cancel_halfway); |
| + message_loop()->Run(); |
| + } |
| + |
| + ToolbarSearchAnimator& animator() { |
| + return browser()->search_delegate()->toolbar_search_animator(); |
| + } |
| + |
| + ui::MultiAnimation* background_animation() { |
| + return animator().background_animation_.get(); |
| + } |
| + |
| + ui::SlideAnimation* separator_animation() { |
| + return animator().separator_animation_.get(); |
| + } |
| + |
| + double GetGradientOpacity() const { |
| + return browser()->search_delegate()->toolbar_search_animator(). |
| + GetGradientOpacity(); |
| + } |
| + |
| + double GetSeparatorOpacity() const { |
| + return browser()->search_delegate()->toolbar_search_animator(). |
| + GetSeparatorOpacity(); |
| + } |
| + |
| + bool IsBackgroundChanging() { |
| + return animator().background_animation_->is_animating(); |
| + } |
| + |
| + bool IsSeparatorFading() { |
| + return animator().separator_animation_->is_animating(); |
| + } |
| + |
| + bool IsSeparatorShowing() { |
| + return animator().separator_animation_->IsShowing(); |
| + } |
| + |
| + bool IsSeparatorHiding() { |
| + return animator().separator_animation_->IsClosing(); |
| + } |
| + |
| + private: |
| + scoped_ptr<ToolbarSearchAnimatorTestObserver> default_observer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTest); |
| +}; |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, StateWithoutAnimation) { |
| + SetMode(Mode::MODE_NTP, false); |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(0.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| + |
| + SetMode(Mode::MODE_SEARCH, false); |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| + |
| + SetMode(Mode::MODE_DEFAULT, false); |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(1.0f, GetSeparatorOpacity()); |
| + |
| + SetMode(Mode::MODE_SEARCH, false); |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| + |
| + SetMode(Mode::MODE_NTP, false); |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(0.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, NTPToSearch) { |
| + SetMode(Mode::MODE_NTP, false); |
| + |
| + // Set mode to |SEARCH| to start background change animation. |
| + SetMode(Mode::MODE_SEARCH, true); |
| + |
| + // Verify the opacities before letting animation run in message loop. |
| + EXPECT_EQ(0.0f, GetGradientOpacity()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| + |
| + EXPECT_TRUE(IsBackgroundChanging()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + |
| + RunMessageLoop(false); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, SearchToNTP) { |
| + SetMode(Mode::MODE_SEARCH, false); |
| + // Set mode to |NTP|. |
| + SetMode(Mode::MODE_NTP, true); |
| + |
| + // TODO(kuan): check with UX folks if we should animate from gradient to flat |
| + // background. |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetGradientOpacity()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, NTPToSearchToDefault) { |
| + SetMode(Mode::MODE_NTP, false); |
| + // Set mode to |SEARCH| to start background change animation. |
| + SetMode(Mode::MODE_SEARCH, true); |
| + // Set mode to |DEFAULT| to start separator fade in animation. |
| + SetMode(Mode::MODE_DEFAULT, true); |
| + |
| + // Verify the opacities before letting animation run in message loop. |
| + EXPECT_EQ(0.0f, GetGradientOpacity()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| + |
| + EXPECT_TRUE(IsBackgroundChanging()); |
| + EXPECT_TRUE(IsSeparatorFading()); |
| + EXPECT_TRUE(IsSeparatorShowing()); |
| + |
| + RunMessageLoop(false); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(1.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, SearchToDefault) { |
| + SetMode(Mode::MODE_SEARCH, false); |
| + // Set mode to |DEFAULT| to start separator fade in animation. |
| + SetMode(Mode::MODE_DEFAULT, true); |
| + |
| + // Verify the opacities before letting animation run in message loop. |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_TRUE(IsSeparatorFading()); |
| + EXPECT_TRUE(IsSeparatorShowing()); |
| + |
| + RunMessageLoop(false); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_TRUE(IsSeparatorShowing()); |
| + EXPECT_EQ(1.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, DefaultToSearch) { |
| + // chrome::search::Mode is initialized to |DEFAULT|. |
| + // Set mode to |SEARCH| to start separator fade out animation. |
| + SetMode(Mode::MODE_SEARCH, true); |
| + |
| + // Verify the opacities before letting animation run in message loop. |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_EQ(1.0f, GetSeparatorOpacity()); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_TRUE(IsSeparatorFading()); |
| + EXPECT_TRUE(IsSeparatorHiding()); |
| + |
| + RunMessageLoop(false); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, NTPToDefault) { |
| + SetMode(Mode::MODE_NTP, false); |
| + // Set mode to |DEFAULT| to start separator fade in animation. |
| + SetMode(Mode::MODE_DEFAULT, true); |
| + |
| + // Verify the opacities before letting animation run in message loop. |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| + |
| + // TODO(kuan): check with UX folks if we should animate from flat to |
| + // gradient background. |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_TRUE(IsSeparatorFading()); |
| + EXPECT_TRUE(IsSeparatorShowing()); |
| + |
| + RunMessageLoop(false); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(1.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(1.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, DefaultToNTP) { |
| + // chrome::search::Mode is initialized to |DEFAULT|. |
| + // Set mode to |NTP| to start separator fade out animation. |
| + SetMode(Mode::MODE_NTP, true); |
| + |
| + // Verify the opacities before letting animation run in message loop. |
| + EXPECT_EQ(0.0f, GetGradientOpacity()); |
| + EXPECT_EQ(1.0f, GetSeparatorOpacity()); |
| + |
| + // TODO(kuan): check with UX folks if we should animate from gradient to flat |
| + // background. |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_TRUE(IsSeparatorFading()); |
| + EXPECT_TRUE(IsSeparatorHiding()); |
| + |
| + RunMessageLoop(false); |
| + |
| + EXPECT_FALSE(IsBackgroundChanging()); |
| + EXPECT_EQ(0.0f, GetGradientOpacity()); |
| + EXPECT_FALSE(IsSeparatorFading()); |
| + EXPECT_EQ(0.0f, GetSeparatorOpacity()); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, ObserverForFinish) { |
| + MockObserver observer; |
| + animator().AddObserver(&observer); |
| + |
| + // OnToolbar*AnimatorProgressed are expected to be called at least 3 times - |
| + // when animation starts, when animation ends, and at least once when |
| + // animation progresses. |
| + EXPECT_CALL(observer, OnToolbarBackgroundAnimatorProgressed()). |
| + 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.
|
| + EXPECT_CALL(observer, OnToolbarBackgroundAnimatorCanceled(NULL)).Times(0); |
| + EXPECT_CALL(observer, OnToolbarSeparatorAnimatorProgressed()). |
| + Times(AtLeast(3)); |
| + EXPECT_CALL(observer, OnToolbarSeparatorAnimatorCanceled()).Times(0); |
| + |
| + SetMode(Mode::MODE_NTP, false); |
| + // Set mode to |SEARCH| to start background change animation. |
| + SetMode(Mode::MODE_SEARCH, true); |
| + // Set mode to |DEFAULT| to start separator fade in animation. |
| + SetMode(Mode::MODE_DEFAULT, true); |
| + |
| + RunMessageLoop(false); |
| + |
| + animator().RemoveObserver(&observer); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, ObserverForCancelBackground) { |
| + MockObserver observer; |
| + animator().AddObserver(&observer); |
| + |
| + // OnToolbarBackgroundAnimatorProgressed is expected to be called at least 2 |
| + // times - when animation starts and at least once when animation progresses. |
| + EXPECT_CALL(observer, OnToolbarBackgroundAnimatorProgressed()). |
| + Times(AtLeast(2)); |
| + EXPECT_CALL(observer, OnToolbarBackgroundAnimatorCanceled( |
| + chrome::GetTabContentsAt(browser(), 0))).Times(1); |
| + EXPECT_CALL(observer, OnToolbarSeparatorAnimatorProgressed()). |
| + Times(0); |
| + EXPECT_CALL(observer, OnToolbarSeparatorAnimatorCanceled()).Times(0); |
| + |
| + SetMode(Mode::MODE_NTP, false); |
| + // Set mode to |SEARCH| to start background change animation. |
| + SetMode(Mode::MODE_SEARCH, true); |
| + |
| + RunMessageLoop(true); |
| + |
| + // Add second tab, make it active, and set its mode to |NTP|. |
| + AddTab(browser(), GURL("http://foo/1")); |
| + chrome::ActivateTabAt(browser(), 1, true); |
| + chrome::GetTabContentsAt(browser(), 1)->search_tab_helper()->model()->SetMode( |
| + Mode(Mode::MODE_NTP, false)); |
| + |
| + animator().RemoveObserver(&observer); |
| +} |
| + |
| +TEST_F(ToolbarSearchAnimatorTest, ObserverForCancelSeparator) { |
| + MockObserver observer; |
| + animator().AddObserver(&observer); |
| + |
| + // OnToolbarSeparatorAnimatorProgressed is expected to be called at least 2 |
| + // times - when animation starts and at least once when animation progresses. |
| + EXPECT_CALL(observer, OnToolbarBackgroundAnimatorProgressed()). |
| + Times(0); |
| + EXPECT_CALL(observer, OnToolbarBackgroundAnimatorCanceled( |
| + chrome::GetTabContentsAt(browser(), 0))).Times(0); |
| + EXPECT_CALL(observer, OnToolbarSeparatorAnimatorProgressed()). |
| + Times(AtLeast(2)); |
| + EXPECT_CALL(observer, OnToolbarSeparatorAnimatorCanceled()).Times(1); |
| + |
| + // chrome::search::Mode is initialized to |DEFAULT|. |
| + // Set mode to |SEARCH| to start separator fade out animation. |
| + SetMode(Mode::MODE_SEARCH, true); |
| + |
| + RunMessageLoop(true); |
| + |
| + // Add second tab, make it active, and set its mode to |NTP|. |
| + AddTab(browser(), GURL("http://foo/1")); |
| + chrome::ActivateTabAt(browser(), 1, true); |
| + chrome::GetTabContentsAt(browser(), 1)->search_tab_helper()->model()->SetMode( |
| + Mode(Mode::MODE_NTP, false)); |
| + |
| + animator().RemoveObserver(&observer); |
| +} |
| + |
| +} // namespace search |
| +} // namespace chrome |