NerfUS mobile node that manages a target for the Nerf gun firing range

Dependencies:   LedController mbed-rtos mbed NerfUSXbee Servomotor TargetManager

Fork of NerfUS by NerfUS

Committer:
Maxime Dupuis
Date:
Thu Mar 30 15:06:01 2017 -0400
Revision:
35:7ee45f6d6484
Parent:
34:85994e0501fb
Child:
37:8fc7845ae95b
Receive TargetInfo in bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Maxime Dupuis 28:8fc53a870691 1 #include "gmock/gmock.h"
Maxime Dupuis 28:8fc53a870691 2 #include "gtest/gtest.h"
Maxime Dupuis 28:8fc53a870691 3
Maxime Dupuis 30:412a779cf607 4 #include "mocks/MockNerfusTicker.hpp"
Maxime Dupuis 28:8fc53a870691 5 #include "mocks/MockTarget.hpp"
Maxime Dupuis 28:8fc53a870691 6 #include "TargetManager.hpp"
Maxime Dupuis 28:8fc53a870691 7
Maxime Dupuis 35:7ee45f6d6484 8 #include "stdint.h"
Maxime Dupuis 35:7ee45f6d6484 9
Maxime Dupuis 28:8fc53a870691 10 class TargetManagerTest : public ::testing::Test
Maxime Dupuis 28:8fc53a870691 11 {
Maxime Dupuis 28:8fc53a870691 12 public:
Maxime Dupuis 28:8fc53a870691 13 TargetManagerTest()
Maxime Dupuis 28:8fc53a870691 14 {
Maxime Dupuis 30:412a779cf607 15 targets.push_back(&mock_target_a);
Maxime Dupuis 30:412a779cf607 16 targets.push_back(&mock_target_b);
Maxime Dupuis 28:8fc53a870691 17 }
Maxime Dupuis 28:8fc53a870691 18
Maxime Dupuis 30:412a779cf607 19 MockTarget mock_target_a;
Maxime Dupuis 30:412a779cf607 20 MockTarget mock_target_b;
Maxime Dupuis 30:412a779cf607 21 std::vector<TargetInterface*> targets;
Maxime Dupuis 30:412a779cf607 22
Maxime Dupuis 30:412a779cf607 23 MockNerfusTicker mock_nerfus_ticker;
Maxime Dupuis 28:8fc53a870691 24 };
Maxime Dupuis 28:8fc53a870691 25
Maxime Dupuis 28:8fc53a870691 26 TEST_F(TargetManagerTest, LibraryBuilds)
Maxime Dupuis 28:8fc53a870691 27 {
Maxime Dupuis 30:412a779cf607 28 TargetManager target_manager(targets, mock_nerfus_ticker);
Maxime Dupuis 28:8fc53a870691 29 }
Maxime Dupuis 28:8fc53a870691 30
Maxime Dupuis 31:c6dc9811ed14 31 TEST_F(TargetManagerTest, WhenNoTargetIsActiveThenCallsToTargetHitAndTargetMissedDoNothing)
Maxime Dupuis 31:c6dc9811ed14 32 {
Maxime Dupuis 31:c6dc9811ed14 33 TargetManager target_manager(targets, mock_nerfus_ticker);
Maxime Dupuis 31:c6dc9811ed14 34
Maxime Dupuis 31:c6dc9811ed14 35 target_manager.target_missed(0);
Maxime Dupuis 31:c6dc9811ed14 36 target_manager.target_hit(0);
Maxime Dupuis 31:c6dc9811ed14 37 }
Maxime Dupuis 32:50b777bab5a4 38
Maxime Dupuis 32:50b777bab5a4 39 TEST_F(TargetManagerTest, WhenExecutingAllyTargetThenUseTheCorrectTargetTypeAndTimeout)
Maxime Dupuis 32:50b777bab5a4 40 {
Maxime Dupuis 32:50b777bab5a4 41 EXPECT_CALL(mock_target_a, ally_command());
Maxime Dupuis 32:50b777bab5a4 42 EXPECT_CALL(mock_nerfus_ticker, start(1000));
Maxime Dupuis 32:50b777bab5a4 43
Maxime Dupuis 32:50b777bab5a4 44 TargetManager target_manager(targets, mock_nerfus_ticker);
Maxime Dupuis 34:85994e0501fb 45 target_manager.execute(make_TargetInfo(0, TARGET_TYPE_ALLY, 1000));
Maxime Dupuis 32:50b777bab5a4 46 }
Maxime Dupuis 32:50b777bab5a4 47
Maxime Dupuis 32:50b777bab5a4 48 TEST_F(TargetManagerTest, WhenExecutingEnemyTargetThenUseTheCorrectTargetTypeAndTimeout)
Maxime Dupuis 32:50b777bab5a4 49 {
Maxime Dupuis 32:50b777bab5a4 50 EXPECT_CALL(mock_target_b, enemy_command());
Maxime Dupuis 32:50b777bab5a4 51 EXPECT_CALL(mock_nerfus_ticker, start(2000));
Maxime Dupuis 32:50b777bab5a4 52
Maxime Dupuis 32:50b777bab5a4 53 TargetManager target_manager(targets, mock_nerfus_ticker);
Maxime Dupuis 34:85994e0501fb 54 target_manager.execute(make_TargetInfo(1, TARGET_TYPE_ENEMY, 2000));
Maxime Dupuis 32:50b777bab5a4 55 }
Maxime Dupuis 32:50b777bab5a4 56
Maxime Dupuis 32:50b777bab5a4 57 TEST_F(TargetManagerTest, WhenActiveTargetIsHitThenSendTheEvent)
Maxime Dupuis 32:50b777bab5a4 58 {
Maxime Dupuis 32:50b777bab5a4 59 EXPECT_CALL(mock_target_a, ally_command());
Maxime Dupuis 32:50b777bab5a4 60 EXPECT_CALL(mock_nerfus_ticker, start(1000));
Maxime Dupuis 32:50b777bab5a4 61 EXPECT_CALL(mock_nerfus_ticker, get_time_ms())
Maxime Dupuis 32:50b777bab5a4 62 .WillOnce(::testing::Return(42));
Maxime Dupuis 32:50b777bab5a4 63 EXPECT_CALL(mock_nerfus_ticker, stop());
Maxime Dupuis 32:50b777bab5a4 64 EXPECT_CALL(mock_target_a, hit(42));
Maxime Dupuis 32:50b777bab5a4 65
Maxime Dupuis 32:50b777bab5a4 66 TargetManager target_manager(targets, mock_nerfus_ticker);
Maxime Dupuis 34:85994e0501fb 67 target_manager.execute(make_TargetInfo(0, TARGET_TYPE_ALLY, 1000));
Maxime Dupuis 32:50b777bab5a4 68
Maxime Dupuis 32:50b777bab5a4 69 target_manager.target_hit(0);
Maxime Dupuis 32:50b777bab5a4 70 }
Maxime Dupuis 32:50b777bab5a4 71
Maxime Dupuis 32:50b777bab5a4 72 TEST_F(TargetManagerTest, WhenActiveTargetIsMissedThenSendTheEvent)
Maxime Dupuis 32:50b777bab5a4 73 {
Maxime Dupuis 32:50b777bab5a4 74 EXPECT_CALL(mock_target_a, ally_command());
Maxime Dupuis 32:50b777bab5a4 75 EXPECT_CALL(mock_nerfus_ticker, start(1000));
Maxime Dupuis 32:50b777bab5a4 76 EXPECT_CALL(mock_nerfus_ticker, get_time_ms())
Maxime Dupuis 32:50b777bab5a4 77 .WillOnce(::testing::Return(42));
Maxime Dupuis 32:50b777bab5a4 78 EXPECT_CALL(mock_nerfus_ticker, stop());
Maxime Dupuis 32:50b777bab5a4 79 EXPECT_CALL(mock_target_a, timeout(42));
Maxime Dupuis 32:50b777bab5a4 80
Maxime Dupuis 32:50b777bab5a4 81 TargetManager target_manager(targets, mock_nerfus_ticker);
Maxime Dupuis 34:85994e0501fb 82 target_manager.execute(make_TargetInfo(0, TARGET_TYPE_ALLY, 1000));
Maxime Dupuis 32:50b777bab5a4 83
Maxime Dupuis 32:50b777bab5a4 84 target_manager.target_missed(0);
Maxime Dupuis 32:50b777bab5a4 85 }
Maxime Dupuis 32:50b777bab5a4 86
Maxime Dupuis 35:7ee45f6d6484 87 TEST_F(TargetManagerTest, WhenReceivingCommandInBytesThenExecuteIt)
Maxime Dupuis 35:7ee45f6d6484 88 {
Maxime Dupuis 35:7ee45f6d6484 89 TargetManager target_manager(targets, mock_nerfus_ticker);
Maxime Dupuis 35:7ee45f6d6484 90
Maxime Dupuis 35:7ee45f6d6484 91 EXPECT_CALL(mock_target_b, ally_command());
Maxime Dupuis 35:7ee45f6d6484 92 EXPECT_CALL(mock_nerfus_ticker, start(0x1234));
Maxime Dupuis 35:7ee45f6d6484 93
Maxime Dupuis 35:7ee45f6d6484 94 std::vector<uint8_t> target_info_bytes;
Maxime Dupuis 35:7ee45f6d6484 95 target_info_bytes.push_back(0x00); //Message from coordinator
Maxime Dupuis 35:7ee45f6d6484 96 target_info_bytes.push_back(0x01); //Second target
Maxime Dupuis 35:7ee45f6d6484 97 target_info_bytes.push_back(0x00); //Mode ally
Maxime Dupuis 35:7ee45f6d6484 98 target_info_bytes.push_back(0x12); //Timeout MSB
Maxime Dupuis 35:7ee45f6d6484 99 target_info_bytes.push_back(0x34); //Timeout MSB
Maxime Dupuis 35:7ee45f6d6484 100
Maxime Dupuis 35:7ee45f6d6484 101 target_manager.execute(target_info_bytes);
Maxime Dupuis 35:7ee45f6d6484 102 }
Maxime Dupuis 35:7ee45f6d6484 103