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 "chrome/installer/setup/setup_util_unittest.h" |
| 6 |
5 #include <windows.h> | 7 #include <windows.h> |
6 | 8 |
7 #include <string> | 9 #include <string> |
8 | 10 |
| 11 #include "base/command_line.h" |
9 #include "base/file_util.h" | 12 #include "base/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
12 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/process_util.h" |
13 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
14 #include "base/time.h" | 18 #include "base/time.h" |
15 #include "base/win/scoped_handle.h" | 19 #include "base/win/scoped_handle.h" |
| 20 #include "base/win/windows_version.h" |
16 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/installer/setup/setup_util.h" | 22 #include "chrome/installer/setup/setup_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
19 | 24 |
20 namespace { | 25 namespace { |
21 | 26 |
22 class SetupUtilTestWithDir : public testing::Test { | 27 class SetupUtilTestWithDir : public testing::Test { |
23 protected: | 28 protected: |
24 virtual void SetUp() { | 29 virtual void SetUp() { |
25 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | 30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 { | 187 { |
183 installer::ScopedTokenPrivilege dup_scoped_privilege(kTestedPrivilege); | 188 installer::ScopedTokenPrivilege dup_scoped_privilege(kTestedPrivilege); |
184 ASSERT_TRUE(dup_scoped_privilege.is_enabled()); | 189 ASSERT_TRUE(dup_scoped_privilege.is_enabled()); |
185 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); | 190 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
186 } | 191 } |
187 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); | 192 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
188 } | 193 } |
189 | 194 |
190 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); | 195 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
191 } | 196 } |
| 197 |
| 198 const char kAdjustProcessPriority[] = "adjust-process-priority"; |
| 199 |
| 200 PriorityClassChangeResult DoProcessPriorityAdjustment() { |
| 201 return installer::AdjustProcessPriority() ? PCCR_CHANGED : PCCR_UNCHANGED; |
| 202 } |
| 203 |
| 204 namespace { |
| 205 |
| 206 // A scoper that sets/resets the current process's priority class. |
| 207 class ScopedPriorityClass { |
| 208 public: |
| 209 // Applies |priority_class|, returning an instance if a change was made. |
| 210 // Otherwise, returns an empty scoped_ptr. |
| 211 static scoped_ptr<ScopedPriorityClass> Create(DWORD priority_class); |
| 212 ~ScopedPriorityClass(); |
| 213 |
| 214 private: |
| 215 explicit ScopedPriorityClass(DWORD original_priority_class); |
| 216 DWORD original_priority_class_; |
| 217 DISALLOW_COPY_AND_ASSIGN(ScopedPriorityClass); |
| 218 }; |
| 219 |
| 220 scoped_ptr<ScopedPriorityClass> ScopedPriorityClass::Create( |
| 221 DWORD priority_class) { |
| 222 HANDLE this_process = ::GetCurrentProcess(); |
| 223 DWORD original_priority_class = ::GetPriorityClass(this_process); |
| 224 EXPECT_NE(0U, original_priority_class); |
| 225 if (original_priority_class && original_priority_class != priority_class) { |
| 226 BOOL result = ::SetPriorityClass(this_process, priority_class); |
| 227 EXPECT_NE(FALSE, result); |
| 228 if (result) { |
| 229 return scoped_ptr<ScopedPriorityClass>( |
| 230 new ScopedPriorityClass(original_priority_class)); |
| 231 } |
| 232 } |
| 233 return scoped_ptr<ScopedPriorityClass>(); |
| 234 } |
| 235 |
| 236 ScopedPriorityClass::ScopedPriorityClass(DWORD original_priority_class) |
| 237 : original_priority_class_(original_priority_class) {} |
| 238 |
| 239 ScopedPriorityClass::~ScopedPriorityClass() { |
| 240 BOOL result = ::SetPriorityClass(::GetCurrentProcess(), |
| 241 original_priority_class_); |
| 242 EXPECT_NE(FALSE, result); |
| 243 } |
| 244 |
| 245 PriorityClassChangeResult RelaunchAndDoProcessPriorityAdjustment() { |
| 246 CommandLine cmd_line(*CommandLine::ForCurrentProcess()); |
| 247 cmd_line.AppendSwitch(kAdjustProcessPriority); |
| 248 base::ProcessHandle process_handle = NULL; |
| 249 int exit_code = 0; |
| 250 if (!base::LaunchProcess(cmd_line, base::LaunchOptions(), |
| 251 &process_handle)) { |
| 252 ADD_FAILURE() << " to launch subprocess."; |
| 253 } else if (!base::WaitForExitCode(process_handle, &exit_code)) { |
| 254 ADD_FAILURE() << " to wait for subprocess to exit."; |
| 255 } else { |
| 256 return static_cast<PriorityClassChangeResult>(exit_code); |
| 257 } |
| 258 return PCCR_UNKNOWN; |
| 259 } |
| 260 |
| 261 } // namespace |
| 262 |
| 263 // Launching a subprocess at normal priority class is a noop. |
| 264 TEST(SetupUtilTest, AdjustFromNormalPriority) { |
| 265 ASSERT_EQ(NORMAL_PRIORITY_CLASS, ::GetPriorityClass(::GetCurrentProcess())); |
| 266 EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment()); |
| 267 } |
| 268 |
| 269 // Launching a subprocess below normal priority class drops it to bg mode for |
| 270 // sufficiently recent operating systems. |
| 271 TEST(SetupUtilTest, AdjustFromBelowNormalPriority) { |
| 272 scoped_ptr<ScopedPriorityClass> below_normal = |
| 273 ScopedPriorityClass::Create(BELOW_NORMAL_PRIORITY_CLASS); |
| 274 ASSERT_TRUE(below_normal); |
| 275 if (base::win::GetVersion() > base::win::VERSION_SERVER_2003) |
| 276 EXPECT_EQ(PCCR_CHANGED, RelaunchAndDoProcessPriorityAdjustment()); |
| 277 else |
| 278 EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment()); |
| 279 } |
OLD | NEW |