public function SetUp()
 {
     // Although this script is run from 'www/unittests', the actual Enhance
     // unit tests are run from 'www', so the unit test data directory must be
     // relative to that.
     $testDir = 'unittests/data';
     // After running through the body of GetDevToolsProgress, the results is
     // cached in a file, devToolsProgress.json.gz, so that it doesn't need
     // to be re-computed. However, for these tests, we do want the body of
     // GetDevToolsProgress to be run each time.
     $cachedFileName = "{$testDir}/devToolsProgress.json.gz";
     if (file_exists($cachedFileName)) {
         unlink($cachedFileName);
     }
     $this->progress = GetDevToolsProgress($testDir, 'sample', false);
 }
/**
* Calculate the progress for all of the images in a given directory
*/
function GetVisualProgress($testPath, $run, $cached, $options = null, $end = null, $startOffset = null)
{
    $frames = null;
    if (substr($testPath, 0, 1) !== '.') {
        $testPath = './' . $testPath;
    }
    $testInfo = GetTestInfo($testPath);
    $completed = IsTestRunComplete($run, $testInfo);
    $video_directory = "{$testPath}/video_{$run}";
    if ($cached) {
        $video_directory .= '_cached';
    }
    $cache_file = "{$testPath}/{$run}.{$cached}.visual.dat";
    if (!isset($startOffset)) {
        $startOffset = 0;
    }
    $dirty = false;
    $current_version = VIDEO_CODE_VERSION;
    if (isset($end)) {
        if (is_numeric($end)) {
            $end = (int) ($end * 1000);
        } else {
            unset($end);
        }
    }
    if (!isset($end) && !isset($options) && gz_is_file($cache_file)) {
        $frames = json_decode(gz_file_get_contents($cache_file), true);
        if (!array_key_exists('frames', $frames) || !array_key_exists('version', $frames)) {
            unset($frames);
        } elseif (array_key_exists('version', $frames) && $frames['version'] !== $current_version) {
            unset($frames);
        }
    }
    if ((!isset($frames) || !count($frames)) && is_dir($video_directory)) {
        $frames = array('version' => $current_version);
        $frames['frames'] = array();
        $dirty = true;
        $base_path = substr($video_directory, 1);
        $files = scandir($video_directory);
        $last_file = null;
        $first_file = null;
        $previous_file = null;
        foreach ($files as $file) {
            if (strpos($file, 'frame_') !== false && strpos($file, '.hist') === false) {
                $parts = explode('_', $file);
                if (count($parts) >= 2) {
                    $time = (int) $parts[1] * 100 - $startOffset;
                    if ($time >= 0 && (!isset($end) || $time <= $end)) {
                        if (isset($previous_file) && !array_key_exists(0, $frames['frames']) && $time > 0) {
                            $frames['frames'][0] = array('path' => "{$base_path}/{$previous_file}", 'file' => $previous_file);
                            $first_file = $previous_file;
                        } elseif (!isset($first_file)) {
                            $first_file = $file;
                        }
                        $last_file = $file;
                        $frames['frames'][$time] = array('path' => "{$base_path}/{$file}", 'file' => $file);
                    }
                    $previous_file = $file;
                }
            } elseif (strpos($file, 'ms_') !== false && strpos($file, '.hist') === false) {
                $parts = explode('_', $file);
                if (count($parts) >= 2) {
                    $time = intval($parts[1]) - $startOffset;
                    if ($time >= 0 && (!isset($end) || $time <= $end)) {
                        if (isset($previous_file) && !array_key_exists(0, $frames['frames']) && $time > 0) {
                            $frames['frames'][0] = array('path' => "{$base_path}/{$previous_file}", 'file' => $previous_file);
                            $first_file = $previous_file;
                        } elseif (!isset($first_file)) {
                            $first_file = $file;
                        }
                        $last_file = $file;
                        $frames['frames'][$time] = array('path' => "{$base_path}/{$file}", 'file' => $file);
                    }
                    $previous_file = $file;
                }
            }
        }
        if (count($frames['frames']) == 1) {
            foreach ($frames['frames'] as $time => &$frame) {
                $frame['progress'] = 100;
                $frames['complete'] = $time;
            }
        } elseif (isset($first_file) && strlen($first_file) && isset($last_file) && strlen($last_file) && count($frames['frames'])) {
            $start_histogram = GetImageHistogram("{$video_directory}/{$first_file}", $options);
            $final_histogram = GetImageHistogram("{$video_directory}/{$last_file}", $options);
            foreach ($frames['frames'] as $time => &$frame) {
                $histogram = GetImageHistogram("{$video_directory}/{$frame['file']}", $options);
                $frame['progress'] = CalculateFrameProgress($histogram, $start_histogram, $final_histogram, 5);
                if ($frame['progress'] == 100 && !array_key_exists('complete', $frames)) {
                    $frames['complete'] = $time;
                }
            }
        }
    }
    if (isset($frames) && !array_key_exists('SpeedIndex', $frames)) {
        $dirty = true;
        $frames['SpeedIndex'] = CalculateSpeedIndex($frames);
    }
    if (isset($frames)) {
        $frames['visualComplete'] = 0;
        foreach ($frames['frames'] as $time => &$frame) {
            if ($frame['progress'] > 0 && !array_key_exists('startRender', $frames)) {
                $frames['startRender'] = $time;
            }
            if ($frame['progress'] == 100) {
                $frames['visualComplete'] = $time;
                break;
            }
        }
    }
    $devTools = GetDevToolsProgress($testPath, $run, $cached);
    if (isset($devTools)) {
        if (!isset($frames)) {
            $frames = array();
        }
        $frames['DevTools'] = $devTools;
    }
    if ($completed && !isset($end) && !isset($options) && $dirty && isset($frames) && count($frames)) {
        gz_file_put_contents($cache_file, json_encode($frames));
    }
    return $frames;
}