[command] Add NetworkButton (#2373)

Closes #2371
This commit is contained in:
Austin Shalit
2020-04-05 23:07:17 -07:00
committed by GitHub
parent 0ad0ec0985
commit e504b3ecbd
5 changed files with 222 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj2.command.button;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.CommandTestBase;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
class NetworkButtonTest extends CommandTestBase {
@BeforeEach
void setup() {
NetworkTableInstance.getDefault().startLocal();
}
@AfterEach
void teardown() {
NetworkTableInstance.getDefault().deleteAllEntries();
NetworkTableInstance.getDefault().stopLocal();
}
@Test
void setNetworkButtonTest() {
var scheduler = CommandScheduler.getInstance();
var commandHolder = new MockCommandHolder(true);
var command = commandHolder.getMock();
var entry = NetworkTableInstance.getDefault()
.getTable("TestTable")
.getEntry("Test");
var button = new NetworkButton("TestTable", "Test");
entry.setBoolean(false);
button.whenPressed(command);
scheduler.run();
verify(command, never()).schedule(true);
entry.setBoolean(true);
scheduler.run();
scheduler.run();
verify(command).schedule(true);
}
}

View File

@@ -0,0 +1,46 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include <networktables/NetworkTableInstance.h>
#include "../CommandTestBase.h"
#include "frc2/command/CommandScheduler.h"
#include "frc2/command/RunCommand.h"
#include "frc2/command/WaitUntilCommand.h"
#include "frc2/command/button/NetworkButton.h"
#include "gtest/gtest.h"
using namespace frc2;
class NetworkButtonTest : public CommandTestBase {
void SetUp() override { nt::NetworkTableInstance::GetDefault().StartLocal(); }
void TearDown() override {
nt::NetworkTableInstance::GetDefault().DeleteAllEntries();
nt::NetworkTableInstance::GetDefault().StopLocal();
}
};
TEST_F(NetworkButtonTest, SetNetworkButtonTest) {
auto& scheduler = CommandScheduler::GetInstance();
auto entry = nt::NetworkTableInstance::GetDefault()
.GetTable("TestTable")
->GetEntry("Test");
bool finished = false;
WaitUntilCommand command([&finished] { return finished; });
NetworkButton("TestTable", "Test").WhenActive(&command);
scheduler.Run();
EXPECT_FALSE(scheduler.IsScheduled(&command));
entry.SetBoolean(true);
scheduler.Run();
EXPECT_TRUE(scheduler.IsScheduled(&command));
finished = true;
scheduler.Run();
EXPECT_FALSE(scheduler.IsScheduled(&command));
}