|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT GmbH) - initial implementation |
| 11 | + */ |
| 12 | +package org.eclipse.tm4e.ui.tests.themes; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | + |
| 19 | +import org.eclipse.tm4e.core.grammar.IGrammar; |
| 20 | +import org.eclipse.tm4e.core.registry.IGrammarSource; |
| 21 | +import org.eclipse.tm4e.core.registry.Registry; |
| 22 | +import org.eclipse.tm4e.ui.tests.support.TMEditor; |
| 23 | +import org.eclipse.tm4e.ui.tests.support.TestUtils; |
| 24 | +import org.eclipse.tm4e.ui.themes.css.CSSTokenProvider; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +class CSSThemeColorizationTest { |
| 30 | + |
| 31 | + private static final String SAMPLE_TEXT = "let a = '';\nlet b = 10;\nlet c = true;"; |
| 32 | + |
| 33 | + private IGrammar grammar; |
| 34 | + private TMEditor editor; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setup() throws Exception { |
| 38 | + TestUtils.assertNoTM4EThreadsRunning(); |
| 39 | + grammar = new Registry().addGrammar(IGrammarSource.fromResource(getClass(), "/grammars/TypeScript.tmLanguage.json")); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + void tearDown() throws Exception { |
| 44 | + if (editor != null) { |
| 45 | + editor.dispose(); |
| 46 | + editor = null; |
| 47 | + } |
| 48 | + TestUtils.assertNoTM4EThreadsRunning(); |
| 49 | + } |
| 50 | + |
| 51 | + private static void assertStyleRange( |
| 52 | + final String styleRanges, |
| 53 | + final int offset, |
| 54 | + final int length, |
| 55 | + final String expectedFontStyle, |
| 56 | + final String expectedForegroundColor) { |
| 57 | + assertThat(styleRanges).contains( |
| 58 | + "StyleRange {" + offset + ", " + length + ", fontStyle=" + expectedFontStyle + ", foreground=Color {" |
| 59 | + + expectedForegroundColor + ", 255}}"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void darkCssThemeColorsAreApplied() throws Exception { |
| 64 | + try (var in = Files.newInputStream(Path.of("../org.eclipse.tm4e.ui/themes/Dark.css"))) { |
| 65 | + final var theme = new CSSTokenProvider(in); |
| 66 | + editor = new TMEditor(grammar, theme, SAMPLE_TEXT); |
| 67 | + |
| 68 | + final var commands = editor.execute(); |
| 69 | + assertThat(commands).hasSize(1); |
| 70 | + final String ranges = commands.get(0).getStyleRanges(); |
| 71 | + |
| 72 | + // let -> storage (matches .storage / .storage.type) |
| 73 | + assertStyleRange(ranges, 0, 3, "normal", "86, 156, 214"); |
| 74 | + // '' -> string |
| 75 | + assertStyleRange(ranges, 8, 2, "normal", "206, 145, 120"); |
| 76 | + // 10 -> constant.numeric |
| 77 | + assertStyleRange(ranges, 20, 2, "normal", "181, 206, 168"); |
| 78 | + // true -> constant.language |
| 79 | + assertStyleRange(ranges, 32, 4, "normal", "86, 156, 214"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void monokaiCssThemeColorsAreApplied() throws Exception { |
| 85 | + try (var in = Files.newInputStream(Path.of("../org.eclipse.tm4e.ui/themes/Monokai.css"))) { |
| 86 | + final var theme = new CSSTokenProvider(in); |
| 87 | + editor = new TMEditor(grammar, theme, SAMPLE_TEXT); |
| 88 | + |
| 89 | + final var commands = editor.execute(); |
| 90 | + assertThat(commands).hasSize(1); |
| 91 | + final String ranges = commands.get(0).getStyleRanges(); |
| 92 | + |
| 93 | + // let -> storage.type |
| 94 | + assertStyleRange(ranges, 0, 3, "italic", "102, 217, 239"); |
| 95 | + // '' -> string |
| 96 | + assertStyleRange(ranges, 8, 2, "normal", "230, 219, 116"); |
| 97 | + // 10 -> constant.numeric |
| 98 | + assertStyleRange(ranges, 20, 2, "normal", "174, 129, 255"); |
| 99 | + // true -> constant.language |
| 100 | + assertStyleRange(ranges, 32, 4, "normal", "174, 129, 255"); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments