1#!/bin/bash -p
2
3# Copyright (c) 2012 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7set -eu
8
9# Environment sanitization. Set a known-safe PATH. Clear environment variables
10# that might impact the interpreter's operation. The |bash -p| invocation
11# on the #! line takes the bite out of BASH_ENV, ENV, and SHELLOPTS (among
12# other features), but clearing them here ensures that they won't impact any
13# shell scripts used as utility programs. SHELLOPTS is read-only and can't be
14# unset, only unexported.
15export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
16unset BASH_ENV CDPATH ENV GLOBIGNORE IFS POSIXLY_CORRECT
17export -n SHELLOPTS
18
19readonly ScriptDir=$(dirname "$(echo ${0} | sed -e "s,^\([^/]\),$(pwd)/\1,")")
20readonly ScriptName=$(basename "${0}")
21readonly ThisScript="${ScriptDir}/${ScriptName}"
22readonly SimExecutable="${BUILD_DIR}/ninja-iossim/${CONFIGURATION}/iossim"
23
24# Helper to print a line formatted for Xcodes build output parser.
25XcodeNote() {
26  echo "${ThisScript}:${1}: note: ${2}"
27}
28
29# Helper to print a divider to make things stick out in a busy output window.
30XcodeHeader() {
31  echo "note: _________________________________________________________________"
32  echo "note: _________________________________________________________________"
33  echo "note: _________________________________________________________________"
34  XcodeNote "$1" ">>>>>     $2"
35  echo "note: _________________________________________________________________"
36  echo "note: _________________________________________________________________"
37  echo "note: _________________________________________________________________"
38}
39
40# Kills the iPhone Simulator if it is running.
41KillSimulator() {
42  /usr/bin/killall "iPhone Simulator" 2> /dev/null || true
43}
44
45# Runs tests via the iPhone Simulator for multiple devices.
46RunTests() {
47  local -r appPath="${TARGET_BUILD_DIR}/${PRODUCT_NAME}.app"
48
49  if [[ ! -x "${SimExecutable}" ]]; then
50    echo "Unable to run tests: ${SimExecutable} was not found/executable."
51    exit 1
52  fi
53
54  for device in 'iPhone' 'iPad'; do
55    iosVersion="6.1"
56    KillSimulator
57    local command=(
58      "${SimExecutable}" "-d${device}" "-s${iosVersion}" "${appPath}"
59    )
60    # Pass along any command line flags
61    if [[ "$#" -gt 0 ]]; then
62      command+=( "--" "${@}" )
63    fi
64    XcodeHeader ${LINENO} "Launching tests for ${device} (iOS ${iosVersion})"
65    "${command[@]}"
66
67    # If the command didn't exit successfully, abort.
68    if [[ $? -ne 0 ]]; then
69      exit $?;
70    fi
71  done
72}
73
74# Time to get to work.
75
76if [[ "${PLATFORM_NAME}" != "iphonesimulator" ]]; then
77  XcodeNote ${LINENO} "Skipping running of unittests for device build."
78else
79  if [[ "$#" -gt 0 ]]; then
80    RunTests "${@}"
81  else
82    RunTests
83  fi
84  KillSimulator
85fi
86
87exit 0
88