#!/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.

"""Runs DomDistillers jstests.

This uses ChromeDriver (https://sites.google.com/a/chromium.org/chromedriver/) to run the jstests.
This requires that the ChromeDriver executable is on the PATH and that Selenium WebDriver is
installed.

In addition, ChromeDriver assumes that Chrome is available at /usr/bin/google-chrome.
"""

import argparse
import os
import sys
import time
import urllib

try:
  from selenium import webdriver
except:
  print 'ERROR:'
  print 'Couldn\'t import webdriver. Please run `sudo ./install-build-deps.sh`.'
  sys.exit(1)

def main(argv):
  parser = argparse.ArgumentParser()
  parser.add_argument('--filter', help='See gtest_filter syntax.')
  parser.add_argument('--repeat', type=int, default=1, help='Number of times to repeat the tests.')
  parser.add_argument('--debug_level', help='Verbosity level of debug messages.')
  parser.add_argument('--no_console_log',
      action='store_true', help='Disable the console log output.')
  parser.add_argument('--shuffle', type=int, help='Set to 1 to run test cases in random order.')
  parser.add_argument('--no_sandbox', type=int, help='Set to 1 to add --no-sandbox option to Chrome.')
  options = parser.parse_args(argv)

  params = {}
  if options.filter:
    params['filter'] = options.filter

  if options.debug_level:
    params['debug_level'] = int(options.debug_level)

  if options.no_console_log:
    params['console_log'] = '0'

  if options.shuffle:
    params['shuffle'] = options.shuffle

  image_loaded = "return window.image_loaded"
  test_runner = "return org.chromium.distiller.JsTestEntry.run()"
  test_html = os.path.abspath(os.path.join(os.path.dirname(__file__), "war", "test.html"))
  test_html += "?" + urllib.urlencode(params)

  chrome_options = webdriver.ChromeOptions()

  # Travis-CI uses OpenVZ containers which are incompatible with the sandbox technology.
  # See https://code.google.com/p/chromium/issues/detail?id=31077 for more information.
  # Ref: https://github.com/travis-ci/travis-ci/issues/938#issuecomment-16336150
  # Drone.io also has issues running newer versions of Chrome.
  # Ref: http://crbug.com/495254
  if options.no_sandbox:
    chrome_options.add_argument("--no-sandbox")

  driver = webdriver.Chrome(chrome_options=chrome_options)
  for i in range(options.repeat):
    driver.get("file://" + test_html)
    while not driver.execute_script(image_loaded):
      print "Wait for image loading..."
      time.sleep(0.1)

    start = time.time()
    result = driver.execute_script(test_runner)
    end = time.time()

    if not result['success'] or options.repeat == i+1:
      print result['log'].encode('utf-8')
    print 'Tests run: %d, Failures: %d, Skipped: %d, Time elapsed: %0.3f sec' % (result['numTests'],
        result['failed'], result['skipped'], end - start)
    if not result['success']:
      driver.quit()
      if options.repeat > 1:
        print 'Failed at run #%d/%d' % (i+1, options.repeat)
      return 1
  driver.quit()
  if options.repeat > 1:
    print 'Passed %d runs' % (options.repeat)
  return 0

if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))

