Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Side by Side Diff: chrome/browser/ui/search/toolbar_search_animator_unittest.cc

Issue 10816027: alternate ntp: toolbar background and separator animation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address dhollowa's comments Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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"
sky 2012/08/03 22:18:41 Include order should be the same as that of the th
kuan 2012/08/13 20:19:46 Done.
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
20 using ::testing::AtLeast;
21
22 namespace chrome {
23 namespace search {
24
25 namespace {
26
27 class MockObserver : public ToolbarSearchAnimatorObserver {
sky 2012/08/03 22:18:41 Ben says no gmock. Write the code by hand so it's
kuan 2012/08/13 20:19:46 Done.
28 public:
29 MockObserver() {}
30
31 MOCK_METHOD0(OnToolbarBackgroundAnimatorProgressed, void());
32 MOCK_METHOD1(OnToolbarBackgroundAnimatorCanceled, void(TabContents*));
33
34 private:
35 DISALLOW_COPY_AND_ASSIGN(MockObserver);
36 };
37
38 } // namespace
39
40 class ToolbarSearchAnimatorTestObserver : public ToolbarSearchAnimatorObserver {
41 public:
42 explicit ToolbarSearchAnimatorTestObserver(ToolbarSearchAnimator* animator)
43 : animator_(animator),
44 cancel_halfway_(false) {
45 }
46
47 virtual void OnToolbarBackgroundAnimatorProgressed() OVERRIDE {
48 if (cancel_halfway_ &&
49 animator_->background_animation_->GetCurrentValue() > 0.5) {
50 MessageLoop::current()->Quit();
51 return;
52 }
53 QuitMessageLoopIfDone();
54 }
55
56 virtual void OnToolbarBackgroundAnimatorCanceled(
57 TabContents* tab_contents) OVERRIDE {
58 if (!cancel_halfway_)
59 QuitMessageLoopIfDone();
60 }
61
62 void set_cancel_halfway(bool cancel) {
63 cancel_halfway_ = cancel;
64 }
65
66 private:
67 void QuitMessageLoopIfDone() {
68 if (!animator_->background_animation_->is_animating())
69 MessageLoop::current()->Quit();
70 }
71
72 ToolbarSearchAnimator* animator_;
73
74 bool cancel_halfway_;
75
76 DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTestObserver);
77 };
78
79 class ToolbarSearchAnimatorTest : public BrowserWithTestWindowTest {
80 protected:
81 ToolbarSearchAnimatorTest() {}
82
83 virtual void SetUp() OVERRIDE {
84 CommandLine* command_line = CommandLine::ForCurrentProcess();
85 command_line->AppendSwitch(switches::kEnableInstantExtendedAPI);
86
87 BrowserWithTestWindowTest::SetUp();
88
89 animator().InitBackgroundAnimation(0, 0);
90
91 AddTab(browser(), GURL("http://foo/0"));
92 default_observer_.reset(new ToolbarSearchAnimatorTestObserver(&animator()));
93 animator().AddObserver(default_observer_.get());
94 }
95
96 virtual void TearDown() OVERRIDE {
97 RemoveDefaultObserver();
98 BrowserWithTestWindowTest::TearDown();
99 }
100
101 void RemoveDefaultObserver() {
102 if (default_observer_.get()) {
103 animator().RemoveObserver(default_observer_.get());
104 default_observer_.reset(NULL);
105 }
106 }
107
108 void SetMode(const Mode::Type mode, bool animate) {
109 TabContents* contents = chrome::GetTabContentsAt(browser(), 0);
110 contents->search_tab_helper()->model()->SetMode(Mode(mode, animate));
111 }
112
113 void RunMessageLoop(bool cancel_halfway) {
114 // Run the message loop. ToolbarSearchAnimatorTestObserver quits the
115 // message loop when all animations have stopped.
116 if (default_observer_.get())
117 default_observer_->set_cancel_halfway(cancel_halfway);
118 message_loop()->Run();
119 }
120
121 ToolbarSearchAnimator& animator() {
122 return browser()->search_delegate()->toolbar_search_animator();
123 }
124
125 ui::MultiAnimation* background_animation() {
126 return animator().background_animation_.get();
127 }
128
129 double GetGradientOpacity() const {
130 return browser()->search_delegate()->toolbar_search_animator().
131 GetGradientOpacity();
132 }
133
134 bool IsBackgroundChanging() {
135 return animator().background_animation_->is_animating();
136 }
137
138 private:
139 scoped_ptr<ToolbarSearchAnimatorTestObserver> default_observer_;
140
141 DISALLOW_COPY_AND_ASSIGN(ToolbarSearchAnimatorTest);
142 };
143
144 TEST_F(ToolbarSearchAnimatorTest, StateWithoutAnimation) {
145 SetMode(Mode::MODE_NTP, false);
146 EXPECT_FALSE(IsBackgroundChanging());
147 EXPECT_EQ(0.0f, GetGradientOpacity());
148
149 SetMode(Mode::MODE_SEARCH, false);
150 EXPECT_FALSE(IsBackgroundChanging());
151 EXPECT_EQ(1.0f, GetGradientOpacity());
152
153 SetMode(Mode::MODE_DEFAULT, false);
154 EXPECT_FALSE(IsBackgroundChanging());
155 EXPECT_EQ(1.0f, GetGradientOpacity());
156
157 SetMode(Mode::MODE_SEARCH, false);
158 EXPECT_FALSE(IsBackgroundChanging());
159 EXPECT_EQ(1.0f, GetGradientOpacity());
160
161 SetMode(Mode::MODE_NTP, false);
162 EXPECT_FALSE(IsBackgroundChanging());
163 EXPECT_EQ(0.0f, GetGradientOpacity());
164 }
165
166 TEST_F(ToolbarSearchAnimatorTest, NTPToSearch) {
167 SetMode(Mode::MODE_NTP, false);
168 // Set mode to |SEARCH| to start background change animation.
169 SetMode(Mode::MODE_SEARCH, true);
170
171 // Verify the opacity before letting animation run in message loop.
172 EXPECT_EQ(0.0f, GetGradientOpacity());
173
174 EXPECT_TRUE(IsBackgroundChanging());
175
176 RunMessageLoop(false);
177
178 EXPECT_FALSE(IsBackgroundChanging());
179 EXPECT_EQ(1.0f, GetGradientOpacity());
180 }
181
182 TEST_F(ToolbarSearchAnimatorTest, SearchToNTP) {
183 SetMode(Mode::MODE_SEARCH, false);
184 SetMode(Mode::MODE_NTP, true);
185
186 // TODO(kuan): check with UX folks if we should animate from gradient to flat
187 // background.
188 EXPECT_FALSE(IsBackgroundChanging());
189 EXPECT_EQ(0.0f, GetGradientOpacity());
190 }
191
192 TEST_F(ToolbarSearchAnimatorTest, SearchToDefault) {
193 SetMode(Mode::MODE_SEARCH, false);
194 SetMode(Mode::MODE_DEFAULT, true);
195
196 EXPECT_FALSE(IsBackgroundChanging());
197 EXPECT_EQ(1.0f, GetGradientOpacity());
198 }
199
200 TEST_F(ToolbarSearchAnimatorTest, DefaultToSearch) {
201 // chrome::search::Mode is initialized to |DEFAULT|.
202 SetMode(Mode::MODE_SEARCH, true);
203
204 EXPECT_FALSE(IsBackgroundChanging());
205 EXPECT_EQ(1.0f, GetGradientOpacity());
206 }
207
208 TEST_F(ToolbarSearchAnimatorTest, NTPToDefault) {
209 SetMode(Mode::MODE_NTP, false);
210 SetMode(Mode::MODE_DEFAULT, true);
211
212 // TODO(kuan): check with UX folks if we should animate from flat to
213 // gradient background.
214 EXPECT_FALSE(IsBackgroundChanging());
215 EXPECT_EQ(1.0f, GetGradientOpacity());
216 }
217
218 TEST_F(ToolbarSearchAnimatorTest, DefaultToNTP) {
219 // chrome::search::Mode is initialized to |DEFAULT|.
220 SetMode(Mode::MODE_NTP, true);
221
222 // TODO(kuan): check with UX folks if we should animate from gradient to flat
223 // background.
224 EXPECT_FALSE(IsBackgroundChanging());
225 EXPECT_EQ(0.0f, GetGradientOpacity());
226 }
227
228 TEST_F(ToolbarSearchAnimatorTest, Observer) {
229 MockObserver observer;
230 animator().AddObserver(&observer);
231
232 // OnToolbarBackgroundAnimatorProgressed is expected to be called at least
233 // once - when animation ends, more if animation duration is > 0;
234 // We've set animation duration to 0 for testing, so we only check for at
235 // least once.
236 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorProgressed()).
237 Times(AtLeast(1));
238 EXPECT_CALL(observer, OnToolbarBackgroundAnimatorCanceled(NULL)).Times(0);
239
240 SetMode(Mode::MODE_NTP, false);
241 SetMode(Mode::MODE_SEARCH, true);
242 SetMode(Mode::MODE_DEFAULT, true);
243
244 RunMessageLoop(false);
245
246 animator().RemoveObserver(&observer);
247 }
248
249 } // namespace search
250 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698