Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(754)

Unified Diff: base/ios/weak_nsobject_unittest.mm

Issue 862133002: Update from https://crrev.com/312398 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/ios/weak_nsobject_unittest.mm
diff --git a/base/ios/weak_nsobject_unittest.mm b/base/ios/weak_nsobject_unittest.mm
index 9758aedfc26ac258856f3d5ab973b602eee8bdfa..325dcd21aad84f7a2cb7c6608ccb0fb3d0935358 100644
--- a/base/ios/weak_nsobject_unittest.mm
+++ b/base/ios/weak_nsobject_unittest.mm
@@ -3,16 +3,19 @@
// found in the LICENSE file.
#include "base/basictypes.h"
+#include "base/bind.h"
#include "base/ios/weak_nsobject.h"
#include "base/mac/scoped_nsobject.h"
+#include "base/message_loop/message_loop.h"
+#include "base/single_thread_task_runner.h"
+#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
-using base::WeakNSObject;
-
+namespace base {
namespace {
TEST(WeakNSObjectTest, WeakNSObject) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObject<NSObject> w1(p1);
EXPECT_TRUE(w1);
p1.reset();
@@ -20,7 +23,7 @@ TEST(WeakNSObjectTest, WeakNSObject) {
}
TEST(WeakNSObjectTest, MultipleWeakNSObject) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObject<NSObject> w1(p1);
WeakNSObject<NSObject> w2(w1);
EXPECT_TRUE(w1);
@@ -32,7 +35,7 @@ TEST(WeakNSObjectTest, MultipleWeakNSObject) {
}
TEST(WeakNSObjectTest, WeakNSObjectDies) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
{
WeakNSObject<NSObject> w1(p1);
EXPECT_TRUE(w1);
@@ -40,7 +43,7 @@ TEST(WeakNSObjectTest, WeakNSObjectDies) {
}
TEST(WeakNSObjectTest, WeakNSObjectReset) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObject<NSObject> w1(p1);
EXPECT_TRUE(w1);
w1.reset();
@@ -50,8 +53,8 @@ TEST(WeakNSObjectTest, WeakNSObjectReset) {
}
TEST(WeakNSObjectTest, WeakNSObjectResetWithObject) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
- base::scoped_nsobject<NSObject> p2([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p2([[NSObject alloc] init]);
WeakNSObject<NSObject> w1(p1);
EXPECT_TRUE(w1);
w1.reset(p2);
@@ -61,7 +64,7 @@ TEST(WeakNSObjectTest, WeakNSObjectResetWithObject) {
}
TEST(WeakNSObjectTest, WeakNSObjectEmpty) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObject<NSObject> w1;
EXPECT_FALSE(w1);
w1.reset(p1);
@@ -71,7 +74,7 @@ TEST(WeakNSObjectTest, WeakNSObjectEmpty) {
}
TEST(WeakNSObjectTest, WeakNSObjectCopy) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObject<NSObject> w1(p1);
WeakNSObject<NSObject> w2(w1);
EXPECT_TRUE(w1);
@@ -82,7 +85,7 @@ TEST(WeakNSObjectTest, WeakNSObjectCopy) {
}
TEST(WeakNSObjectTest, WeakNSObjectAssignment) {
- base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
WeakNSObject<NSObject> w1(p1);
WeakNSObject<NSObject> w2;
EXPECT_FALSE(w2);
@@ -94,4 +97,40 @@ TEST(WeakNSObjectTest, WeakNSObjectAssignment) {
EXPECT_FALSE(w2);
}
+// Touches |weak_data| by increasing its length by 1. Used to check that the
+// weak object can be dereferenced.
+void TouchWeakData(const WeakNSObject<NSMutableData>& weak_data) {
+ if (!weak_data)
+ return;
+ [weak_data increaseLengthBy:1];
+}
+
+// Makes a copy of |weak_object| on the current thread and posts a task to touch
+// the weak object on its original thread.
+void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object,
+ scoped_refptr<SingleThreadTaskRunner> runner) {
+ WeakNSObject<NSMutableData> weak_copy(weak_object);
+ runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy));
+}
+
+// Tests that the weak object can be copied on a different thread.
+TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) {
+ MessageLoop loop;
+ Thread other_thread("WeakNSObjectCopyOnOtherThread");
+ other_thread.Start();
+
+ scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]);
+ WeakNSObject<NSMutableData> weak(data);
+
+ scoped_refptr<SingleThreadTaskRunner> runner = loop.task_runner();
+ other_thread.task_runner()->PostTask(
+ FROM_HERE, Bind(&CopyWeakNSObjectAndPost, weak, runner));
+ other_thread.Stop();
+ loop.RunUntilIdle();
+
+ // Check that TouchWeakData was called.
+ EXPECT_EQ(1u, [data length]);
+}
+
} // namespace
+} // namespace base
« no previous file with comments | « base/ios/weak_nsobject.h ('k') | base/mac/cocoa_protocols.h » ('j') | shell/BUILD.gn » ('J')

Powered by Google App Engine
This is Rietveld 408576698