]> git.basschouten.com Git - openhab-addons.git/blob
25d99226082616246f83820f6981bd0034ca7a01
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nanoleaf.internal.layout;
14
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
17
18 import java.nio.charset.Charset;
19 import java.nio.file.Files;
20 import java.nio.file.Path;
21
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;
30
31 import com.google.gson.Gson;
32
33 /**
34  * Test for layout
35  *
36  * @author Jørgen Austvik - Initial contribution
37  */
38 @NonNullByDefault
39 public class NanoleafLayoutTest {
40
41     @TempDir
42     static @Nullable Path temporaryDirectory;
43
44     @ParameterizedTest
45     @ValueSource(strings = { "lasvegas.json", "theduck.json", "squares.json", "wings.json", "spaceinvader.json",
46             "lines.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);
50
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);
55
56         PanelLayout panelLayout = controllerInfo.getPanelLayout();
57         assertNotNull(panelLayout, "The controller info should contain panel layout");
58
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");
63
64         Path outFile = Files.createTempFile(temporaryDirectory, fileName.replace(".json", ""), ".png");
65         Files.write(outFile, result);
66
67         // For inspecting images on own computer
68         // Path permanentOutFile = Files.createFile(Path.of("/tmp", fileName.replace(".json", "") + ".png"));
69         // Files.write(permanentOutFile, result);
70     }
71
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) };
75
76         @Override
77         public HSBType getHSBForPanel(Integer panelId) {
78             return testColors[panelId % testColors.length];
79         }
80     }
81 }