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

Side by Side Diff: chrome/browser/ui/views/new_task_manager_view_browsertest.cc

Issue 1992623002: Task Manager: Preserve selection when rows are added (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Self-review fixes. Created 4 years, 7 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/strings/pattern.h"
8 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/sessions/session_tab_helper.h"
11 #include "chrome/browser/task_management/task_manager_browsertest_util.h"
12 #include "chrome/browser/task_management/task_manager_tester.h"
13 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/browser_dialogs.h" 14 #include "chrome/browser/ui/browser_dialogs.h"
15 #include "chrome/browser/ui/browser_tabstrip.h"
16 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
10 #include "chrome/browser/ui/task_manager/task_manager_columns.h" 17 #include "chrome/browser/ui/task_manager/task_manager_columns.h"
11 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" 18 #include "chrome/browser/ui/task_manager/task_manager_table_model.h"
12 #include "chrome/browser/ui/views/new_task_manager_view.h" 19 #include "chrome/browser/ui/views/new_task_manager_view.h"
13 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/in_process_browser_test.h" 21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "chrome/test/base/ui_test_utils.h"
15 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
16 #include "components/prefs/scoped_user_pref_update.h" 24 #include "components/prefs/scoped_user_pref_update.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_delegate.h"
27 #include "content/public/test/browser_test_utils.h"
17 #include "content/public/test/test_utils.h" 28 #include "content/public/test/test_utils.h"
29 #include "net/dns/mock_host_resolver.h"
30 #include "net/test/embedded_test_server/embedded_test_server.h"
18 #include "ui/views/controls/table/table_view.h" 31 #include "ui/views/controls/table/table_view.h"
19 32
20 namespace task_management { 33 namespace task_management {
21 34
35 using browsertest_util::WaitForTaskManagerRows;
afakhry 2016/05/18 23:12:19 Porting those tests paid off really well after all
ncarter (slow) 2016/05/19 00:12:58 Acknowledged.
36
22 class NewTaskManagerViewTest : public InProcessBrowserTest { 37 class NewTaskManagerViewTest : public InProcessBrowserTest {
23 public: 38 public:
24 NewTaskManagerViewTest() {} 39 NewTaskManagerViewTest() {}
25 ~NewTaskManagerViewTest() override {} 40 ~NewTaskManagerViewTest() override {}
26 41
42 void SetUpOnMainThread() override {
43 host_resolver()->AddRule("*", "127.0.0.1");
44 ASSERT_TRUE(embedded_test_server()->Start());
afakhry 2016/05/18 23:12:19 Shouldn't you be calling here the parent's SetUpOn
ncarter (slow) 2016/05/19 00:12:59 InProcessBrowserTest::SetUpOnMainThread has an emp
afakhry 2016/05/19 00:57:33 It would be safer to call it though, just in case
ncarter (slow) 2016/05/19 17:12:34 I don't think this is likely to happen. This funct
45 }
46
27 void TearDownOnMainThread() override { 47 void TearDownOnMainThread() override {
28 // Make sure the task manager is closed (if any). 48 // Make sure the task manager is closed (if any).
29 chrome::HideTaskManager(); 49 chrome::HideTaskManager();
30 content::RunAllPendingInMessageLoop(); 50 content::RunAllPendingInMessageLoop();
31 ASSERT_FALSE(GetView()); 51 ASSERT_FALSE(GetView());
32 52
33 InProcessBrowserTest::TearDownOnMainThread(); 53 InProcessBrowserTest::TearDownOnMainThread();
34 } 54 }
35 55
36 NewTaskManagerView* GetView() const { 56 NewTaskManagerView* GetView() const {
37 return NewTaskManagerView::GetInstanceForTests(); 57 return NewTaskManagerView::GetInstanceForTests();
38 } 58 }
39 59
40 views::TableView* GetTable() const { 60 views::TableView* GetTable() const {
41 return GetView() ? GetView()->tab_table_ : nullptr; 61 return GetView() ? GetView()->tab_table_ : nullptr;
42 } 62 }
43 63
64 void PressKillButton() { GetView()->Accept(); }
65
44 void ClearStoredColumnSettings() const { 66 void ClearStoredColumnSettings() const {
45 PrefService* local_state = g_browser_process->local_state(); 67 PrefService* local_state = g_browser_process->local_state();
46 if (!local_state) 68 if (!local_state)
47 FAIL(); 69 FAIL();
48 70
49 DictionaryPrefUpdate dict_update(local_state, 71 DictionaryPrefUpdate dict_update(local_state,
50 prefs::kTaskManagerColumnVisibility); 72 prefs::kTaskManagerColumnVisibility);
51 dict_update->Clear(); 73 dict_update->Clear();
52 } 74 }
53 75
54 void ToggleColumnVisibility(NewTaskManagerView* view, int col_id) { 76 void ToggleColumnVisibility(NewTaskManagerView* view, int col_id) {
55 DCHECK(view); 77 DCHECK(view);
56 view->table_model_->ToggleColumnVisibility(col_id); 78 view->table_model_->ToggleColumnVisibility(col_id);
57 } 79 }
58 80
81 content::WebContents* FindTabById(int32_t tab_id) {
82 for (TabContentsIterator it; !it.done(); it.Next()) {
83 if (SessionTabHelper::IdForTab(*it) == tab_id) {
afakhry 2016/05/18 23:12:19 Nit: ditto for the curly braces.
ncarter (slow) 2016/05/19 00:12:58 Done.
84 return *it;
85 }
86 }
87 return nullptr;
88 }
89
90 int GetRowForTab(content::WebContents* tab) {
afakhry 2016/05/18 23:12:19 Nit: (optional) Maybe prefer to call this: int G
ncarter (slow) 2016/05/19 00:12:59 I used FindWebContentsForTabId and FindRowForTab()
afakhry 2016/05/19 00:57:33 Acknowledged.
91 int32_t tab_id = SessionTabHelper::IdForTab(tab);
92 auto tester = TaskManagerTester::Create(base::Closure());
afakhry 2016/05/18 23:12:19 Do we create the TaskManagerTester every time we c
ncarter (slow) 2016/05/19 00:12:59 I'll consider this if we need to use the tester in
afakhry 2016/05/19 00:57:33 Acknowledged.
93 for (int i = 0; i < tester->GetRowCount(); i++) {
afakhry 2016/05/18 23:12:19 Nit: (Not that it matters much here, but ...) ++i
ncarter (slow) 2016/05/19 00:12:58 Done.
94 if (tester->GetTabId(i) == tab_id)
95 return i;
96 }
97 return -1;
98 }
99
59 private: 100 private:
60 DISALLOW_COPY_AND_ASSIGN(NewTaskManagerViewTest); 101 DISALLOW_COPY_AND_ASSIGN(NewTaskManagerViewTest);
61 }; 102 };
62 103
63 // Tests that all defined columns have a corresponding string IDs for keying 104 // Tests that all defined columns have a corresponding string IDs for keying
64 // into the user preferences dictionary. 105 // into the user preferences dictionary.
65 IN_PROC_BROWSER_TEST_F(NewTaskManagerViewTest, AllColumnsHaveStringIds) { 106 IN_PROC_BROWSER_TEST_F(NewTaskManagerViewTest, AllColumnsHaveStringIds) {
66 for (size_t i = 0; i < kColumnsSize; ++i) 107 for (size_t i = 0; i < kColumnsSize; ++i)
67 EXPECT_NE("", GetColumnIdAsString(kColumns[i].id)); 108 EXPECT_NE("", GetColumnIdAsString(kColumns[i].id));
68 } 109 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 EXPECT_TRUE(table->is_sorted()); 180 EXPECT_TRUE(table->is_sorted());
140 EXPECT_FALSE(table->sort_descriptors().front().ascending); 181 EXPECT_FALSE(table->sort_descriptors().front().ascending);
141 EXPECT_EQ(table->sort_descriptors().front().column_id, sorted_col_id); 182 EXPECT_EQ(table->sort_descriptors().front().column_id, sorted_col_id);
142 } 183 }
143 for (size_t i = 0; i < kColumnsSize; ++i) { 184 for (size_t i = 0; i < kColumnsSize; ++i) {
144 EXPECT_EQ(!kColumns[i].default_visibility, 185 EXPECT_EQ(!kColumns[i].default_visibility,
145 table->IsColumnVisible(kColumns[i].id)); 186 table->IsColumnVisible(kColumns[i].id));
146 } 187 }
147 } 188 }
148 189
190 IN_PROC_BROWSER_TEST_F(NewTaskManagerViewTest, SelectionConsistency) {
191 chrome::ShowTaskManager(browser());
192
193 // Set up a total of three tabs in different processes.
194 ui_test_utils::NavigateToURL(
195 browser(), embedded_test_server()->GetURL("a.com", "/title2.html"));
196 ui_test_utils::NavigateToURLWithDisposition(
197 browser(), embedded_test_server()->GetURL("b.com", "/title2.html"),
198 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
199 ui_test_utils::NavigateToURLWithDisposition(
200 browser(), embedded_test_server()->GetURL("c.com", "/title2.html"),
201 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
202
203 // Wait for their titles to appear in the TaskManager. There should be three
204 // rows.
205 auto pattern = browsertest_util::MatchTab("Title *");
206 int rows = 3;
207 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(rows, pattern));
208
209 // Find the three tabs we set up, in TaskManager model order. Because we have
210 // not sorted the table yet, this should also be their UI display order.
211 auto tester = TaskManagerTester::Create(base::Closure());
212 std::vector<content::WebContents*> tabs;
213 for (int i = 0; i < tester->GetRowCount(); i++) {
afakhry 2016/05/18 23:12:19 Nit: ++i
ncarter (slow) 2016/05/19 00:12:58 Done.
214 // Filter based on our title.
215 if (!base::MatchPattern(tester->GetRowTitle(i), pattern))
216 continue;
217 tabs.push_back(FindTabById(tester->GetTabId(i)));
afakhry 2016/05/18 23:12:19 Since FindTabById() can return nullptr, maybe cons
ncarter (slow) 2016/05/19 00:12:58 Done; I just added an EXPECT.
218 }
219 EXPECT_EQ(3U, tabs.size());
220
221 // Select the middle row, and store its tab id.
222 GetTable()->Select(GetRowForTab(tabs[1]));
223 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[1]));
224
225 // Add 3 rows above the selection. The selected tab should not change.
226 for (int i = 0; i < 3; i++) {
afakhry 2016/05/18 23:12:19 Nit: ++i
ncarter (slow) 2016/05/19 00:12:58 Done.
227 ASSERT_TRUE(content::ExecuteScript(tabs[0], "window.open('title3.html');"));
228 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[1]));
229 }
230 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 3), pattern));
231 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[1]));
232
233 // Add 2 rows below the selection. The selected tab should not change.
234 for (int i = 0; i < 2; i++) {
afakhry 2016/05/18 23:12:19 Nit: ++i
ncarter (slow) 2016/05/19 00:12:59 Done.
235 ASSERT_TRUE(content::ExecuteScript(tabs[2], "window.open('title3.html');"));
236 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[1]));
237 }
238 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 2), pattern));
239 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[1]));
240
241 // Add a new row in the same process as the selection. The selected tab should
242 // not change.
243 ASSERT_TRUE(content::ExecuteScript(tabs[1], "window.open('title3.html');"));
244 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[1]));
245 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 1), pattern));
246 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[1]));
247 EXPECT_EQ(1, GetTable()->SelectedRowCount());
248
249 // Press the button, which kills the process of the selected row.
250 PressKillButton();
251
252 // Two rows should disappear.
253 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows -= 2), pattern));
254
255 // A later row should now be selected. The selection should be after the 4
256 // rows sharing the tabs[0] process, and it should be at or before
257 // the tabs[2] row.
258 ASSERT_LT(GetRowForTab(tabs[0]) + 3, GetTable()->FirstSelectedRow());
259 ASSERT_LE(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[2]));
260
261 // Now select tabs[2].
262 GetTable()->Select(GetRowForTab(tabs[2]));
263
264 // Focus and reload one of the sad tabs. It should reappear in the TM. The
265 // other sad tab should not reappear.
266 tabs[1]->GetDelegate()->ActivateContents(tabs[1]);
267 chrome::Reload(browser(), CURRENT_TAB);
268 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 1), pattern));
269
270 // tabs[2] should still be selected.
271 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[2]));
272
273 // Close tabs[0]. The selection should not change.
274 chrome::CloseWebContents(browser(), tabs[0], false);
275 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows -= 1), pattern));
276 EXPECT_EQ(GetTable()->FirstSelectedRow(), GetRowForTab(tabs[2]));
277 }
278
149 } // namespace task_management 279 } // namespace task_management
150 280
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698