1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import unittest
5
6from telemetry.internal.platform import gpu_device
7from telemetry.internal.platform import gpu_info
8
9
10class TestGPUInfo(unittest.TestCase):
11
12  def testConstruction(self):
13    data = {
14        'devices': [
15            {'vendor_id': 1000, 'device_id': 2000,
16             'vendor_string': 'a', 'device_string': 'b'},
17            {'vendor_id': 3000, 'device_id': 4000,
18             'vendor_string': 'k', 'device_string': 'l'}
19        ],
20        'aux_attributes': {
21            'optimus': False,
22            'amd_switchable': False,
23            'lenovo_dcute': False,
24            'driver_vendor': 'c',
25            'driver_version': 'd',
26            'driver_date': 'e',
27            'gl_version_string': 'g',
28            'gl_vendor': 'h',
29            'gl_renderer': 'i',
30            'gl_extensions': 'j',
31        }
32    }
33    info = gpu_info.GPUInfo.FromDict(data)
34    self.assertTrue(len(info.devices) == 2)
35    self.assertTrue(isinstance(info.devices[0], gpu_device.GPUDevice))
36    self.assertEquals(info.devices[0].vendor_id, 1000)
37    self.assertEquals(info.devices[0].device_id, 2000)
38    self.assertEquals(info.devices[0].vendor_string, 'a')
39    self.assertEquals(info.devices[0].device_string, 'b')
40    self.assertTrue(isinstance(info.devices[1], gpu_device.GPUDevice))
41    self.assertEquals(info.devices[1].vendor_id, 3000)
42    self.assertEquals(info.devices[1].device_id, 4000)
43    self.assertEquals(info.devices[1].vendor_string, 'k')
44    self.assertEquals(info.devices[1].device_string, 'l')
45    self.assertEquals(info.aux_attributes['optimus'], False)
46    self.assertEquals(info.aux_attributes['amd_switchable'], False)
47    self.assertEquals(info.aux_attributes['lenovo_dcute'], False)
48    self.assertEquals(info.aux_attributes['driver_vendor'], 'c')
49    self.assertEquals(info.aux_attributes['driver_version'], 'd')
50    self.assertEquals(info.aux_attributes['driver_date'], 'e')
51    self.assertEquals(info.aux_attributes['gl_version_string'], 'g')
52    self.assertEquals(info.aux_attributes['gl_vendor'], 'h')
53    self.assertEquals(info.aux_attributes['gl_renderer'], 'i')
54    self.assertEquals(info.aux_attributes['gl_extensions'], 'j')
55
56  def testMissingAttrsFromDict(self):
57    data = {
58        'devices': [{'vendor_id': 1000, 'device_id': 2000,
59                     'vendor_string': 'a', 'device_string': 'b'}]
60    }
61
62    for k in data:
63      data_copy = data.copy()
64      del data_copy[k]
65      try:
66        gpu_info.GPUInfo.FromDict(data_copy)
67        self.fail('Should raise exception if attribute "%s" is missing' % k)
68      except AssertionError:
69        raise
70      except KeyError:
71        pass
72
73  def testMissingDevices(self):
74    data = {
75        'devices': []
76    }
77
78    try:
79      gpu_info.GPUInfo.FromDict(data)
80      self.fail('Should raise exception if devices array is empty')
81    except AssertionError:
82      raise
83    except Exception:
84      pass
85