blob: ef58bb4bed1c9da339618fa4e70fb84fb0eb19e4 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/Preferences.h" // NOLINT(build/include_order)
6
7#include <cstdio>
8#include <fstream>
9
James Kuszmaulb13e13f2023-11-22 20:44:04 -080010#include <gtest/gtest.h>
James Kuszmaulcf324122023-01-14 14:07:17 -080011#include <networktables/MultiSubscriber.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080012#include <networktables/NetworkTableInstance.h>
13#include <ntcore.h>
Austin Schuh812d0d12021-11-04 20:16:48 -070014#include <units/time.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080015
16#include "frc/Timer.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080017
James Kuszmaulcf324122023-01-14 14:07:17 -080018static const char* kFileName = "networktables.json";
Austin Schuh812d0d12021-11-04 20:16:48 -070019static constexpr auto kSaveTime = 1.2_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080020
21/**
22 * If we write a new networktables.ini with some sample values, test that
23 * we get those same values back using the Preference class.
24 */
25TEST(PreferencesTest, ReadPreferencesFromFile) {
26 auto inst = nt::NetworkTableInstance::GetDefault();
27 inst.StopServer();
Austin Schuh812d0d12021-11-04 20:16:48 -070028
Brian Silverman8fce7482020-01-05 13:18:21 -080029 std::remove(kFileName);
30 std::ofstream preferencesFile(kFileName);
James Kuszmaulcf324122023-01-14 14:07:17 -080031 preferencesFile << "[" << std::endl;
32 preferencesFile << "{\"type\":\"string\","
33 << "\"name\":\"/Preferences/testFileGetString\","
34 << "\"value\":\"Hello, preferences file\","
35 << "\"properties\":{\"persistent\":true}}," << std::endl;
36 preferencesFile << "{\"type\":\"int\","
37 << "\"name\":\"/Preferences/testFileGetInt\","
38 << "\"value\":1,"
39 << "\"properties\":{\"persistent\":true}}," << std::endl;
40 preferencesFile << "{\"type\":\"double\","
41 << "\"name\":\"/Preferences/testFileGetDouble\","
42 << "\"value\":0.5,"
43 << "\"properties\":{\"persistent\":true}}," << std::endl;
44 preferencesFile << "{\"type\":\"float\","
45 << "\"name\":\"/Preferences/testFileGetFloat\","
46 << "\"value\":0.25,"
47 << "\"properties\":{\"persistent\":true}}," << std::endl;
48 preferencesFile << "{\"type\":\"boolean\","
49 << "\"name\":\"/Preferences/testFileGetBoolean\","
50 << "\"value\":true,"
51 << "\"properties\":{\"persistent\":true}}]" << std::endl;
Brian Silverman8fce7482020-01-05 13:18:21 -080052 preferencesFile.close();
Austin Schuh812d0d12021-11-04 20:16:48 -070053
James Kuszmaulcf324122023-01-14 14:07:17 -080054 nt::MultiSubscriber suball{inst, {{std::string_view{}}}};
Brian Silverman8fce7482020-01-05 13:18:21 -080055 inst.StartServer();
56
James Kuszmaulcf324122023-01-14 14:07:17 -080057 int count = 0;
58 while ((inst.GetNetworkMode() & NT_NET_MODE_STARTING) != 0) {
59 frc::Wait(10_ms);
60 count++;
61 if (count > 30) {
62 FAIL() << "timed out waiting for server startup";
63 }
64 }
65
Brian Silverman8fce7482020-01-05 13:18:21 -080066 EXPECT_EQ("Hello, preferences file",
Austin Schuh812d0d12021-11-04 20:16:48 -070067 frc::Preferences::GetString("testFileGetString"));
68 EXPECT_EQ(1, frc::Preferences::GetInt("testFileGetInt"));
69 EXPECT_FLOAT_EQ(0.5, frc::Preferences::GetDouble("testFileGetDouble"));
70 EXPECT_FLOAT_EQ(0.25f, frc::Preferences::GetFloat("testFileGetFloat"));
71 EXPECT_TRUE(frc::Preferences::GetBoolean("testFileGetBoolean"));
Brian Silverman8fce7482020-01-05 13:18:21 -080072}
73
74/**
75 * If we set some values using the Preferences class, test that they show up
James Kuszmaulcf324122023-01-14 14:07:17 -080076 * in networktables.json
Brian Silverman8fce7482020-01-05 13:18:21 -080077 */
78TEST(PreferencesTest, WritePreferencesToFile) {
79 auto inst = nt::NetworkTableInstance::GetDefault();
80 inst.StartServer();
James Kuszmaulcf324122023-01-14 14:07:17 -080081
82 int count = 0;
83 while ((inst.GetNetworkMode() & NT_NET_MODE_STARTING) != 0) {
84 frc::Wait(10_ms);
85 count++;
86 if (count > 30) {
87 FAIL() << "timed out waiting for server startup";
88 }
89 }
90
Austin Schuh812d0d12021-11-04 20:16:48 -070091 frc::Preferences::Remove("testFileGetString");
92 frc::Preferences::Remove("testFileGetInt");
93 frc::Preferences::Remove("testFileGetDouble");
94 frc::Preferences::Remove("testFileGetFloat");
95 frc::Preferences::Remove("testFileGetBoolean");
Brian Silverman8fce7482020-01-05 13:18:21 -080096
Austin Schuh812d0d12021-11-04 20:16:48 -070097 frc::Wait(kSaveTime);
Brian Silverman8fce7482020-01-05 13:18:21 -080098
Austin Schuh812d0d12021-11-04 20:16:48 -070099 frc::Preferences::SetString("testFileSetString", "Hello, preferences file");
100 frc::Preferences::SetInt("testFileSetInt", 1);
101 frc::Preferences::SetDouble("testFileSetDouble", 0.5);
102 frc::Preferences::SetFloat("testFileSetFloat", 0.25f);
103 frc::Preferences::SetBoolean("testFileSetBoolean", true);
Brian Silverman8fce7482020-01-05 13:18:21 -0800104
Austin Schuh812d0d12021-11-04 20:16:48 -0700105 frc::Wait(kSaveTime);
Brian Silverman8fce7482020-01-05 13:18:21 -0800106
107 static char const* kExpectedFileContents[] = {
James Kuszmaulcf324122023-01-14 14:07:17 -0800108 "[",
109 " {",
110 " \"name\": \"/Preferences/testFileSetString\",",
111 " \"type\": \"string\",",
112 " \"value\": \"Hello, preferences file\",",
113 " \"properties\": {",
114 " \"persistent\": true",
115 " }",
116 " },",
117 " {",
118 " \"name\": \"/Preferences/testFileSetInt\",",
119 " \"type\": \"int\",",
120 " \"value\": 1,",
121 " \"properties\": {",
122 " \"persistent\": true",
123 " }",
124 " },",
125 " {",
126 " \"name\": \"/Preferences/testFileSetDouble\",",
127 " \"type\": \"double\",",
128 " \"value\": 0.5,",
129 " \"properties\": {",
130 " \"persistent\": true",
131 " }",
132 " },",
133 " {",
134 " \"name\": \"/Preferences/testFileSetFloat\",",
135 " \"type\": \"float\",",
136 " \"value\": 0.25,",
137 " \"properties\": {",
138 " \"persistent\": true",
139 " }",
140 " },",
141 " {",
142 " \"name\": \"/Preferences/testFileSetBoolean\",",
143 " \"type\": \"boolean\",",
144 " \"value\": true,",
145 " \"properties\": {",
146 " \"persistent\": true",
147 " }",
148 " }",
149 "]"};
Brian Silverman8fce7482020-01-05 13:18:21 -0800150
151 std::ifstream preferencesFile(kFileName);
152 for (auto& kExpectedFileContent : kExpectedFileContents) {
153 ASSERT_FALSE(preferencesFile.eof())
154 << "Preferences file prematurely reached EOF";
155
156 std::string line;
157 std::getline(preferencesFile, line);
158
159 ASSERT_EQ(kExpectedFileContent, line)
James Kuszmaulcf324122023-01-14 14:07:17 -0800160 << "A line in networktables.json was not correct";
Brian Silverman8fce7482020-01-05 13:18:21 -0800161 }
162}