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 /*
  29  * @test
  30  * @key headful
  31  * @bug 7188612
  32  * @summary AccessibleTableHeader and AccessibleJTableCell should stick to
  33  *    AccessibleComponent.getLocationOnScreen api.
  34  * @author Frank Ding
  35  */
  36 
  37 import javax.accessibility.AccessibleComponent;
  38 import javax.accessibility.AccessibleTable;
  39 import javax.swing.JComponent;
  40 import javax.swing.JFrame;
  41 import javax.swing.JTable;
  42 import javax.swing.SwingUtilities;
  43 
  44 public class JTableAccessibleGetLocationOnScreen {
  45     private static JFrame frame;
  46     private static JTable table;
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51 
  52             @Override
  53             public void run() {
  54                 constructInEDT();
  55                 try {
  56                     assertGetLocation();
  57                 } finally {
  58                     frame.dispose();
  59                 }
  60             }
  61         });
  62 
  63     }
  64 
  65     private static void constructInEDT() {
  66         String[] columnNames = { "col1", "col2", };
  67         Object[][] data = { { "row1, col1", "row1, col2" },
  68                 { "row2, col1", "row2, col2" }, };
  69 
  70         frame = new JFrame(
  71                 "JTable AccessibleTableHeader and AccessibleJTableCell test");
  72         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73         table = new JTable(data, columnNames);
  74         frame.add(table);
  75         frame.pack();
  76     }
  77 
  78     private static void assertGetLocation() {
  79         // the frame is now invisible
  80         // test getLocationOnScreen() of
  81         // JTable$AccessibleJTable$AccessibleJTableHeaderCell
  82         // and JTable$AccessibleJTable$AccessibleJTableCell
  83         AccessibleTable accessibleTable = (AccessibleTable) table
  84                 .getAccessibleContext();
  85         AccessibleTable header = accessibleTable.getAccessibleColumnHeader();
  86         AccessibleComponent accessibleComp1 = (AccessibleComponent) header
  87                 .getAccessibleAt(0, 0);
  88         // getLocation() must be null according to its javadoc and no exception
  89         // is thrown
  90         if (null != accessibleComp1.getLocationOnScreen()) {
  91             throw new RuntimeException(
  92                     "JTable$AccessibleJTable$AccessibleJTableHeaderCell."
  93                             + "getLocation() must be null");
  94         }
  95 
  96         JComponent.AccessibleJComponent accessibleJComponent =
  97                 (JComponent.AccessibleJComponent) table.getAccessibleContext();
  98         AccessibleComponent accessibleComp2 = (AccessibleComponent)
  99                 accessibleJComponent.getAccessibleChild(3);
 100         // getLocation() must be null according to its javadoc and no exception
 101         // is thrown
 102         if (null != accessibleComp2.getLocationOnScreen()) {
 103             throw new RuntimeException("JTable$AccessibleJTable$"
 104                     + "AccessibleJTableCell.getLocation() must be null");
 105         }
 106 
 107     }
 108 }