diff options
author | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-20 15:59:49 +0000 |
---|---|---|
committer | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-20 15:59:49 +0000 |
commit | ed2f2331443f02b22b1a8051407debf03215b95d (patch) | |
tree | 04c704aa43ee77a4d44c709150495580ca289b8f /base | |
parent | 173cb8a00462e07619c3de56e62dcbba56389ccc (diff) | |
download | chromium_src-ed2f2331443f02b22b1a8051407debf03215b95d.zip chromium_src-ed2f2331443f02b22b1a8051407debf03215b95d.tar.gz chromium_src-ed2f2331443f02b22b1a8051407debf03215b95d.tar.bz2 |
Create a gtest test fixture subclass that allows for platform specific setup and teardown hooks. On the Mac use this to create an outer Autorelease pool.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1089 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.xcodeproj/project.pbxproj | 6 | ||||
-rw-r--r-- | base/file_util_unittest.cc | 13 | ||||
-rw-r--r-- | base/path_service_unittest.cc | 7 | ||||
-rw-r--r-- | base/platform_test.h | 61 | ||||
-rw-r--r-- | base/platform_test_mac.mm | 40 |
5 files changed, 123 insertions, 4 deletions
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj index 6320270..efd02ac 100644 --- a/base/base.xcodeproj/project.pbxproj +++ b/base/base.xcodeproj/project.pbxproj @@ -112,6 +112,7 @@ A5A026550E4A214600498DA9 /* file_util.cc in Sources */ = {isa = PBXBuildFile; fileRef = A5A026540E4A214600498DA9 /* file_util.cc */; }; A5A0268E0E4A2BDC00498DA9 /* file_util_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = A5A0268D0E4A2BDC00498DA9 /* file_util_posix.cc */; }; A5A0270B0E4A630D00498DA9 /* file_util_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = A5A0270A0E4A630D00498DA9 /* file_util_mac.mm */; }; + A5CB82980E5C74E300FD6825 /* platform_test_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = A5CB82970E5C74E300FD6825 /* platform_test_mac.mm */; }; A5CE1D2B0E55F4D800AD0606 /* file_util_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = A5A0282D0E4CFA8500498DA9 /* file_util_unittest.cc */; }; ABF4B98F0DC2BA6900A6E319 /* base_paths_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABF4B98E0DC2BA6900A6E319 /* base_paths_mac.mm */; }; ABF4B99E0DC2BB6000A6E319 /* clipboard_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABF4B99D0DC2BB6000A6E319 /* clipboard_mac.mm */; }; @@ -495,6 +496,8 @@ A5A0270A0E4A630D00498DA9 /* file_util_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = file_util_mac.mm; sourceTree = "<group>"; }; A5A0276B0E4BA33700498DA9 /* build_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = build_config.h; path = ../build/build_config.h; sourceTree = SOURCE_ROOT; }; A5A0282D0E4CFA8500498DA9 /* file_util_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = file_util_unittest.cc; sourceTree = "<group>"; }; + A5CB82960E5C74E300FD6825 /* platform_test.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = platform_test.h; sourceTree = "<group>"; }; + A5CB82970E5C74E300FD6825 /* platform_test_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = platform_test_mac.mm; sourceTree = "<group>"; }; ABF4B98E0DC2BA6900A6E319 /* base_paths_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = base_paths_mac.mm; sourceTree = "<group>"; }; ABF4B99D0DC2BB6000A6E319 /* clipboard_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = clipboard_mac.mm; sourceTree = "<group>"; }; ABF4B9B40DC2BC9F00A6E319 /* path_service.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = path_service.cc; sourceTree = "<group>"; }; @@ -751,6 +754,8 @@ 8254033E0D92D2210006B936 /* pickle.cc */, 8254033F0D92D2210006B936 /* pickle.h */, E4562C680E2803C3005E4685 /* pickle_unittest.cc */, + A5CB82960E5C74E300FD6825 /* platform_test.h */, + A5CB82970E5C74E300FD6825 /* platform_test_mac.mm */, 82E23FCC0D9C219600F8B40A /* platform_thread.cc */, 82E23FCB0D9C219600F8B40A /* platform_thread.h */, 825403400D92D2210006B936 /* port.h */, @@ -1199,6 +1204,7 @@ 7B78D3950E54FE0100609465 /* observer_list_unittest.cc in Sources */, 7B78D3960E54FE0100609465 /* path_service_unittest.cc in Sources */, 7B78D3970E54FE0100609465 /* pickle_unittest.cc in Sources */, + A5CB82980E5C74E300FD6825 /* platform_test_mac.mm in Sources */, 7B8505D50E5B441000730B43 /* png_codec_unittest.cc in Sources */, 7B78D3980E54FE0100609465 /* pr_time_test.cc in Sources */, 7B8505D30E5B43EE00730B43 /* rect_unittest.cc in Sources */, diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc index 9a75cd6..54e2da7 100644 --- a/base/file_util_unittest.cc +++ b/base/file_util_unittest.cc @@ -43,14 +43,18 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" +#include "base/platform_test.h" #include "base/string_util.h" #include "testing/gtest/include/gtest/gtest.h" namespace { -class FileUtilTest : public testing::Test { +// file_util winds up using autoreleased objects on the Mac, so this needs +// to be a PlatformTest +class FileUtilTest : public PlatformTest { protected: virtual void SetUp() { + PlatformTest::SetUp(); // Name a subdirectory of the temp directory. ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); file_util::AppendToPath(&test_dir_, L"FileUtilTest"); @@ -60,6 +64,7 @@ class FileUtilTest : public testing::Test { file_util::CreateDirectory(test_dir_.c_str()); } virtual void TearDown() { + PlatformTest::TearDown(); // Clean up test directory ASSERT_TRUE(file_util::Delete(test_dir_, true)); ASSERT_FALSE(file_util::PathExists(test_dir_)); @@ -594,9 +599,11 @@ TEST_F(FileUtilTest, GetFileCreationLocalTime) { } #endif -typedef testing::Test ReadOnlyFileUtilTest; +// file_util winds up using autoreleased objects on the Mac, so this needs +// to be a PlatformTest +typedef PlatformTest ReadOnlyFileUtilTest; -TEST(ReadOnlyFileUtilTest, ContentsEqual) { +TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { std::wstring data_dir; ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); file_util::AppendToPath(&data_dir, L"base"); diff --git a/base/path_service_unittest.cc b/base/path_service_unittest.cc index 970ea0c..ffc6992 100644 --- a/base/path_service_unittest.cc +++ b/base/path_service_unittest.cc @@ -31,6 +31,7 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" +#include "base/platform_test.h" #if defined(OS_WIN) #include "base/win_util.h" #endif @@ -58,11 +59,15 @@ bool ReturnsInvalidPath(int dir_type) { } // namespace +// On the Mac this winds up using some autoreleased objects, so we need to +// be a PlatformTest. +typedef PlatformTest PathServiceTest; + // Test that all PathService::Get calls return a value and a true result // in the development environment. (This test was created because a few // later changes to Get broke the semantics of the function and yielded the // correct value while returning false.) -TEST(PathServiceTest, Get) { +TEST_F(PathServiceTest, Get) { for (int key = base::DIR_CURRENT; key < base::PATH_END; ++key) { EXPECT_PRED1(ReturnsValidPath, key); } diff --git a/base/platform_test.h b/base/platform_test.h new file mode 100644 index 0000000..4a701cb --- /dev/null +++ b/base/platform_test.h @@ -0,0 +1,61 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef BASE_PLATFORM_TEST_H_ +#define BASE_PLATFORM_TEST_H_ + +#include "build/build_config.h" +#include "testing/gtest/include/gtest/gtest.h" + +#if defined(OS_MACOSX) +#ifdef __OBJC__ +@class NSAutoreleasePool; +#else +class NSAutoreleasePool; +#endif + +// The purpose of this class us to provide a hook for platform-specific +// SetUp and TearDown across unit tests. For example, on the Mac, it +// creates and releases an outer AutoreleasePool for each test. For now, it's +// only implemented on the Mac. To enable this for another platform, just +// adjust the #ifdefs and add a platform_test_<platform>.cc implementation file. +class PlatformTest : public testing::Test { + protected: + virtual void SetUp(); + virtual void TearDown(); + + private: + NSAutoreleasePool* pool_; +}; +#else +typedef testing::Test PlatformTest; +#endif // OS_MACOSX + +#endif // BASE_PLATFORM_TEST_H_ + diff --git a/base/platform_test_mac.mm b/base/platform_test_mac.mm new file mode 100644 index 0000000..c8b0785 --- /dev/null +++ b/base/platform_test_mac.mm @@ -0,0 +1,40 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "base/platform_test.h" + +#import <Cocoa/Cocoa.h> + +void PlatformTest::SetUp() { + pool_ = [[NSAutoreleasePool alloc] init]; +} + +void PlatformTest::TearDown() { + [pool_ release]; +} |