Leosac  0.8.0
Open Source Access Control
Led.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "LeosacFwd.hpp"
22 #include "helper/TestHelper.hpp"
24 #include "tools/runtimeoptions.hpp"
25 #include <string>
26 
27 using namespace Leosac::Module::LedBuzzer;
28 using namespace Leosac::Test::Helper;
29 using namespace Leosac::Hardware;
30 
31 namespace Leosac
32 {
33 namespace Test
34 {
36 {
37  private:
38  // starts the led module
39  virtual bool run_module(zmqpp::socket *pipe) override
40  {
41  boost::property_tree::ptree cfg, module_cfg, leds_cfg, led1_cfg;
42 
43  led1_cfg.add("name", "my_led");
44  led1_cfg.add("gpio", "my_gpio");
45  led1_cfg.add("default_blink_duration", "1000");
46  led1_cfg.add("default_blink_speed", "100");
47 
48  leds_cfg.add_child("led", led1_cfg);
49  module_cfg.add_child("leds", leds_cfg);
50 
51  cfg.add("name", "LED_BUZZER");
52  cfg.add_child("module_config", module_cfg);
53 
54  return test_run_module<LEDBuzzerModule>(&ctx_, pipe, cfg);
55  }
56 
57  public:
59  : TestHelper()
60  , gpio_(ctx_, "my_gpio")
61  , gpio_actor_(std::bind(&FakeGPIO::run, &gpio_, std::placeholders::_1))
62  {
63  bus_sub_.subscribe("");
64  }
65 
67  {
68  }
69 
71 
72  // to allow the fake gpio to react to command.
73  zmqpp::actor gpio_actor_;
74 };
75 
76 TEST_F(LedTest, turnOn)
77 {
78  // create a facade
79  FLED my_led(ctx_, "my_led");
80  ASSERT_TRUE(my_led.isOff());
81 
82  ASSERT_TRUE(my_led.turnOn());
83  ASSERT_TRUE(my_led.isOn());
84  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "ON"));
85 }
86 
87 TEST_F(LedTest, turnOff)
88 {
89  FLED my_led(ctx_, "my_led");
90  ASSERT_TRUE(my_led.isOff());
91 
92  ASSERT_TRUE(my_led.turnOn());
93  ASSERT_TRUE(my_led.isOn());
94  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "ON"));
95 
96  ASSERT_TRUE(my_led.turnOff());
97  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "OFF"));
98  ASSERT_TRUE(my_led.isOff());
99 }
100 
101 TEST_F(LedTest, toggle)
102 {
103  FLED my_led(ctx_, "my_led");
104  ASSERT_TRUE(my_led.isOff());
105 
106  // fake gpio start off
107  ASSERT_TRUE(my_led.toggle());
108  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "ON"));
109  ASSERT_TRUE(my_led.isOn());
110 
111  ASSERT_TRUE(my_led.toggle());
112  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "OFF"));
113  ASSERT_TRUE(my_led.isOff());
114 }
115 
119 TEST_F(LedTest, blink1)
120 {
121  FLED my_led(ctx_, "my_led");
122  ASSERT_TRUE(my_led.isOff());
123  ASSERT_TRUE(my_led.state().st == FLED::State::OFF);
124  ASSERT_FALSE(my_led.isBlinking());
125 
126  my_led.blink(10, 1);
127  ASSERT_TRUE(my_led.isBlinking());
128  ASSERT_TRUE(my_led.state().st == FLED::State::BLINKING);
129  // we should see 10 changes
130  for (int i = 0; i < 5; ++i)
131  {
132  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "ON"));
133  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "OFF"));
134  }
135  ASSERT_TRUE(my_led.isOff());
136  ASSERT_EQ(FLED::State::OFF, my_led.state().st);
137 }
138 
143 TEST_F(LedTest, blink2)
144 {
145  FLED my_led(ctx_, "my_led");
146  my_led.turnOn();
147  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "ON"));
148  ASSERT_TRUE(my_led.isOn());
149  ASSERT_TRUE(my_led.state().st == FLED::State::ON);
150  ASSERT_FALSE(my_led.isBlinking());
151 
152  my_led.blink(18, 2);
153  ASSERT_TRUE(my_led.isBlinking());
154  ASSERT_TRUE(my_led.state().st == FLED::State::BLINKING);
155  // we should see 9 changes
156  for (int i = 0; i < 4; ++i)
157  {
158  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "OFF"));
159  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "ON"));
160  }
161  ASSERT_TRUE(bus_read(bus_sub_, "S_my_gpio", "OFF"));
162  ASSERT_TRUE(my_led.isOff());
163  ASSERT_EQ(FLED::State::OFF, my_led.state().st);
164 }
165 
169 TEST_F(LedTest, blink3)
170 {
171  FLED my_led(ctx_, "my_led");
172  ASSERT_TRUE(my_led.isOff());
173  ASSERT_TRUE(my_led.state().st == FLED::State::OFF);
174  ASSERT_FALSE(my_led.isBlinking());
175 
176  my_led.blink(100, 10);
177  ASSERT_TRUE(my_led.isBlinking());
178  ASSERT_TRUE(my_led.state().st == FLED::State::BLINKING);
179 
180  ASSERT_EQ(my_led.state().duration, 100);
181  ASSERT_EQ(my_led.state().speed, 10);
182 }
183 }
184 }
Leosac::Hardware::FLED::State::speed
int64_t speed
Set only if st is BLINKING, it represents the speed of blinking.
Definition: FLED.hpp:77
runtimeoptions.hpp
RuntimeOptions class declaration.
Leosac::Hardware::FLED::State::st
enum Leosac::Hardware::FLED::State::@0 st
Internal state of the LED.
Leosac::Hardware::FLED::turnOff
bool turnOff()
Turn the LED OFF by sending a message to the backend LED impl.
Definition: FLED.cpp:67
Leosac::Hardware::FLED::isOff
bool isOff()
Similar to isOn().
Definition: FLED.cpp:142
LeosacFwd.hpp
Leosac::Test::LedTest::LedTest
LedTest()
Definition: Led.cpp:58
Leosac::Hardware::FLED
A Facade to a LED object.
Definition: FLED.hpp:44
Leosac::Test::TEST_F
TEST_F(LedTest, blink3)
Test regression for #61.
Definition: Led.cpp:169
Leosac::Hardware::FLED::isOn
bool isOn()
Query the value of the GPIO and returns true if the LED is ON.
Definition: FLED.cpp:128
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Hardware::FLED::state
State state()
Return the state of the device.
Definition: FLED.cpp:152
Leosac::Module::LedBuzzer
Namespace where implementation of Led (or buzzer) support is done.
Definition: LedBuzzerImpl.hpp:32
TestHelper.hpp
Leosac::Hardware::FLED::toggle
bool toggle()
Toggle the LED value by sending a message to the backend LED impl.
Definition: FLED.cpp:80
Leosac::Hardware::FLED::turnOn
bool turnOn(int duration)
Turn the LED ON and turn it OFF duration milliseconds later.
Definition: FLED.cpp:49
Leosac::Test::Helper::bus_read
bool bus_read(zmqpp::socket &sub, Content... content)
Make a blocking read on the bus, return true if content match the message.
Definition: TestHelper.hpp:103
Leosac::Test::LedTest::gpio_
FakeGPIO gpio_
Definition: Led.cpp:70
Leosac::Test::Helper
Unit testing utility class that helps writing test.
Definition: FakeGPIO.hpp:28
Leosac::Hardware::FLED::isBlinking
bool isBlinking()
Returns true is the LED is currently blinking.
Definition: FLED.cpp:147
LEDBuzzerModule.hpp
Leosac::Hardware::FLED::State::duration
int64_t duration
Set only if st is BLINKING, it represents the total duration of blinking.
Definition: FLED.hpp:72
Leosac::Test::LedTest::gpio_actor_
zmqpp::actor gpio_actor_
Definition: Led.cpp:73
FLED.hpp
Leosac::Test::LedTest::run_module
virtual bool run_module(zmqpp::socket *pipe) override
Called in the module's actor's thread.
Definition: Led.cpp:39
Leosac::Test::Helper::TestHelper
Base class for test fixtures, it defines a ZMQ context, a BUS and a sub socket connect to the bus.
Definition: TestHelper.hpp:116
Leosac::Hardware::FLED::blink
bool blink()
Make the LED blink.
Definition: FLED.cpp:93
Leosac::Test::LedTest::~LedTest
~LedTest()
Definition: Led.cpp:66
Leosac::Hardware
Provides facade classes to hardware device implementation.
Definition: Buzzer.cpp:25
Leosac::Test::Helper::FakeGPIO
A test helper class that emulate a GPIO pin.
Definition: FakeGPIO.hpp:34
Leosac::Test::LedTest
Definition: Led.cpp:35