1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import logging
7import os
8
9import pyauto_functional  # Must be imported before pyauto
10import pyauto
11
12
13class GpuTest(pyauto.PyUITest):
14  """GPU Tests Runner."""
15
16  def _GetGpuPID(self):
17    """Fetch the pid of the GPU process."""
18    child_processes = self.GetBrowserInfo()['child_processes']
19    for x in child_processes:
20       if x['type'] == 'GPU':
21         return x['pid']
22    return None
23
24  def _IsHardwareAccelerated(self, feature):
25    """Check if gpu is enabled in the machine before running any tests."""
26    self.NavigateToURL('about:gpu')
27    def IsFeatureStatusLoaded():
28      """Returns whether the feature status UI has been loaded.
29
30      The about:gpu page fetches status for features asynchronously, so use
31      this to check if the fetch is done.
32      """
33      js = """
34        var list = document.querySelector(".feature-status-list");
35        domAutomationController.send(list.hasChildNodes() ? "done" : "");
36      """
37      return self.ExecuteJavascript(js)
38    self.assertTrue(self.WaitUntil(IsFeatureStatusLoaded, 10))
39    search = feature + ': Hardware accelerated'
40    find_result = self.FindInPage(search)['match_count']
41    if find_result:
42      # about:gpu page starts a gpu process. Restart the browser to clear
43      # the state. We could kill the gpu process, but navigating to a page
44      # after killing the gpu can lead to flakiness.
45      # See crbug.com/93423.
46      self.RestartBrowser()
47      return True
48    else:
49      logging.warn('Hardware acceleration not available')
50      return False
51
52  def _VerifyGPUProcessOnPage(self, url):
53    url = self.GetFileURLForDataPath('pyauto_private', 'gpu', url)
54    self.NavigateToURL(url)
55    self.assertTrue(self.WaitUntil(
56      lambda: self._GetGpuPID() is not None), msg='No process for GPU')
57
58  def testSingleGpuProcess(self):
59    """Verify there's only one gpu process shared across all uses."""
60    self.assertTrue(self._IsHardwareAccelerated('WebGL'))
61    url = self.GetFileURLForDataPath('pyauto_private',
62                                     'gpu', 'WebGLField.html')
63    self.AppendTab(pyauto.GURL(url))
64    # Open a new window.
65    self.OpenNewBrowserWindow(True)
66    self.NavigateToURL(url, 1, 0)
67    # Open a new incognito window.
68    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
69    self.NavigateToURL(url, 1, 0)
70    # Verify there's only 1 gpu process.
71    gpu_process_count = 0
72    for x in self.GetBrowserInfo()['child_processes']:
73      if x['type'] == 'GPU':
74        gpu_process_count += 1
75    self.assertEqual(1, gpu_process_count)
76
77
78if __name__ == '__main__':
79  pyauto_functional.Main()
80