1 /*
   2  * Copyright (c) 2016, 2018, 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  * @test
  26  * @key headful
  27  * @bug 8022810 8196616
  28  * @summary Cannot list all the available display modes on Ubuntu linux in case
  29  *          of two screen devices
  30  * @run main CompareToXrandrTest
  31  */
  32 
  33 import java.awt.GraphicsEnvironment;
  34 import java.awt.GraphicsDevice;
  35 import java.io.BufferedReader;
  36 import java.io.File;
  37 import java.io.InputStreamReader;
  38 import java.util.Arrays;
  39 import java.util.Set;
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 import java.util.stream.Collectors;
  43 
  44 public class CompareToXrandrTest {
  45 
  46     public static void main(String[] args) throws Exception {
  47         if (!new File("/usr/bin/xrandr").exists()) {
  48             System.out.println("No xrandr tool to compare");
  49             return;
  50         }
  51 
  52         try (BufferedReader reader = new BufferedReader(new InputStreamReader(
  53                 Runtime.getRuntime().exec("/usr/bin/xrandr").getInputStream()))) {
  54             Pattern pattern = Pattern.compile("^\\s*(\\d+x\\d+)");
  55 
  56             for (GraphicsDevice d : GraphicsEnvironment
  57                     .getLocalGraphicsEnvironment().getScreenDevices()) {
  58 
  59                 Set<String> xrandrModes = reader.lines().map(pattern::matcher)
  60                         .filter(Matcher::find).map(m -> m.group(1))
  61                         .collect(Collectors.toSet());
  62 
  63                 Set<String> javaModes = Arrays.stream(d.getDisplayModes())
  64                         .map(m -> m.getWidth() + "x" + m.getHeight())
  65                         .collect(Collectors.toSet());
  66 
  67                 if (!xrandrModes.equals(javaModes)) {
  68                     throw new RuntimeException("Failed");
  69                 } else {
  70                     System.out.println("Device " + d + ": " + javaModes.size() +
  71                             " modes found.");
  72                 }
  73             }
  74         }
  75     }
  76 }