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

Side by Side Diff: Source/platform/LifecycleContextTest.cpp

Issue 968633002: Simplify lifecycle notifiers and observers. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 9 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 | Annotate | Revision Log
OLDNEW
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 16 matching lines...) Expand all
27 #include "config.h" 27 #include "config.h"
28 28
29 #include "platform/LifecycleNotifier.h" 29 #include "platform/LifecycleNotifier.h"
30 #include "platform/heap/Handle.h" 30 #include "platform/heap/Handle.h"
31 #include <gtest/gtest.h> 31 #include <gtest/gtest.h>
32 32
33 using namespace blink; 33 using namespace blink;
34 34
35 namespace blink { 35 namespace blink {
36 36
37 class DummyContext final : public NoBaseWillBeGarbageCollectedFinalized<DummyCon text>, public LifecycleNotifier<DummyContext> { 37 class TestingObserver;
38
39 class DummyContext final : public NoBaseWillBeGarbageCollectedFinalized<DummyCon text>, public LifecycleNotifier<DummyContext, TestingObserver> {
38 public: 40 public:
39 DummyContext() 41 DummyContext()
40 : LifecycleNotifier<DummyContext>(this) 42 : LifecycleNotifier<DummyContext, TestingObserver>(this)
41 { 43 {
42 } 44 }
43 45
46 void addObserver(TestingObserver* observer)
47 {
48 LifecycleNotifier<DummyContext, TestingObserver>::addObserver(observer);
49 }
50
51 void removeObserver(TestingObserver* observer)
52 {
53 LifecycleNotifier<DummyContext, TestingObserver>::removeObserver(observe r);
54 }
55
44 DEFINE_INLINE_TRACE() 56 DEFINE_INLINE_TRACE()
45 { 57 {
46 LifecycleNotifier<DummyContext>::trace(visitor); 58 LifecycleNotifier<DummyContext, TestingObserver>::trace(visitor);
47 } 59 }
48 }; 60 };
49 61
50 template<> void observeContext(DummyContext* context, LifecycleObserver<DummyCon text>* observer) 62 class TestingObserver final : public NoBaseWillBeGarbageCollectedFinalized<Testi ngObserver>, public LifecycleObserver<DummyContext, TestingObserver, DummyContex t> {
51 {
52 context->addObserver(observer);
53 }
54
55 template<> void unobserveContext(DummyContext* context, LifecycleObserver<DummyC ontext>* observer)
56 {
57 context->removeObserver(observer);
58 }
59
60 class TestingObserver final : public GarbageCollectedFinalized<TestingObserver>, public LifecycleObserver<DummyContext> {
61 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(TestingObserver); 63 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(TestingObserver);
62 public: 64 public:
63 explicit TestingObserver(DummyContext* context) 65 explicit TestingObserver(DummyContext* context)
64 : LifecycleObserver<DummyContext>(context) 66 : LifecycleObserver<DummyContext, TestingObserver, DummyContext>(context )
65 , m_contextDestroyedCalled(false) 67 , m_contextDestroyedCalled(false)
66 { } 68 {
69 setContext(context);
70 }
67 71
68 virtual void contextDestroyed() override 72 virtual void contextDestroyed() override
69 { 73 {
70 LifecycleObserver<DummyContext>::contextDestroyed(); 74 LifecycleObserver<DummyContext, TestingObserver, DummyContext>::contextD estroyed();
71 m_contextDestroyedCalled = true; 75 m_contextDestroyedCalled = true;
72 } 76 }
73 77
74 DEFINE_INLINE_TRACE() 78 DEFINE_INLINE_TRACE()
75 { 79 {
76 LifecycleObserver<DummyContext>::trace(visitor); 80 LifecycleObserver<DummyContext, TestingObserver, DummyContext>::trace(vi sitor);
77 } 81 }
78 82
79 bool m_contextDestroyedCalled; 83 bool m_contextDestroyedCalled;
80 84
81 void unobserve() { setContext(nullptr); } 85 void unobserve() { setContext(nullptr); }
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 OwnPtrWillBePersistent<TestingObserver> observer = adoptPtrWillBeNoop(new Te stingObserver(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 OwnPtrWillBePersistent<TestingObserver> observer = adoptPtrWillBeNoop(new Te stingObserver(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698