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

Unified Diff: ios/web/weak_nsobject_counter_unittest.mm

Issue 988383002: Upstream various ios/web utilities and helpers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@web-public-upstreaming
Patch Set: Created 5 years, 9 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: ios/web/weak_nsobject_counter_unittest.mm
diff --git a/ios/web/weak_nsobject_counter_unittest.mm b/ios/web/weak_nsobject_counter_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..b981d2d6ef1066cd1e22ddf4814345e9be22d972
--- /dev/null
+++ b/ios/web/weak_nsobject_counter_unittest.mm
@@ -0,0 +1,85 @@
+// Copyright 2014 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.
+
+#import "ios/web/weak_nsobject_counter.h"
+
+#import <Foundation/Foundation.h>
+
+#import "base/ios/weak_nsobject.h"
+#import "base/mac/scoped_nsobject.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// Tests that a WeakNSObjectCounter correctly maintains a weak reference and
+// calculates the size correctly when many objects are added.
+TEST(WeakNSObjectCounter, ManyObjects) {
+ web::WeakNSObjectCounter object_counter;
+ base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ base::scoped_nsobject<NSObject> p2([[NSObject alloc] init]);
+ base::scoped_nsobject<NSObject> p3([[NSObject alloc] init]);
+ object_counter.Insert(p1);
+ EXPECT_EQ(1U, object_counter.Size());
+ object_counter.Insert(p2);
+ EXPECT_EQ(2U, object_counter.Size());
+ object_counter.Insert(p3);
+ EXPECT_EQ(3U, object_counter.Size());
+ // Make sure p3 is correctly removed from the object counter.
+ p3.reset();
+ EXPECT_FALSE(p3);
+ EXPECT_EQ(2U, object_counter.Size());
+ // Make sure p2 is correctly removed from the object counter.
+ p2.reset();
+ EXPECT_EQ(1U, object_counter.Size());
+ // Make sure p1 is correctly removed from the object counter.
+ p1.reset();
+ EXPECT_FALSE(p1);
+ EXPECT_EQ(0U, object_counter.Size());
+}
+
+// Tests that WeakNSObjectCounter correctly works when the object outlives the
+// WeakNSObjectCounter that is observing it.
+TEST(WeakNSObjectCounter, ObjectOutlivesWeakNSObjectCounter) {
+ base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ {
+ web::WeakNSObjectCounter object_counter;
+ object_counter.Insert(p1);
+ EXPECT_EQ(1U, object_counter.Size());
+ }
+ p1.reset();
+ EXPECT_FALSE(p1);
+}
+
+// Tests that WeakNSObjectCounter does not add the same object twice.
+TEST(WeakNSObjectCounter, SameObject) {
+ base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ base::WeakNSObject<NSObject> p2(p1);
+ // Make sure they are the same object.
+ ASSERT_EQ(p1, p2);
+ web::WeakNSObjectCounter object_counter;
+ object_counter.Insert(p1);
+ EXPECT_EQ(1U, object_counter.Size());
+ object_counter.Insert(p2);
+ EXPECT_EQ(1U, object_counter.Size());
+}
+
+// Tests that WeakNSObjectCounter correctly maintains a reference to the object
+// and reduces its size only when the last reference to the object goes away.
+TEST(WeakNSObjectCounter, LastReference) {
+ web::WeakNSObjectCounter object_counter;
+ base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
+ object_counter.Insert(p1);
+ EXPECT_EQ(1U, object_counter.Size());
+ base::scoped_nsobject<NSObject> p2(p1);
+ base::scoped_nsobject<NSObject> p3(p1);
+ p1.reset();
+ EXPECT_EQ(1U, object_counter.Size());
+ p2.reset();
+ EXPECT_EQ(1U, object_counter.Size());
+ // Last reference goes away.
+ p3.reset();
+ EXPECT_FALSE(object_counter.Size());
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698