|
| 1 | +package org.eclipse.swt.tests.gtk; |
| 2 | + |
| 3 | +import java.util.function.Supplier; |
| 4 | + |
| 5 | +import org.eclipse.swt.SWT; |
| 6 | +import org.eclipse.swt.graphics.GC; |
| 7 | +import org.eclipse.swt.graphics.Image; |
| 8 | +import org.eclipse.swt.layout.GridData; |
| 9 | +import org.eclipse.swt.layout.GridLayout; |
| 10 | +import org.eclipse.swt.widgets.Shell; |
| 11 | +import org.eclipse.swt.widgets.Tree; |
| 12 | +import org.eclipse.swt.widgets.TreeItem; |
| 13 | +import org.junit.After; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class Test_Gtk_Tree_Virtual_setImage { |
| 19 | + |
| 20 | + private static final int WINDOW_WIDTH = 300; |
| 21 | + private static final int WINDOW_HEIGHT = 100; |
| 22 | + private static final int IMAGE_SIZE = 70; |
| 23 | + |
| 24 | + private Shell shell; |
| 25 | + private Tree tree; |
| 26 | + private Image image; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() { |
| 30 | + shell = new Shell(); |
| 31 | + shell.setLayout(new GridLayout()); |
| 32 | + } |
| 33 | + |
| 34 | + @After |
| 35 | + public void tearDown() { |
| 36 | + if (tree != null) { |
| 37 | + tree.dispose(); |
| 38 | + tree = null; |
| 39 | + } |
| 40 | + if (image != null) { |
| 41 | + image.dispose(); |
| 42 | + image = null; |
| 43 | + } |
| 44 | + if (shell != null) { |
| 45 | + shell.dispose(); |
| 46 | + shell = null; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void test_initial_img_5_2() { |
| 52 | + do_test_initial_img(false, 5, 2); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void test_initial_img_5_2_wCheckbox() { |
| 57 | + do_test_initial_img(true, 5, 2); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void test_initial_img_50_22() { |
| 62 | + do_test_initial_img(false, 50, 22); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void test_initial_img_50_22_wCheckbox() { |
| 67 | + do_test_initial_img(true, 50, 22); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void test_set_img_50_22() { |
| 72 | + do_test_set_img(false, 50, 22); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void test_set_img_50_22_wCheckbox() { |
| 77 | + do_test_set_img(true, 50, 22); |
| 78 | + } |
| 79 | + |
| 80 | + private void do_test_initial_img(boolean withCheckbox, int totalRows, int imgRowIdx) { |
| 81 | + createImage(); |
| 82 | + createTree(withCheckbox, totalRows, imgRowIdx, () -> image); |
| 83 | + shell.pack(); |
| 84 | + shell.open(); |
| 85 | + |
| 86 | + executePendingUiTasks(); |
| 87 | + |
| 88 | + tree.showItem(tree.getItem(imgRowIdx)); |
| 89 | + |
| 90 | + executePendingUiTasks(); |
| 91 | + |
| 92 | + assertAllTreeRowsResized(); |
| 93 | + } |
| 94 | + |
| 95 | + private void do_test_set_img(boolean withCheckbox, int totalRows, int imgRowIdx) { |
| 96 | + createTree(withCheckbox, totalRows, imgRowIdx, () -> image); |
| 97 | + shell.pack(); |
| 98 | + shell.open(); |
| 99 | + |
| 100 | + executePendingUiTasks(); |
| 101 | + |
| 102 | + tree.showItem(tree.getItem(imgRowIdx)); |
| 103 | + |
| 104 | + executePendingUiTasks(); |
| 105 | + |
| 106 | + tree.showItem(tree.getItem(totalRows - 1)); |
| 107 | + |
| 108 | + executePendingUiTasks(); |
| 109 | + |
| 110 | + createImage(); |
| 111 | + |
| 112 | + executePendingUiTasks(); |
| 113 | + |
| 114 | + tree.clear(imgRowIdx, false); |
| 115 | + |
| 116 | + executePendingUiTasks(); |
| 117 | + |
| 118 | + tree.showItem(tree.getItem(imgRowIdx)); |
| 119 | + |
| 120 | + executePendingUiTasks(); |
| 121 | + |
| 122 | + assertAllTreeRowsResized(); |
| 123 | + } |
| 124 | + |
| 125 | + private void createImage() { |
| 126 | + this.image = new Image(shell.getDisplay(), IMAGE_SIZE, IMAGE_SIZE); |
| 127 | + var color = shell.getDisplay().getSystemColor(SWT.COLOR_GREEN); |
| 128 | + var gc = new GC(image); |
| 129 | + gc.setForeground(color); |
| 130 | + gc.setBackground(color); |
| 131 | + gc.fillRectangle(0, 0, IMAGE_SIZE, IMAGE_SIZE); |
| 132 | + gc.dispose(); |
| 133 | + } |
| 134 | + |
| 135 | + private void createTree(boolean withCheckbox, int totalRows, int imgRowIdx, Supplier<Image> getImage) { |
| 136 | + tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER | (withCheckbox ? SWT.CHECK : 0)); |
| 137 | + var layoutData = new GridData(GridData.FILL_BOTH); |
| 138 | + layoutData.widthHint = WINDOW_WIDTH; |
| 139 | + layoutData.heightHint = WINDOW_HEIGHT; |
| 140 | + tree.setLayoutData(layoutData); |
| 141 | + tree.addListener(SWT.SetData, event -> { |
| 142 | + var item = (TreeItem) event.item; |
| 143 | + var idx = tree.indexOf(item); |
| 144 | + item.setText("node " + idx); |
| 145 | + if (idx == imgRowIdx) { |
| 146 | + var image = getImage.get(); |
| 147 | + item.setImage(image); |
| 148 | + } |
| 149 | + item.setItemCount(0); |
| 150 | + }); |
| 151 | + tree.setItemCount(totalRows); |
| 152 | + } |
| 153 | + |
| 154 | + private void executePendingUiTasks() { |
| 155 | + while (shell.getDisplay().readAndDispatch()) { |
| 156 | + // perform next pending task |
| 157 | + } |
| 158 | + try { |
| 159 | + // This sleep is required because |
| 160 | + // - gtk perform some updates/renderings on its own inner |
| 161 | + // timers (e.g. a timer for every frame drawing) |
| 162 | + // - readAndDispatch() doesn't wait for those timers |
| 163 | + // - we need to wait for this timers to get fully updated/rendered tree |
| 164 | + Thread.sleep(100); |
| 165 | + } catch (InterruptedException e) { |
| 166 | + Thread.currentThread().interrupt(); |
| 167 | + throw new RuntimeException(e); |
| 168 | + } |
| 169 | + while (shell.getDisplay().readAndDispatch()) { |
| 170 | + // perform next pending task |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private void assertAllTreeRowsResized() { |
| 175 | + var minHeight = IMAGE_SIZE; |
| 176 | + var maxHeight = 2 * IMAGE_SIZE - 1; |
| 177 | + for (var i = 0; i < tree.getItemCount(); i++) { |
| 178 | + var item = tree.getItem(i); |
| 179 | + var height = item.getBounds().height; |
| 180 | + Assert.assertTrue( |
| 181 | + String.format("Expected row[%d].height in [%d;%d], but was %d.", i, minHeight, maxHeight, height), |
| 182 | + minHeight <= height && height <= maxHeight); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments