test/jdk/javax/swing/JList/SetSelectedValueTest.java
author coleenp
Mon, 30 Sep 2019 13:10:11 -0400
changeset 58409 a595e67d6683
parent 49085 cf931670015b
permissions -rw-r--r--
8184732: Deadlock detection improvements for 'special' locks Summary: Assert that special ranked locks cannot safepoint and allow_vm_block and remove locks from the exceptional lock list in no_safepoint_verifier. Reviewed-by: dholmes, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49085
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     1
/*
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     4
 *
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     8
 *
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    13
 * accompanied this code).
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    14
 *
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    18
 *
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    21
 * questions.
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    22
 */
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    23
/*
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    24
 * @test
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    25
 * @key headful
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    26
 * @bug 5076761
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    27
 * @summary Verifies that the selection is cleared when setSelectedValue is
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    28
 *          called with null
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    29
 * @run main SetSelectedValueTest
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    30
 */
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    31
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    32
import javax.swing.SwingUtilities;
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    33
import javax.swing.DefaultListModel;
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    34
import javax.swing.JList;
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    35
import javax.swing.ListSelectionModel;
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    36
import java.util.Collections;
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    37
import java.util.List;
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    38
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    39
public class SetSelectedValueTest {
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    41
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    42
        SwingUtilities.invokeAndWait(new Runnable() {
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    43
            public void run() {
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    44
                // Create a JList with 2 elements
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    45
                DefaultListModel dlm = new DefaultListModel();
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    46
                JList list = new JList<String>(dlm);
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    47
                list.setSelectionMode(
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    48
                        ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    49
                dlm.addElement("1");
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    50
                dlm.addElement("2");
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    51
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    52
                // Select both the elements added in list
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    53
                list.setSelectionInterval(0, 1);
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    54
                checkSelectionByList(list, List.of("1", "2"));
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    55
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    56
                // Set the selected value as null. This should clear the
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    57
                // selection
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    58
                list.setSelectedValue(null, true);
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    59
                checkSelectionByList(list, Collections.emptyList());
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    60
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    61
                // Select both the elements added in list
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    62
                list.setSelectionInterval(0, 1);
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    63
                checkSelectionByList(list, List.of("1", "2"));
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    64
            }
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    65
        });
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    66
    }
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    67
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    68
    static void checkSelectionByList(JList list, List<String> selectionList)
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    69
            throws RuntimeException {
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    70
        List<String> listSelection = list.getSelectedValuesList();
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    71
        if (!listSelection.equals(selectionList)) {
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    72
            System.out.println("Expected: " + selectionList);
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    73
            System.out.println("Actual: " + listSelection);
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    74
            throw new RuntimeException("Wrong selection");
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    75
        }
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    76
    }
cf931670015b 5076761: JList.setSelectedValue(null, ...) doesn't do anything
pbansal
parents:
diff changeset
    77
}