[Password Manager] Relax matching for username overrides.
[chromium-blink-merge.git] / base / ios / weak_nsobject_unittest.mm
blob81de993cf25222542fc1122b5b7de5772a386712
1 // Copyright 2013 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 #include "base/basictypes.h"
6 #include "base/bind.h"
7 #include "base/ios/weak_nsobject.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
15 namespace {
17 TEST(WeakNSObjectTest, WeakNSObject) {
18   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
19   WeakNSObject<NSObject> w1(p1);
20   EXPECT_TRUE(w1);
21   p1.reset();
22   EXPECT_FALSE(w1);
25 TEST(WeakNSObjectTest, MultipleWeakNSObject) {
26   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
27   WeakNSObject<NSObject> w1(p1);
28   WeakNSObject<NSObject> w2(w1);
29   EXPECT_TRUE(w1);
30   EXPECT_TRUE(w2);
31   EXPECT_TRUE(w1.get() == w2.get());
32   p1.reset();
33   EXPECT_FALSE(w1);
34   EXPECT_FALSE(w2);
37 TEST(WeakNSObjectTest, WeakNSObjectDies) {
38   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
39   {
40     WeakNSObject<NSObject> w1(p1);
41     EXPECT_TRUE(w1);
42   }
45 TEST(WeakNSObjectTest, WeakNSObjectReset) {
46   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
47   WeakNSObject<NSObject> w1(p1);
48   EXPECT_TRUE(w1);
49   w1.reset();
50   EXPECT_FALSE(w1);
51   EXPECT_TRUE(p1);
52   EXPECT_TRUE([p1 description]);
55 TEST(WeakNSObjectTest, WeakNSObjectResetWithObject) {
56   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
57   scoped_nsobject<NSObject> p2([[NSObject alloc] init]);
58   WeakNSObject<NSObject> w1(p1);
59   EXPECT_TRUE(w1);
60   w1.reset(p2);
61   EXPECT_TRUE(w1);
62   EXPECT_TRUE([p1 description]);
63   EXPECT_TRUE([p2 description]);
66 TEST(WeakNSObjectTest, WeakNSObjectEmpty) {
67   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
68   WeakNSObject<NSObject> w1;
69   EXPECT_FALSE(w1);
70   w1.reset(p1);
71   EXPECT_TRUE(w1);
72   p1.reset();
73   EXPECT_FALSE(w1);
76 TEST(WeakNSObjectTest, WeakNSObjectCopy) {
77   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
78   WeakNSObject<NSObject> w1(p1);
79   WeakNSObject<NSObject> w2(w1);
80   EXPECT_TRUE(w1);
81   EXPECT_TRUE(w2);
82   p1.reset();
83   EXPECT_FALSE(w1);
84   EXPECT_FALSE(w2);
87 TEST(WeakNSObjectTest, WeakNSObjectAssignment) {
88   scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
89   WeakNSObject<NSObject> w1(p1);
90   WeakNSObject<NSObject> w2;
91   EXPECT_FALSE(w2);
92   w2 = w1;
93   EXPECT_TRUE(w1);
94   EXPECT_TRUE(w2);
95   p1.reset();
96   EXPECT_FALSE(w1);
97   EXPECT_FALSE(w2);
100 // Touches |weak_data| by increasing its length by 1. Used to check that the
101 // weak object can be dereferenced.
102 void TouchWeakData(const WeakNSObject<NSMutableData>& weak_data) {
103   if (!weak_data)
104     return;
105   [weak_data increaseLengthBy:1];
108 // Makes a copy of |weak_object| on the current thread and posts a task to touch
109 // the weak object on its original thread.
110 void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object,
111                              scoped_refptr<SingleThreadTaskRunner> runner) {
112   // Copy using constructor.
113   WeakNSObject<NSMutableData> weak_copy1(weak_object);
114   runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy1));
115   // Copy using assignment operator.
116   WeakNSObject<NSMutableData> weak_copy2 = weak_object;
117   runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy2));
120 // Tests that the weak object can be copied on a different thread.
121 TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) {
122   MessageLoop loop;
123   Thread other_thread("WeakNSObjectCopyOnOtherThread");
124   other_thread.Start();
126   scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]);
127   WeakNSObject<NSMutableData> weak(data);
129   scoped_refptr<SingleThreadTaskRunner> runner = loop.task_runner();
130   other_thread.task_runner()->PostTask(
131       FROM_HERE, Bind(&CopyWeakNSObjectAndPost, weak, runner));
132   other_thread.Stop();
133   loop.RunUntilIdle();
135   // Check that TouchWeakData was called and the object touched twice.
136   EXPECT_EQ(2u, [data length]);
139 }  // namespace
140 }  // namespace base