# Copyright (c) 2014 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Test harness for testing chrome apps / extensions."""

import argparse
import cStringIO
import contextlib
import hashlib
import logging
import os
import shutil
import subprocess
import sys
import tempfile
import threading
import urllib
import urllib2
import urlparse
import zipfile

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
SRC_DIR = os.path.dirname(SCRIPT_DIR)
sys.path.insert(0, os.path.join(SRC_DIR, 'build_tools'))

import httpd


# Pinned chrome revision. Update this to pull in a new chrome.
# Try to select a version that exists on all platforms.
CHROME_REVISION = '293496'

OUT_DIR = os.path.join(SRC_DIR, 'out')
CHROME_SYNC_DIR = os.path.join(OUT_DIR, 'downloaded_chrome')

GS_URL = 'http://storage.googleapis.com'
CHROME_URL_FORMAT = GS_URL + '/chromium-browser-continuous/%s/%s/%s'

TESTING_LIB = os.path.join(SCRIPT_DIR, 'chrome_test.js')
TESTING_EXTENSION = os.path.join(SCRIPT_DIR, 'extension')
TESTING_TCP_APP = os.path.join(SCRIPT_DIR, 'tcpapp');

RETURNCODE_KILL = -9

LOG_LEVEL_MAP = {
    'ERROR': logging.ERROR,
    'WARNING': logging.WARNING,
    'INFO': logging.INFO,
    'DEBUG': logging.DEBUG,
}


def ChromeUrl(arch):
  """Get the URL to download chrome from.

  Args:
    arch: Chrome architecture to select i686/x86_64/pnacl.
  Returns:
    URL to download a zip file from.
  """
  if sys.platform == 'win32':
    filename = 'chrome-win32.zip'
    target = 'Win_x64'