OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 5 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 // When |source| is deleted both |tab_to_delete| and |tab_strip| are deleted. | 52 // When |source| is deleted both |tab_to_delete| and |tab_strip| are deleted. |
53 // |tab_to_delete| and |tab_strip| may be NULL. | 53 // |tab_to_delete| and |tab_strip| may be NULL. |
54 DeleteWebContentsOnDestroyedObserver(WebContents* source, | 54 DeleteWebContentsOnDestroyedObserver(WebContents* source, |
55 WebContents* tab_to_delete, | 55 WebContents* tab_to_delete, |
56 TabStripModel* tab_strip) | 56 TabStripModel* tab_strip) |
57 : WebContentsObserver(source), | 57 : WebContentsObserver(source), |
58 tab_to_delete_(tab_to_delete), | 58 tab_to_delete_(tab_to_delete), |
59 tab_strip_(tab_strip) { | 59 tab_strip_(tab_strip) { |
60 } | 60 } |
61 | 61 |
62 virtual void WebContentsDestroyed() override { | 62 void WebContentsDestroyed() override { |
63 WebContents* tab_to_delete = tab_to_delete_; | 63 WebContents* tab_to_delete = tab_to_delete_; |
64 tab_to_delete_ = NULL; | 64 tab_to_delete_ = NULL; |
65 TabStripModel* tab_strip_to_delete = tab_strip_; | 65 TabStripModel* tab_strip_to_delete = tab_strip_; |
66 tab_strip_ = NULL; | 66 tab_strip_ = NULL; |
67 delete tab_to_delete; | 67 delete tab_to_delete; |
68 delete tab_strip_to_delete; | 68 delete tab_strip_to_delete; |
69 } | 69 } |
70 | 70 |
71 private: | 71 private: |
72 WebContents* tab_to_delete_; | 72 WebContents* tab_to_delete_; |
73 TabStripModel* tab_strip_; | 73 TabStripModel* tab_strip_; |
74 | 74 |
75 DISALLOW_COPY_AND_ASSIGN(DeleteWebContentsOnDestroyedObserver); | 75 DISALLOW_COPY_AND_ASSIGN(DeleteWebContentsOnDestroyedObserver); |
76 }; | 76 }; |
77 | 77 |
78 class TabStripDummyDelegate : public TestTabStripModelDelegate { | 78 class TabStripDummyDelegate : public TestTabStripModelDelegate { |
79 public: | 79 public: |
80 TabStripDummyDelegate() : run_unload_(false) {} | 80 TabStripDummyDelegate() : run_unload_(false) {} |
81 virtual ~TabStripDummyDelegate() {} | 81 ~TabStripDummyDelegate() override {} |
82 | 82 |
83 void set_run_unload_listener(bool value) { run_unload_ = value; } | 83 void set_run_unload_listener(bool value) { run_unload_ = value; } |
84 | 84 |
85 virtual bool RunUnloadListenerBeforeClosing(WebContents* contents) override { | 85 bool RunUnloadListenerBeforeClosing(WebContents* contents) override { |
86 return run_unload_; | 86 return run_unload_; |
87 } | 87 } |
88 | 88 |
89 private: | 89 private: |
90 // Whether to report that we need to run an unload listener before closing. | 90 // Whether to report that we need to run an unload listener before closing. |
91 bool run_unload_; | 91 bool run_unload_; |
92 | 92 |
93 DISALLOW_COPY_AND_ASSIGN(TabStripDummyDelegate); | 93 DISALLOW_COPY_AND_ASSIGN(TabStripDummyDelegate); |
94 }; | 94 }; |
95 | 95 |
96 const char kTabStripModelTestIDUserDataKey[] = "TabStripModelTestIDUserData"; | 96 const char kTabStripModelTestIDUserDataKey[] = "TabStripModelTestIDUserData"; |
97 | 97 |
98 class TabStripModelTestIDUserData : public base::SupportsUserData::Data { | 98 class TabStripModelTestIDUserData : public base::SupportsUserData::Data { |
99 public: | 99 public: |
100 explicit TabStripModelTestIDUserData(int id) : id_(id) {} | 100 explicit TabStripModelTestIDUserData(int id) : id_(id) {} |
101 virtual ~TabStripModelTestIDUserData() {} | 101 ~TabStripModelTestIDUserData() override {} |
102 int id() { return id_; } | 102 int id() { return id_; } |
103 | 103 |
104 private: | 104 private: |
105 int id_; | 105 int id_; |
106 }; | 106 }; |
107 | 107 |
108 class DummySingleWebContentsDialogManager | 108 class DummySingleWebContentsDialogManager |
109 : public web_modal::SingleWebContentsDialogManager { | 109 : public web_modal::SingleWebContentsDialogManager { |
110 public: | 110 public: |
111 explicit DummySingleWebContentsDialogManager( | 111 explicit DummySingleWebContentsDialogManager( |
112 NativeWebContentsModalDialog dialog, | 112 NativeWebContentsModalDialog dialog, |
113 web_modal::SingleWebContentsDialogManagerDelegate* delegate) | 113 web_modal::SingleWebContentsDialogManagerDelegate* delegate) |
114 : delegate_(delegate), | 114 : delegate_(delegate), |
115 dialog_(dialog) {} | 115 dialog_(dialog) {} |
116 virtual ~DummySingleWebContentsDialogManager() {} | 116 ~DummySingleWebContentsDialogManager() override {} |
117 | 117 |
118 virtual void Show() override {} | 118 void Show() override {} |
119 virtual void Hide() override {} | 119 void Hide() override {} |
120 virtual void Close() override { | 120 void Close() override { delegate_->WillClose(dialog_); } |
121 delegate_->WillClose(dialog_); | 121 void Focus() override {} |
122 } | 122 void Pulse() override {} |
123 virtual void Focus() override {} | 123 void HostChanged(web_modal::WebContentsModalDialogHost* new_host) override {} |
124 virtual void Pulse() override {} | 124 NativeWebContentsModalDialog dialog() override { return dialog_; } |
125 virtual void HostChanged( | |
126 web_modal::WebContentsModalDialogHost* new_host) override {} | |
127 virtual NativeWebContentsModalDialog dialog() override { return dialog_; } | |
128 | 125 |
129 private: | 126 private: |
130 web_modal::SingleWebContentsDialogManagerDelegate* delegate_; | 127 web_modal::SingleWebContentsDialogManagerDelegate* delegate_; |
131 NativeWebContentsModalDialog dialog_; | 128 NativeWebContentsModalDialog dialog_; |
132 | 129 |
133 DISALLOW_COPY_AND_ASSIGN(DummySingleWebContentsDialogManager); | 130 DISALLOW_COPY_AND_ASSIGN(DummySingleWebContentsDialogManager); |
134 }; | 131 }; |
135 | 132 |
136 // Test Browser-like class for TabStripModelTest.TabBlockedState. | 133 // Test Browser-like class for TabStripModelTest.TabBlockedState. |
137 class TabBlockedStateTestBrowser | 134 class TabBlockedStateTestBrowser |
138 : public TabStripModelObserver, | 135 : public TabStripModelObserver, |
139 public web_modal::WebContentsModalDialogManagerDelegate { | 136 public web_modal::WebContentsModalDialogManagerDelegate { |
140 public: | 137 public: |
141 explicit TabBlockedStateTestBrowser(TabStripModel* tab_strip_model) | 138 explicit TabBlockedStateTestBrowser(TabStripModel* tab_strip_model) |
142 : tab_strip_model_(tab_strip_model) { | 139 : tab_strip_model_(tab_strip_model) { |
143 tab_strip_model_->AddObserver(this); | 140 tab_strip_model_->AddObserver(this); |
144 } | 141 } |
145 | 142 |
146 virtual ~TabBlockedStateTestBrowser() { | 143 ~TabBlockedStateTestBrowser() override { |
147 tab_strip_model_->RemoveObserver(this); | 144 tab_strip_model_->RemoveObserver(this); |
148 } | 145 } |
149 | 146 |
150 private: | 147 private: |
151 // TabStripModelObserver | 148 // TabStripModelObserver |
152 virtual void TabInsertedAt(WebContents* contents, | 149 void TabInsertedAt(WebContents* contents, |
153 int index, | 150 int index, |
154 bool foreground) override { | 151 bool foreground) override { |
155 web_modal::WebContentsModalDialogManager* manager = | 152 web_modal::WebContentsModalDialogManager* manager = |
156 web_modal::WebContentsModalDialogManager::FromWebContents(contents); | 153 web_modal::WebContentsModalDialogManager::FromWebContents(contents); |
157 if (manager) | 154 if (manager) |
158 manager->SetDelegate(this); | 155 manager->SetDelegate(this); |
159 } | 156 } |
160 | 157 |
161 // WebContentsModalDialogManagerDelegate | 158 // WebContentsModalDialogManagerDelegate |
162 virtual void SetWebContentsBlocked(content::WebContents* contents, | 159 void SetWebContentsBlocked(content::WebContents* contents, |
163 bool blocked) override { | 160 bool blocked) override { |
164 int index = tab_strip_model_->GetIndexOfWebContents(contents); | 161 int index = tab_strip_model_->GetIndexOfWebContents(contents); |
165 ASSERT_GE(index, 0); | 162 ASSERT_GE(index, 0); |
166 tab_strip_model_->SetTabBlocked(index, blocked); | 163 tab_strip_model_->SetTabBlocked(index, blocked); |
167 } | 164 } |
168 | 165 |
169 TabStripModel* tab_strip_model_; | 166 TabStripModel* tab_strip_model_; |
170 | 167 |
171 DISALLOW_COPY_AND_ASSIGN(TabBlockedStateTestBrowser); | 168 DISALLOW_COPY_AND_ASSIGN(TabBlockedStateTestBrowser); |
172 }; | 169 }; |
173 | 170 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 model->SetSelectionFromModel(selection_model); | 259 model->SetSelectionFromModel(selection_model); |
263 } | 260 } |
264 }; | 261 }; |
265 | 262 |
266 class MockTabStripModelObserver : public TabStripModelObserver { | 263 class MockTabStripModelObserver : public TabStripModelObserver { |
267 public: | 264 public: |
268 explicit MockTabStripModelObserver(TabStripModel* model) | 265 explicit MockTabStripModelObserver(TabStripModel* model) |
269 : empty_(true), | 266 : empty_(true), |
270 deleted_(false), | 267 deleted_(false), |
271 model_(model) {} | 268 model_(model) {} |
272 virtual ~MockTabStripModelObserver() {} | 269 ~MockTabStripModelObserver() override {} |
273 | 270 |
274 enum TabStripModelObserverAction { | 271 enum TabStripModelObserverAction { |
275 INSERT, | 272 INSERT, |
276 CLOSE, | 273 CLOSE, |
277 DETACH, | 274 DETACH, |
278 ACTIVATE, | 275 ACTIVATE, |
279 DEACTIVATE, | 276 DEACTIVATE, |
280 SELECT, | 277 SELECT, |
281 MOVE, | 278 MOVE, |
282 CHANGE, | 279 CHANGE, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 return (s.src_contents == state.src_contents && | 338 return (s.src_contents == state.src_contents && |
342 s.dst_contents == state.dst_contents && | 339 s.dst_contents == state.dst_contents && |
343 s.src_index == state.src_index && | 340 s.src_index == state.src_index && |
344 s.dst_index == state.dst_index && | 341 s.dst_index == state.dst_index && |
345 s.change_reason == state.change_reason && | 342 s.change_reason == state.change_reason && |
346 s.foreground == state.foreground && | 343 s.foreground == state.foreground && |
347 s.action == state.action); | 344 s.action == state.action); |
348 } | 345 } |
349 | 346 |
350 // TabStripModelObserver implementation: | 347 // TabStripModelObserver implementation: |
351 virtual void TabInsertedAt(WebContents* contents, | 348 void TabInsertedAt(WebContents* contents, |
352 int index, | 349 int index, |
353 bool foreground) override { | 350 bool foreground) override { |
354 empty_ = false; | 351 empty_ = false; |
355 State s(contents, index, INSERT); | 352 State s(contents, index, INSERT); |
356 s.foreground = foreground; | 353 s.foreground = foreground; |
357 states_.push_back(s); | 354 states_.push_back(s); |
358 } | 355 } |
359 virtual void ActiveTabChanged(WebContents* old_contents, | 356 void ActiveTabChanged(WebContents* old_contents, |
360 WebContents* new_contents, | 357 WebContents* new_contents, |
361 int index, | 358 int index, |
362 int reason) override { | 359 int reason) override { |
363 State s(new_contents, index, ACTIVATE); | 360 State s(new_contents, index, ACTIVATE); |
364 s.src_contents = old_contents; | 361 s.src_contents = old_contents; |
365 s.change_reason = reason; | 362 s.change_reason = reason; |
366 states_.push_back(s); | 363 states_.push_back(s); |
367 } | 364 } |
368 virtual void TabSelectionChanged( | 365 void TabSelectionChanged(TabStripModel* tab_strip_model, |
369 TabStripModel* tab_strip_model, | 366 const ui::ListSelectionModel& old_model) override { |
370 const ui::ListSelectionModel& old_model) override { | |
371 State s(model()->GetActiveWebContents(), model()->active_index(), SELECT); | 367 State s(model()->GetActiveWebContents(), model()->active_index(), SELECT); |
372 s.src_contents = model()->GetWebContentsAt(old_model.active()); | 368 s.src_contents = model()->GetWebContentsAt(old_model.active()); |
373 s.src_index = old_model.active(); | 369 s.src_index = old_model.active(); |
374 states_.push_back(s); | 370 states_.push_back(s); |
375 } | 371 } |
376 virtual void TabMoved(WebContents* contents, | 372 void TabMoved(WebContents* contents, int from_index, int to_index) override { |
377 int from_index, | |
378 int to_index) override { | |
379 State s(contents, to_index, MOVE); | 373 State s(contents, to_index, MOVE); |
380 s.src_index = from_index; | 374 s.src_index = from_index; |
381 states_.push_back(s); | 375 states_.push_back(s); |
382 } | 376 } |
383 | 377 |
384 virtual void TabClosingAt(TabStripModel* tab_strip_model, | 378 void TabClosingAt(TabStripModel* tab_strip_model, |
385 WebContents* contents, | 379 WebContents* contents, |
386 int index) override { | 380 int index) override { |
387 states_.push_back(State(contents, index, CLOSE)); | 381 states_.push_back(State(contents, index, CLOSE)); |
388 } | 382 } |
389 virtual void TabDetachedAt(WebContents* contents, int index) override { | 383 void TabDetachedAt(WebContents* contents, int index) override { |
390 states_.push_back(State(contents, index, DETACH)); | 384 states_.push_back(State(contents, index, DETACH)); |
391 } | 385 } |
392 virtual void TabDeactivated(WebContents* contents) override { | 386 void TabDeactivated(WebContents* contents) override { |
393 states_.push_back(State(contents, model()->active_index(), DEACTIVATE)); | 387 states_.push_back(State(contents, model()->active_index(), DEACTIVATE)); |
394 } | 388 } |
395 virtual void TabChangedAt(WebContents* contents, | 389 void TabChangedAt(WebContents* contents, |
396 int index, | 390 int index, |
397 TabChangeType change_type) override { | 391 TabChangeType change_type) override { |
398 states_.push_back(State(contents, index, CHANGE)); | 392 states_.push_back(State(contents, index, CHANGE)); |
399 } | 393 } |
400 virtual void TabReplacedAt(TabStripModel* tab_strip_model, | 394 void TabReplacedAt(TabStripModel* tab_strip_model, |
401 WebContents* old_contents, | 395 WebContents* old_contents, |
402 WebContents* new_contents, | 396 WebContents* new_contents, |
403 int index) override { | 397 int index) override { |
404 State s(new_contents, index, REPLACED); | 398 State s(new_contents, index, REPLACED); |
405 s.src_contents = old_contents; | 399 s.src_contents = old_contents; |
406 states_.push_back(s); | 400 states_.push_back(s); |
407 } | 401 } |
408 virtual void TabPinnedStateChanged(WebContents* contents, | 402 void TabPinnedStateChanged(WebContents* contents, int index) override { |
409 int index) override { | |
410 states_.push_back(State(contents, index, PINNED)); | 403 states_.push_back(State(contents, index, PINNED)); |
411 } | 404 } |
412 virtual void TabStripEmpty() override { | 405 void TabStripEmpty() override { empty_ = true; } |
413 empty_ = true; | 406 void WillCloseAllTabs() override { |
414 } | |
415 virtual void WillCloseAllTabs() override { | |
416 states_.push_back(State(NULL, -1, CLOSE_ALL)); | 407 states_.push_back(State(NULL, -1, CLOSE_ALL)); |
417 } | 408 } |
418 virtual void CloseAllTabsCanceled() override { | 409 void CloseAllTabsCanceled() override { |
419 states_.push_back(State(NULL, -1, CLOSE_ALL_CANCELED)); | 410 states_.push_back(State(NULL, -1, CLOSE_ALL_CANCELED)); |
420 } | 411 } |
421 virtual void TabStripModelDeleted() override { | 412 void TabStripModelDeleted() override { deleted_ = true; } |
422 deleted_ = true; | |
423 } | |
424 | 413 |
425 void ClearStates() { | 414 void ClearStates() { |
426 states_.clear(); | 415 states_.clear(); |
427 } | 416 } |
428 | 417 |
429 bool empty() const { return empty_; } | 418 bool empty() const { return empty_; } |
430 bool deleted() const { return deleted_; } | 419 bool deleted() const { return deleted_; } |
431 TabStripModel* model() { return model_; } | 420 TabStripModel* model() { return model_; } |
432 | 421 |
433 private: | 422 private: |
(...skipping 2150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2584 WebContents* moved_contents = strip_src.DetachWebContentsAt(1); | 2573 WebContents* moved_contents = strip_src.DetachWebContentsAt(1); |
2585 EXPECT_EQ(contents2, moved_contents); | 2574 EXPECT_EQ(contents2, moved_contents); |
2586 | 2575 |
2587 // Attach the tab to the destination tab strip. | 2576 // Attach the tab to the destination tab strip. |
2588 strip_dst.AppendWebContents(moved_contents, true); | 2577 strip_dst.AppendWebContents(moved_contents, true); |
2589 EXPECT_TRUE(strip_dst.IsTabBlocked(0)); | 2578 EXPECT_TRUE(strip_dst.IsTabBlocked(0)); |
2590 | 2579 |
2591 strip_dst.CloseAllTabs(); | 2580 strip_dst.CloseAllTabs(); |
2592 strip_src.CloseAllTabs(); | 2581 strip_src.CloseAllTabs(); |
2593 } | 2582 } |
OLD | NEW |