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 "chrome/browser/ui/search/toolbar_search_animator.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/browser_tabstrip.h" |
| 10 #include "chrome/browser/ui/search/search_delegate.h" |
| 11 #include "chrome/browser/ui/search/search_model.h" |
| 12 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 13 #include "chrome/browser/ui/search/toolbar_search_animator_observer.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/test/base/browser_with_test_window_test.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/animation/multi_animation.h" |
| 19 |
| 20 namespace chrome { |
| 21 namespace search { |
| 22 |
| 23 class ToolbarSearchAnimatorTestObserver : public ToolbarSearchAnimatorObserver { |
| 24 public: |
| 25 explicit ToolbarSearchAnimatorTestObserver(ToolbarSearchAnimator* animator) |
| 26 : animator_(animator), |
| 27 got_progressed_(false), |
| 28 got_canceled_(false), |
| 29 cancel_halfway_(false) { |
| 30 } |
| 31 |
| 32 virtual void OnToolbarBackgroundAnimatorProgressed() OVERRIDE { |
| 33 got_progressed_ = true; |
| 34 if (cancel_halfway_ && animator_->background_animation_.get() && |
| 35 animator_->background_animation_->GetCurrentValue() > 0.5) { |
| 36 MessageLoop::current()->Quit(); |
| 37 return; |
| 38 } |
| 39 QuitMessageLoopIfDone(); |
| 40 } |
| 41 |
| 42 virtual void OnToolbarBackgroundAnimatorCanceled( |
| 43 TabContents* tab_contents) OVERRIDE { |
| 44 got_canceled_ = true; |
| 45 if (!cancel_halfway_) |
| 46 QuitMessageLoopIfDone(); |
| 47 } |
| 48 |
| 49 void set_cancel_halfway(bool cancel) { |
| 50 cancel_halfway_ = cancel; |
| 51 } |
| 52 |
| 53 bool has_progressed() const { return got_progressed_; } |
| 54 |
| 55 bool has_canceled() const { return got_canceled_; } |
| 56 |
| 57 private: |
| 58 void QuitMessageLoopIfDone() { |
| 59 if (!(animator_->background_animation_.get() && |
| 60 animator_->background_animation_->is_animating())) { |
| 61 MessageLoop::current()->Quit(); |
| 62 } |
| 63 } |
| 64 |
| 65 ToolbarSearchAnimator* animator_; |
| 66 |
| 67 bool got_progressed_; |
| 68 |
| 69 bool got_canceled_; |
| 70 |
| 71 bool cancel_halfway_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTestObserver); |
| 74 }; |
| 75 |
| 76 class ToolbarSearchAnimatorTest : public BrowserWithTestWindowTest { |
| 77 protected: |
| 78 ToolbarSearchAnimatorTest() {} |
| 79 |
| 80 virtual void SetUp() OVERRIDE { |
| 81 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 82 command_line->AppendSwitch(switches::kEnableInstantExtendedAPI); |
| 83 |
| 84 BrowserWithTestWindowTest::SetUp(); |
| 85 |
| 86 animator().background_change_delay_ms_ = 0; |
| 87 animator().background_change_duration_ms_ = 0; |
| 88 |
| 89 AddTab(browser(), GURL("http://foo/0")); |
| 90 default_observer_.reset(new ToolbarSearchAnimatorTestObserver(&animator())); |
| 91 animator().AddObserver(default_observer_.get()); |
| 92 } |
| 93 |
| 94 virtual void TearDown() OVERRIDE { |
| 95 RemoveDefaultObserver(); |
| 96 BrowserWithTestWindowTest::TearDown(); |
| 97 } |
| 98 |
| 99 void RemoveDefaultObserver() { |
| 100 if (default_observer_.get()) { |
| 101 animator().RemoveObserver(default_observer_.get()); |
| 102 default_observer_.reset(NULL); |
| 103 } |
| 104 } |
| 105 |
| 106 void SetMode(const Mode::Type mode, bool animate) { |
| 107 TabContents* contents = chrome::GetTabContentsAt(browser(), 0); |
| 108 contents->search_tab_helper()->model()->SetMode(Mode(mode, animate)); |
| 109 } |
| 110 |
| 111 void RunMessageLoop(bool cancel_halfway) { |
| 112 // Run the message loop. ToolbarSearchAnimatorTestObserver quits the |
| 113 // message loop when all animations have stopped. |
| 114 if (default_observer_.get()) |
| 115 default_observer_->set_cancel_halfway(cancel_halfway); |
| 116 message_loop()->Run(); |
| 117 } |
| 118 |
| 119 ToolbarSearchAnimator& animator() { |
| 120 return browser()->search_delegate()->toolbar_search_animator(); |
| 121 } |
| 122 |
| 123 ToolbarSearchAnimatorTestObserver* default_observer() const { |
| 124 return default_observer_.get(); |
| 125 } |
| 126 |
| 127 double GetGradientOpacity() const { |
| 128 return browser()->search_delegate()->toolbar_search_animator(). |
| 129 GetGradientOpacity(); |
| 130 } |
| 131 |
| 132 bool IsBackgroundChanging() { |
| 133 return animator().background_animation_.get() && |
| 134 animator().background_animation_->is_animating(); |
| 135 } |
| 136 |
| 137 private: |
| 138 scoped_ptr<ToolbarSearchAnimatorTestObserver> default_observer_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTest); |
| 141 }; |
| 142 |
| 143 TEST_F(ToolbarSearchAnimatorTest, StateWithoutAnimation) { |
| 144 SetMode(Mode::MODE_NTP, false); |
| 145 EXPECT_FALSE(IsBackgroundChanging()); |
| 146 EXPECT_EQ(0.0f, GetGradientOpacity()); |
| 147 |
| 148 SetMode(Mode::MODE_SEARCH, false); |
| 149 EXPECT_FALSE(IsBackgroundChanging()); |
| 150 EXPECT_EQ(1.0f, GetGradientOpacity()); |
| 151 |
| 152 SetMode(Mode::MODE_DEFAULT, false); |
| 153 EXPECT_FALSE(IsBackgroundChanging()); |
| 154 EXPECT_EQ(1.0f, GetGradientOpacity()); |
| 155 |
| 156 SetMode(Mode::MODE_SEARCH, false); |
| 157 EXPECT_FALSE(IsBackgroundChanging()); |
| 158 EXPECT_EQ(1.0f, GetGradientOpacity()); |
| 159 |
| 160 SetMode(Mode::MODE_NTP, false); |
| 161 EXPECT_FALSE(IsBackgroundChanging()); |
| 162 EXPECT_EQ(0.0f, GetGradientOpacity()); |
| 163 } |
| 164 |
| 165 TEST_F(ToolbarSearchAnimatorTest, NTPToSearch) { |
| 166 SetMode(Mode::MODE_NTP, false); |
| 167 // Set mode to |SEARCH| to start background change animation. |
| 168 SetMode(Mode::MODE_SEARCH, true); |
| 169 |
| 170 // Verify the opacity before letting animation run in message loop. |
| 171 EXPECT_EQ(0.0f, GetGradientOpacity()); |
| 172 |
| 173 EXPECT_TRUE(IsBackgroundChanging()); |
| 174 |
| 175 RunMessageLoop(false); |
| 176 |
| 177 EXPECT_FALSE(IsBackgroundChanging()); |
| 178 EXPECT_EQ(1.0f, GetGradientOpacity()); |
| 179 } |
| 180 |
| 181 TEST_F(ToolbarSearchAnimatorTest, SearchToNTP) { |
| 182 SetMode(Mode::MODE_SEARCH, false); |
| 183 SetMode(Mode::MODE_NTP, true); |
| 184 |
| 185 // TODO(kuan): check with UX folks if we should animate from gradient to flat |
| 186 // background. |
| 187 EXPECT_FALSE(IsBackgroundChanging()); |
| 188 EXPECT_EQ(0.0f, GetGradientOpacity()); |
| 189 } |
| 190 |
| 191 TEST_F(ToolbarSearchAnimatorTest, SearchToDefault) { |
| 192 SetMode(Mode::MODE_SEARCH, false); |
| 193 SetMode(Mode::MODE_DEFAULT, true); |
| 194 |
| 195 EXPECT_FALSE(IsBackgroundChanging()); |
| 196 EXPECT_EQ(1.0f, GetGradientOpacity()); |
| 197 } |
| 198 |
| 199 TEST_F(ToolbarSearchAnimatorTest, DefaultToSearch) { |
| 200 // chrome::search::Mode is initialized to |DEFAULT|. |
| 201 SetMode(Mode::MODE_SEARCH, true); |
| 202 |
| 203 EXPECT_FALSE(IsBackgroundChanging()); |
| 204 EXPECT_EQ(1.0f, GetGradientOpacity()); |
| 205 } |
| 206 |
| 207 TEST_F(ToolbarSearchAnimatorTest, NTPToDefault) { |
| 208 SetMode(Mode::MODE_NTP, false); |
| 209 SetMode(Mode::MODE_DEFAULT, true); |
| 210 |
| 211 // TODO(kuan): check with UX folks if we should animate from flat to |
| 212 // gradient background. |
| 213 EXPECT_FALSE(IsBackgroundChanging()); |
| 214 EXPECT_EQ(1.0f, GetGradientOpacity()); |
| 215 } |
| 216 |
| 217 TEST_F(ToolbarSearchAnimatorTest, DefaultToNTP) { |
| 218 // chrome::search::Mode is initialized to |DEFAULT|. |
| 219 SetMode(Mode::MODE_NTP, true); |
| 220 |
| 221 // TODO(kuan): check with UX folks if we should animate from gradient to flat |
| 222 // background. |
| 223 EXPECT_FALSE(IsBackgroundChanging()); |
| 224 EXPECT_EQ(0.0f, GetGradientOpacity()); |
| 225 } |
| 226 |
| 227 TEST_F(ToolbarSearchAnimatorTest, Observer) { |
| 228 EXPECT_FALSE(default_observer()->has_progressed()); |
| 229 EXPECT_FALSE(default_observer()->has_canceled()); |
| 230 |
| 231 SetMode(Mode::MODE_NTP, false); |
| 232 SetMode(Mode::MODE_SEARCH, true); |
| 233 SetMode(Mode::MODE_DEFAULT, true); |
| 234 |
| 235 RunMessageLoop(false); |
| 236 |
| 237 EXPECT_TRUE(default_observer()->has_progressed()); |
| 238 EXPECT_FALSE(default_observer()->has_canceled()); |
| 239 } |
| 240 |
| 241 } // namespace search |
| 242 } // namespace chrome |
OLD | NEW |