| 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 <algorithm> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "base/strings/string16.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "chrome/browser/chromeos/memory/oom_priority_manager.h" | |
| 12 #include "chrome/common/url_constants.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "url/gurl.h" | |
| 15 | |
| 16 namespace chromeos { | |
| 17 | |
| 18 typedef testing::Test OomPriorityManagerTest; | |
| 19 | |
| 20 namespace { | |
| 21 enum TestIndicies { | |
| 22 kSelected, | |
| 23 kPinned, | |
| 24 kApp, | |
| 25 kPlayingAudio, | |
| 26 kRecent, | |
| 27 kOld, | |
| 28 kReallyOld, | |
| 29 kOldButPinned, | |
| 30 kReloadableUI, | |
| 31 }; | |
| 32 } // namespace | |
| 33 | |
| 34 // Tests the sorting comparator so that we know it's producing the | |
| 35 // desired order. | |
| 36 TEST_F(OomPriorityManagerTest, Comparator) { | |
| 37 chromeos::OomPriorityManager::TabStatsList test_list; | |
| 38 const base::TimeTicks now = base::TimeTicks::Now(); | |
| 39 | |
| 40 // Add kSelected last to verify we are sorting the array. | |
| 41 | |
| 42 { | |
| 43 OomPriorityManager::TabStats stats; | |
| 44 stats.is_pinned = true; | |
| 45 stats.renderer_handle = kPinned; | |
| 46 stats.child_process_host_id = kPinned; | |
| 47 test_list.push_back(stats); | |
| 48 } | |
| 49 | |
| 50 { | |
| 51 OomPriorityManager::TabStats stats; | |
| 52 stats.is_app = true; | |
| 53 stats.renderer_handle = kApp; | |
| 54 stats.child_process_host_id = kApp; | |
| 55 test_list.push_back(stats); | |
| 56 } | |
| 57 | |
| 58 { | |
| 59 OomPriorityManager::TabStats stats; | |
| 60 stats.is_playing_audio = true; | |
| 61 stats.renderer_handle = kPlayingAudio; | |
| 62 stats.child_process_host_id = kPlayingAudio; | |
| 63 test_list.push_back(stats); | |
| 64 } | |
| 65 | |
| 66 { | |
| 67 OomPriorityManager::TabStats stats; | |
| 68 stats.last_active = now - base::TimeDelta::FromSeconds(10); | |
| 69 stats.renderer_handle = kRecent; | |
| 70 stats.child_process_host_id = kRecent; | |
| 71 test_list.push_back(stats); | |
| 72 } | |
| 73 | |
| 74 { | |
| 75 OomPriorityManager::TabStats stats; | |
| 76 stats.last_active = now - base::TimeDelta::FromMinutes(15); | |
| 77 stats.renderer_handle = kOld; | |
| 78 stats.child_process_host_id = kOld; | |
| 79 test_list.push_back(stats); | |
| 80 } | |
| 81 | |
| 82 { | |
| 83 OomPriorityManager::TabStats stats; | |
| 84 stats.last_active = now - base::TimeDelta::FromDays(365); | |
| 85 stats.renderer_handle = kReallyOld; | |
| 86 stats.child_process_host_id = kReallyOld; | |
| 87 test_list.push_back(stats); | |
| 88 } | |
| 89 | |
| 90 { | |
| 91 OomPriorityManager::TabStats stats; | |
| 92 stats.is_pinned = true; | |
| 93 stats.last_active = now - base::TimeDelta::FromDays(365); | |
| 94 stats.renderer_handle = kOldButPinned; | |
| 95 stats.child_process_host_id = kOldButPinned; | |
| 96 test_list.push_back(stats); | |
| 97 } | |
| 98 | |
| 99 { | |
| 100 OomPriorityManager::TabStats stats; | |
| 101 stats.is_reloadable_ui = true; | |
| 102 stats.renderer_handle = kReloadableUI; | |
| 103 stats.child_process_host_id = kReloadableUI; | |
| 104 test_list.push_back(stats); | |
| 105 } | |
| 106 | |
| 107 // This entry sorts to the front, so by adding it last we verify that | |
| 108 // we are actually sorting the array. | |
| 109 { | |
| 110 OomPriorityManager::TabStats stats; | |
| 111 stats.is_selected = true; | |
| 112 stats.renderer_handle = kSelected; | |
| 113 stats.child_process_host_id = kSelected; | |
| 114 test_list.push_back(stats); | |
| 115 } | |
| 116 | |
| 117 std::sort(test_list.begin(), | |
| 118 test_list.end(), | |
| 119 OomPriorityManager::CompareTabStats); | |
| 120 | |
| 121 int index = 0; | |
| 122 EXPECT_EQ(kSelected, test_list[index++].renderer_handle); | |
| 123 EXPECT_EQ(kPinned, test_list[index++].renderer_handle); | |
| 124 EXPECT_EQ(kOldButPinned, test_list[index++].renderer_handle); | |
| 125 EXPECT_EQ(kApp, test_list[index++].renderer_handle); | |
| 126 EXPECT_EQ(kPlayingAudio, test_list[index++].renderer_handle); | |
| 127 EXPECT_EQ(kRecent, test_list[index++].renderer_handle); | |
| 128 EXPECT_EQ(kOld, test_list[index++].renderer_handle); | |
| 129 EXPECT_EQ(kReallyOld, test_list[index++].renderer_handle); | |
| 130 EXPECT_EQ(kReloadableUI, test_list[index++].renderer_handle); | |
| 131 | |
| 132 index = 0; | |
| 133 EXPECT_EQ(kSelected, test_list[index++].child_process_host_id); | |
| 134 EXPECT_EQ(kPinned, test_list[index++].child_process_host_id); | |
| 135 EXPECT_EQ(kOldButPinned, test_list[index++].child_process_host_id); | |
| 136 EXPECT_EQ(kApp, test_list[index++].child_process_host_id); | |
| 137 EXPECT_EQ(kPlayingAudio, test_list[index++].child_process_host_id); | |
| 138 EXPECT_EQ(kRecent, test_list[index++].child_process_host_id); | |
| 139 EXPECT_EQ(kOld, test_list[index++].child_process_host_id); | |
| 140 EXPECT_EQ(kReallyOld, test_list[index++].child_process_host_id); | |
| 141 EXPECT_EQ(kReloadableUI, test_list[index++].child_process_host_id); | |
| 142 } | |
| 143 | |
| 144 TEST_F(OomPriorityManagerTest, IsReloadableUI) { | |
| 145 EXPECT_TRUE(OomPriorityManager::IsReloadableUI( | |
| 146 GURL(chrome::kChromeUIDownloadsURL))); | |
| 147 EXPECT_TRUE(OomPriorityManager::IsReloadableUI( | |
| 148 GURL(chrome::kChromeUIHistoryURL))); | |
| 149 EXPECT_TRUE(OomPriorityManager::IsReloadableUI( | |
| 150 GURL(chrome::kChromeUINewTabURL))); | |
| 151 EXPECT_TRUE(OomPriorityManager::IsReloadableUI( | |
| 152 GURL(chrome::kChromeUISettingsURL))); | |
| 153 | |
| 154 // Debugging URLs are not included. | |
| 155 EXPECT_FALSE(OomPriorityManager::IsReloadableUI( | |
| 156 GURL(chrome::kChromeUIDiscardsURL))); | |
| 157 EXPECT_FALSE(OomPriorityManager::IsReloadableUI( | |
| 158 GURL(chrome::kChromeUINetInternalsURL))); | |
| 159 | |
| 160 // Prefix matches are included. | |
| 161 EXPECT_TRUE(OomPriorityManager::IsReloadableUI( | |
| 162 GURL("chrome://settings/fakeSetting"))); | |
| 163 } | |
| 164 | |
| 165 TEST_F(OomPriorityManagerTest, GetProcessHandles) { | |
| 166 OomPriorityManager::TabStats stats; | |
| 167 std::vector<OomPriorityManager::ProcessInfo> process_id_pairs; | |
| 168 | |
| 169 // Empty stats list gives empty |process_id_pairs| list. | |
| 170 OomPriorityManager::TabStatsList empty_list; | |
| 171 process_id_pairs = | |
| 172 OomPriorityManager::GetChildProcessInfos(empty_list); | |
| 173 EXPECT_EQ(0u, process_id_pairs.size()); | |
| 174 | |
| 175 // Two tabs in two different processes generates two | |
| 176 // |child_process_host_id| out. | |
| 177 OomPriorityManager::TabStatsList two_list; | |
| 178 stats.child_process_host_id = 100; | |
| 179 stats.renderer_handle = 101; | |
| 180 two_list.push_back(stats); | |
| 181 stats.child_process_host_id = 200; | |
| 182 stats.renderer_handle = 201; | |
| 183 two_list.push_back(stats); | |
| 184 process_id_pairs = OomPriorityManager::GetChildProcessInfos(two_list); | |
| 185 EXPECT_EQ(2u, process_id_pairs.size()); | |
| 186 EXPECT_EQ(100, process_id_pairs[0].first); | |
| 187 EXPECT_EQ(101, process_id_pairs[0].second); | |
| 188 EXPECT_EQ(200, process_id_pairs[1].first); | |
| 189 EXPECT_EQ(201, process_id_pairs[1].second); | |
| 190 | |
| 191 // Zero handles are removed. | |
| 192 OomPriorityManager::TabStatsList zero_handle_list; | |
| 193 stats.child_process_host_id = 100; | |
| 194 stats.renderer_handle = 0; | |
| 195 zero_handle_list.push_back(stats); | |
| 196 process_id_pairs = | |
| 197 OomPriorityManager::GetChildProcessInfos(zero_handle_list); | |
| 198 EXPECT_EQ(0u, process_id_pairs.size()); | |
| 199 | |
| 200 // Two tabs in the same process generates one handle out. When a duplicate | |
| 201 // occurs the later instance is dropped. | |
| 202 OomPriorityManager::TabStatsList same_process_list; | |
| 203 stats.child_process_host_id = 100; | |
| 204 stats.renderer_handle = 101; | |
| 205 same_process_list.push_back(stats); | |
| 206 stats.child_process_host_id = 200; | |
| 207 stats.renderer_handle = 201; | |
| 208 same_process_list.push_back(stats); | |
| 209 stats.child_process_host_id = 300; | |
| 210 stats.renderer_handle = 101; // Duplicate. | |
| 211 same_process_list.push_back(stats); | |
| 212 process_id_pairs = | |
| 213 OomPriorityManager::GetChildProcessInfos(same_process_list); | |
| 214 EXPECT_EQ(2u, process_id_pairs.size()); | |
| 215 EXPECT_EQ(100, process_id_pairs[0].first); | |
| 216 EXPECT_EQ(101, process_id_pairs[0].second); | |
| 217 EXPECT_EQ(200, process_id_pairs[1].first); | |
| 218 EXPECT_EQ(201, process_id_pairs[1].second); | |
| 219 } | |
| 220 | |
| 221 } // namespace chromeos | |
| OLD | NEW |