2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Preferences.h" // NOLINT(build/include_order)
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-09-14 20:52:06 -07:00
|
|
|
#include <cstdio>
|
2016-09-05 13:55:31 -07:00
|
|
|
#include <fstream>
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2022-10-08 10:01:31 -07:00
|
|
|
#include <networktables/MultiSubscriber.h>
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <ntcore.h>
|
2021-05-28 22:06:59 -07:00
|
|
|
#include <units/time.h>
|
2017-12-07 23:34:29 -08:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Timer.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
static const char* kFileName = "networktables.json";
|
2021-05-28 22:06:59 -07:00
|
|
|
static constexpr auto kSaveTime = 1.2_s;
|
2014-07-30 17:32:14 -04:00
|
|
|
|
|
|
|
|
/**
|
2015-08-13 23:17:19 -07:00
|
|
|
* If we write a new networktables.ini with some sample values, test that
|
2014-07-30 17:32:14 -04:00
|
|
|
* we get those same values back using the Preference class.
|
|
|
|
|
*/
|
|
|
|
|
TEST(PreferencesTest, ReadPreferencesFromFile) {
|
2017-09-07 22:40:18 -07:00
|
|
|
auto inst = nt::NetworkTableInstance::GetDefault();
|
|
|
|
|
inst.StopServer();
|
2021-05-31 10:21:34 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::remove(kFileName);
|
|
|
|
|
std::ofstream preferencesFile(kFileName);
|
2022-10-08 10:01:31 -07:00
|
|
|
preferencesFile << "[" << std::endl;
|
|
|
|
|
preferencesFile << "{\"type\":\"string\","
|
|
|
|
|
<< "\"name\":\"/Preferences/testFileGetString\","
|
|
|
|
|
<< "\"value\":\"Hello, preferences file\","
|
|
|
|
|
<< "\"properties\":{\"persistent\":true}}," << std::endl;
|
|
|
|
|
preferencesFile << "{\"type\":\"int\","
|
|
|
|
|
<< "\"name\":\"/Preferences/testFileGetInt\","
|
|
|
|
|
<< "\"value\":1,"
|
|
|
|
|
<< "\"properties\":{\"persistent\":true}}," << std::endl;
|
|
|
|
|
preferencesFile << "{\"type\":\"double\","
|
|
|
|
|
<< "\"name\":\"/Preferences/testFileGetDouble\","
|
|
|
|
|
<< "\"value\":0.5,"
|
|
|
|
|
<< "\"properties\":{\"persistent\":true}}," << std::endl;
|
|
|
|
|
preferencesFile << "{\"type\":\"float\","
|
|
|
|
|
<< "\"name\":\"/Preferences/testFileGetFloat\","
|
|
|
|
|
<< "\"value\":0.25,"
|
|
|
|
|
<< "\"properties\":{\"persistent\":true}}," << std::endl;
|
|
|
|
|
preferencesFile << "{\"type\":\"boolean\","
|
|
|
|
|
<< "\"name\":\"/Preferences/testFileGetBoolean\","
|
|
|
|
|
<< "\"value\":true,"
|
|
|
|
|
<< "\"properties\":{\"persistent\":true}}]" << std::endl;
|
2015-06-25 15:07:55 -04:00
|
|
|
preferencesFile.close();
|
2021-05-31 10:21:34 -07:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
nt::MultiSubscriber suball{inst, {{std::string_view{}}}};
|
2017-09-07 22:40:18 -07:00
|
|
|
inst.StartServer();
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
int count = 0;
|
|
|
|
|
while ((inst.GetNetworkMode() & NT_NET_MODE_STARTING) != 0) {
|
|
|
|
|
frc::Wait(10_ms);
|
|
|
|
|
count++;
|
|
|
|
|
if (count > 30) {
|
|
|
|
|
FAIL() << "timed out waiting for server startup";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_EQ("Hello, preferences file",
|
2021-06-24 00:58:27 -04:00
|
|
|
frc::Preferences::GetString("testFileGetString"));
|
|
|
|
|
EXPECT_EQ(1, frc::Preferences::GetInt("testFileGetInt"));
|
|
|
|
|
EXPECT_FLOAT_EQ(0.5, frc::Preferences::GetDouble("testFileGetDouble"));
|
|
|
|
|
EXPECT_FLOAT_EQ(0.25f, frc::Preferences::GetFloat("testFileGetFloat"));
|
|
|
|
|
EXPECT_TRUE(frc::Preferences::GetBoolean("testFileGetBoolean"));
|
2014-07-30 17:32:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If we set some values using the Preferences class, test that they show up
|
2022-10-08 10:01:31 -07:00
|
|
|
* in networktables.json
|
2014-07-30 17:32:14 -04:00
|
|
|
*/
|
|
|
|
|
TEST(PreferencesTest, WritePreferencesToFile) {
|
2017-09-07 22:40:18 -07:00
|
|
|
auto inst = nt::NetworkTableInstance::GetDefault();
|
|
|
|
|
inst.StartServer();
|
2022-10-08 10:01:31 -07:00
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
while ((inst.GetNetworkMode() & NT_NET_MODE_STARTING) != 0) {
|
|
|
|
|
frc::Wait(10_ms);
|
|
|
|
|
count++;
|
|
|
|
|
if (count > 30) {
|
|
|
|
|
FAIL() << "timed out waiting for server startup";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 00:58:27 -04:00
|
|
|
frc::Preferences::Remove("testFileGetString");
|
|
|
|
|
frc::Preferences::Remove("testFileGetInt");
|
|
|
|
|
frc::Preferences::Remove("testFileGetDouble");
|
|
|
|
|
frc::Preferences::Remove("testFileGetFloat");
|
|
|
|
|
frc::Preferences::Remove("testFileGetBoolean");
|
2021-05-31 10:21:34 -07:00
|
|
|
|
|
|
|
|
frc::Wait(kSaveTime);
|
|
|
|
|
|
2021-06-24 00:58:27 -04:00
|
|
|
frc::Preferences::SetString("testFileSetString", "Hello, preferences file");
|
|
|
|
|
frc::Preferences::SetInt("testFileSetInt", 1);
|
|
|
|
|
frc::Preferences::SetDouble("testFileSetDouble", 0.5);
|
|
|
|
|
frc::Preferences::SetFloat("testFileSetFloat", 0.25f);
|
|
|
|
|
frc::Preferences::SetBoolean("testFileSetBoolean", true);
|
2021-05-31 10:21:34 -07:00
|
|
|
|
|
|
|
|
frc::Wait(kSaveTime);
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
static char const* kExpectedFileContents[] = {
|
2022-10-08 10:01:31 -07:00
|
|
|
"[",
|
|
|
|
|
" {",
|
|
|
|
|
" \"name\": \"/Preferences/testFileSetString\",",
|
|
|
|
|
" \"type\": \"string\",",
|
|
|
|
|
" \"value\": \"Hello, preferences file\",",
|
|
|
|
|
" \"properties\": {",
|
|
|
|
|
" \"persistent\": true",
|
|
|
|
|
" }",
|
|
|
|
|
" },",
|
|
|
|
|
" {",
|
|
|
|
|
" \"name\": \"/Preferences/testFileSetInt\",",
|
|
|
|
|
" \"type\": \"int\",",
|
|
|
|
|
" \"value\": 1,",
|
|
|
|
|
" \"properties\": {",
|
|
|
|
|
" \"persistent\": true",
|
|
|
|
|
" }",
|
|
|
|
|
" },",
|
|
|
|
|
" {",
|
|
|
|
|
" \"name\": \"/Preferences/testFileSetDouble\",",
|
|
|
|
|
" \"type\": \"double\",",
|
|
|
|
|
" \"value\": 0.5,",
|
|
|
|
|
" \"properties\": {",
|
|
|
|
|
" \"persistent\": true",
|
|
|
|
|
" }",
|
|
|
|
|
" },",
|
|
|
|
|
" {",
|
|
|
|
|
" \"name\": \"/Preferences/testFileSetFloat\",",
|
|
|
|
|
" \"type\": \"float\",",
|
|
|
|
|
" \"value\": 0.25,",
|
|
|
|
|
" \"properties\": {",
|
|
|
|
|
" \"persistent\": true",
|
|
|
|
|
" }",
|
|
|
|
|
" },",
|
|
|
|
|
" {",
|
|
|
|
|
" \"name\": \"/Preferences/testFileSetBoolean\",",
|
|
|
|
|
" \"type\": \"boolean\",",
|
|
|
|
|
" \"value\": true,",
|
|
|
|
|
" \"properties\": {",
|
|
|
|
|
" \"persistent\": true",
|
|
|
|
|
" }",
|
|
|
|
|
" }",
|
|
|
|
|
"]"};
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::ifstream preferencesFile(kFileName);
|
2015-06-23 04:49:51 -07:00
|
|
|
for (auto& kExpectedFileContent : kExpectedFileContents) {
|
2015-06-25 15:07:55 -04:00
|
|
|
ASSERT_FALSE(preferencesFile.eof())
|
|
|
|
|
<< "Preferences file prematurely reached EOF";
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string line;
|
|
|
|
|
std::getline(preferencesFile, line);
|
2014-07-30 17:32:14 -04:00
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
ASSERT_EQ(kExpectedFileContent, line)
|
2022-10-08 10:01:31 -07:00
|
|
|
<< "A line in networktables.json was not correct";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-07-30 17:32:14 -04:00
|
|
|
}
|