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
  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  * @build org.jemmy2ext.JemmyExt
  50  * @build com.sun.swingset3.demos.button.ButtonDemo
  51  * @run testng ButtonDemoScreenshotTest
  52  */
  53 @Listeners(GuiTestListener.class)
  54 public class ButtonDemoScreenshotTest {
  55 
  56     private static final int BUTTON_COUNT = 6; // TODO: Decide about "open browser" buttons (value was 8 originally)
  57 
  58     @Test
  59     public void test() throws Exception {
  60         Robot rob = new Robot();
  61 
  62         new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication();
  63 
  64         JFrameOperator mainFrame = new JFrameOperator(DEMO_TITLE);
  65         waitImageIsStill(rob, mainFrame);
  66 
  67         // Check all the buttons
  68         for (int i = 0; i < BUTTON_COUNT; i++) {
  69             checkButton(mainFrame, i, rob);
  70         }
  71     }
  72 
  73     public void checkButton(JFrameOperator jfo, int i, Robot rob) {
  74         JButtonOperator button = new JButtonOperator(jfo, i);
  75 
  76         Point loc = button.getLocationOnScreen();
  77         rob.mouseMove(loc.x, loc.y);
  78 
  79         BufferedImage initialButtonImage = capture(rob, button);
  80         assertNotBlack(initialButtonImage);
  81         save(initialButtonImage, "button" + i + "_0initial.png");
  82         rob.mousePress(InputEvent.BUTTON1_MASK);
  83         try {
  84             waitPressed(button);
  85             BufferedImage pressedButtonImage = capture(rob, button);
  86             assertNotBlack(pressedButtonImage);
  87             save(pressedButtonImage, "button" + i + "_1pressed.png");
  88 
  89             StrictImageComparator sComparator = new StrictImageComparator();
  90             assertNotEquals("Button " + i + " Test", sComparator, initialButtonImage, pressedButtonImage);
  91         } finally {
  92             rob.mouseRelease(InputEvent.BUTTON1_MASK);
  93         }
  94     }
  95 
  96 }