diff options
author | Patrick Scott <phanna@android.com> | 2010-02-04 10:37:17 -0500 |
---|---|---|
committer | Patrick Scott <phanna@android.com> | 2010-02-04 10:39:42 -0500 |
commit | c7f5f8508d98d5952d42ed7648c2a8f30a4da156 (patch) | |
tree | dd51dbfbf6670daa61279b3a19e7b1835b301dbf /base/weak_ptr_unittest.cc | |
parent | 139d8152182f9093f03d9089822b688e49fa7667 (diff) | |
download | external_chromium-c7f5f8508d98d5952d42ed7648c2a8f30a4da156.zip external_chromium-c7f5f8508d98d5952d42ed7648c2a8f30a4da156.tar.gz external_chromium-c7f5f8508d98d5952d42ed7648c2a8f30a4da156.tar.bz2 |
Initial source checkin.
The source files were determined by building net_unittests in chromium's source
tree. Some of the obvious libraries were left out (v8, gmock, gtest).
The Android.mk file has all the sources (minus unittests and tools) that were
used during net_unittests compilation. Nothing builds yet because of STL but
that is the next task. The .cpp files will most likely not compile anyways
because of the LOCAL_CPP_EXTENSION mod. I will have to break this into multiple
projects to get around that limitation.
Diffstat (limited to 'base/weak_ptr_unittest.cc')
-rw-r--r-- | base/weak_ptr_unittest.cc | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/base/weak_ptr_unittest.cc b/base/weak_ptr_unittest.cc new file mode 100644 index 0000000..6d38caa --- /dev/null +++ b/base/weak_ptr_unittest.cc @@ -0,0 +1,134 @@ +// Copyright (c) 2009 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 "testing/gtest/include/gtest/gtest.h" +#include "base/thread.h" +#include "base/weak_ptr.h" + +namespace base { +namespace { + +template <class T> +class OffThreadObjectCreator { + public: + static T* NewObject() { + T* result; + { + Thread creator_thread("creator_thread"); + creator_thread.Start(); + creator_thread.message_loop()->PostTask(FROM_HERE, + NewRunnableFunction(OffThreadObjectCreator::CreateObject, &result)); + } + DCHECK(result); // We synchronized on thread destruction above. + return result; + } + private: + static void CreateObject(T** result) { + *result = new T; + } +}; + +struct Base {}; +struct Derived : Base {}; + +struct Producer : SupportsWeakPtr<Producer> {}; +struct Consumer { WeakPtr<Producer> producer; }; + +} // namespace + +TEST(WeakPtrTest, Basic) { + int data; + WeakPtrFactory<int> factory(&data); + WeakPtr<int> ptr = factory.GetWeakPtr(); + EXPECT_EQ(&data, ptr.get()); +} + +TEST(WeakPtrTest, Comparison) { + int data; + WeakPtrFactory<int> factory(&data); + WeakPtr<int> ptr = factory.GetWeakPtr(); + WeakPtr<int> ptr2 = ptr; + EXPECT_TRUE(ptr == ptr2); +} + +TEST(WeakPtrTest, OutOfScope) { + WeakPtr<int> ptr; + EXPECT_TRUE(ptr.get() == NULL); + { + int data; + WeakPtrFactory<int> factory(&data); + ptr = factory.GetWeakPtr(); + } + EXPECT_TRUE(ptr.get() == NULL); +} + +TEST(WeakPtrTest, Multiple) { + WeakPtr<int> a, b; + { + int data; + WeakPtrFactory<int> factory(&data); + a = factory.GetWeakPtr(); + b = factory.GetWeakPtr(); + EXPECT_EQ(&data, a.get()); + EXPECT_EQ(&data, b.get()); + } + EXPECT_TRUE(a.get() == NULL); + EXPECT_TRUE(b.get() == NULL); +} + +TEST(WeakPtrTest, UpCast) { + Derived data; + WeakPtrFactory<Derived> factory(&data); + WeakPtr<Base> ptr = factory.GetWeakPtr(); + ptr = factory.GetWeakPtr(); + EXPECT_EQ(ptr.get(), &data); +} + +TEST(WeakPtrTest, SupportsWeakPtr) { + Producer f; + WeakPtr<Producer> ptr = f.AsWeakPtr(); + EXPECT_EQ(&f, ptr.get()); +} + +TEST(WeakPtrTest, InvalidateWeakPtrs) { + int data; + WeakPtrFactory<int> factory(&data); + WeakPtr<int> ptr = factory.GetWeakPtr(); + EXPECT_EQ(&data, ptr.get()); + EXPECT_TRUE(factory.HasWeakPtrs()); + factory.InvalidateWeakPtrs(); + EXPECT_TRUE(ptr.get() == NULL); + EXPECT_FALSE(factory.HasWeakPtrs()); +} + +TEST(WeakPtrTest, HasWeakPtrs) { + int data; + WeakPtrFactory<int> factory(&data); + { + WeakPtr<int> ptr = factory.GetWeakPtr(); + EXPECT_TRUE(factory.HasWeakPtrs()); + } + EXPECT_FALSE(factory.HasWeakPtrs()); +} + +TEST(WeakPtrTest, SingleThreaded1) { + // Test that it is OK to create a class that supports weak references on one + // thread, but use it on another. This tests that we do not trip runtime + // checks that ensure that a weak reference is not used by multiple threads. + scoped_ptr<Producer> producer(OffThreadObjectCreator<Producer>::NewObject()); + WeakPtr<Producer> weak_producer = producer->AsWeakPtr(); + EXPECT_EQ(producer.get(), weak_producer.get()); +} + +TEST(WeakPtrTest, SingleThreaded2) { + // Test that it is OK to create a class that has a WeakPtr member on one + // thread, but use it on another. This tests that we do not trip runtime + // checks that ensure that a weak reference is not used by multiple threads. + scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject()); + Producer producer; + consumer->producer = producer.AsWeakPtr(); + EXPECT_EQ(&producer, consumer->producer.get()); +} + +} // namespace base |