| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All Rights Reserved. | 2 * Copyright (C) 2013 Google 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 COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 * | 24 * |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "config.h" | 27 #include "config.h" |
| 28 | 28 |
| 29 #include "platform/LifecycleContext.h" |
| 30 |
| 29 #include "platform/LifecycleNotifier.h" | 31 #include "platform/LifecycleNotifier.h" |
| 30 #include "platform/heap/Handle.h" | 32 #include "platform/heap/Handle.h" |
| 31 #include <gtest/gtest.h> | 33 #include <gtest/gtest.h> |
| 32 | 34 |
| 33 using namespace blink; | 35 using namespace blink; |
| 34 | 36 |
| 35 namespace blink { | 37 namespace blink { |
| 36 | 38 |
| 37 class DummyContext final : public NoBaseWillBeGarbageCollectedFinalized<DummyCon
text>, public LifecycleNotifier<DummyContext> { | 39 class DummyContext final : public NoBaseWillBeGarbageCollectedFinalized<DummyCon
text>, public LifecycleContext<DummyContext> { |
| 38 public: | 40 public: |
| 39 DummyContext() | 41 PassOwnPtr<LifecycleNotifier<DummyContext>> createLifecycleNotifier() |
| 40 : LifecycleNotifier<DummyContext>(this) | |
| 41 { | 42 { |
| 43 return LifecycleNotifier<DummyContext>::create(this); |
| 44 } |
| 45 LifecycleNotifier<DummyContext>& lifecycleNotifier() |
| 46 { |
| 47 return static_cast<LifecycleNotifier<DummyContext>&>(LifecycleContext<Du
mmyContext>::lifecycleNotifier()); |
| 42 } | 48 } |
| 43 | 49 |
| 44 void trace(Visitor* visitor) | 50 private: |
| 45 { | 51 OwnPtr<LifecycleNotifier<DummyContext>> m_lifecycleNotifier; |
| 46 LifecycleNotifier<DummyContext>::trace(visitor); | |
| 47 } | |
| 48 }; | 52 }; |
| 49 | 53 |
| 50 template<> void observeContext(DummyContext* context, LifecycleObserver<DummyCon
text>* observer) | 54 template<> void observerContext(DummyContext* context, LifecycleObserver<DummyCo
ntext>* observer) |
| 51 { | 55 { |
| 52 context->addObserver(observer); | 56 context->wasObservedBy(observer); |
| 53 } | 57 } |
| 54 | 58 |
| 55 template<> void unobserveContext(DummyContext* context, LifecycleObserver<DummyC
ontext>* observer) | 59 template<> void unobserverContext(DummyContext* context, LifecycleObserver<Dummy
Context>* observer) |
| 56 { | 60 { |
| 57 context->removeObserver(observer); | 61 context->wasUnobservedBy(observer); |
| 58 } | 62 } |
| 59 | 63 |
| 60 class TestingObserver final : public GarbageCollectedFinalized<TestingObserver>,
public LifecycleObserver<DummyContext> { | 64 class TestingObserver final : public GarbageCollectedFinalized<TestingObserver>,
public LifecycleObserver<DummyContext> { |
| 61 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(TestingObserver); | 65 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(TestingObserver); |
| 62 public: | 66 public: |
| 63 explicit TestingObserver(DummyContext* context) | 67 explicit TestingObserver(DummyContext* context) |
| 64 : LifecycleObserver<DummyContext>(context) | 68 : LifecycleObserver<DummyContext>(context) |
| 65 , m_contextDestroyedCalled(false) | 69 , m_contextDestroyedCalled(false) |
| 66 { } | 70 { } |
| 67 | 71 |
| 68 virtual void contextDestroyed() override | 72 virtual void contextDestroyed() override |
| 69 { | 73 { |
| 70 LifecycleObserver<DummyContext>::contextDestroyed(); | 74 LifecycleObserver<DummyContext>::contextDestroyed(); |
| 71 m_contextDestroyedCalled = true; | 75 m_contextDestroyedCalled = true; |
| 72 } | 76 } |
| 73 | 77 |
| 74 void trace(Visitor* visitor) | 78 void trace(Visitor* visitor) |
| 75 { | 79 { |
| 76 LifecycleObserver<DummyContext>::trace(visitor); | 80 LifecycleObserver<DummyContext>::trace(visitor); |
| 77 } | 81 } |
| 78 | 82 |
| 79 bool m_contextDestroyedCalled; | 83 bool m_contextDestroyedCalled; |
| 80 | 84 |
| 81 void unobserve() { setContext(nullptr); } | 85 void unobserve() { observeContext(0); } |
| 82 }; | 86 }; |
| 83 | 87 |
| 84 TEST(LifecycleContextTest, shouldObserveContextDestroyed) | 88 TEST(LifecycleContextTest, shouldObserveContextDestroyed) |
| 85 { | 89 { |
| 86 OwnPtrWillBeRawPtr<DummyContext> context = adoptPtrWillBeNoop(new DummyConte
xt()); | 90 OwnPtrWillBeRawPtr<DummyContext> context = adoptPtrWillBeNoop(new DummyConte
xt()); |
| 87 Persistent<TestingObserver> observer = new TestingObserver(context.get()); | 91 Persistent<TestingObserver> observer = new TestingObserver(context.get()); |
| 88 | 92 |
| 89 EXPECT_EQ(observer->lifecycleContext(), context.get()); | 93 EXPECT_EQ(observer->lifecycleContext(), context.get()); |
| 90 EXPECT_FALSE(observer->m_contextDestroyedCalled); | 94 EXPECT_FALSE(observer->m_contextDestroyedCalled); |
| 91 context->notifyContextDestroyed(); | 95 context->notifyContextDestroyed(); |
| 92 context = nullptr; | 96 context = nullptr; |
| 93 Heap::collectAllGarbage(); | 97 Heap::collectAllGarbage(); |
| 94 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); | 98 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); |
| 95 EXPECT_TRUE(observer->m_contextDestroyedCalled); | 99 EXPECT_TRUE(observer->m_contextDestroyedCalled); |
| 96 } | 100 } |
| 97 | 101 |
| 98 TEST(LifecycleContextTest, shouldNotObserveContextDestroyedIfUnobserve) | 102 TEST(LifecycleContextTest, shouldNotObserveContextDestroyedIfUnobserve) |
| 99 { | 103 { |
| 100 OwnPtrWillBeRawPtr<DummyContext> context = adoptPtrWillBeNoop(new DummyConte
xt()); | 104 OwnPtrWillBeRawPtr<DummyContext> context = adoptPtrWillBeNoop(new DummyConte
xt()); |
| 101 Persistent<TestingObserver> observer = new TestingObserver(context.get()); | 105 Persistent<TestingObserver> observer = new TestingObserver(context.get()); |
| 102 observer->unobserve(); | 106 observer->unobserve(); |
| 103 context->notifyContextDestroyed(); | 107 context->notifyContextDestroyed(); |
| 104 context = nullptr; | 108 context = nullptr; |
| 105 Heap::collectAllGarbage(); | 109 Heap::collectAllGarbage(); |
| 106 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); | 110 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); |
| 107 EXPECT_FALSE(observer->m_contextDestroyedCalled); | 111 EXPECT_FALSE(observer->m_contextDestroyedCalled); |
| 108 } | 112 } |
| 109 | 113 |
| 110 } | 114 } |
| OLD | NEW |