mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpilib] Shuffleboard: Keep duplicates on SelectTab() (#5198)
This commit is contained in:
@@ -20,12 +20,16 @@ struct ShuffleboardInstance::Impl {
|
||||
bool tabsChanged = false;
|
||||
std::shared_ptr<nt::NetworkTable> rootTable;
|
||||
std::shared_ptr<nt::NetworkTable> rootMetaTable;
|
||||
nt::StringPublisher selectedTabPub;
|
||||
};
|
||||
|
||||
ShuffleboardInstance::ShuffleboardInstance(nt::NetworkTableInstance ntInstance)
|
||||
: m_impl(new Impl) {
|
||||
m_impl->rootTable = ntInstance.GetTable(Shuffleboard::kBaseTableName);
|
||||
m_impl->rootMetaTable = m_impl->rootTable->GetSubTable(".metadata");
|
||||
m_impl->selectedTabPub =
|
||||
m_impl->rootMetaTable->GetStringTopic("Selected")
|
||||
.Publish(nt::PubSubOptions{.keepDuplicates = true});
|
||||
HAL_Report(HALUsageReporting::kResourceType_Shuffleboard, 0);
|
||||
}
|
||||
|
||||
@@ -75,9 +79,9 @@ void ShuffleboardInstance::DisableActuatorWidgets() {
|
||||
}
|
||||
|
||||
void ShuffleboardInstance::SelectTab(int index) {
|
||||
m_impl->rootMetaTable->GetEntry("Selected").SetDouble(index);
|
||||
m_impl->selectedTabPub.Set(std::to_string(index));
|
||||
}
|
||||
|
||||
void ShuffleboardInstance::SelectTab(std::string_view title) {
|
||||
m_impl->rootMetaTable->GetEntry("Selected").SetString(title);
|
||||
m_impl->selectedTabPub.Set(title);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include <string_view>
|
||||
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
#include <networktables/NetworkTableListener.h>
|
||||
#include <networktables/StringTopic.h>
|
||||
|
||||
#include "frc/shuffleboard/ShuffleboardInstance.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -106,3 +108,22 @@ TEST(ShuffleboardInstanceTest, NestedActuatorWidgetsAreDisabled) {
|
||||
EXPECT_FALSE(controllable)
|
||||
<< "The nested actuator widget should have been disabled";
|
||||
}
|
||||
|
||||
TEST(ShuffleboardInstanceTest, DuplicateSelectTabs) {
|
||||
NTWrapper ntInst;
|
||||
frc::detail::ShuffleboardInstance shuffleboardInst{ntInst.inst};
|
||||
std::atomic_int counter = 0;
|
||||
auto listener = nt::NetworkTableListener::CreateListener(
|
||||
ntInst.inst.GetStringTopic("/Shuffleboard/.metadata/Selected"),
|
||||
nt::EventFlags::kValueAll | nt::EventFlags::kImmediate,
|
||||
[&counter](auto& event) { counter++; });
|
||||
|
||||
// There shouldn't be anything there
|
||||
EXPECT_EQ(0, counter);
|
||||
|
||||
shuffleboardInst.SelectTab("tab1");
|
||||
shuffleboardInst.SelectTab("tab1");
|
||||
EXPECT_TRUE(ntInst.inst.WaitForListenerQueue(0.005))
|
||||
<< "Listener queue timed out!";
|
||||
EXPECT_EQ(2, counter);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import edu.wpi.first.hal.FRCNetComm.tResourceType;
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.networktables.NetworkTable;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import edu.wpi.first.networktables.PubSubOption;
|
||||
import edu.wpi.first.networktables.StringPublisher;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@@ -32,7 +33,8 @@ final class ShuffleboardInstance implements ShuffleboardRoot {
|
||||
requireNonNullParam(ntInstance, "ntInstance", "ShuffleboardInstance");
|
||||
m_rootTable = ntInstance.getTable(Shuffleboard.kBaseTableName);
|
||||
m_rootMetaTable = m_rootTable.getSubTable(".metadata");
|
||||
m_selectedTabPub = m_rootMetaTable.getStringTopic("Selected").publish();
|
||||
m_selectedTabPub =
|
||||
m_rootMetaTable.getStringTopic("Selected").publish(PubSubOption.keepDuplicates(true));
|
||||
HAL.report(tResourceType.kResourceType_Shuffleboard, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.networktables.GenericEntry;
|
||||
import edu.wpi.first.networktables.NetworkTableEntry;
|
||||
import edu.wpi.first.networktables.NetworkTableEvent.Kind;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import java.util.EnumSet;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -121,4 +124,28 @@ class ShuffleboardInstanceTest {
|
||||
controllable = controllableEntry.getValue().getBoolean();
|
||||
assertFalse(controllable, "The nested actuator widget should have been disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDuplicateSelectTabs() {
|
||||
int listener = 0;
|
||||
AtomicInteger counter = new AtomicInteger();
|
||||
try {
|
||||
listener =
|
||||
m_ntInstance.addListener(
|
||||
m_ntInstance.getStringTopic("/Shuffleboard/.metadata/Selected"),
|
||||
EnumSet.of(Kind.kValueAll, Kind.kImmediate),
|
||||
event -> counter.incrementAndGet());
|
||||
|
||||
// There shouldn't be anything there
|
||||
assertEquals(0, counter.get());
|
||||
|
||||
m_shuffleboardInstance.selectTab("tab1");
|
||||
m_shuffleboardInstance.selectTab("tab1");
|
||||
assertTrue(m_ntInstance.waitForListenerQueue(0.005), "Listener queue timed out!");
|
||||
assertEquals(2, counter.get());
|
||||
|
||||
} finally {
|
||||
m_ntInstance.removeListener(listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user