2 * Copyright (c) 2010-2022 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;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.nanoleaf.internal.model.Layout;
23 import org.openhab.binding.nanoleaf.internal.model.Write;
25 import com.google.gson.Gson;
30 * @author Stefan Höhn - Initial contribution
34 public class LayoutTest {
36 private final Gson gson = new Gson();
37 String layout1Json = "";
38 String layoutInconsistentPanelNoJson = "";
42 layout1Json = "{\n" + " \"numPanels\": 14,\n" + " \"sideLength\": 0,\n"
43 + " \"positionData\": [\n" + " {\n" + " \"panelId\": 60147,\n"
44 + " \"x\": 199,\n" + " \"y\": 99,\n" + " \"o\": 0,\n"
45 + " \"shapeType\": 3\n" + " },\n" + " {\n" + " \"panelId\": 61141,\n"
46 + " \"x\": 200,\n" + " \"y\": 199,\n" + " \"o\": 90,\n"
47 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 42064,\n"
48 + " \"x\": 100,\n" + " \"y\": 200,\n" + " \"o\": 180,\n"
49 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 186,\n"
50 + " \"x\": 0,\n" + " \"y\": 200,\n" + " \"o\": 180,\n"
51 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 19209,\n"
52 + " \"x\": 0,\n" + " \"y\": 100,\n" + " \"o\": 270,\n"
53 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 36604,\n"
54 + " \"x\": 300,\n" + " \"y\": 99,\n" + " \"o\": 0,\n"
55 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 37121,\n"
56 + " \"x\": 400,\n" + " \"y\": 99,\n" + " \"o\": 270,\n"
57 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 45187,\n"
58 + " \"x\": 400,\n" + " \"y\": 199,\n" + " \"o\": 270,\n"
59 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 33626,\n"
60 + " \"x\": 500,\n" + " \"y\": 199,\n" + " \"o\": 270,\n"
61 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 10523,\n"
62 + " \"x\": 600,\n" + " \"y\": 199,\n" + " \"o\": 270,\n"
63 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 54086,\n"
64 + " \"x\": 599,\n" + " \"y\": 99,\n" + " \"o\": 540,\n"
65 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 3512,\n"
66 + " \"x\": 699,\n" + " \"y\": 99,\n" + " \"o\": 540,\n"
67 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 16398,\n"
68 + " \"x\": 799,\n" + " \"y\": 99,\n" + " \"o\": 540,\n"
69 + " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 39163,\n"
70 + " \"x\": 800,\n" + " \"y\": 199,\n" + " \"o\": 630,\n"
71 + " \"shapeType\": 2\n" + " }\n" + " ]\n" + " }";
72 layoutInconsistentPanelNoJson = "{\"numPanels\":15,\"sideLength\":100,\"positionData\":[{\"panelId\":41451,\"x\":350,\"y\":0,\"o\":0,\"shapeType\":3},{\"panelId\":8134,\"x\":350,\"y\":150,\"o\":0,\"shapeType\":2},{\"panelId\":58086,\"x\":200,\"y\":100,\"o\":270,\"shapeType\":2},{\"panelId\":38724,\"x\":300,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":48111,\"x\":200,\"y\":200,\"o\":270,\"shapeType\":2},{\"panelId\":56093,\"x\":100,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":55836,\"x\":0,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":31413,\"x\":100,\"y\":300,\"o\":90,\"shapeType\":2},{\"panelId\":9162,\"x\":300,\"y\":300,\"o\":90,\"shapeType\":2},{\"panelId\":13276,\"x\":400,\"y\":300,\"o\":90,\"shapeType\":2},{\"panelId\":17870,\"x\":400,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":5164,\"x\":500,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":64279,\"x\":600,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":39755,\"x\":500,\"y\":100,\"o\":90,\"shapeType\":2}]}";
76 public void testTheRightLayoutView() {
78 Layout layout = gson.fromJson(layout1Json, Layout.class);
80 layout = new Layout();
82 String layoutView = layout.getLayoutView();
83 assertThat(layoutView, is(equalTo(
84 " 186 42064 61141 45187 33626 10523 39163 \n"
86 + "19209 60147 36604 37121 54086 3512 16398 \n")));
90 public void testTheInconsistentLayoutView() {
92 Layout layout = gson.fromJson(layoutInconsistentPanelNoJson, Layout.class);
94 layout = new Layout();
96 String layoutView = layout.getLayoutView();
97 assertThat(layoutView,
98 is(equalTo(" 31413 9162 13276 \n"
100 + "55836 56093 48111 38724 17870 5164 64279 \n"
108 public void testEffects() {
109 Write write = new Write();
110 write.setCommand("display");
111 write.setAnimType("static");
112 write.setLoop(false);
114 int quotient = Integer.divideUnsigned(Integer.valueOf(panelID), 256);
115 int remainder = Integer.remainderUnsigned(Integer.valueOf(panelID), 256);
116 write.setAnimData(String.format("0 1 %d %d %d %d %d 0 0 10", quotient, remainder, 20, 40, 60));
117 String content = gson.toJson(write);
118 assertThat(content, containsStringIgnoringCase("palette"));
119 assertThat(content, is(not(containsStringIgnoringCase("colorType"))));