hunspell: Cleanup to fix the header include guards under google/ directory.
[chromium-blink-merge.git] / ios / web / weak_nsobject_counter_unittest.mm
blob45eb5fb1b0ec368e7259151c76938e8adc8dcd61
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ios/web/weak_nsobject_counter.h"
7 #import <Foundation/Foundation.h>
9 #import "base/ios/weak_nsobject.h"
10 #import "base/mac/scoped_nsobject.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace {
15 // Tests that a WeakNSObjectCounter correctly maintains a weak reference and
16 // calculates the size correctly when many objects are added.
17 TEST(WeakNSObjectCounter, ManyObjects) {
18   web::WeakNSObjectCounter object_counter;
19   base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
20   base::scoped_nsobject<NSObject> p2([[NSObject alloc] init]);
21   base::scoped_nsobject<NSObject> p3([[NSObject alloc] init]);
22   object_counter.Insert(p1);
23   EXPECT_EQ(1U, object_counter.Size());
24   object_counter.Insert(p2);
25   EXPECT_EQ(2U, object_counter.Size());
26   object_counter.Insert(p3);
27   EXPECT_EQ(3U, object_counter.Size());
28   // Make sure p3 is correctly removed from the object counter.
29   p3.reset();
30   EXPECT_FALSE(p3);
31   EXPECT_EQ(2U, object_counter.Size());
32   // Make sure p2 is correctly removed from the object counter.
33   p2.reset();
34   EXPECT_EQ(1U, object_counter.Size());
35   // Make sure p1 is correctly removed from the object counter.
36   p1.reset();
37   EXPECT_FALSE(p1);
38   EXPECT_EQ(0U, object_counter.Size());
41 // Tests that WeakNSObjectCounter correctly works when the object outlives the
42 // WeakNSObjectCounter that is observing it.
43 TEST(WeakNSObjectCounter, ObjectOutlivesWeakNSObjectCounter) {
44   base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
45   {
46     web::WeakNSObjectCounter object_counter;
47     object_counter.Insert(p1);
48     EXPECT_EQ(1U, object_counter.Size());
49   }
50   p1.reset();
51   EXPECT_FALSE(p1);
54 // Tests that WeakNSObjectCounter does not add the same object twice.
55 TEST(WeakNSObjectCounter, SameObject) {
56   base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
57   base::WeakNSObject<NSObject> p2(p1);
58   // Make sure they are the same object.
59   ASSERT_EQ(p1, p2);
60   web::WeakNSObjectCounter object_counter;
61   object_counter.Insert(p1);
62   EXPECT_EQ(1U, object_counter.Size());
63   object_counter.Insert(p2);
64   EXPECT_EQ(1U, object_counter.Size());
67 // Tests that WeakNSObjectCounters keep track of the same object correctly.
68 TEST(WeakNSObjectCounter, SameObjectTwoCounters) {
69   base::scoped_nsobject<NSObject> object([[NSObject alloc] init]);
70   web::WeakNSObjectCounter object_counter_1;
71   web::WeakNSObjectCounter object_counter_2;
72   object_counter_1.Insert(object);
73   EXPECT_EQ(1U, object_counter_1.Size());
75   object_counter_2.Insert(object);
76   EXPECT_EQ(1U, object_counter_1.Size());
77   EXPECT_EQ(1U, object_counter_2.Size());
79   object.reset();
80   EXPECT_EQ(0U, object_counter_1.Size());
81   EXPECT_EQ(0U, object_counter_2.Size());
84 // Tests that WeakNSObjectCounter correctly maintains a reference to the object
85 // and reduces its size only when the last reference to the object goes away.
86 TEST(WeakNSObjectCounter, LastReference) {
87   web::WeakNSObjectCounter object_counter;
88   base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
89   object_counter.Insert(p1);
90   EXPECT_EQ(1U, object_counter.Size());
91   base::scoped_nsobject<NSObject> p2(p1);
92   base::scoped_nsobject<NSObject> p3(p1);
93   p1.reset();
94   EXPECT_EQ(1U, object_counter.Size());
95   p2.reset();
96   EXPECT_EQ(1U, object_counter.Size());
97   // Last reference goes away.
98   p3.reset();
99   EXPECT_FALSE(object_counter.Size());
102 }  // namespace