diff options
Diffstat (limited to 'chrome/common/service_process_util_unittest.cc')
-rw-r--r-- | chrome/common/service_process_util_unittest.cc | 179 |
1 files changed, 6 insertions, 173 deletions
diff --git a/chrome/common/service_process_util_unittest.cc b/chrome/common/service_process_util_unittest.cc index 84db35b..eccc4ff 100644 --- a/chrome/common/service_process_util_unittest.cc +++ b/chrome/common/service_process_util_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -239,125 +239,16 @@ MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestShutdown) { #include <CoreFoundation/CoreFoundation.h> -#include <launch.h> -#include <sys/stat.h> - #include "base/file_path.h" #include "base/file_util.h" #include "base/mac/mac_util.h" -#include "base/mac/scoped_cftyperef.h" -#include "base/message_loop.h" #include "base/scoped_temp_dir.h" -#include "base/stringprintf.h" -#include "base/sys_string_conversions.h" #include "base/test/test_timeouts.h" #include "base/threading/thread.h" #include "chrome/common/mac/launchd.h" +#include "chrome/common/mac/mock_launchd.h" #include "testing/gtest/include/gtest/gtest.h" -// TODO(dmaclach): Write this in terms of a real mock. -// http://crbug.com/76923 -class MockLaunchd : public Launchd { - public: - MockLaunchd(const FilePath& file, MessageLoop* loop) - : file_(file), - message_loop_(loop), - restart_called_(false), - remove_called_(false), - checkin_called_(false), - write_called_(false), - delete_called_(false) { - } - virtual ~MockLaunchd() { } - - virtual CFDictionaryRef CopyExports() OVERRIDE { - ADD_FAILURE(); - return NULL; - } - - virtual CFDictionaryRef CopyJobDictionary(CFStringRef label) OVERRIDE { - ADD_FAILURE(); - return NULL; - } - - virtual CFDictionaryRef CopyDictionaryByCheckingIn(CFErrorRef* error) - OVERRIDE { - checkin_called_ = true; - CFStringRef program = CFSTR(LAUNCH_JOBKEY_PROGRAM); - CFStringRef program_args = CFSTR(LAUNCH_JOBKEY_PROGRAMARGUMENTS); - const void *keys[] = { program, program_args }; - base::mac::ScopedCFTypeRef<CFStringRef> path( - base::SysUTF8ToCFStringRef(file_.value())); - const void *array_values[] = { path.get() }; - base::mac::ScopedCFTypeRef<CFArrayRef> args( - CFArrayCreate(kCFAllocatorDefault, - array_values, - 1, - &kCFTypeArrayCallBacks)); - const void *values[] = { path, args }; - return CFDictionaryCreate(kCFAllocatorDefault, - keys, - values, - arraysize(keys), - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - } - - virtual bool RemoveJob(CFStringRef label, CFErrorRef* error) OVERRIDE { - remove_called_ = true; - message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); - return true; - } - - virtual bool RestartJob(Domain domain, - Type type, - CFStringRef name, - CFStringRef session_type) OVERRIDE { - restart_called_ = true; - message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); - return true; - } - - virtual CFMutableDictionaryRef CreatePlistFromFile( - Domain domain, - Type type, - CFStringRef name) OVERRIDE { - base::mac::ScopedCFTypeRef<CFDictionaryRef> dict( - CopyDictionaryByCheckingIn(NULL)); - return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, dict); - } - - virtual bool WritePlistToFile(Domain domain, - Type type, - CFStringRef name, - CFDictionaryRef dict) OVERRIDE { - write_called_ = true; - return true; - } - - virtual bool DeletePlist(Domain domain, - Type type, - CFStringRef name) OVERRIDE { - delete_called_ = true; - return true; - } - - bool restart_called() const { return restart_called_; } - bool remove_called() const { return remove_called_; } - bool checkin_called() const { return checkin_called_; } - bool write_called() const { return write_called_; } - bool delete_called() const { return delete_called_; } - - private: - FilePath file_; - MessageLoop* message_loop_; - bool restart_called_; - bool remove_called_; - bool checkin_called_; - bool write_called_; - bool delete_called_; -}; - class ServiceProcessStateFileManipulationTest : public ::testing::Test { protected: ServiceProcessStateFileManipulationTest() @@ -370,10 +261,10 @@ class ServiceProcessStateFileManipulationTest : public ::testing::Test { options.message_loop_type = MessageLoop::TYPE_IO; ASSERT_TRUE(io_thread_.StartWithOptions(options)); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - ASSERT_TRUE(MakeABundle(GetTempDirPath(), - "Test", - &bundle_path_, - &executable_path_)); + ASSERT_TRUE(MockLaunchd::MakeABundle(GetTempDirPath(), + "Test", + &bundle_path_, + &executable_path_)); mock_launchd_.reset(new MockLaunchd(executable_path_, &loop_)); scoped_launchd_instance_.reset( new Launchd::ScopedInstance(mock_launchd_.get())); @@ -386,64 +277,6 @@ class ServiceProcessStateFileManipulationTest : public ::testing::Test { TestTimeouts::action_max_timeout_ms()); } - bool MakeABundle(const FilePath& dst, - const std::string& name, - FilePath* bundle_root, - FilePath* executable) { - *bundle_root = dst.Append(name + std::string(".app")); - FilePath contents = bundle_root->AppendASCII("Contents"); - FilePath mac_os = contents.AppendASCII("MacOS"); - *executable = mac_os.Append(name); - FilePath info_plist = contents.Append("Info.plist"); - - if (!file_util::CreateDirectory(mac_os)) { - return false; - } - const char *data = "#! testbundle\n"; - int len = strlen(data); - if (file_util::WriteFile(*executable, data, len) != len) { - return false; - } - if (chmod(executable->value().c_str(), 0555) != 0) { - return false; - } - - const char* info_plist_format = - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" - "<plist version=\"1.0\">\n" - "<dict>\n" - " <key>CFBundleDevelopmentRegion</key>\n" - " <string>English</string>\n" - " <key>CFBundleIdentifier</key>\n" - " <string>com.test.%s</string>\n" - " <key>CFBundleInfoDictionaryVersion</key>\n" - " <string>6.0</string>\n" - " <key>CFBundleExecutable</key>\n" - " <string>%s</string>\n" - " <key>CFBundleVersion</key>\n" - " <string>1</string>\n" - "</dict>\n" - "</plist>\n"; - std::string info_plist_data = base::StringPrintf(info_plist_format, - name.c_str(), - name.c_str()); - len = info_plist_data.length(); - if (file_util::WriteFile(info_plist, info_plist_data.c_str(), len) != len) { - return false; - } - const UInt8* bundle_root_path = - reinterpret_cast<const UInt8*>(bundle_root->value().c_str()); - base::mac::ScopedCFTypeRef<CFURLRef> url( - CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, - bundle_root_path, - bundle_root->value().length(), - true)); - base::mac::ScopedCFTypeRef<CFBundleRef> bundle( - CFBundleCreate(kCFAllocatorDefault, url)); - return bundle.get(); - } - const MockLaunchd* mock_launchd() const { return mock_launchd_.get(); } const FilePath& executable_path() const { return executable_path_; } const FilePath& bundle_path() const { return bundle_path_; } |