summaryrefslogtreecommitdiffstats
path: root/chrome/test/chromedriver/embed_mobile_devices_in_cpp.py
blob: 58665feee4107f1b2e61c1c59bd51da7e9e08829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Embeds standalone JavaScript snippets in C++ code.

The script requires the Source/devtools/front_end/emulated_devices/module.json
file from Blink that lists the known mobile devices to be passed in as the only
argument.  The list of known devices will be written to a C-style string to be
parsed with JSONReader.
"""

import json
import optparse
import re
import subprocess
import sys

import cpp_source


def main():
  parser = optparse.OptionParser()
  parser.add_option(
      '', '--directory', type='string', default='.',
      help='Path to directory where the cc/h files should be created')
  options, args = parser.parse_args()

  devices = {}
  file_name = args[0]
  inside_list = False
  with open(file_name, 'r') as f:
    emulated_devices = json.load(f)
  extensions = emulated_devices['extensions']
  for extension in extensions:
    if extension['type'] == 'emulated-device':
      device = extension['device']
      devices[device['title']] = {
        'userAgent': device['user-agent'],
        'width': device['screen']['vertical']['width'],
        'height': device['screen']['vertical']['height'],
        'deviceScaleFactor': device['screen']['device-pixel-ratio'],
        'touch': 'touch' in device['capabilities'],
        'mobile': 'mobile' in device['capabilities'],
      }

  output_dir = 'chrome/test/chromedriver/chrome'
  cpp_source.WriteSource('mobile_device_list',
                         output_dir,
                         options.directory,
                         {'kMobileDevices': json.dumps(devices)})

  clang_format = ['clang-format', '-i']
  subprocess.Popen(clang_format + ['%s/mobile_device_list.cc' % output_dir])
  subprocess.Popen(clang_format + ['%s/mobile_device_list.h' % output_dir])


if __name__ == '__main__':
  sys.exit(main())