From f23525da7d0eed9e12626f550338d2503ba95943 Mon Sep 17 00:00:00 2001 From: gbr Date: Sat, 25 Mar 2023 16:07:34 -0700 Subject: [PATCH] Added IndentWidthTest. --- .../.classpath | 11 +++ .../.gitignore | 1 + .../indentguide/painter/IndentWidthTest.java | 88 +++++++++++++++++++ .../{util => painter}/LineTest.java | 4 +- .../indentguide/preferences/GuidePage.java | 20 +++-- 5 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 net.certiv.tools.indentguide.plugin.test/.gitignore create mode 100644 net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/painter/IndentWidthTest.java rename net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/{util => painter}/LineTest.java (88%) diff --git a/net.certiv.tools.indentguide.plugin.test/.classpath b/net.certiv.tools.indentguide.plugin.test/.classpath index dee3a50..381dc3a 100644 --- a/net.certiv.tools.indentguide.plugin.test/.classpath +++ b/net.certiv.tools.indentguide.plugin.test/.classpath @@ -1,10 +1,21 @@ + + + + + + + + + + + diff --git a/net.certiv.tools.indentguide.plugin.test/.gitignore b/net.certiv.tools.indentguide.plugin.test/.gitignore new file mode 100644 index 0000000..ef64d7b --- /dev/null +++ b/net.certiv.tools.indentguide.plugin.test/.gitignore @@ -0,0 +1 @@ +/attic/ diff --git a/net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/painter/IndentWidthTest.java b/net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/painter/IndentWidthTest.java new file mode 100644 index 0000000..b827efb --- /dev/null +++ b/net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/painter/IndentWidthTest.java @@ -0,0 +1,88 @@ +package net.certiv.tools.indentguide.painter; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class IndentWidthTest { + + public final static boolean isWin = SWT.getPlatform().startsWith("win32"); + public final static boolean isCocoa = SWT.getPlatform().startsWith("cocoa"); + public final static boolean isGTK = SWT.getPlatform().equals("gtk"); + public final static boolean isWinOS = System.getProperty("os.name").startsWith("Windows"); + public final static boolean isLinux = System.getProperty("os.name").equals("Linux"); + + String[] fontnames = new String[] { // + "Consolas", "Courier New", "Menlo", // + "Fira Code", "Source Code Pro", "Liberation Mono" // + }; + + Display display; + Shell shell; + GC gc; + + StyledText widget; + + @BeforeEach + public void setUp() { + // display = new Display(); + shell = new Shell(display); + gc = new GC(shell); + + shell.setSize(200, 200); + shell.setLayout(new FillLayout()); + + widget = new StyledText(shell, SWT.BORDER); + widget.setText("This is some dummy text. Sufficient text to have the scroll bars appear."); + } + + @AfterEach + public void tearDown() { + if (gc != null) gc.dispose(); + if (shell != null) shell.dispose(); + } + + @Test + void test_stringExtent() { + for (int size = 7; size < 15; size++) { + for (String name : fontnames) { + FontData fd = new FontData(name, size, SWT.NORMAL); + Font font = new Font(widget.getDisplay(), fd); + widget.setFont(font); + gc.setFont(font); + + Map widths0 = new LinkedHashMap<>(); + Map widths1 = new LinkedHashMap<>(); + for (int col = 1; col <= 10; col++) { + Point p = gc.stringExtent(" "); + widths0.put(col, p.x * col); + + p = gc.stringExtent(" ".repeat(col)); + widths1.put(col, p.x); + } + System.out.printf("Font %-20s: size [%02d] multiply widths %s\n", name, size, widths0); + System.out.printf("Font %-20s: size [%02d] *repeat* widths %s\n", name, size, widths1); + + gc.setFont(null); + widget.setFont(null); + font.dispose(); + + assertTrue(widths0.equals(widths1), String.format("Font %s did not match.", name)); + } + } + } +} diff --git a/net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/util/LineTest.java b/net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/painter/LineTest.java similarity index 88% rename from net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/util/LineTest.java rename to net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/painter/LineTest.java index 2912587..3c364c4 100644 --- a/net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/util/LineTest.java +++ b/net.certiv.tools.indentguide.plugin.test/src/test/java/net/certiv/tools/indentguide/painter/LineTest.java @@ -1,4 +1,4 @@ -package net.certiv.tools.indentguide.util; +package net.certiv.tools.indentguide.painter; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -6,7 +6,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; -import net.certiv.tools.indentguide.painter.Line; +import net.certiv.tools.indentguide.util.Utils; class LineTest { diff --git a/net.certiv.tools.indentguide.plugin/src/main/java/net/certiv/tools/indentguide/preferences/GuidePage.java b/net.certiv.tools.indentguide.plugin/src/main/java/net/certiv/tools/indentguide/preferences/GuidePage.java index 96abca3..f15f3dd 100644 --- a/net.certiv.tools.indentguide.plugin/src/main/java/net/certiv/tools/indentguide/preferences/GuidePage.java +++ b/net.certiv.tools.indentguide.plugin/src/main/java/net/certiv/tools/indentguide/preferences/GuidePage.java @@ -65,7 +65,7 @@ public GuidePage() { Set exclude = Utils.undelimit(getPreferenceStore().getString(Pref.CONTENT_TYPES)); excludeTypes = exclude.stream() // .map(e -> Utils.getPlatformTextType(e)) // - .filter(t -> !t.equals(txtType)) // + .filter(t -> t != null && !txtType.equals(t)) // .collect(Collectors.toCollection(LinkedHashSet::new)); } @@ -131,7 +131,8 @@ private void createContentTypesGroup(Composite parent) { Composite comp = createGroup(parent, Messages.contenttype_group_label, true, 1); blocks.add(comp); - CheckboxTreeViewer viewer = new CheckboxTreeViewer(comp, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); + CheckboxTreeViewer viewer = new CheckboxTreeViewer(comp, + SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); viewer.getControl().setFont(comp.getFont()); GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 150).grab(true, true).applyTo(viewer.getControl()); parts.add(viewer); @@ -186,7 +187,8 @@ private void createVerticalSpacer(Composite comp, int lines) { private void createVerticalSpacer(Composite comp, int lines, int span) { Label spacer = new Label(comp, SWT.NONE); int height = Utils.lineHeight(comp, lines); - GridDataFactory.fillDefaults().hint(SWT.DEFAULT, height).grab(true, false).span(span, 1).applyTo(spacer); + GridDataFactory.fillDefaults().hint(SWT.DEFAULT, height).grab(true, false).span(span, 1) + .applyTo(spacer); } private Label createLabel(Composite comp, String text) { @@ -207,7 +209,8 @@ private Button createLabeledCheckbox(Composite comp, String label, String key) { return btn; } - private Combo createLabeledCombo(Composite comp, String label, String trail, String[] styles, String key) { + private Combo createLabeledCombo(Composite comp, String label, String trail, String[] styles, + String key) { createLabel(comp, label); Combo combo = new Combo(comp, SWT.READ_ONLY); combo.setData(key); @@ -224,7 +227,8 @@ private Combo createLabeledCombo(Composite comp, String label, String trail, Str return combo; } - private Spinner createLabeledSpinner(Composite comp, String label, String trail, int min, int max, String key) { + private Spinner createLabeledSpinner(Composite comp, String label, String trail, int min, int max, + String key) { createLabel(comp, label); Spinner spin = new Spinner(comp, SWT.BORDER); @@ -332,7 +336,8 @@ public boolean performOk() { } /** - * Returns the types of the checked, and optionally not grayed, elements in the tree viewer. + * Returns the types of the checked, and optionally not grayed, elements in the tree + * viewer. * * @param viewer the tree viewer * @param grayed include grayed if {@code true}; exclude grayed if {@code false} @@ -343,7 +348,8 @@ private Set getChecked(CheckboxTreeViewer viewer, boolean grayed) .collect(Collectors.toCollection(LinkedHashSet::new)); if (grayed) return checked; - return checked.stream().filter(t -> !viewer.getGrayed(t)).collect(Collectors.toCollection(LinkedHashSet::new)); + return checked.stream().filter(t -> !viewer.getGrayed(t)) + .collect(Collectors.toCollection(LinkedHashSet::new)); } /** Returns the types of the unchecked tree viewer items */