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/views/ash/tab_scrubber.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
| 13 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 14 #include "chrome/browser/ui/views/tabs/tab.h" |
| 15 #include "chrome/browser/ui/views/tabs/tab_strip.h" |
| 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/test/test_utils.h" |
| 21 #include "ui/aura/test/event_generator.h" |
| 22 #include "ui/aura/window.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 class TabScrubberTest : public InProcessBrowserTest, |
| 27 public TabStripModelObserver { |
| 28 public: |
| 29 TabScrubberTest() |
| 30 : tab_strip_(NULL), |
| 31 event_generator_(NULL), |
| 32 target_index_(-1) { |
| 33 } |
| 34 |
| 35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 36 command_line->AppendSwitch(switches::kNaturalScrollDefault); |
| 37 command_line->AppendSwitch(switches::kAshEnableTabScrubbing); |
| 38 } |
| 39 |
| 40 virtual void SetUpOnMainThread() OVERRIDE { |
| 41 aura::Window* window = browser()->window()->GetNativeWindow(); |
| 42 BrowserView* browser_view = |
| 43 BrowserView::GetBrowserViewForNativeWindow(window); |
| 44 tab_strip_ = browser_view->tabstrip(); |
| 45 browser()->tab_strip_model()->AddObserver(this); |
| 46 aura::RootWindow* root = window->GetRootWindow(); |
| 47 event_generator_.reset(new aura::test::EventGenerator(root, window)); |
| 48 event_generator_->set_async(true); |
| 49 TabScrubber::GetInstance()->set_activation_delay( |
| 50 base::TimeDelta::FromMilliseconds(0)); |
| 51 } |
| 52 |
| 53 virtual void CleanUpOnMainThread() { |
| 54 browser()->tab_strip_model()->RemoveObserver(this); |
| 55 } |
| 56 |
| 57 int GetStartX(int index, TabScrubber::Direction direction) { |
| 58 return TabScrubber::GetStartPoint(tab_strip_, index, direction).x(); |
| 59 } |
| 60 |
| 61 enum ScrubType { |
| 62 EACH_TAB, |
| 63 SKIP_TABS, |
| 64 REPEAT_TABS, |
| 65 }; |
| 66 |
| 67 void Scrub(int index, ScrubType scrub_type) { |
| 68 activation_order_.clear(); |
| 69 int active_index = browser()->tab_strip_model()->active_index(); |
| 70 ASSERT_NE(index, active_index); |
| 71 ASSERT_TRUE(scrub_type != SKIP_TABS || ((index - active_index) % 2) == 0); |
| 72 TabScrubber::Direction direction; |
| 73 int increment; |
| 74 if (index < active_index) { |
| 75 direction = TabScrubber::LEFT; |
| 76 increment = -1; |
| 77 } else { |
| 78 direction = TabScrubber::RIGHT; |
| 79 increment = 1; |
| 80 } |
| 81 if (scrub_type == SKIP_TABS) |
| 82 increment *= 2; |
| 83 int last = GetStartX(active_index, direction); |
| 84 std::vector<gfx::Point> offsets; |
| 85 for (int i = active_index + increment; i != (index + increment); |
| 86 i += increment) { |
| 87 int tab_center = tab_strip_->tab_at(i)->bounds().CenterPoint().x(); |
| 88 offsets.push_back(gfx::Point(tab_center - last, 0)); |
| 89 last = GetStartX(i, direction); |
| 90 if (scrub_type == REPEAT_TABS) { |
| 91 offsets.push_back(gfx::Point(increment, 0)); |
| 92 last += increment; |
| 93 } |
| 94 } |
| 95 event_generator_->ScrollSequence(gfx::Point(0, 0), |
| 96 base::TimeDelta::FromMilliseconds(100), |
| 97 offsets, |
| 98 3); |
| 99 } |
| 100 |
| 101 void AddTabs(int num_tabs) { |
| 102 for (int i = 0; i < num_tabs; ++i) |
| 103 AddBlankTabAndShow(browser()); |
| 104 ASSERT_EQ(num_tabs + 1, browser()->tab_strip_model()->count()); |
| 105 ASSERT_EQ(num_tabs, browser()->tab_strip_model()->active_index()); |
| 106 } |
| 107 |
| 108 void RunUntilTabActive(int target) { |
| 109 base::RunLoop run_loop; |
| 110 quit_closure_ = content::GetQuitTaskForRunLoop(&run_loop); |
| 111 target_index_ = target; |
| 112 content::RunThisRunLoop(&run_loop); |
| 113 target_index_ = -1; |
| 114 } |
| 115 |
| 116 // TabStripModelObserver overrides. |
| 117 virtual void TabInsertedAt(content::WebContents* contents, |
| 118 int index, |
| 119 bool foreground) {} |
| 120 virtual void TabClosingAt(TabStripModel* tab_strip_model, |
| 121 content::WebContents* contents, |
| 122 int index) {} |
| 123 virtual void TabDetachedAt(content::WebContents* contents, int index) {} |
| 124 virtual void TabDeactivated(content::WebContents* contents) {} |
| 125 virtual void ActiveTabChanged(content::WebContents* old_contents, |
| 126 content::WebContents* new_contents, |
| 127 int index, |
| 128 bool user_gesture) { |
| 129 activation_order_.push_back(index); |
| 130 if (index == target_index_) |
| 131 quit_closure_.Run(); |
| 132 } |
| 133 |
| 134 virtual void TabSelectionChanged(TabStripModel* tab_strip_model, |
| 135 const ui::ListSelectionModel& old_model) {} |
| 136 virtual void TabMoved(content::WebContents* contents, |
| 137 int from_index, |
| 138 int to_index) {} |
| 139 virtual void TabChangedAt(content::WebContents* contents, |
| 140 int index, |
| 141 TabChangeType change_type) {} |
| 142 virtual void TabReplacedAt(TabStripModel* tab_strip_model, |
| 143 content::WebContents* old_contents, |
| 144 content::WebContents* new_contents, |
| 145 int index) {} |
| 146 virtual void TabPinnedStateChanged(content::WebContents* contents, |
| 147 int index) {} |
| 148 virtual void TabMiniStateChanged(content::WebContents* contents, int index) {} |
| 149 virtual void TabBlockedStateChanged(content::WebContents* contents, |
| 150 int index) {} |
| 151 virtual void TabStripEmpty() {} |
| 152 virtual void TabStripModelDeleted() {} |
| 153 |
| 154 // Shortcut to tab_strip of browser(). |
| 155 TabStrip* tab_strip_; |
| 156 // History of tab activation. Scrub() resets it. |
| 157 std::vector<int> activation_order_; |
| 158 scoped_ptr<aura::test::EventGenerator> event_generator_; |
| 159 |
| 160 private: |
| 161 base::Closure quit_closure_; |
| 162 int target_index_; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(TabScrubberTest); |
| 165 }; |
| 166 |
| 167 } // namespace |
| 168 |
| 169 // Swipe a single tab in each direction. |
| 170 IN_PROC_BROWSER_TEST_F(TabScrubberTest, Single) { |
| 171 AddTabs(1); |
| 172 |
| 173 Scrub(0, EACH_TAB); |
| 174 RunUntilTabActive(0); |
| 175 EXPECT_EQ(1U, activation_order_.size()); |
| 176 EXPECT_EQ(0, activation_order_[0]); |
| 177 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 178 |
| 179 Scrub(1, EACH_TAB); |
| 180 RunUntilTabActive(1); |
| 181 EXPECT_EQ(1U, activation_order_.size()); |
| 182 EXPECT_EQ(1, activation_order_[0]); |
| 183 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 184 } |
| 185 |
| 186 // Swipe 4 tabs in each direction. Each of the tabs should become active. |
| 187 IN_PROC_BROWSER_TEST_F(TabScrubberTest, Multi) { |
| 188 AddTabs(4); |
| 189 |
| 190 Scrub(0, EACH_TAB); |
| 191 RunUntilTabActive(0); |
| 192 ASSERT_EQ(4U, activation_order_.size()); |
| 193 EXPECT_EQ(3, activation_order_[0]); |
| 194 EXPECT_EQ(2, activation_order_[1]); |
| 195 EXPECT_EQ(1, activation_order_[2]); |
| 196 EXPECT_EQ(0, activation_order_[3]); |
| 197 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 198 |
| 199 Scrub(4, EACH_TAB); |
| 200 RunUntilTabActive(4); |
| 201 ASSERT_EQ(4U, activation_order_.size()); |
| 202 EXPECT_EQ(1, activation_order_[0]); |
| 203 EXPECT_EQ(2, activation_order_[1]); |
| 204 EXPECT_EQ(3, activation_order_[2]); |
| 205 EXPECT_EQ(4, activation_order_[3]); |
| 206 EXPECT_EQ(4, browser()->tab_strip_model()->active_index()); |
| 207 } |
| 208 |
| 209 // Swipe 4 tabs in each direction with an extra swipe within each. The same |
| 210 // 4 tabs should become active. |
| 211 IN_PROC_BROWSER_TEST_F(TabScrubberTest, Repeated) { |
| 212 AddTabs(4); |
| 213 |
| 214 Scrub(0, REPEAT_TABS); |
| 215 RunUntilTabActive(0); |
| 216 ASSERT_EQ(4U, activation_order_.size()); |
| 217 EXPECT_EQ(3, activation_order_[0]); |
| 218 EXPECT_EQ(2, activation_order_[1]); |
| 219 EXPECT_EQ(1, activation_order_[2]); |
| 220 EXPECT_EQ(0, activation_order_[3]); |
| 221 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 222 |
| 223 Scrub(4, REPEAT_TABS); |
| 224 RunUntilTabActive(4); |
| 225 ASSERT_EQ(4U, activation_order_.size()); |
| 226 EXPECT_EQ(1, activation_order_[0]); |
| 227 EXPECT_EQ(2, activation_order_[1]); |
| 228 EXPECT_EQ(3, activation_order_[2]); |
| 229 EXPECT_EQ(4, activation_order_[3]); |
| 230 EXPECT_EQ(4, browser()->tab_strip_model()->active_index()); |
| 231 } |
| 232 |
| 233 // Confirm that we get the last tab made active when we skip tabs. |
| 234 // These tests have 5 total tabs. We will only received scroll events |
| 235 // on tabs 0, 2 and 4. |
| 236 IN_PROC_BROWSER_TEST_F(TabScrubberTest, Skipped) { |
| 237 AddTabs(4); |
| 238 |
| 239 Scrub(0, SKIP_TABS); |
| 240 RunUntilTabActive(0); |
| 241 EXPECT_EQ(2U, activation_order_.size()); |
| 242 EXPECT_EQ(2, activation_order_[0]); |
| 243 EXPECT_EQ(0, activation_order_[1]); |
| 244 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 245 |
| 246 Scrub(4, SKIP_TABS); |
| 247 RunUntilTabActive(4); |
| 248 EXPECT_EQ(2U, activation_order_.size()); |
| 249 EXPECT_EQ(2, activation_order_[0]); |
| 250 EXPECT_EQ(4, activation_order_[1]); |
| 251 EXPECT_EQ(4, browser()->tab_strip_model()->active_index()); |
| 252 } |
| 253 |
| 254 // Confirm that nothing happens when the swipe is small. |
| 255 IN_PROC_BROWSER_TEST_F(TabScrubberTest, NoChange) { |
| 256 AddTabs(1); |
| 257 |
| 258 event_generator_->ScrollSequence(gfx::Point(0, 0), |
| 259 base::TimeDelta::FromMilliseconds(100), |
| 260 -1, |
| 261 0, |
| 262 1, |
| 263 3); |
| 264 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 265 |
| 266 event_generator_->ScrollSequence(gfx::Point(0, 0), |
| 267 base::TimeDelta::FromMilliseconds(100), |
| 268 1, |
| 269 0, |
| 270 1, |
| 271 3); |
| 272 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 273 } |
| 274 |
| 275 // Confirm that very large swipes go to the beginning and and of the tabstrip. |
| 276 IN_PROC_BROWSER_TEST_F(TabScrubberTest, Bounds) { |
| 277 AddTabs(1); |
| 278 |
| 279 event_generator_->ScrollSequence(gfx::Point(0, 0), |
| 280 base::TimeDelta::FromMilliseconds(100), |
| 281 -10000, |
| 282 0, |
| 283 1, |
| 284 3); |
| 285 RunUntilTabActive(0); |
| 286 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 287 |
| 288 event_generator_->ScrollSequence(gfx::Point(0, 0), |
| 289 base::TimeDelta::FromMilliseconds(100), |
| 290 10000, |
| 291 0, |
| 292 1, |
| 293 3); |
| 294 RunUntilTabActive(1); |
| 295 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 296 } |
| 297 |
| 298 |
OLD | NEW |