diff options
author | scottbyer@chromium.org <scottbyer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-13 23:22:48 +0000 |
---|---|---|
committer | scottbyer@chromium.org <scottbyer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-13 23:22:48 +0000 |
commit | 8e9bdb10c7ecd199837ba1afa807897f8edfa29a (patch) | |
tree | 55745f96ac822179e395e988480862765759eef1 /chrome/common/mac | |
parent | e2dd00f441bdf782f0965cc40ef4f61b3f16082f (diff) | |
download | chromium_src-8e9bdb10c7ecd199837ba1afa807897f8edfa29a.zip chromium_src-8e9bdb10c7ecd199837ba1afa807897f8edfa29a.tar.gz chromium_src-8e9bdb10c7ecd199837ba1afa807897f8edfa29a.tar.bz2 |
Refactor mock launchd into it's own file for use by another test.
This is a pre-factoring to fixing up the multi-process unit tests from
http://codereview.chromium.org/8905023/ to also work on the Mac. A follow-on
changelist will enhance MockLaunchd to allow for easier re-use of
service/helper process tests across platforms.
BUG=98049
TEST=none
Review URL: http://codereview.chromium.org/8970006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117720 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/mac')
-rw-r--r-- | chrome/common/mac/mock_launchd.cc | 145 | ||||
-rw-r--r-- | chrome/common/mac/mock_launchd.h | 74 |
2 files changed, 219 insertions, 0 deletions
diff --git a/chrome/common/mac/mock_launchd.cc b/chrome/common/mac/mock_launchd.cc new file mode 100644 index 0000000..f9310df --- /dev/null +++ b/chrome/common/mac/mock_launchd.cc @@ -0,0 +1,145 @@ +// 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. + +#include "chrome/common/mac/mock_launchd.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/mac/scoped_cftyperef.h" +#include "base/message_loop.h" +#include "base/stringprintf.h" +#include "base/sys_string_conversions.h" +#include "chrome/common/mac/launchd.h" +#include "testing/gtest/include/gtest/gtest.h" + +// static +bool MockLaunchd::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(); +} + +CFDictionaryRef MockLaunchd::CopyExports() { + ADD_FAILURE(); + return NULL; +} + +CFDictionaryRef MockLaunchd::CopyJobDictionary(CFStringRef label) { + ADD_FAILURE(); + return NULL; +} + +CFDictionaryRef MockLaunchd::CopyDictionaryByCheckingIn(CFErrorRef* error) { + 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); +} + +bool MockLaunchd::RemoveJob(CFStringRef label, CFErrorRef* error) { + remove_called_ = true; + message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); + return true; +} + +bool MockLaunchd::RestartJob(Domain domain, + Type type, + CFStringRef name, + CFStringRef session_type) { + restart_called_ = true; + message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); + return true; +} + +CFMutableDictionaryRef MockLaunchd::CreatePlistFromFile( + Domain domain, + Type type, + CFStringRef name) { + base::mac::ScopedCFTypeRef<CFDictionaryRef> dict( + CopyDictionaryByCheckingIn(NULL)); + return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, dict); +} + +bool MockLaunchd::WritePlistToFile(Domain domain, + Type type, + CFStringRef name, + CFDictionaryRef dict) { + write_called_ = true; + return true; +} + +bool MockLaunchd::DeletePlist(Domain domain, + Type type, + CFStringRef name) { + delete_called_ = true; + return true; +} diff --git a/chrome/common/mac/mock_launchd.h b/chrome/common/mac/mock_launchd.h new file mode 100644 index 0000000..5d043c6 --- /dev/null +++ b/chrome/common/mac/mock_launchd.h @@ -0,0 +1,74 @@ +// 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. + +#ifndef CHROME_COMMON_MAC_MOCK_LAUNCHD_H_ +#define CHROME_COMMON_MAC_MOCK_LAUNCHD_H_ +#pragma once + +#include <launch.h> + +#include "base/file_path.h" +#include "base/mac/scoped_cftyperef.h" +#include "chrome/common/mac/launchd.h" + +class MessageLoop; + +// TODO(dmaclach): Write this in terms of a real mock. +// http://crbug.com/76923 +class MockLaunchd : public Launchd { + public: + static bool MakeABundle(const FilePath& dst, + const std::string& name, + FilePath* bundle_root, + FilePath* executable); + + 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; + virtual CFDictionaryRef CopyJobDictionary(CFStringRef label) OVERRIDE; + virtual CFDictionaryRef CopyDictionaryByCheckingIn(CFErrorRef* error) + OVERRIDE; + virtual bool RemoveJob(CFStringRef label, CFErrorRef* error) OVERRIDE; + virtual bool RestartJob(Domain domain, + Type type, + CFStringRef name, + CFStringRef session_type) OVERRIDE; + virtual CFMutableDictionaryRef CreatePlistFromFile( + Domain domain, + Type type, + CFStringRef name) OVERRIDE; + virtual bool WritePlistToFile(Domain domain, + Type type, + CFStringRef name, + CFDictionaryRef dict) OVERRIDE; + virtual bool DeletePlist(Domain domain, + Type type, + CFStringRef name) OVERRIDE; + + 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_; +}; + +#endif // CHROME_COMMON_MAC_MOCK_LAUNCHD_H_ |