Sort string IDs used in Files.app and friends.
[chromium-blink-merge.git] / base / process / process_unittest.cc
bloba5ba83473311967d927f0e0c99094faa25f7a9c5
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"
15 namespace {
17 #if defined(OS_WIN)
18 const int kExpectedStillRunningExitCode = 0x102;
19 #else
20 const int kExpectedStillRunningExitCode = 0;
21 #endif
23 } // namespace
25 namespace base {
27 class ProcessTest : public MultiProcessTest {
30 TEST_F(ProcessTest, Create) {
31 Process process(SpawnChild("SimpleChildProcess"));
32 ASSERT_TRUE(process.IsValid());
33 ASSERT_FALSE(process.is_current());
34 process.Close();
35 ASSERT_FALSE(process.IsValid());
38 TEST_F(ProcessTest, CreateCurrent) {
39 Process process = Process::Current();
40 ASSERT_TRUE(process.IsValid());
41 ASSERT_TRUE(process.is_current());
42 process.Close();
43 ASSERT_FALSE(process.IsValid());
46 TEST_F(ProcessTest, Move) {
47 Process process1(SpawnChild("SimpleChildProcess"));
48 EXPECT_TRUE(process1.IsValid());
50 Process process2;
51 EXPECT_FALSE(process2.IsValid());
53 process2 = process1.Pass();
54 EXPECT_TRUE(process2.IsValid());
55 EXPECT_FALSE(process1.IsValid());
56 EXPECT_FALSE(process2.is_current());
58 Process process3 = Process::Current();
59 process2 = process3.Pass();
60 EXPECT_TRUE(process2.is_current());
61 EXPECT_TRUE(process2.IsValid());
62 EXPECT_FALSE(process3.IsValid());
65 TEST_F(ProcessTest, Duplicate) {
66 Process process1(SpawnChild("SimpleChildProcess"));
67 ASSERT_TRUE(process1.IsValid());
69 Process process2 = process1.Duplicate();
70 ASSERT_TRUE(process1.IsValid());
71 ASSERT_TRUE(process2.IsValid());
72 EXPECT_EQ(process1.pid(), process2.pid());
73 EXPECT_FALSE(process1.is_current());
74 EXPECT_FALSE(process2.is_current());
76 process1.Close();
77 ASSERT_TRUE(process2.IsValid());
80 TEST_F(ProcessTest, DuplicateCurrent) {
81 Process process1 = Process::Current();
82 ASSERT_TRUE(process1.IsValid());
84 Process process2 = process1.Duplicate();
85 ASSERT_TRUE(process1.IsValid());
86 ASSERT_TRUE(process2.IsValid());
87 EXPECT_EQ(process1.pid(), process2.pid());
88 EXPECT_TRUE(process1.is_current());
89 EXPECT_TRUE(process2.is_current());
91 process1.Close();
92 ASSERT_TRUE(process2.IsValid());
95 TEST_F(ProcessTest, DeprecatedGetProcessFromHandle) {
96 Process process1(SpawnChild("SimpleChildProcess"));
97 ASSERT_TRUE(process1.IsValid());
99 Process process2 = Process::DeprecatedGetProcessFromHandle(process1.Handle());
100 ASSERT_TRUE(process1.IsValid());
101 ASSERT_TRUE(process2.IsValid());
102 EXPECT_EQ(process1.pid(), process2.pid());
103 EXPECT_FALSE(process1.is_current());
104 EXPECT_FALSE(process2.is_current());
106 process1.Close();
107 ASSERT_TRUE(process2.IsValid());
110 MULTIPROCESS_TEST_MAIN(SleepyChildProcess) {
111 PlatformThread::Sleep(TestTimeouts::action_max_timeout());
112 return 0;
115 TEST_F(ProcessTest, Terminate) {
116 Process process(SpawnChild("SleepyChildProcess"));
117 ASSERT_TRUE(process.IsValid());
119 const int kDummyExitCode = 42;
120 int exit_code = kDummyExitCode;
121 EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
122 GetTerminationStatus(process.Handle(), &exit_code));
123 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
125 exit_code = kDummyExitCode;
126 int kExpectedExitCode = 250;
127 process.Terminate(kExpectedExitCode);
128 WaitForSingleProcess(process.Handle(), TestTimeouts::action_max_timeout());
130 EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING,
131 GetTerminationStatus(process.Handle(), &exit_code));
132 #if !defined(OS_POSIX)
133 // The POSIX implementation actually ignores the exit_code.
134 EXPECT_EQ(kExpectedExitCode, exit_code);
135 #endif
138 // Ensure that the priority of a process is restored correctly after
139 // backgrounding and restoring.
140 // Note: a platform may not be willing or able to lower the priority of
141 // a process. The calls to SetProcessBackground should be noops then.
142 TEST_F(ProcessTest, SetProcessBackgrounded) {
143 Process process(SpawnChild("SimpleChildProcess"));
144 int old_priority = process.GetPriority();
145 #if defined(OS_WIN)
146 EXPECT_TRUE(process.SetProcessBackgrounded(true));
147 EXPECT_TRUE(process.IsProcessBackgrounded());
148 EXPECT_TRUE(process.SetProcessBackgrounded(false));
149 EXPECT_FALSE(process.IsProcessBackgrounded());
150 #else
151 process.SetProcessBackgrounded(true);
152 process.SetProcessBackgrounded(false);
153 #endif
154 int new_priority = process.GetPriority();
155 EXPECT_EQ(old_priority, new_priority);
158 // Same as SetProcessBackgrounded but to this very process. It uses
159 // a different code path at least for Windows.
160 TEST_F(ProcessTest, SetProcessBackgroundedSelf) {
161 Process process = Process::Current();
162 int old_priority = process.GetPriority();
163 #if defined(OS_WIN)
164 EXPECT_TRUE(process.SetProcessBackgrounded(true));
165 EXPECT_TRUE(process.IsProcessBackgrounded());
166 EXPECT_TRUE(process.SetProcessBackgrounded(false));
167 EXPECT_FALSE(process.IsProcessBackgrounded());
168 #else
169 process.SetProcessBackgrounded(true);
170 process.SetProcessBackgrounded(false);
171 #endif
172 int new_priority = process.GetPriority();
173 EXPECT_EQ(old_priority, new_priority);
176 } // namespace base