File: ActionQueueTest.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (156 lines) | stat: -rw-r--r-- 3,958 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * ActionQueueTest.cpp
 * Test fixture for the ActionQueue class
 * Copyright (C) 2010 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>

#include "ola/ActionQueue.h"
#include "ola/Callback.h"
#include "ola/testing/TestUtils.h"


using ola::Action;
using ola::ActionQueue;
using ola::SingleUseCallback0;

class ActionQueueTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(ActionQueueTest);
  CPPUNIT_TEST(testEmptyQueue);
  CPPUNIT_TEST(testSimpleQueue);
  CPPUNIT_TEST(testFailedQueue);
  CPPUNIT_TEST_SUITE_END();

 public:
    void setUp();
    void CommandsComplete(ActionQueue *queue);
    void testEmptyQueue();
    void testSimpleQueue();
    void testFailedQueue();

 private:
    ActionQueue *m_received_queue;
};


/*
 * A basic test action
 */
class MockAction: public Action {
 public:
    explicit MockAction(bool fatal = false, bool fail = false):
      Action(),
      m_fatal(fatal),
      m_failed(fail),
      m_executed(false) {}

    bool IsFatal() const { return m_fatal; }
    bool Failed() const { return m_failed; }
    bool Executed() const { return m_executed; }

    void Perform(SingleUseCallback0<void> *on_done);

 private:
    bool m_fatal;
    bool m_failed;
    bool m_executed;
};


void MockAction::Perform(SingleUseCallback0<void> *on_done) {
  m_executed = true;
  on_done->Run();
}


CPPUNIT_TEST_SUITE_REGISTRATION(ActionQueueTest);


void ActionQueueTest::setUp() {
  m_received_queue = NULL;
}


void ActionQueueTest::CommandsComplete(ActionQueue *queue) {
  m_received_queue = queue;
}


/*
 * Check that an empty queue works.
 */
void ActionQueueTest::testEmptyQueue() {
  ActionQueue queue(
      NewSingleCallback(this, &ActionQueueTest::CommandsComplete));
  queue.NextAction();
  OLA_ASSERT_EQ(&queue, m_received_queue);
  OLA_ASSERT_TRUE(queue.WasSuccessful());

  // try calling next item to make sure nothing happens
  queue.NextAction();
}


/*
 * Test that a simple queue works
 */
void ActionQueueTest::testSimpleQueue() {
  ActionQueue queue(
      NewSingleCallback(this, &ActionQueueTest::CommandsComplete));

  MockAction *action1 = new MockAction();
  queue.AddAction(action1);
  MockAction *action2 = new MockAction();
  queue.AddAction(action2);

  queue.NextAction();
  OLA_ASSERT_EQ(&queue, m_received_queue);
  OLA_ASSERT_TRUE(queue.WasSuccessful());
  OLA_ASSERT_TRUE(action1->Executed());
  OLA_ASSERT_TRUE(action2->Executed());

  // try calling next item to make sure nothing happens
  queue.NextAction();
}


/*
 * Test that a simple queue works
 */
void ActionQueueTest::testFailedQueue() {
  ActionQueue queue(
      NewSingleCallback(this, &ActionQueueTest::CommandsComplete));

  MockAction *action1 = new MockAction(false, true);
  queue.AddAction(action1);
  MockAction *action2 = new MockAction(true, true);
  queue.AddAction(action2);
  MockAction *action3 = new MockAction();
  queue.AddAction(action3);

  queue.NextAction();
  OLA_ASSERT_EQ(&queue, m_received_queue);
  OLA_ASSERT_FALSE(queue.WasSuccessful());
  OLA_ASSERT_TRUE(action1->Executed());
  OLA_ASSERT_TRUE(action2->Executed());
  OLA_ASSERT_FALSE(action3->Executed());

  // try calling next item to make sure nothing happens
  queue.NextAction();
}