1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Red Hat Inc. All rights reserved.
   4  *
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * The contents of this file are subject to the terms of either the Universal Permissive License
   8  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   9  *
  10  * or the following license:
  11  *
  12  * Redistribution and use in source and binary forms, with or without modification, are permitted
  13  * provided that the following conditions are met:
  14  *
  15  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  16  * and the following disclaimer.
  17  *
  18  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  19  * conditions and the following disclaimer in the documentation and/or other materials provided with
  20  * the distribution.
  21  *
  22  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  23  * endorse or promote products derived from this software without specific prior written permission.
  24  *
  25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  32  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33  */
  34 package org.openjdk.jmc.test.jemmy.misc.wrappers;
  35 
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 
  39 import org.eclipse.swt.widgets.Display;
  40 import org.eclipse.swt.widgets.Shell;
  41 import org.openjdk.jmc.test.jemmy.misc.base.wrappers.MCJemmyBase;
  42 import org.openjdk.jmc.test.jemmy.misc.fetchers.Fetcher;
  43 import org.openjdk.jmc.ui.misc.ChartCanvas;
  44 import org.jemmy.Point;
  45 import org.jemmy.control.Wrap;
  46 import org.jemmy.input.StringPopupOwner;
  47 import org.jemmy.interfaces.Parent;
  48 import org.jemmy.resources.StringComparePolicy;
  49 
  50 /**
  51  * The Jemmy wrapper for the Mission Control Chart Canvas.
  52  */
  53 public class MCChartCanvas extends MCJemmyBase {
  54 
  55         private MCChartCanvas(Wrap<? extends ChartCanvas> ChartCanvasWrap) {
  56                 this.control = ChartCanvasWrap;
  57         }
  58 
  59         /**
  60          * Returns all visible {@link MCChartCanvas} objects underneath the supplied shell
  61          *
  62          * @param shell
  63          *            the shell from where to start the search for the ChartCanvas object
  64          * @return a {@link List} of {@link MCChartCanvas} objects
  65          */
  66         @SuppressWarnings("unchecked")
  67         public static List<MCChartCanvas> getAll(Wrap<? extends Shell> shell) {
  68                 List<Wrap<? extends ChartCanvas>> list = getVisible(shell.as(Parent.class, ChartCanvas.class).lookup(ChartCanvas.class));
  69                 List<MCChartCanvas> canvases = new ArrayList<>();
  70                 for (int i = 0; i < list.size(); i++) {
  71                         canvases.add(new MCChartCanvas(list.get(i)));
  72                 }
  73                 return canvases;
  74         }
  75 
  76         /**
  77          * Returns the first visible {@link MCChartCanvas} object underneath the supplied shell
  78          *
  79          * @param shell
  80          *            the shell from where to start the search for the ChartCanvas object
  81          * @return a {@link MCChartCanvas} object
  82          */
  83         public static MCChartCanvas getFirst(Wrap<? extends Shell> shell) {
  84                 return getAll(shell).get(0);
  85         }
  86 
  87         /**
  88          * Returns the first visible {@link MCChartCanvas} object underneath the Mission Control main shell
  89          *
  90          * @return a {@link MCChartCanvas} object
  91          */
  92         public static MCChartCanvas getChartCanvas() {
  93                 return getFirst(getShell());
  94         }
  95 
  96         /**
  97          * Clicks a specific menu item in the context menu
  98          * @param menuItemText
  99          *            the menu item of interest
 100          */
 101         @SuppressWarnings("unchecked")
 102         public void clickContextMenuItem(String menuItemText) {
 103                 focusMc();
 104                 StringPopupOwner<Shell> contextMenu = control.as(StringPopupOwner.class);
 105                 contextMenu.setPolicy(StringComparePolicy.SUBSTRING);
 106                 contextMenu.push(getRelativeClickPoint(), new String[]{menuItemText});
 107         }
 108 
 109         /**
 110          * Checks the isEnabled value for a menu item in the context menu
 111          *
 112          * @param menuItemText
 113          *            the menu item of interest
 114          * @return the isEnabled value for the menu item of interest
 115          */
 116         public boolean isContextMenuItemEnabled(String menuItemText) {
 117                 return this.isContextMenuItemEnabled(getRelativeClickPoint(), menuItemText);
 118         }
 119 
 120         /**
 121          * Calculates the click point of the Chart Canvas
 122          *
 123          * @return the Point of the Chart Canvas
 124          */
 125         private Point getRelativeClickPoint() {
 126                 Fetcher<Point> fetcher = new Fetcher<Point>() {
 127                         @Override
 128                         public void run() {
 129                                 setOutput(new Point(control.getScreenBounds().x / 2, control.getScreenBounds().y / 2));
 130                         }
 131                 };
 132                 Display.getDefault().syncExec(fetcher);
 133                 return fetcher.getOutput();
 134         }
 135 }