| 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 18 matching lines...) Expand all  Loading... | 
| 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 GarbageCollectedFinalized<DummyContext>, publi
     c LifecycleNotifier<DummyContext, TestingObserver> { | 36 class DummyContext final : public GarbageCollectedFinalized<DummyContext>, publi
     c LifecycleNotifier<DummyContext, TestingObserver> { | 
| 37     USING_GARBAGE_COLLECTED_MIXIN(DummyContext); | 37     USING_GARBAGE_COLLECTED_MIXIN(DummyContext); | 
| 38 public: | 38 public: | 
| 39     static RawPtr<DummyContext> create() | 39     static DummyContext* create() | 
| 40     { | 40     { | 
| 41         return 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 GarbageCollectedFinalized<TestingObserver>,
      public LifecycleObserver<DummyContext, TestingObserver, DummyContext> { | 50 class TestingObserver final : public GarbageCollectedFinalized<TestingObserver>,
      public LifecycleObserver<DummyContext, TestingObserver, DummyContext> { | 
| 51     USING_GARBAGE_COLLECTED_MIXIN(TestingObserver); | 51     USING_GARBAGE_COLLECTED_MIXIN(TestingObserver); | 
| 52 public: | 52 public: | 
| 53     static RawPtr<TestingObserver> create(DummyContext* context) | 53     static TestingObserver* create(DummyContext* context) | 
| 54     { | 54     { | 
| 55         return 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); | 
| 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(RawPtr<TestingObserver> observerToRemoveO
     nDestruct) | 76     void setObserverToRemoveAndDestroy(TestingObserver* observerToRemoveOnDestru
     ct) | 
| 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
     ; } | 
| 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     Member<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     RawPtr<DummyContext> context = DummyContext::create(); | 98     DummyContext* context = DummyContext::create(); | 
| 99     Persistent<TestingObserver> observer = TestingObserver::create(context.get()
     ); | 99     Persistent<TestingObserver> observer = TestingObserver::create(context); | 
| 100 | 100 | 
| 101     EXPECT_EQ(observer->lifecycleContext(), context.get()); | 101     EXPECT_EQ(observer->lifecycleContext(), context); | 
| 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     RawPtr<DummyContext> context = DummyContext::create(); | 112     DummyContext* context = DummyContext::create(); | 
| 113     Persistent<TestingObserver> observer = TestingObserver::create(context.get()
     ); | 113     Persistent<TestingObserver> observer = TestingObserver::create(context); | 
| 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     DummyContext* context = DummyContext::create(); | 
| 125     // hash set is on the heap and membership is handled implicitly by the garba
     ge collector. | 125     Persistent<TestingObserver> observer = TestingObserver::create(context); | 
| 126     RawPtr<DummyContext> context = DummyContext::create(); | 126     TestingObserver* innerObserver = TestingObserver::create(context); | 
| 127     Persistent<TestingObserver> observer = TestingObserver::create(context.get()
     ); |  | 
| 128     RawPtr<TestingObserver> innerObserver = TestingObserver::create(context.get(
     )); |  | 
| 129     // Attach the observer to the other. When 'observer' is notified | 127     // Attach the observer to the other. When 'observer' is notified | 
| 130     // of destruction, it will remove & destroy 'innerObserver'. | 128     // of destruction, it will remove & destroy 'innerObserver'. | 
| 131     observer->setObserverToRemoveAndDestroy(innerObserver.release()); | 129     observer->setObserverToRemoveAndDestroy(innerObserver); | 
| 132 | 130 | 
| 133     EXPECT_EQ(observer->lifecycleContext(), context.get()); | 131     EXPECT_EQ(observer->lifecycleContext(), context); | 
| 134     EXPECT_EQ(observer->innerObserver()->lifecycleContext(), context.get()); | 132     EXPECT_EQ(observer->innerObserver()->lifecycleContext(), context); | 
| 135     EXPECT_FALSE(observer->contextDestroyedCalled()); | 133     EXPECT_FALSE(observer->contextDestroyedCalled()); | 
| 136     EXPECT_FALSE(observer->innerObserver()->contextDestroyedCalled()); | 134     EXPECT_FALSE(observer->innerObserver()->contextDestroyedCalled()); | 
| 137 | 135 | 
| 138     context->notifyContextDestroyed(); | 136     context->notifyContextDestroyed(); | 
| 139     EXPECT_EQ(observer->innerObserver(), nullptr); | 137     EXPECT_EQ(observer->innerObserver(), nullptr); | 
| 140     context = nullptr; | 138     context = nullptr; | 
| 141     Heap::collectAllGarbage(); | 139     Heap::collectAllGarbage(); | 
| 142     EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); | 140     EXPECT_EQ(observer->lifecycleContext(), static_cast<DummyContext*>(0)); | 
| 143     EXPECT_TRUE(observer->contextDestroyedCalled()); | 141     EXPECT_TRUE(observer->contextDestroyedCalled()); | 
| 144 } | 142 } | 
| 145 | 143 | 
| 146 } // namespace blink | 144 } // namespace blink | 
| OLD | NEW | 
|---|