]> git.basschouten.com Git - openhab-addons.git/blob
9626f729680d3aab51102f95558720fd8ccf6e2a
[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;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
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;
24
25 import com.google.gson.Gson;
26
27 /**
28  * Test for the Layout
29  *
30  * @author Stefan Höhn - Initial contribution
31  */
32
33 @NonNullByDefault
34 public class LayoutTest {
35
36     private final Gson gson = new Gson();
37     String layout1Json = "";
38     String layoutInconsistentPanelNoJson = "";
39
40     @BeforeEach
41     public void setup() {
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}]}";
73     }
74
75     @Test
76     public void testTheRightLayoutView() {
77         @Nullable
78         Layout layout = gson.fromJson(layout1Json, Layout.class);
79         if (layout == null) {
80             layout = new Layout();
81         }
82         String layoutView = layout.getLayoutView();
83         assertThat(layoutView, is(equalTo(
84                 "  186       42064       61141                   45187       33626       10523                   39163 \n"
85                         + "                                                                                                      \n"
86                         + "19209                   60147       36604       37121                   54086        3512       16398 \n")));
87     }
88
89     @Test
90     public void testTheInconsistentLayoutView() {
91         @Nullable
92         Layout layout = gson.fromJson(layoutInconsistentPanelNoJson, Layout.class);
93         if (layout == null) {
94             layout = new Layout();
95         }
96         String layoutView = layout.getLayoutView();
97         assertThat(layoutView,
98                 is(equalTo("            31413                    9162       13276                         \n"
99                         + "                                                                              \n"
100                         + "55836       56093       48111       38724       17870        5164       64279 \n"
101                         + "                                           8134                               \n"
102                         + "                        58086                               39755             \n"
103                         + "                                                                              \n"
104                         + "                                          41451                               \n")));
105     }
106
107     @Test
108     public void testEffects() {
109         Write write = new Write();
110         write.setCommand("display");
111         write.setAnimType("static");
112         write.setLoop(false);
113         int panelID = 123;
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"))));
120     }
121 }