OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/process/process.h" | 5 #include "base/process/process.h" |
6 | 6 |
7 #include "base/process/kill.h" | 7 #include "base/process/kill.h" |
8 #include "base/test/multiprocess_test.h" | 8 #include "base/test/multiprocess_test.h" |
9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/multiprocess_func_list.h" | 12 #include "testing/multiprocess_func_list.h" |
13 | 13 |
| 14 #if defined(OS_MACOSX) |
| 15 #include <mach/mach.h> |
| 16 #endif // OS_MACOSX |
| 17 |
14 namespace { | 18 namespace { |
15 | 19 |
16 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
17 const int kExpectedStillRunningExitCode = 0x102; | 21 const int kExpectedStillRunningExitCode = 0x102; |
18 #else | 22 #else |
19 const int kExpectedStillRunningExitCode = 0; | 23 const int kExpectedStillRunningExitCode = 0; |
20 #endif | 24 #endif |
21 | 25 |
22 } // namespace | 26 } // namespace |
23 | 27 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 process.Terminate(kDummyExitCode, false); | 167 process.Terminate(kDummyExitCode, false); |
164 } | 168 } |
165 | 169 |
166 // Ensure that the priority of a process is restored correctly after | 170 // Ensure that the priority of a process is restored correctly after |
167 // backgrounding and restoring. | 171 // backgrounding and restoring. |
168 // Note: a platform may not be willing or able to lower the priority of | 172 // Note: a platform may not be willing or able to lower the priority of |
169 // a process. The calls to SetProcessBackground should be noops then. | 173 // a process. The calls to SetProcessBackground should be noops then. |
170 TEST_F(ProcessTest, SetProcessBackgrounded) { | 174 TEST_F(ProcessTest, SetProcessBackgrounded) { |
171 Process process(SpawnChild("SimpleChildProcess")); | 175 Process process(SpawnChild("SimpleChildProcess")); |
172 int old_priority = process.GetPriority(); | 176 int old_priority = process.GetPriority(); |
173 #if defined(OS_WIN) | 177 #if defined(OS_MACOSX) |
| 178 // On the Mac, backgrounding a process requires a port to that process. |
| 179 // In the browser it's available through the MachBroker class, which is not |
| 180 // part of base. Additionally, there is an indefinite amount of time between |
| 181 // spawning a process and receiving its port. Because this test just checks |
| 182 // the ability to background/foreground a process, we can use the current |
| 183 // process's port instead. |
| 184 mach_port_t process_port = mach_task_self(); |
| 185 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, true)); |
| 186 EXPECT_TRUE(process.IsProcessBackgrounded(process_port)); |
| 187 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, false)); |
| 188 EXPECT_FALSE(process.IsProcessBackgrounded(process_port)); |
| 189 #elif defined(OS_WIN) |
174 EXPECT_TRUE(process.SetProcessBackgrounded(true)); | 190 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
175 EXPECT_TRUE(process.IsProcessBackgrounded()); | 191 EXPECT_TRUE(process.IsProcessBackgrounded()); |
176 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 192 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
177 EXPECT_FALSE(process.IsProcessBackgrounded()); | 193 EXPECT_FALSE(process.IsProcessBackgrounded()); |
178 #else | 194 #else |
179 process.SetProcessBackgrounded(true); | 195 process.SetProcessBackgrounded(true); |
180 process.SetProcessBackgrounded(false); | 196 process.SetProcessBackgrounded(false); |
181 #endif | 197 #endif |
182 int new_priority = process.GetPriority(); | 198 int new_priority = process.GetPriority(); |
183 EXPECT_EQ(old_priority, new_priority); | 199 EXPECT_EQ(old_priority, new_priority); |
184 } | 200 } |
185 | 201 |
186 // Same as SetProcessBackgrounded but to this very process. It uses | 202 // Same as SetProcessBackgrounded but to this very process. It uses |
187 // a different code path at least for Windows. | 203 // a different code path at least for Windows. |
188 TEST_F(ProcessTest, SetProcessBackgroundedSelf) { | 204 TEST_F(ProcessTest, SetProcessBackgroundedSelf) { |
189 Process process = Process::Current(); | 205 Process process = Process::Current(); |
190 int old_priority = process.GetPriority(); | 206 int old_priority = process.GetPriority(); |
191 #if defined(OS_WIN) | 207 #if defined(OS_MACOSX) |
| 208 mach_port_t process_port = mach_task_self(); |
| 209 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, true)); |
| 210 EXPECT_TRUE(process.IsProcessBackgrounded(process_port)); |
| 211 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, false)); |
| 212 EXPECT_FALSE(process.IsProcessBackgrounded(process_port)); |
| 213 #elif defined(OS_WIN) |
192 EXPECT_TRUE(process.SetProcessBackgrounded(true)); | 214 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
193 EXPECT_TRUE(process.IsProcessBackgrounded()); | 215 EXPECT_TRUE(process.IsProcessBackgrounded()); |
194 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 216 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
195 EXPECT_FALSE(process.IsProcessBackgrounded()); | 217 EXPECT_FALSE(process.IsProcessBackgrounded()); |
196 #else | 218 #else |
197 process.SetProcessBackgrounded(true); | 219 process.SetProcessBackgrounded(true); |
198 process.SetProcessBackgrounded(false); | 220 process.SetProcessBackgrounded(false); |
199 #endif | 221 #endif |
200 int new_priority = process.GetPriority(); | 222 int new_priority = process.GetPriority(); |
201 EXPECT_EQ(old_priority, new_priority); | 223 EXPECT_EQ(old_priority, new_priority); |
202 } | 224 } |
203 | 225 |
204 } // namespace base | 226 } // namespace base |
OLD | NEW |