#!/usr/bin/env perl

use lib 'lib';
use strict;
use warnings;

use App::ForkProve;
use Cwd ();
use TAP::Harness;
use Test::Deep;
use Test::More;
use WebDriver::Tiny;

my ( $pid, @pids ) = $$;

END { if ( $pid == $$ ) { kill 15, $_ for @pids } }

close STDOUT and exec '~/Downloads/chromedriver' unless $_ = fork;
push @pids, $_;

close STDOUT and exec qw/phantomjs -w --webdriver-loglevel=ERROR/
    unless $_ = fork;
push @pids, $_;

our @backends = (
    {   args => {
            capabilities => {
                chromeOptions => { binary => '/usr/bin/google-chrome-unstable' },
            },
            port => 9515,
        },
        capabilities => {
            acceptSslCerts           => bool(1),
            applicationCacheEnabled  => bool(0),
            browserConnectionEnabled => bool(0),
            browserName              => 'chrome',
            chrome                   => { userDataDir => re(qr(^/tmp/)) },
            cssSelectorsEnabled      => bool(1),
            databaseEnabled          => bool(0),
            handlesAlerts            => bool(1),
            hasTouchScreen           => bool(0),
            javascriptEnabled        => bool(1),
            locationContextEnabled   => bool(1),
            mobileEmulationEnabled   => bool(0),
            nativeEvents             => bool(1),
            platform                 => 'Linux',
            rotatable                => bool(0),
            takesHeapSnapshot        => bool(1),
            takesScreenshot          => bool(1),
            version                  => re(qr/^[\d.]+$/),
            webStorageEnabled        => bool(1),
        },
        maximized  => [ 1920, 1053 ],
        name       => 'ChromeDriver',
        user_agent => qr/Chrome/,
    },
    {   args       => { port => 8910 },
        capabilities => {
            acceptSslCerts           => bool(0),
            applicationCacheEnabled  => bool(0),
            browserConnectionEnabled => bool(0),
            browserName              => 'phantomjs',
            cssSelectorsEnabled      => bool(1),
            databaseEnabled          => bool(0),
            driverName               => 'ghostdriver',
            driverVersion            => re(qr/^[\d.]+$/),
            handlesAlerts            => bool(0),
            javascriptEnabled        => bool(1),
            locationContextEnabled   => bool(0),
            nativeEvents             => bool(1),
            platform                 => re(qr/linux/),
            proxy                    => { proxyType => 'direct' },
            rotatable                => bool(0),
            takesScreenshot          => bool(1),
            version                  => re(qr/^[\d.]+$/),
            webStorageEnabled        => bool(0),
        },
        maximized  => [ 1366, 768 ],
        name       => 'PhantomJS',
        user_agent => qr/PhantomJS/,
    },
);

# FIXME Hack for Travis for now.
shift @backends unless -e "$ENV{HOME}/Downloads/chromedriver";

our $url = 'file://' . Cwd::fastcwd . '/xt/test.html';

exit TAP::Harness->new( { color => 1, verbosity => 1 } )
    ->runtests( @ARGV ? @ARGV : <{t,xt}/*.t> )->exit;
