Files
swt-cctv/src/test/java/CameraSettingsTest.java
rattatwinko 965bed6801 some swing stuff cause swt is heavy and swing isnt
+ SwingCameraWindow
+ SwingCCTVManager

---
fixes in WebcamCaptureLoop.java : Include thread safety, and some more stuff which is needed for Swing components.

Planning on deprecating SWT in general
2026-01-11 21:09:51 +01:00

98 lines
3.1 KiB
Java

import io.swtc.networking.CameraConfig;
import io.swtc.networking.CameraSettings;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CameraSettingsTest {
// Must match the filename used in CameraSettings.java
private final File TEST_FILE = new File("network_cameras.json");
@BeforeEach
@AfterEach
void cleanUp() {
// Ensure we start and end with a clean slate to avoid side effects
if (TEST_FILE.exists()) {
TEST_FILE.delete();
}
}
@Test
void testLoadReturnsEmptyListWhenNoFile() {
// If the file doesn't exist, it should return an empty list (not null)
List<CameraConfig> result = CameraSettings.load();
assertNotNull(result, "Load should never return null");
assertTrue(result.isEmpty(), "Should return empty list if file doesn't exist");
}
@Test
void testSaveAndLoad() {
// 1. Create a config (Using your actual constructor)
CameraConfig config = new CameraConfig("FrontDoor", "http://192.168.1.100/mjpeg");
// 2. Save it
CameraSettings.save(config);
// 3. Verify file creation
assertTrue(TEST_FILE.exists(), "File should be created after save");
// 4. Load it back
List<CameraConfig> loaded = CameraSettings.load();
// 5. Verify contents
assertEquals(1, loaded.size());
assertEquals("FrontDoor", loaded.get(0).getName());
assertEquals("http://192.168.1.100/mjpeg", loaded.get(0).getUrl());
}
@Test
void testSaveMultiple() {
// Save two distinct cameras
CameraSettings.save(new CameraConfig("Cam1", "rtsp://10.0.0.1/stream"));
CameraSettings.save(new CameraConfig("Cam2", "rtsp://10.0.0.2/stream"));
List<CameraConfig> loaded = CameraSettings.load();
assertEquals(2, loaded.size());
assertEquals("Cam1", loaded.get(0).getName());
assertEquals("Cam2", loaded.get(1).getName());
}
@Test
void testDelete() {
// Setup: Save two cameras
CameraSettings.save(new CameraConfig("Garage", "http://1.1.1.1"));
CameraSettings.save(new CameraConfig("Garden", "http://2.2.2.2"));
// Action: Delete "Garage"
CameraSettings.delete("Garage");
// Verify: Only "Garden" remains
List<CameraConfig> result = CameraSettings.load();
assertEquals(1, result.size());
assertEquals("Garden", result.get(0).getName());
}
@Test
void testLoadCorruptFile() throws IOException {
// Manually write broken JSON to the file
try (FileWriter writer = new FileWriter(TEST_FILE)) {
writer.write("{ \"this is broken json\": ... ");
}
// The code catches IOException and returns empty list
List<CameraConfig> result = CameraSettings.load();
assertNotNull(result);
assertTrue(result.isEmpty(), "Should handle corrupt JSON gracefully by returning empty list");
}
}