2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nanoleaf.internal.layout;
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
18 import java.nio.charset.Charset;
19 import java.nio.file.Files;
20 import java.nio.file.Path;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.io.TempDir;
25 import org.junit.jupiter.params.ParameterizedTest;
26 import org.junit.jupiter.params.provider.ValueSource;
27 import org.openhab.binding.nanoleaf.internal.model.ControllerInfo;
28 import org.openhab.binding.nanoleaf.internal.model.PanelLayout;
29 import org.openhab.core.library.types.HSBType;
31 import com.google.gson.Gson;
36 * @author Jørgen Austvik - Initial contribution
39 public class NanoleafLayoutTest {
42 static @Nullable Path temporaryDirectory;
45 @ValueSource(strings = { "lasvegas.json", "theduck.json", "squares.json", "wings.json", "spaceinvader.json",
47 public void testFile(String fileName) throws Exception {
48 Path file = Path.of("src/test/resources/", fileName);
49 assertTrue(Files.exists(file), "File should exist: " + file);
51 Gson gson = new Gson();
52 ControllerInfo controllerInfo = gson.fromJson(Files.readString(file, Charset.defaultCharset()),
53 ControllerInfo.class);
54 assertNotNull(controllerInfo, "File should contain controller info: " + file);
56 PanelLayout panelLayout = controllerInfo.getPanelLayout();
57 assertNotNull(panelLayout, "The controller info should contain panel layout");
59 LayoutSettings settings = new LayoutSettings(true, true, true, true);
60 byte[] result = NanoleafLayout.render(panelLayout, new TestPanelState(), settings);
61 assertNotNull(result, "Should be able to render the layout: " + fileName);
62 assertTrue(result.length > 0, "Should get content back, but got " + result.length + "bytes");
64 Path outFile = Files.createTempFile(temporaryDirectory, fileName.replace(".json", ""), ".png");
65 Files.write(outFile, result);
67 // For inspecting images on own computer
68 // Path permanentOutFile = Files.createFile(Path.of("/tmp", fileName.replace(".json", "") + ".png"));
69 // Files.write(permanentOutFile, result);
72 private class TestPanelState implements PanelState {
73 private final HSBType[] testColors = { HSBType.fromRGB(160, 120, 40), HSBType.fromRGB(80, 60, 20),
74 HSBType.fromRGB(120, 90, 30), HSBType.fromRGB(200, 150, 60) };
77 public HSBType getHSBForPanel(Integer panelId) {
78 return testColors[panelId % testColors.length];