Debian Codegraph @

examples/AllTests/EventDispatcherTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTestExt/MockSupport.h"
30 #include "EventDispatcher.h"
31
32 class ObserverMock : public EventObserver
33 {
34 public:
35 virtual void notify(const Event& event, int timeOutInSeconds)
36 {
37 mock().actualCall("notify").onObject(this).withParameterOfType("Event", "event", (void*) &event).withParameter("timeOutInSeconds", timeOutInSeconds);
38 }
39 virtual void notifyRegistration(EventObserver* newObserver)
40 {
41 mock().actualCall("notifyRegistration").onObject(this).withParameter("newObserver", newObserver);
42 }
43 };
44
45 class EventComparator : public MockNamedValueComparator
46 {
47 public:
48 virtual bool isEqual(const void* object1, const void* object2)
49 {
50 return ((const Event*)object1)->type == ((const Event*)object2)->type;
51 }
52 virtual SimpleString valueToString(const void* object)
53 {
54 return StringFrom(((const Event*)object)->type);
55 }
56 };
57
58
59 TEST_GROUP(EventDispatcher)
60 {
61 Event event;
62 EventDispatcher* dispatcher;
63 ObserverMock observer;
64 ObserverMock observer2;
65 EventComparator eventComparator;
66
67 void setup()
68 {
69 dispatcher = new EventDispatcher;
70 mock().installComparator("Event", eventComparator);
71 }
72 void teardown()
73 {
74 delete dispatcher;
75 mock().removeAllComparatorsAndCopiers();
76 }
77 };
78
79
80 TEST(EventDispatcher, EventWithoutRegistrationsResultsIntoNoCalls)
81 {
82 dispatcher->dispatchEvent(event, 10);
83 }
84
85 TEST(EventDispatcher, EventWithRegistrationForEventResultsIntoCallback)
86 {
87 mock().expectOneCall("notify").onObject(&observer).withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
88 event.type = IMPORTANT_EVENT;
89
90 dispatcher->registerObserver(IMPORTANT_EVENT, &observer);
91 dispatcher->dispatchEvent(event, 10);
92 }
93
94 TEST(EventDispatcher, DifferentEventWithRegistrationDoesNotResultIntoCallback)
95 {
96 event.type = LESS_IMPORTANT_EVENT;
97 dispatcher->registerObserver(IMPORTANT_EVENT, &observer);
98 dispatcher->dispatchEvent(event, 10);
99 }
100
101 TEST(EventDispatcher, RegisterTwoObserversResultIntoTwoCallsAndARegistrationNotification)
102 {
103 mock().expectOneCall("notify").onObject(&observer).withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
104 mock().expectOneCall("notify").onObject(&observer2).withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
105 mock().expectOneCall("notifyRegistration").onObject(&observer).withParameter("newObserver", &observer2);
106
107 event.type = IMPORTANT_EVENT;
108 dispatcher->registerObserver(IMPORTANT_EVENT, &observer);
109 dispatcher->registerObserver(IMPORTANT_EVENT, &observer2);
110 dispatcher->dispatchEvent(event, 10);
111 }