Path: blob/master/test/jdk/com/sun/java/swing/plaf/windows/RevalidateOnPropertyChange.java
41162 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Dimension;24import java.awt.EventQueue;25import java.awt.FlowLayout;26import java.awt.Font;27import java.awt.Robot;28import java.awt.Toolkit;29import java.lang.reflect.Method;3031import javax.swing.JButton;32import javax.swing.JFrame;33import javax.swing.UIManager;34import javax.swing.plaf.FontUIResource;3536/**37* @test38* @key headful39* @bug 807525540* @summary tests if the desktop property changes this will force the UIs to41* update all known Frames42* @requires (os.family == "windows")43* @modules java.desktop/java.awt:open java.desktop/sun.awt44*/45public final class RevalidateOnPropertyChange {4647private static JFrame frame;48private static JButton button;49private static volatile Dimension sizeAfter;50private static volatile Dimension sizeBefore;51private static volatile boolean flag;5253public static void main(String[] args) throws Exception {54System.setProperty("swing.useSystemFontSettings", "true");55UIManager.put("Application.useSystemFontSettings", true);5657// this functionality is supported on windows in "Windows and Metal L&F"58test("com.sun.java.swing.plaf.windows.WindowsLookAndFeel",59"win.defaultGUI.font",60new Font(Font.DIALOG, FontUIResource.BOLD, 40));61test("javax.swing.plaf.metal.MetalLookAndFeel",62"win.ansiVar.font.height", 70);63}6465/**66* Emulates the property change via reflection.67*/68static void test(String laf, String prop, Object value) throws Exception {69Class cls = Toolkit.class;70Method setDesktopProperty =71cls.getDeclaredMethod("setDesktopProperty", String.class,72Object.class);73setDesktopProperty.setAccessible(true);7475UIManager.setLookAndFeel(laf);76EventQueue.invokeAndWait(RevalidateOnPropertyChange::createGUI);7778Robot r = new Robot();79r.waitForIdle();8081EventQueue.invokeAndWait(() -> {82sizeBefore = button.getSize();83});8485Toolkit toolkit = Toolkit.getDefaultToolkit();86toolkit.addPropertyChangeListener(prop, evt -> flag = true);87setDesktopProperty.invoke(toolkit, prop, value);88r.waitForIdle();8990EventQueue.invokeAndWait(() -> {91sizeAfter = button.getSize();92frame.dispose();93});9495if (!flag) {96throw new RuntimeException("The listener was not notified");97}98if (sizeAfter.equals(sizeBefore)) {99throw new RuntimeException("Size was not changed :" + sizeAfter);100}101}102103private static void createGUI() {104frame = new JFrame();105button = new JButton(UIManager.getLookAndFeel().getName());106frame.setLayout(new FlowLayout());107frame.getContentPane().add(button);108frame.setSize(400, 400);109frame.setLocationRelativeTo(null);110frame.setVisible(true);111}112}113114115