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