1 /*
   2  * Copyright (c) 2011, 2016, 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 import com.sun.swingset3.demos.button.ButtonDemo;
  25 import org.jtregext.GuiTestListener;
  26 import java.awt.Point;
  27 import java.awt.Robot;
  28 import java.awt.event.InputEvent;
  29 import java.awt.image.BufferedImage;
  30 import org.netbeans.jemmy.ClassReference;
  31 import org.netbeans.jemmy.image.StrictImageComparator;
  32 import org.netbeans.jemmy.operators.JButtonOperator;
  33 import org.netbeans.jemmy.operators.JFrameOperator;
  34 import static org.jemmy2ext.JemmyExt.*;
  35 import org.testng.annotations.Test;
  36 import static com.sun.swingset3.demos.button.ButtonDemo.*;
  37 import org.testng.annotations.Listeners;
  38 
  39 /*
  40  * @test
  41  * @key headful screenshots intermittent
  42  * @summary Verifies buttons on SwingSet3 ButtonDemo page by clicking each
  43  *          button, taking its screenshots and checking that pressed button
  44  *          image is different from initial button image.
  45  *
  46  * @library /sanity/client/lib/jemmy/src
  47  * @library /sanity/client/lib/Extensions/src
  48  * @library /sanity/client/lib/SwingSet3/src
  49  * @modules java.desktop
  50  *          java.logging
  51  * @build org.jemmy2ext.JemmyExt
  52  * @build com.sun.swingset3.demos.button.ButtonDemo
  53  * @run testng ButtonDemoScreenshotTest
  54  */
  55 @Listeners(GuiTestListener.class)
  56 public class ButtonDemoScreenshotTest {
  57 
  58     private static final int BUTTON_COUNT = 6; // TODO: Decide about "open browser" buttons (value was 8 originally)
  59 
  60     @Test
  61     public void test() throws Exception {
  62         Robot rob = new Robot();
  63 
  64         new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication();
  65 
  66         JFrameOperator mainFrame = new JFrameOperator(DEMO_TITLE);
  67         waitImageIsStill(rob, mainFrame);
  68 
  69         // Check all the buttons
  70         for (int i = 0; i < BUTTON_COUNT; i++) {
  71             checkButton(mainFrame, i, rob);
  72         }
  73     }
  74 
  75     public void checkButton(JFrameOperator jfo, int i, Robot rob) {
  76         JButtonOperator button = new JButtonOperator(jfo, i);
  77 
  78         Point loc = button.getLocationOnScreen();
  79         rob.mouseMove(loc.x, loc.y);
  80 
  81         BufferedImage initialButtonImage = capture(rob, button);
  82         assertNotBlack(initialButtonImage);
  83         save(initialButtonImage, "button" + i + "_0initial.png");
  84         rob.mousePress(InputEvent.BUTTON1_MASK);
  85         try {
  86             waitPressed(button);
  87             BufferedImage pressedButtonImage = capture(rob, button);
  88             assertNotBlack(pressedButtonImage);
  89             save(pressedButtonImage, "button" + i + "_1pressed.png");
  90 
  91             StrictImageComparator sComparator = new StrictImageComparator();
  92             assertNotEquals("Button " + i + " Test", sComparator, initialButtonImage, pressedButtonImage);
  93         } finally {
  94             rob.mouseRelease(InputEvent.BUTTON1_MASK);
  95         }
  96     }
  97 
  98 }