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

Side by Side Diff: third_party/WebKit/Source/wtf/FunctionalTest.cpp

Issue 2091713002: Specialize base::IsWeakReceiver for Blink weak pointers to support base::Bind (1/5) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +#include for build fix Created 4 years, 5 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 unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/Functional.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "wtf/Functional.h" 26 #include "wtf/Functional.h"
27 27
28 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "wtf/RefCounted.h" 29 #include "wtf/RefCounted.h"
30 #include "wtf/WeakPtr.h"
30 #include <utility> 31 #include <utility>
31 32
32 namespace WTF { 33 namespace WTF {
33 34
34 class UnwrappedClass { 35 class UnwrappedClass {
35 public: 36 public:
36 explicit UnwrappedClass(int value) 37 explicit UnwrappedClass(int value)
37 : m_value(value) 38 : m_value(value)
38 { 39 {
39 } 40 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 private: 72 private:
72 int m_value; 73 int m_value;
73 }; 74 };
74 75
75 template<> struct ParamStorageTraits<ClassToBeWrapped> { 76 template<> struct ParamStorageTraits<ClassToBeWrapped> {
76 using StorageType = WrappedClass; 77 using StorageType = WrappedClass;
77 static StorageType wrap(const ClassToBeWrapped& value) { return value.wrap() ; } 78 static StorageType wrap(const ClassToBeWrapped& value) { return value.wrap() ; }
78 static UnwrappedClass unwrap(const StorageType& value) { return value.unwrap (); } 79 static UnwrappedClass unwrap(const StorageType& value) { return value.unwrap (); }
79 }; 80 };
80 81
82 class HasWeakPtrSupport {
83 public:
84 HasWeakPtrSupport() : m_weakPtrFactory(this) {}
85
86 WTF::WeakPtr<HasWeakPtrSupport> createWeakPtr() {
87 return m_weakPtrFactory.createWeakPtr();
88 }
89
90 void revokeAll()
91 {
92 m_weakPtrFactory.revokeAll();
93 }
94
95 void increment(int* counter)
96 {
97 ++*counter;
98 }
99
100 private:
101 WTF::WeakPtrFactory<HasWeakPtrSupport> m_weakPtrFactory;
102 };
103
81 namespace { 104 namespace {
82 105
83 int returnFortyTwo() 106 int returnFortyTwo()
84 { 107 {
85 return 42; 108 return 42;
86 } 109 }
87 110
88 TEST(FunctionalTest, Basic) 111 TEST(FunctionalTest, Basic)
89 { 112 {
90 std::unique_ptr<Function<int()>> returnFortyTwoFunction = bind(returnFortyTw o); 113 std::unique_ptr<Function<int()>> returnFortyTwoFunction = bind(returnFortyTw o);
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 CountCopy lvalue; 557 CountCopy lvalue;
535 std::unique_ptr<Function<int(const CountCopy&)>> bound1 = bind(takeCountCopy AsConstReference); 558 std::unique_ptr<Function<int(const CountCopy&)>> bound1 = bind(takeCountCopy AsConstReference);
536 EXPECT_EQ(0, (*bound1)(lvalue)); // No copies! 559 EXPECT_EQ(0, (*bound1)(lvalue)); // No copies!
537 EXPECT_EQ(0, (*bound1)(CountCopy())); 560 EXPECT_EQ(0, (*bound1)(CountCopy()));
538 561
539 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue ); 562 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue );
540 EXPECT_EQ(2, (*bound2)(lvalue)); // At PartBoundFunctionImpl::operator() and at the destination function. 563 EXPECT_EQ(2, (*bound2)(lvalue)); // At PartBoundFunctionImpl::operator() and at the destination function.
541 EXPECT_LE((*bound2)(CountCopy()), 2); // Compiler is allowed to optimize one copy away if the argument is rvalue. 564 EXPECT_LE((*bound2)(CountCopy()), 2); // Compiler is allowed to optimize one copy away if the argument is rvalue.
542 } 565 }
543 566
567 TEST(FunctionalTest, WeakPtr)
568 {
569 HasWeakPtrSupport obj;
570 int counter = 0;
571 std::unique_ptr<WTF::SameThreadClosure> bound = WTF::bind(&HasWeakPtrSupport ::increment, obj.createWeakPtr(), WTF::unretained(&counter));
572
573 (*bound)();
574 EXPECT_EQ(1, counter);
575
576 obj.revokeAll();
577 (*bound)();
578 EXPECT_EQ(1, counter);
579 }
580
544 } // anonymous namespace 581 } // anonymous namespace
545 582
546 } // namespace WTF 583 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Functional.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698