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