1 /*
   2  * Copyright (c) 2012, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * Portions Copyright (c) 2012 IBM Corporation
  26  */
  27 
  28 /* @test
  29  * @bug 7188612
  30  * @summary AccessibleTableHeader and AccessibleJTableCell should stick to
  31  *    AccessibleComponent.getLocationOnScreen api.
  32  * @author Frank Ding
  33  */
  34 
  35 import javax.accessibility.AccessibleComponent;
  36 import javax.accessibility.AccessibleTable;
  37 import javax.swing.JComponent;
  38 import javax.swing.JFrame;
  39 import javax.swing.JTable;
  40 import javax.swing.SwingUtilities;
  41 
  42 public class JTableAccessibleGetLocationOnScreen {
  43     private static JFrame frame;
  44     private static JTable table;
  45 
  46     public static void main(String[] args) throws Exception {
  47 
  48         SwingUtilities.invokeAndWait(new Runnable() {
  49 
  50             @Override
  51             public void run() {
  52                 constructInEDT();
  53                 try {
  54                     assertGetLocation();
  55                 } finally {
  56                     frame.dispose();
  57                 }
  58             }
  59         });
  60 
  61     }
  62 
  63     private static void constructInEDT() {
  64         String[] columnNames = { "col1", "col2", };
  65         Object[][] data = { { "row1, col1", "row1, col2" },
  66                 { "row2, col1", "row2, col2" }, };
  67 
  68         frame = new JFrame(
  69                 "JTable AccessibleTableHeader and AccessibleJTableCell test");
  70         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  71         table = new JTable(data, columnNames);
  72         frame.add(table);
  73         frame.pack();
  74     }
  75 
  76     private static void assertGetLocation() {
  77         // the frame is now invisible
  78         // test getLocationOnScreen() of
  79         // JTable$AccessibleJTable$AccessibleJTableHeaderCell
  80         // and JTable$AccessibleJTable$AccessibleJTableCell
  81         AccessibleTable accessibleTable = (AccessibleTable) table
  82                 .getAccessibleContext();
  83         AccessibleTable header = accessibleTable.getAccessibleColumnHeader();
  84         AccessibleComponent accessibleComp1 = (AccessibleComponent) header
  85                 .getAccessibleAt(0, 0);
  86         // getLocation() must be null according to its javadoc and no exception
  87         // is thrown
  88         if (null != accessibleComp1.getLocationOnScreen()) {
  89             throw new RuntimeException(
  90                     "JTable$AccessibleJTable$AccessibleJTableHeaderCell."
  91                             + "getLocation() must be null");
  92         }
  93 
  94         JComponent.AccessibleJComponent accessibleJComponent =
  95                 (JComponent.AccessibleJComponent) table.getAccessibleContext();
  96         AccessibleComponent accessibleComp2 = (AccessibleComponent)
  97                 accessibleJComponent.getAccessibleChild(3);
  98         // getLocation() must be null according to its javadoc and no exception
  99         // is thrown
 100         if (null != accessibleComp2.getLocationOnScreen()) {
 101             throw new RuntimeException("JTable$AccessibleJTable$"
 102                     + "AccessibleJTableCell.getLocation() must be null");
 103         }
 104 
 105     }
 106 }