| 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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 #include "platform/LifecycleNotifier.h" | 27 #include "platform/LifecycleNotifier.h" |
| 28 #include "platform/LifecycleObserver.h" | 28 #include "platform/LifecycleObserver.h" |
| 29 #include "platform/heap/Handle.h" | 29 #include "platform/heap/Handle.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 31 |
| 32 namespace blink { | 32 namespace blink { |
| 33 | 33 |
| 34 class TestingObserver; | 34 class TestingObserver; |
| 35 | 35 |
| 36 class DummyContext final : public NoBaseWillBeGarbageCollectedFinalized<DummyCon
text>, public LifecycleNotifier<DummyContext, TestingObserver> { | 36 class DummyContext final : public GarbageCollectedFinalized<DummyContext>, publi
c LifecycleNotifier<DummyContext, TestingObserver> { |
| 37 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(DummyContext); | 37 USING_GARBAGE_COLLECTED_MIXIN(DummyContext); |
| 38 public: | 38 public: |
| 39 static PassOwnPtrWillBeRawPtr<DummyContext> create() | 39 static RawPtr<DummyContext> create() |
| 40 { | 40 { |
| 41 return adoptPtrWillBeNoop(new DummyContext()); | 41 return (new DummyContext()); |
| 42 } | 42 } |
| 43 | 43 |
| 44 DEFINE_INLINE_TRACE() | 44 DEFINE_INLINE_TRACE() |
| 45 { | 45 { |
| 46 LifecycleNotifier<DummyContext, TestingObserver>::trace(visitor); | 46 LifecycleNotifier<DummyContext, TestingObserver>::trace(visitor); |
| 47 } | 47 } |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 class TestingObserver final : public NoBaseWillBeGarbageCollectedFinalized<Testi
ngObserver>, public LifecycleObserver<DummyContext, TestingObserver, DummyContex
t> { | 50 class TestingObserver final : public GarbageCollectedFinalized<TestingObserver>,
public LifecycleObserver<DummyContext, TestingObserver, DummyContext> { |
| 51 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(TestingObserver); | 51 USING_GARBAGE_COLLECTED_MIXIN(TestingObserver); |
| 52 public: | 52 public: |
| 53 static PassOwnPtrWillBeRawPtr<TestingObserver> create(DummyContext* context) | 53 static RawPtr<TestingObserver> create(DummyContext* context) |
| 54 { | 54 { |
| 55 return adoptPtrWillBeNoop(new TestingObserver(context)); | 55 return (new TestingObserver(context)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void contextDestroyed() override | 58 void contextDestroyed() override |
| 59 { | 59 { |
| 60 LifecycleObserver::contextDestroyed(); | 60 LifecycleObserver::contextDestroyed(); |
| 61 if (m_observerToRemoveOnDestruct) { | 61 if (m_observerToRemoveOnDestruct) { |
| 62 lifecycleContext()->removeObserver(m_observerToRemoveOnDestruct.get(
)); | 62 lifecycleContext()->removeObserver(m_observerToRemoveOnDestruct.get(
)); |
| 63 m_observerToRemoveOnDestruct.clear(); | 63 m_observerToRemoveOnDestruct.clear(); |
| 64 } | 64 } |
| 65 m_contextDestroyedCalled = true; | 65 m_contextDestroyedCalled = true; |
| 66 } | 66 } |
| 67 | 67 |
| 68 DEFINE_INLINE_TRACE() | 68 DEFINE_INLINE_TRACE() |
| 69 { | 69 { |
| 70 visitor->trace(m_observerToRemoveOnDestruct); | 70 visitor->trace(m_observerToRemoveOnDestruct); |
| 71 LifecycleObserver::trace(visitor); | 71 LifecycleObserver::trace(visitor); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void unobserve() { setContext(nullptr); } | 74 void unobserve() { setContext(nullptr); } |
| 75 | 75 |
| 76 void setObserverToRemoveAndDestroy(PassOwnPtrWillBeRawPtr<TestingObserver> o
bserverToRemoveOnDestruct) | 76 void setObserverToRemoveAndDestroy(RawPtr<TestingObserver> observerToRemoveO
nDestruct) |
| 77 { | 77 { |
| 78 ASSERT(!m_observerToRemoveOnDestruct); | 78 ASSERT(!m_observerToRemoveOnDestruct); |
| 79 m_observerToRemoveOnDestruct = observerToRemoveOnDestruct; | 79 m_observerToRemoveOnDestruct = observerToRemoveOnDestruct; |
| 80 } | 80 } |
| 81 | 81 |
| 82 TestingObserver* innerObserver() const { return m_observerToRemoveOnDestruct
.get(); } | 82 TestingObserver* innerObserver() const { return m_observerToRemoveOnDestruct
.get(); } |
| 83 bool contextDestroyedCalled() const { return m_contextDestroyedCalled; } | 83 bool contextDestroyedCalled() const { return m_contextDestroyedCalled; } |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 explicit TestingObserver(DummyContext* context) | 86 explicit TestingObserver(DummyContext* context) |
| 87 : LifecycleObserver(context) | 87 : LifecycleObserver(context) |
| 88 , m_contextDestroyedCalled(false) | 88 , m_contextDestroyedCalled(false) |
| 89 { | 89 { |
| 90 } | 90 } |
| 91 | 91 |
| 92 OwnPtrWillBeMember<TestingObserver> m_observerToRemoveOnDestruct; | 92 Member<TestingObserver> m_observerToRemoveOnDestruct; |
| 93 bool m_contextDestroyedCalled; | 93 bool m_contextDestroyedCalled; |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 TEST(LifecycleContextTest, shouldObserveContextDestroyed) | 96 TEST(LifecycleContextTest, shouldObserveContextDestroyed) |
| 97 { | 97 { |
| 98 OwnPtrWillBeRawPtr<DummyContext> context = DummyContext::create(); | 98 RawPtr<DummyContext> context = DummyContext::create(); |
| 99 OwnPtrWillBePersistent<TestingObserver> observer = TestingObserver::create(c
ontext.get()); | 99 Persistent<TestingObserver> observer = TestingObserver::create(context.get()
); |
| 100 | 100 |
| 101 EXPECT_EQ(observer->lifecycleContext(), context.get()); | 101 EXPECT_EQ(observer->lifecycleContext(), context.get()); |
| 102 EXPECT_FALSE(observer->contextDestroyedCalled()); | 102 EXPECT_FALSE(observer->contextDestroyedCalled()); |
| 103 context->notifyContextDestroyed(); | 103 context->notifyContextDestroyed(); |
| 104 context = nullptr; | 104 context = nullptr; |
| 105 Heap::collectAllGarbage(); | 105 Heap::collectAllGarbage(); |
| 106 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); | 106 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); |
| 107 EXPECT_TRUE(observer->contextDestroyedCalled()); | 107 EXPECT_TRUE(observer->contextDestroyedCalled()); |
| 108 } | 108 } |
| 109 | 109 |
| 110 TEST(LifecycleContextTest, shouldNotObserveContextDestroyedIfUnobserve) | 110 TEST(LifecycleContextTest, shouldNotObserveContextDestroyedIfUnobserve) |
| 111 { | 111 { |
| 112 OwnPtrWillBeRawPtr<DummyContext> context = DummyContext::create(); | 112 RawPtr<DummyContext> context = DummyContext::create(); |
| 113 OwnPtrWillBePersistent<TestingObserver> observer = TestingObserver::create(c
ontext.get()); | 113 Persistent<TestingObserver> observer = TestingObserver::create(context.get()
); |
| 114 observer->unobserve(); | 114 observer->unobserve(); |
| 115 context->notifyContextDestroyed(); | 115 context->notifyContextDestroyed(); |
| 116 context = nullptr; | 116 context = nullptr; |
| 117 Heap::collectAllGarbage(); | 117 Heap::collectAllGarbage(); |
| 118 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); | 118 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); |
| 119 EXPECT_FALSE(observer->contextDestroyedCalled()); | 119 EXPECT_FALSE(observer->contextDestroyedCalled()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST(LifecycleContextTest, observerRemovedDuringNotifyDestroyed) | 122 TEST(LifecycleContextTest, observerRemovedDuringNotifyDestroyed) |
| 123 { | 123 { |
| 124 // FIXME: Oilpan: this test can be removed when the LifecycleNotifier<T>::m_
observers | 124 // FIXME: Oilpan: this test can be removed when the LifecycleNotifier<T>::m_
observers |
| 125 // hash set is on the heap and membership is handled implicitly by the garba
ge collector. | 125 // hash set is on the heap and membership is handled implicitly by the garba
ge collector. |
| 126 OwnPtrWillBeRawPtr<DummyContext> context = DummyContext::create(); | 126 RawPtr<DummyContext> context = DummyContext::create(); |
| 127 OwnPtrWillBePersistent<TestingObserver> observer = TestingObserver::create(c
ontext.get()); | 127 Persistent<TestingObserver> observer = TestingObserver::create(context.get()
); |
| 128 OwnPtrWillBeRawPtr<TestingObserver> innerObserver = TestingObserver::create(
context.get()); | 128 RawPtr<TestingObserver> innerObserver = TestingObserver::create(context.get(
)); |
| 129 // Attach the observer to the other. When 'observer' is notified | 129 // Attach the observer to the other. When 'observer' is notified |
| 130 // of destruction, it will remove & destroy 'innerObserver'. | 130 // of destruction, it will remove & destroy 'innerObserver'. |
| 131 observer->setObserverToRemoveAndDestroy(innerObserver.release()); | 131 observer->setObserverToRemoveAndDestroy(innerObserver.release()); |
| 132 | 132 |
| 133 EXPECT_EQ(observer->lifecycleContext(), context.get()); | 133 EXPECT_EQ(observer->lifecycleContext(), context.get()); |
| 134 EXPECT_EQ(observer->innerObserver()->lifecycleContext(), context.get()); | 134 EXPECT_EQ(observer->innerObserver()->lifecycleContext(), context.get()); |
| 135 EXPECT_FALSE(observer->contextDestroyedCalled()); | 135 EXPECT_FALSE(observer->contextDestroyedCalled()); |
| 136 EXPECT_FALSE(observer->innerObserver()->contextDestroyedCalled()); | 136 EXPECT_FALSE(observer->innerObserver()->contextDestroyedCalled()); |
| 137 | 137 |
| 138 context->notifyContextDestroyed(); | 138 context->notifyContextDestroyed(); |
| 139 EXPECT_EQ(observer->innerObserver(), nullptr); | 139 EXPECT_EQ(observer->innerObserver(), nullptr); |
| 140 context = nullptr; | 140 context = nullptr; |
| 141 Heap::collectAllGarbage(); | 141 Heap::collectAllGarbage(); |
| 142 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); | 142 EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); |
| 143 EXPECT_TRUE(observer->contextDestroyedCalled()); | 143 EXPECT_TRUE(observer->contextDestroyedCalled()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 } // namespace blink | 146 } // namespace blink |
| OLD | NEW |