| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "skia/ext/refptr.h" | 5 #include "skia/ext/refptr.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace skia { | 9 namespace skia { |
| 10 namespace { | 10 namespace { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // Only one change to share the pointer. | 173 // Only one change to share the pointer. |
| 174 object->ResetRefCountChanges(); | 174 object->ResetRefCountChanges(); |
| 175 RefPtr<RefCountCounter> shared; | 175 RefPtr<RefCountCounter> shared; |
| 176 shared = skia::SharePtr(object.get()); | 176 shared = skia::SharePtr(object.get()); |
| 177 EXPECT_EQ(1, object->ref_count_changes()); | 177 EXPECT_EQ(1, object->ref_count_changes()); |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST(RefPtrTest, PassIntoArguments) { | 180 TEST(RefPtrTest, PassIntoArguments) { |
| 181 // No ref count changes when passing an argument with Pass(). | 181 // No ref count changes when passing an argument with Pass(). |
| 182 RefPtr<RefCountCounter> object = skia::AdoptRef(new RefCountCounter); | 182 RefPtr<RefCountCounter> object = skia::AdoptRef(new RefCountCounter); |
| 183 RefPtr<RefCountCounter> object2 = object.Pass(); | 183 RefPtr<RefCountCounter> object2 = std::move(object); |
| 184 auto lambda = [](RefPtr<RefCountCounter> arg) { | 184 auto lambda = [](RefPtr<RefCountCounter> arg) { |
| 185 EXPECT_EQ(0, arg->ref_count_changes()); | 185 EXPECT_EQ(0, arg->ref_count_changes()); |
| 186 }; | 186 }; |
| 187 lambda(object2.Pass()); | 187 lambda(std::move(object2)); |
| 188 } | 188 } |
| 189 | 189 |
| 190 class DestructionNotifier : public SkRefCnt { | 190 class DestructionNotifier : public SkRefCnt { |
| 191 public: | 191 public: |
| 192 DestructionNotifier(bool* flag) : flag_(flag) {} | 192 DestructionNotifier(bool* flag) : flag_(flag) {} |
| 193 ~DestructionNotifier() override { *flag_ = true; } | 193 ~DestructionNotifier() override { *flag_ = true; } |
| 194 | 194 |
| 195 private: | 195 private: |
| 196 bool* flag_; | 196 bool* flag_; |
| 197 }; | 197 }; |
| 198 | 198 |
| 199 TEST(RefPtrTest, PassIntoSelf) { | |
| 200 bool is_destroyed = false; | |
| 201 RefPtr<DestructionNotifier> object = | |
| 202 skia::AdoptRef(new DestructionNotifier(&is_destroyed)); | |
| 203 object = object.Pass(); | |
| 204 ASSERT_FALSE(is_destroyed); | |
| 205 EXPECT_TRUE(object->unique()); | |
| 206 } | |
| 207 | |
| 208 TEST(RefPtrTest, Nullptr) { | 199 TEST(RefPtrTest, Nullptr) { |
| 209 RefPtr<SkRefCnt> null(nullptr); | 200 RefPtr<SkRefCnt> null(nullptr); |
| 210 EXPECT_FALSE(null); | 201 EXPECT_FALSE(null); |
| 211 | 202 |
| 212 bool is_destroyed = false; | 203 bool is_destroyed = false; |
| 213 RefPtr<DestructionNotifier> destroy_me = | 204 RefPtr<DestructionNotifier> destroy_me = |
| 214 skia::AdoptRef(new DestructionNotifier(&is_destroyed)); | 205 skia::AdoptRef(new DestructionNotifier(&is_destroyed)); |
| 215 destroy_me = nullptr; | 206 destroy_me = nullptr; |
| 216 EXPECT_TRUE(is_destroyed); | 207 EXPECT_TRUE(is_destroyed); |
| 217 EXPECT_FALSE(destroy_me); | 208 EXPECT_FALSE(destroy_me); |
| 218 | 209 |
| 219 // Check that returning nullptr from a function correctly causes an implicit | 210 // Check that returning nullptr from a function correctly causes an implicit |
| 220 // conversion. | 211 // conversion. |
| 221 auto lambda = []() -> RefPtr<SkRefCnt> { return nullptr; }; | 212 auto lambda = []() -> RefPtr<SkRefCnt> { return nullptr; }; |
| 222 RefPtr<SkRefCnt> returned = lambda(); | 213 RefPtr<SkRefCnt> returned = lambda(); |
| 223 EXPECT_FALSE(returned); | 214 EXPECT_FALSE(returned); |
| 224 } | 215 } |
| 225 | 216 |
| 226 } // namespace | 217 } // namespace |
| 227 } // namespace skia | 218 } // namespace skia |
| OLD | NEW |