Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / base / process / process_unittest.cc
blob1a2af5034ddfb6fd41a036061369f745910d580d
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
3 // found in the LICENSE file.
5 #include "base/process/process.h"
7 #include "base/process/kill.h"
8 #include "base/test/multiprocess_test.h"
9 #include "base/test/test_timeouts.h"
10 #include "base/threading/platform_thread.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/multiprocess_func_list.h"
14 namespace {
16 #if defined(OS_WIN)
17 const int kExpectedStillRunningExitCode = 0x102;
18 #else
19 const int kExpectedStillRunningExitCode = 0;
20 #endif
22 } // namespace
24 namespace base {
26 class ProcessTest : public MultiProcessTest {
29 TEST_F(ProcessTest, Create) {
30 Process process(SpawnChild("SimpleChildProcess"));
31 ASSERT_TRUE(process.IsValid());
32 ASSERT_FALSE(process.is_current());
33 process.Close();
34 ASSERT_FALSE(process.IsValid());
37 TEST_F(ProcessTest, CreateCurrent) {
38 Process process = Process::Current();
39 ASSERT_TRUE(process.IsValid());
40 ASSERT_TRUE(process.is_current());
41 process.Close();
42 ASSERT_FALSE(process.IsValid());
45 TEST_F(ProcessTest, Move) {
46 Process process1(SpawnChild("SimpleChildProcess"));
47 EXPECT_TRUE(process1.IsValid());
49 Process process2;
50 EXPECT_FALSE(process2.IsValid());
52 process2 = process1.Pass();
53 EXPECT_TRUE(process2.IsValid());
54 EXPECT_FALSE(process1.IsValid());
55 EXPECT_FALSE(process2.is_current());
57 Process process3 = Process::Current();
58 process2 = process3.Pass();
59 EXPECT_TRUE(process2.is_current());
60 EXPECT_TRUE(process2.IsValid());
61 EXPECT_FALSE(process3.IsValid());
64 TEST_F(ProcessTest, Duplicate) {
65 Process process1(SpawnChild("SimpleChildProcess"));
66 ASSERT_TRUE(process1.IsValid());
68 Process process2 = process1.Duplicate();
69 ASSERT_TRUE(process1.IsValid());
70 ASSERT_TRUE(process2.IsValid());
71 EXPECT_EQ(process1.pid(), process2.pid());
72 EXPECT_FALSE(process1.is_current());
73 EXPECT_FALSE(process2.is_current());
75 process1.Close();
76 ASSERT_TRUE(process2.IsValid());
79 TEST_F(ProcessTest, DuplicateCurrent) {
80 Process process1 = Process::Current();
81 ASSERT_TRUE(process1.IsValid());
83 Process process2 = process1.Duplicate();
84 ASSERT_TRUE(process1.IsValid());
85 ASSERT_TRUE(process2.IsValid());
86 EXPECT_EQ(process1.pid(), process2.pid());
87 EXPECT_TRUE(process1.is_current());
88 EXPECT_TRUE(process2.is_current());
90 process1.Close();
91 ASSERT_TRUE(process2.IsValid());
94 TEST_F(ProcessTest, DeprecatedGetProcessFromHandle) {
95 Process process1(SpawnChild("SimpleChildProcess"));
96 ASSERT_TRUE(process1.IsValid());
98 Process process2 = Process::DeprecatedGetProcessFromHandle(process1.Handle());
99 ASSERT_TRUE(process1.IsValid());
100 ASSERT_TRUE(process2.IsValid());
101 EXPECT_EQ(process1.pid(), process2.pid());
102 EXPECT_FALSE(process1.is_current());
103 EXPECT_FALSE(process2.is_current());
105 process1.Close();
106 ASSERT_TRUE(process2.IsValid());
109 MULTIPROCESS_TEST_MAIN(SleepyChildProcess) {
110 PlatformThread::Sleep(TestTimeouts::action_max_timeout());
111 return 0;
114 TEST_F(ProcessTest, Terminate) {
115 Process process(SpawnChild("SleepyChildProcess"));
116 ASSERT_TRUE(process.IsValid());
118 const int kDummyExitCode = 42;
119 int exit_code = kDummyExitCode;
120 EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
121 GetTerminationStatus(process.Handle(), &exit_code));
122 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
124 exit_code = kDummyExitCode;
125 int kExpectedExitCode = 250;
126 process.Terminate(kExpectedExitCode);
127 WaitForSingleProcess(process.Handle(), TestTimeouts::action_max_timeout());
129 EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING,
130 GetTerminationStatus(process.Handle(), &exit_code));
131 #if !defined(OS_POSIX)
132 // The POSIX implementation actually ignores the exit_code.
133 EXPECT_EQ(kExpectedExitCode, exit_code);
134 #endif
137 MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess) {
138 PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10);
139 return 0;
142 TEST_F(ProcessTest, WaitForExit) {
143 Process process(SpawnChild("FastSleepyChildProcess"));
144 ASSERT_TRUE(process.IsValid());
146 const int kDummyExitCode = 42;
147 int exit_code = kDummyExitCode;
148 EXPECT_TRUE(process.WaitForExit(&exit_code));
149 EXPECT_EQ(0, exit_code);
152 TEST_F(ProcessTest, WaitForExitWithTimeout) {
153 Process process(SpawnChild("SleepyChildProcess"));
154 ASSERT_TRUE(process.IsValid());
156 const int kDummyExitCode = 42;
157 int exit_code = kDummyExitCode;
158 TimeDelta timeout = TestTimeouts::tiny_timeout();
159 EXPECT_FALSE(process.WaitForExitWithTimeout(timeout, &exit_code));
160 EXPECT_EQ(kDummyExitCode, exit_code);
162 process.Terminate(kDummyExitCode);
165 // Ensure that the priority of a process is restored correctly after
166 // backgrounding and restoring.
167 // Note: a platform may not be willing or able to lower the priority of
168 // a process. The calls to SetProcessBackground should be noops then.
169 TEST_F(ProcessTest, SetProcessBackgrounded) {
170 Process process(SpawnChild("SimpleChildProcess"));
171 int old_priority = process.GetPriority();
172 #if defined(OS_WIN)
173 EXPECT_TRUE(process.SetProcessBackgrounded(true));
174 EXPECT_TRUE(process.IsProcessBackgrounded());
175 EXPECT_TRUE(process.SetProcessBackgrounded(false));
176 EXPECT_FALSE(process.IsProcessBackgrounded());
177 #else
178 process.SetProcessBackgrounded(true);
179 process.SetProcessBackgrounded(false);
180 #endif
181 int new_priority = process.GetPriority();
182 EXPECT_EQ(old_priority, new_priority);
185 // Same as SetProcessBackgrounded but to this very process. It uses
186 // a different code path at least for Windows.
187 TEST_F(ProcessTest, SetProcessBackgroundedSelf) {
188 Process process = Process::Current();
189 int old_priority = process.GetPriority();
190 #if defined(OS_WIN)
191 EXPECT_TRUE(process.SetProcessBackgrounded(true));
192 EXPECT_TRUE(process.IsProcessBackgrounded());
193 EXPECT_TRUE(process.SetProcessBackgrounded(false));
194 EXPECT_FALSE(process.IsProcessBackgrounded());
195 #else
196 process.SetProcessBackgrounded(true);
197 process.SetProcessBackgrounded(false);
198 #endif
199 int new_priority = process.GetPriority();
200 EXPECT_EQ(old_priority, new_priority);
203 } // namespace base