﻿Param(
    [parameter(Mandatory=$true,Position=0)][String]$Url,
    [parameter(Mandatory=$true,Position=1)][UInt32]$Time,
    [parameter(Mandatory=$false)][string]$AxeCoreNet,
    [parameter(Mandatory=$false)][UInt32]$Iterations=1,
    [parameter(Mandatory=$false)][Switch]$FullScreen
)

try {

    # Since we are a workload, it is important that we handle the cancel event
    # because that is how we are ended when the battery reaches its end condition
    # if we happen to be the workload running at that time.  If we don't, the test
    # can't finish until we naturally end which may be a while depending on how many
    # iterations we have.
    #    
    if ( $AxeCoreNet ) {
        $axeAssembly = [Reflection.Assembly]::LoadFrom( $AxeCoreNet )
        if ( $axeAssembly ) {
            $axeSupport = [Microsoft.Assessments.Runtime.Support]::Initialize()
            if ( $axeSupport ) {

                # Even if we are able to load the axe library, it doesn't mean
                # we were actually run from axe.  We only need to worry about cancel
                # if we were launched by axe.
                #
                $boolRunningUnderAxe = $axeSupport.EngineRunning

                # We want to always use the logger if we can.
                #
                $axeLogger = $axeSupport.CreateLogger()
                if ( $axeLogger ) {
                    $axeLogger.LogMessage( "Beginning browsing workload." )
                }
            }
        }
    }

    Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class WindowsFunctions {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@

    # IE Object documentation: http://msdn.microsoft.com/en-us/library/aa752084.aspx
    #
    $ie = new-object -com internetexplorer.application
    $ie.Visible = $true
    $ie.FullScreen = $FullScreen

    # Loop through the number of iterations.
    #
    for ($i=0; $i -lt $Iterations; $i++) {
 
        # The URL string can contain multiple divided by spaces.
        #   
        $Url.Split(" ") | % {
    
            # Make sure there is something in the URL since multiple spaces between them
            # would result in trying to navigate to empty strings.
            #
            if ( $_.Trim().Length -gt 0 )
            {
                # For each URL, navigate there and wait for the amount of time.
                #
                $ie.Navigate( $_ )
                while ( $ie.Busy ) {
                    sleep -Milliseconds 100
                }
                [void][WindowsFunctions]::SetForegroundWindow( $ie.HWND )

                # How we wait depends on if we need to also be checking the cancel event
                # from Axe.
                #
                if ( $boolRunningUnderAxe ) {
                    if ( $axeSupport.CancelEvent.WaitOne( $Time * 1000 ) ) {
                        if ( $axeLogger ) {
                            $axeLogger.LogMessage( "Exiting because workload has been canceled." )
                        }
                        exit 0
                    }
                } else {
                    sleep -Seconds $Time
                }
            }
        }
    }

} catch {


    # We can also put any error in Axe logs so we don't
    # lose it on the screen which is probably hidden.
    #
    if ( $axeLogger ) {
        $axeLogger.LogErrorCode( $_.Exception.Hresult, $_ )
    }

    throw
    
} finally {

    # Make sure we don't ever leave the IE window open.
    #
    if ( $ie ) {
        $ie.Quit()
    }

    # We should always properly dispose of this guy.
    #
    if ( $axeSupport ) {
        $axeSupport.Dispose()
    }
}