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