| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "na64util/observer.hh" | ||
| 2 | |||
| 3 | #include "na64util/observer.hh" | ||
| 4 | |||
| 5 | #include <gtest/gtest.h> | ||
| 6 | |||
| 7 | /**\file pubsub.cc | ||
| 8 | * | ||
| 9 | * Testing unit for the templated pubusb implementation | ||
| 10 | * */ | ||
| 11 | |||
| 12 | namespace na64dp { | ||
| 13 | namespace test { | ||
| 14 | |||
| 15 | class MockIntObserver : public util::Observable<int>::iObserver { | ||
| 16 | public: | ||
| 17 | bool intUpdated; | ||
| 18 | 1 | MockIntObserver() : intUpdated(false) {} | |
| 19 | protected: | ||
| 20 | 1 | virtual void handle_update( const int & ) override { intUpdated = true; } | |
| 21 | }; | ||
| 22 | |||
| 23 | class MockFloatObserver : public util::Observable<float>::iObserver { | ||
| 24 | public: | ||
| 25 | bool floatUpdated; | ||
| 26 | 1 | MockFloatObserver() : floatUpdated(false) {} | |
| 27 | protected: | ||
| 28 | ✗ | virtual void handle_update( const float & ) override { floatUpdated = true; } | |
| 29 | }; | ||
| 30 | |||
| 31 | class MockObserver : public util::Observable<int>::iObserver | ||
| 32 | , public util::Observable<float>::iObserver { | ||
| 33 | public: | ||
| 34 | bool intUpdated, floatUpdated; | ||
| 35 | 2 | MockObserver() : intUpdated(false), floatUpdated(false) {} | |
| 36 | protected: | ||
| 37 | 2 | virtual void handle_update( const int & ) override { intUpdated = true; } | |
| 38 | 2 | virtual void handle_update( const float & ) override { floatUpdated = true; } | |
| 39 | }; | ||
| 40 | |||
| 41 | 8 | TEST(Observer, basics) { | |
| 42 | 2 | util::Observable<int> intObservable; | |
| 43 | 2 | util::Observable<float> floatObservable; | |
| 44 | |||
| 45 | 2 | MockObserver observer; | |
| 46 | |||
| 47 |
1/1✓ Branch 1 taken 1 times.
|
2 | intObservable.notify_observers(0); |
| 48 |
1/1✓ Branch 1 taken 1 times.
|
2 | floatObservable.notify_observers(0); |
| 49 | |||
| 50 | // assure mock initialized and no notification had changes yet | ||
| 51 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_FALSE( observer.intUpdated ); |
| 52 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_FALSE( observer.floatUpdated ); |
| 53 | |||
| 54 | // bind observer and observables | ||
| 55 |
1/1✓ Branch 1 taken 1 times.
|
2 | intObservable.bind_observer(observer); |
| 56 |
1/1✓ Branch 1 taken 1 times.
|
2 | floatObservable.bind_observer(observer); |
| 57 | |||
| 58 |
1/1✓ Branch 1 taken 1 times.
|
2 | intObservable.notify_observers(0); |
| 59 | |||
| 60 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_TRUE( observer.intUpdated ); |
| 61 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_FALSE( observer.floatUpdated ); |
| 62 | |||
| 63 |
1/1✓ Branch 1 taken 1 times.
|
2 | floatObservable.notify_observers(0); |
| 64 | |||
| 65 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_TRUE( observer.intUpdated ); |
| 66 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_TRUE( observer.floatUpdated ); |
| 67 | |||
| 68 | 2 | observer.intUpdated = observer.floatUpdated = false; | |
| 69 | |||
| 70 |
1/1✓ Branch 1 taken 1 times.
|
2 | intObservable.unbind_observer(observer); |
| 71 | |||
| 72 |
1/1✓ Branch 1 taken 1 times.
|
2 | intObservable.notify_observers(0); |
| 73 |
1/1✓ Branch 1 taken 1 times.
|
2 | floatObservable.notify_observers(0); |
| 74 | |||
| 75 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_FALSE( observer.intUpdated ); |
| 76 |
2/4✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
|
2 | ASSERT_TRUE( observer.floatUpdated ); |
| 77 | 2 | } | |
| 78 | |||
| 79 | 8 | TEST(Observer, observablesSet) { | |
| 80 | 2 | util::Observables observables; | |
| 81 | |||
| 82 | 2 | util::Observables::ConcreteObservable<int> intValue; | |
| 83 | 2 | util::Observables::ConcreteObservable<float> floatValue; | |
| 84 | |||
| 85 | 2 | MockFloatObserver floatObserver; | |
| 86 | 2 | MockIntObserver intObserver; | |
| 87 | 2 | MockObserver observer; | |
| 88 | |||
| 89 | // Check that we can not update non-existing observables | ||
| 90 |
6/12✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✓ Branch 32 taken 1 times.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✓ Branch 37 taken 1 times.
|
2 | EXPECT_THROW( observables.get_observable<int>().notify_observers(0) |
| 91 | 2 | , error::UnknownObservableType ); | |
| 92 |
6/12✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✓ Branch 32 taken 1 times.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✓ Branch 37 taken 1 times.
|
2 | EXPECT_THROW( observables.get_observable<float>().notify_observers(0) |
| 93 | 2 | , error::UnknownObservableType ); | |
| 94 | // Check that nothing was really updated yet | ||
| 95 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(intObserver.intUpdated); |
| 96 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(floatObserver.floatUpdated); |
| 97 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
|
2 | EXPECT_FALSE(observer.intUpdated || observer.floatUpdated); |
| 98 | |||
| 99 | // Add observables and emit event | ||
| 100 |
1/1✓ Branch 1 taken 1 times.
|
2 | observables.add_observable( intValue ); |
| 101 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | observables.get_observable<int>().notify_observers(0); |
| 102 |
6/12✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✓ Branch 32 taken 1 times.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✓ Branch 37 taken 1 times.
|
2 | EXPECT_THROW( observables.get_observable<float>().notify_observers(0) |
| 103 | 2 | , error::UnknownObservableType ); | |
| 104 | // Check that nothing was really updated yet | ||
| 105 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(intObserver.intUpdated); |
| 106 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(floatObserver.floatUpdated); |
| 107 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
|
2 | EXPECT_FALSE(observer.intUpdated || observer.floatUpdated); |
| 108 | |||
| 109 |
1/1✓ Branch 1 taken 1 times.
|
2 | observables.add_observable( floatValue ); |
| 110 | |||
| 111 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | observables.get_observable<int>().bind_observer( intObserver ); |
| 112 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | observables.get_observable<int>().bind_observer( observer ); |
| 113 | // Check that nothing was really updated yet | ||
| 114 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(intObserver.intUpdated); |
| 115 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(floatObserver.floatUpdated); |
| 116 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
|
2 | EXPECT_FALSE(observer.intUpdated || observer.floatUpdated); |
| 117 | |||
| 118 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | observables.get_observable<int>().notify_observers(0); |
| 119 | |||
| 120 | // Check that int observables got updating signal and float is kept intact | ||
| 121 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_TRUE(intObserver.intUpdated); |
| 122 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(floatObserver.floatUpdated); |
| 123 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_TRUE(observer.intUpdated); |
| 124 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | EXPECT_FALSE(observer.floatUpdated); |
| 125 | 2 | } | |
| 126 | |||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 |