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