|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package test.com.sun.javafx.scene.control.behavior; |
| 27 | + |
| 28 | +import java.lang.ref.WeakReference; |
| 29 | + |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import com.sun.javafx.scene.control.behavior.BehaviorBase; |
| 35 | +import com.sun.javafx.scene.control.behavior.ListCellBehavior; |
| 36 | + |
| 37 | +import static javafx.collections.FXCollections.*; |
| 38 | +import static org.junit.Assert.*; |
| 39 | +import static test.com.sun.javafx.scene.control.infrastructure.ControlSkinFactory.*; |
| 40 | + |
| 41 | +import javafx.scene.control.ListView; |
| 42 | + |
| 43 | +/** |
| 44 | + * Test for misbehavior of individual implementations that turned |
| 45 | + * up in binch testing. |
| 46 | + * |
| 47 | + */ |
| 48 | +public class BehaviorCleanupTest { |
| 49 | + |
| 50 | +// ---------- ListView |
| 51 | + |
| 52 | + /** |
| 53 | + * Test cleanup of listener to itemsProperty. |
| 54 | + */ |
| 55 | + @Test |
| 56 | + public void testListViewBehaviorDisposeSetItems() { |
| 57 | + ListView<String> listView = new ListView<>(observableArrayList("one", "two")); |
| 58 | + WeakReference<BehaviorBase<?>> weakRef = new WeakReference<>(createBehavior(listView)); |
| 59 | + weakRef.get().dispose(); |
| 60 | + int last = 1; |
| 61 | + ListCellBehavior.setAnchor(listView, last, false); |
| 62 | + listView.setItems(observableArrayList("other", "again")); |
| 63 | + assertEquals("sanity: anchor unchanged", last, listView.getProperties().get("anchor")); |
| 64 | + listView.getItems().remove(0); |
| 65 | + assertEquals("anchor must not be updated on items modification when disposed", |
| 66 | + last, listView.getProperties().get("anchor")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testListViewBehaviorSetItems() { |
| 71 | + ListView<String> listView = new ListView<>(observableArrayList("one", "two")); |
| 72 | + createBehavior(listView); |
| 73 | + int last = 1; |
| 74 | + ListCellBehavior.setAnchor(listView, last, false); |
| 75 | + listView.setItems(observableArrayList("other", "again")); |
| 76 | + assertEquals("sanity: anchor unchanged", last, listView.getProperties().get("anchor")); |
| 77 | + listView.getItems().remove(0); |
| 78 | + assertEquals("anchor must be updated on items modification", |
| 79 | + last -1, listView.getProperties().get("anchor")); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test cleanup of itemsList listener in ListViewBehavior. |
| 84 | + */ |
| 85 | + @Test |
| 86 | + public void testListViewBehaviorDisposeRemoveItem() { |
| 87 | + ListView<String> listView = new ListView<>(observableArrayList("one", "two")); |
| 88 | + WeakReference<BehaviorBase<?>> weakRef = new WeakReference<>(createBehavior(listView)); |
| 89 | + weakRef.get().dispose(); |
| 90 | + int last = 1; |
| 91 | + ListCellBehavior.setAnchor(listView, last, false); |
| 92 | + listView.getItems().remove(0); |
| 93 | + assertEquals("anchor must not be updated on items modification when disposed", |
| 94 | + last, |
| 95 | + listView.getProperties().get("anchor")); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testListViewBehaviorRemoveItem() { |
| 100 | + ListView<String> listView = new ListView<>(observableArrayList("one", "two")); |
| 101 | + createBehavior(listView); |
| 102 | + int last = 1; |
| 103 | + ListCellBehavior.setAnchor(listView, last, false); |
| 104 | + assertEquals("behavior must set anchor on select", last, listView.getProperties().get("anchor")); |
| 105 | + listView.getItems().remove(0); |
| 106 | + assertEquals("anchor must be updated on items modification", |
| 107 | + last -1, listView.getProperties().get("anchor")); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Test cleanup of selection listeners in ListViewBehavior. |
| 112 | + */ |
| 113 | + @Test |
| 114 | + public void testListViewBehaviorDisposeSelect() { |
| 115 | + ListView<String> listView = new ListView<>(observableArrayList("one", "two")); |
| 116 | + WeakReference<BehaviorBase<?>> weakRef = new WeakReference<>(createBehavior(listView)); |
| 117 | + listView.getSelectionModel().select(1); |
| 118 | + weakRef.get().dispose(); |
| 119 | + listView.getSelectionModel().select(0); |
| 120 | + assertNull("anchor must remain cleared on selecting when disposed", |
| 121 | + listView.getProperties().get("anchor")); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testListViewBehaviorSelect() { |
| 126 | + ListView<String> listView = new ListView<>(observableArrayList("one", "two")); |
| 127 | + createBehavior(listView); |
| 128 | + int last = 1; |
| 129 | + listView.getSelectionModel().select(last); |
| 130 | + assertEquals("anchor must be set", last, listView.getProperties().get("anchor")); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testListViewBehaviorDispose() { |
| 135 | + ListView<String> listView = new ListView<>(observableArrayList("one", "two")); |
| 136 | + WeakReference<BehaviorBase<?>> weakRef = new WeakReference<>(createBehavior(listView)); |
| 137 | + listView.getSelectionModel().select(1); |
| 138 | + weakRef.get().dispose(); |
| 139 | + assertNull("anchor must be cleared after dispose", listView.getProperties().get("anchor")); |
| 140 | + } |
| 141 | + |
| 142 | + //------------------ setup/cleanup |
| 143 | + |
| 144 | + @After |
| 145 | + public void cleanup() { |
| 146 | + Thread.currentThread().setUncaughtExceptionHandler(null); |
| 147 | + } |
| 148 | + |
| 149 | + @Before |
| 150 | + public void setup() { |
| 151 | + Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> { |
| 152 | + if (throwable instanceof RuntimeException) { |
| 153 | + throw (RuntimeException)throwable; |
| 154 | + } else { |
| 155 | + Thread.currentThread().getThreadGroup().uncaughtException(thread, throwable); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments