|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2025 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.mqtt.awtrixlight.internal.app; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import com.google.gson.JsonArray; |
| 20 | +import com.google.gson.JsonObject; |
| 21 | +import com.google.gson.JsonParser; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test cases for the {@link AwtrixApp} object. |
| 25 | + * |
| 26 | + * @author Thomas Lauterbach - Initial contribution |
| 27 | + */ |
| 28 | +class AwtrixAppTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + void testTextWithMixedContent() { |
| 32 | + AwtrixApp app = new AwtrixApp(); |
| 33 | + app.setText("This is a <font color=\"#cc33cc\">Multi</font> Colored <font color=\"#34ab12\">Text</font>!"); |
| 34 | + app.setColor(new int[] { 255, 255, 255 }); // Default white color |
| 35 | + |
| 36 | + String config = app.getAppConfig(); |
| 37 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 38 | + JsonArray text = json.get("text").getAsJsonArray(); |
| 39 | + |
| 40 | + // Should be 5 segments: "This is a ", "Multi", " Colored ", "Text", "!" |
| 41 | + assertEquals(5, text.size()); |
| 42 | + |
| 43 | + // Verify each segment's text |
| 44 | + assertEquals("This is a ", text.get(0).getAsJsonObject().get("t").getAsString()); |
| 45 | + assertEquals("Multi", text.get(1).getAsJsonObject().get("t").getAsString()); |
| 46 | + assertEquals(" Colored ", text.get(2).getAsJsonObject().get("t").getAsString()); |
| 47 | + assertEquals("Text", text.get(3).getAsJsonObject().get("t").getAsString()); |
| 48 | + assertEquals("!", text.get(4).getAsJsonObject().get("t").getAsString()); |
| 49 | + |
| 50 | + // Optionally, verify colors |
| 51 | + assertEquals("ffffff", text.get(0).getAsJsonObject().get("c").getAsString()); // Default color |
| 52 | + assertEquals("cc33cc", text.get(1).getAsJsonObject().get("c").getAsString()); |
| 53 | + assertEquals("ffffff", text.get(2).getAsJsonObject().get("c").getAsString()); // Default color |
| 54 | + assertEquals("34ab12", text.get(3).getAsJsonObject().get("c").getAsString()); |
| 55 | + assertEquals("ffffff", text.get(4).getAsJsonObject().get("c").getAsString()); // Default color |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testTextStartingWithColor() { |
| 60 | + AwtrixApp app = new AwtrixApp(); |
| 61 | + app.setText("<font color=\"#ff0000\">Red</font> text at start"); |
| 62 | + app.setColor(new int[] { 0, 0, 0 }); // Default black color |
| 63 | + |
| 64 | + String config = app.getAppConfig(); |
| 65 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 66 | + |
| 67 | + assertTrue(json.has("text")); |
| 68 | + JsonArray text = json.get("text").getAsJsonArray(); |
| 69 | + assertEquals(2, text.size()); |
| 70 | + assertEquals("Red", text.get(0).getAsJsonObject().get("t").getAsString()); |
| 71 | + assertEquals(" text at start", text.get(1).getAsJsonObject().get("t").getAsString()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testTextWithBrackets() { |
| 76 | + AwtrixApp app = new AwtrixApp(); |
| 77 | + app.setText("<font color=\"#ff0000\">Red <- Nice!</font>"); |
| 78 | + app.setColor(new int[] { 0, 0, 0 }); // Default black color |
| 79 | + |
| 80 | + String config = app.getAppConfig(); |
| 81 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 82 | + |
| 83 | + assertTrue(json.has("text")); |
| 84 | + JsonArray text = json.get("text").getAsJsonArray(); |
| 85 | + assertEquals(1, text.size()); |
| 86 | + assertEquals("Red <- Nice!", text.get(0).getAsJsonObject().get("t").getAsString()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testAdjacentColorSegments() { |
| 91 | + AwtrixApp app = new AwtrixApp(); |
| 92 | + app.setText("<font color=\"#ff0000\">Red</font><font color=\"#0000ff\">Blue</font>"); |
| 93 | + app.setColor(new int[] { 0, 0, 0 }); |
| 94 | + |
| 95 | + String config = app.getAppConfig(); |
| 96 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 97 | + |
| 98 | + assertTrue(json.has("text")); |
| 99 | + JsonArray text = json.get("text").getAsJsonArray(); |
| 100 | + assertEquals(2, text.size()); |
| 101 | + assertEquals("Red", text.get(0).getAsJsonObject().get("t").getAsString()); |
| 102 | + assertEquals("Blue", text.get(1).getAsJsonObject().get("t").getAsString()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testAdjacentColorSegmentsWithSpace() { |
| 107 | + AwtrixApp app = new AwtrixApp(); |
| 108 | + app.setText("<font color=\"#ff0000\">Red</font> <font color=\"#0000ff\">Blue</font>"); |
| 109 | + app.setColor(new int[] { 0, 0, 0 }); |
| 110 | + |
| 111 | + String config = app.getAppConfig(); |
| 112 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 113 | + |
| 114 | + assertTrue(json.has("text")); |
| 115 | + JsonArray text = json.get("text").getAsJsonArray(); |
| 116 | + assertEquals(3, text.size()); |
| 117 | + assertEquals("Red", text.get(0).getAsJsonObject().get("t").getAsString()); |
| 118 | + assertEquals(" ", text.get(1).getAsJsonObject().get("t").getAsString()); |
| 119 | + assertEquals("Blue", text.get(2).getAsJsonObject().get("t").getAsString()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void testPlainText() { |
| 124 | + AwtrixApp app = new AwtrixApp(); |
| 125 | + app.setText("Just plain text with no colors"); |
| 126 | + app.setColor(new int[] { 18, 52, 86 }); // Some default color |
| 127 | + |
| 128 | + String config = app.getAppConfig(); |
| 129 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 130 | + |
| 131 | + assertTrue(json.has("text")); |
| 132 | + String text = json.get("text").getAsString(); |
| 133 | + assertEquals("Just plain text with no colors", text); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testEmptyString() { |
| 138 | + AwtrixApp app = new AwtrixApp(); |
| 139 | + app.setText(""); |
| 140 | + app.setColor(new int[] { 0, 0, 0 }); |
| 141 | + |
| 142 | + String config = app.getAppConfig(); |
| 143 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 144 | + |
| 145 | + // Depending on implementation, the text field might be empty or not present |
| 146 | + if (json.has("text")) { |
| 147 | + assertTrue(json.get("text").getAsString().isEmpty()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void testOnlySpaces() { |
| 153 | + AwtrixApp app = new AwtrixApp(); |
| 154 | + app.setText(" "); |
| 155 | + app.setColor(new int[] { 0, 0, 0 }); |
| 156 | + |
| 157 | + String config = app.getAppConfig(); |
| 158 | + JsonObject json = JsonParser.parseString(config).getAsJsonObject(); |
| 159 | + |
| 160 | + assertTrue(json.has("text")); |
| 161 | + assertEquals(" ", json.get("text").getAsString()); |
| 162 | + } |
| 163 | +} |
0 commit comments