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