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

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: Fixes from review. 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;
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());
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 // Looks up a tab based on its tab ID.
82 content::WebContents* FindWebContentsByTabId(SessionID::id_type tab_id) {
83 for (TabContentsIterator it; !it.done(); it.Next()) {
84 if (SessionTabHelper::IdForTab(*it) == tab_id)
85 return *it;
86 }
87 return nullptr;
88 }
89
90 // Returns the current TaskManagerTableModel index for a particular tab. Don't
91 // cache this value, since it can change whenever the message loop runs.
92 int FindRowForTab(content::WebContents* tab) {
93 int32_t tab_id = SessionTabHelper::IdForTab(tab);
94 auto tester = TaskManagerTester::Create(base::Closure());
sky 2016/05/19 19:08:47 This may be obvious to you, but it's totally not o
ncarter (slow) 2016/05/19 22:55:30 Done.
95 for (int i = 0; i < tester->GetRowCount(); ++i) {
96 if (tester->GetTabId(i) == tab_id)
97 return i;
98 }
99 return -1;
100 }
101
59 private: 102 private:
60 DISALLOW_COPY_AND_ASSIGN(NewTaskManagerViewTest); 103 DISALLOW_COPY_AND_ASSIGN(NewTaskManagerViewTest);
61 }; 104 };
62 105
63 // Tests that all defined columns have a corresponding string IDs for keying 106 // Tests that all defined columns have a corresponding string IDs for keying
64 // into the user preferences dictionary. 107 // into the user preferences dictionary.
65 IN_PROC_BROWSER_TEST_F(NewTaskManagerViewTest, AllColumnsHaveStringIds) { 108 IN_PROC_BROWSER_TEST_F(NewTaskManagerViewTest, AllColumnsHaveStringIds) {
66 for (size_t i = 0; i < kColumnsSize; ++i) 109 for (size_t i = 0; i < kColumnsSize; ++i)
67 EXPECT_NE("", GetColumnIdAsString(kColumns[i].id)); 110 EXPECT_NE("", GetColumnIdAsString(kColumns[i].id));
68 } 111 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 EXPECT_TRUE(table->is_sorted()); 182 EXPECT_TRUE(table->is_sorted());
140 EXPECT_FALSE(table->sort_descriptors().front().ascending); 183 EXPECT_FALSE(table->sort_descriptors().front().ascending);
141 EXPECT_EQ(table->sort_descriptors().front().column_id, sorted_col_id); 184 EXPECT_EQ(table->sort_descriptors().front().column_id, sorted_col_id);
142 } 185 }
143 for (size_t i = 0; i < kColumnsSize; ++i) { 186 for (size_t i = 0; i < kColumnsSize; ++i) {
144 EXPECT_EQ(!kColumns[i].default_visibility, 187 EXPECT_EQ(!kColumns[i].default_visibility,
145 table->IsColumnVisible(kColumns[i].id)); 188 table->IsColumnVisible(kColumns[i].id));
146 } 189 }
147 } 190 }
148 191
192 IN_PROC_BROWSER_TEST_F(NewTaskManagerViewTest, SelectionConsistency) {
193 chrome::ShowTaskManager(browser());
194
195 // Set up a total of three tabs in different processes.
196 ui_test_utils::NavigateToURL(
197 browser(), embedded_test_server()->GetURL("a.com", "/title2.html"));
198 ui_test_utils::NavigateToURLWithDisposition(
199 browser(), embedded_test_server()->GetURL("b.com", "/title2.html"),
200 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
201 ui_test_utils::NavigateToURLWithDisposition(
202 browser(), embedded_test_server()->GetURL("c.com", "/title2.html"),
203 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
204
205 // Wait for their titles to appear in the TaskManager. There should be three
206 // rows.
207 auto pattern = browsertest_util::MatchTab("Title *");
208 int rows = 3;
209 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(rows, pattern));
210
211 // Find the three tabs we set up, in TaskManager model order. Because we have
212 // not sorted the table yet, this should also be their UI display order.
213 auto tester = TaskManagerTester::Create(base::Closure());
214 std::vector<content::WebContents*> tabs;
215 for (int i = 0; i < tester->GetRowCount(); ++i) {
216 // Filter based on our title.
217 if (!base::MatchPattern(tester->GetRowTitle(i), pattern))
218 continue;
219 content::WebContents* tab = FindWebContentsByTabId(tester->GetTabId(i));
220 EXPECT_NE(nullptr, tab);
221 tabs.push_back(tab);
222 }
223 EXPECT_EQ(3U, tabs.size());
224
225 // Select the middle row, and store its tab id.
226 GetTable()->Select(FindRowForTab(tabs[1]));
227 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[1]));
228
229 // Add 3 rows above the selection. The selected tab should not change.
230 for (int i = 0; i < 3; ++i) {
231 ASSERT_TRUE(content::ExecuteScript(tabs[0], "window.open('title3.html');"));
232 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[1]));
233 }
234 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 3), pattern));
235 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[1]));
236
237 // Add 2 rows below the selection. The selected tab should not change.
238 for (int i = 0; i < 2; ++i) {
239 ASSERT_TRUE(content::ExecuteScript(tabs[2], "window.open('title3.html');"));
240 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[1]));
241 }
242 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 2), pattern));
243 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[1]));
244
245 // Add a new row in the same process as the selection. The selected tab should
246 // not change.
247 ASSERT_TRUE(content::ExecuteScript(tabs[1], "window.open('title3.html');"));
248 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[1]));
249 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 1), pattern));
250 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[1]));
251 EXPECT_EQ(1, GetTable()->SelectedRowCount());
252
253 // Press the button, which kills the process of the selected row.
254 PressKillButton();
255
256 // Two rows should disappear.
257 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows -= 2), pattern));
258
259 // A later row should now be selected. The selection should be after the 4
260 // rows sharing the tabs[0] process, and it should be at or before
261 // the tabs[2] row.
262 ASSERT_LT(FindRowForTab(tabs[0]) + 3, GetTable()->FirstSelectedRow());
263 ASSERT_LE(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[2]));
264
265 // Now select tabs[2].
266 GetTable()->Select(FindRowForTab(tabs[2]));
267
268 // Focus and reload one of the sad tabs. It should reappear in the TM. The
269 // other sad tab should not reappear.
270 tabs[1]->GetDelegate()->ActivateContents(tabs[1]);
271 chrome::Reload(browser(), CURRENT_TAB);
272 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows += 1), pattern));
273
274 // tabs[2] should still be selected.
275 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[2]));
276
277 // Close tabs[0]. The selection should not change.
278 chrome::CloseWebContents(browser(), tabs[0], false);
279 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows((rows -= 1), pattern));
280 EXPECT_EQ(GetTable()->FirstSelectedRow(), FindRowForTab(tabs[2]));
281 }
282
149 } // namespace task_management 283 } // namespace task_management
150 284
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698