]> git.basschouten.com Git - openhab-addons.git/blob
2400808d35e675f44089d295b4e32244045f7844
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.util.Collections;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.io.TempDir;
26 import org.junit.jupiter.params.ParameterizedTest;
27 import org.junit.jupiter.params.provider.ValueSource;
28 import org.openhab.binding.nanoleaf.internal.model.ControllerInfo;
29 import org.openhab.binding.nanoleaf.internal.model.PanelLayout;
30 import org.openhab.core.library.types.HSBType;
31
32 import com.google.gson.Gson;
33
34 /**
35  * Test for layout
36  *
37  * @author Jørgen Austvik - Initial contribution
38  */
39 @NonNullByDefault
40 public class NanoleafLayoutTest {
41
42     @TempDir
43     static @Nullable Path temporaryDirectory;
44
45     @ParameterizedTest
46     @ValueSource(strings = { "lasvegas.json", "theduck.json", "squares.json", "wings.json", "spaceinvader.json",
47             "lines.json" })
48     public void testFile(String fileName) throws Exception {
49         Path file = Path.of("src/test/resources/", fileName);
50         assertTrue(Files.exists(file), "File should exist: " + file);
51
52         Gson gson = new Gson();
53         ControllerInfo controllerInfo = gson.fromJson(Files.readString(file, Charset.defaultCharset()),
54                 ControllerInfo.class);
55         assertNotNull(controllerInfo, "File should contain controller info: " + file);
56
57         PanelLayout panelLayout = controllerInfo.getPanelLayout();
58         assertNotNull(panelLayout, "The controller info should contain panel layout");
59
60         LayoutSettings settings = new LayoutSettings(true, true, true, true);
61         byte[] result = NanoleafLayout.render(panelLayout, new TestPanelState(), settings);
62         assertNotNull(result, "Should be able to render the layout: " + fileName);
63         assertTrue(result.length > 0, "Should get content back, but got " + result.length + "bytes");
64
65         Path outFile = Files.createTempFile(temporaryDirectory, fileName.replace(".json", ""), ".png");
66         Files.write(outFile, result);
67
68         // For inspecting images on own computer
69         // Path permanentOutFile = Files.createFile(Path.of("/tmp", fileName.replace(".json", "") + ".png"));
70         // Files.write(permanentOutFile, result);
71     }
72
73     private class TestPanelState extends PanelState {
74         private final HSBType testColors[] = { HSBType.fromRGB(160, 120, 40), HSBType.fromRGB(80, 60, 20),
75                 HSBType.fromRGB(120, 90, 30), HSBType.fromRGB(200, 150, 60) };
76
77         public TestPanelState() {
78             super(Collections.emptyList());
79         }
80
81         @Override
82         public HSBType getHSBForPanel(Integer panelId) {
83             return testColors[panelId % testColors.length];
84         }
85     }
86 }