<html>
<head>
<script src="../http/tests/inspector/inspector-test.js"></script>
<script src="../http/tests/inspector/workspace-test.js"></script>
<script>
function test()
{
    var uiSourceCodes = {};
    var projectDelegates = {};
    function createUISourceCode(projectId, path)
    {
        var projectDelegate = projectDelegates[projectId];
        if (!projectDelegates[projectId]) {
            projectDelegate = new MockProjectDelegate(projectId);
            workspace.addProject(projectDelegate);
            projectDelegates[projectId] = projectDelegate;
        }

        projectDelegate.addUISourceCode(path);
    }

    function MockProjectDelegate(id)
    {
        this._id = id;
    }

    MockProjectDelegate.prototype = {
        id: function() {
            return this._id;
        },

        displayName: function() {},

        addUISourceCode: function(path) {
            var parentPath = path.substring(0, path.lastIndexOf("/"));
            var name = path.substring(path.lastIndexOf("/") + 1);
            var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, path, path, WebInspector.resourceTypes.Script, true, false);
            this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileAdded, fileDescriptor);
        },

        __proto__: WebInspector.Object.prototype
    };

    var fileSystemMapping = new WebInspector.FileSystemMapping();
    var fileSystemPath = "/var/www";
    var projectId = WebInspector.FileSystemProjectDelegate.projectId(fileSystemPath);
    fileSystemMapping.addFileSystem("/var/www");
    fileSystemMapping.addFileMapping("/var/www", "http://localhost/", "/localhost/");
    var workspace = new WebInspector.Workspace(fileSystemMapping);

    function dumpHasMappingForURL(url)
    {
        var result = workspace.hasMappingForURL(url)
        if (result)
            InspectorTest.addResult("    url " + url + " is mapped.");
        else
            InspectorTest.addResult("    url " + url + " is not mapped.");
    }

    function dumpUISourceCodeForURL(url)
    {
        var uiSourceCode = workspace.uiSourceCodeForURL(url)
        InspectorTest.addResult("    url " + url + " is mapped to " + (uiSourceCode ? uiSourceCode.uri() : null));
    }

    function dumpURLForPath(fileSystemPath, filePath)
    {
        var url = workspace.urlForPath(fileSystemPath, filePath)
        InspectorTest.addResult("    path " + fileSystemPath + " / " + filePath + " is mapped to " + (url ? url : null));
    }
    
    createUISourceCode(projectId, "localhost/index.html");

    createUISourceCode("http://www.example.com", "index.html");
    createUISourceCode("http://localhost", "index.html");
    createUISourceCode("http://localhost", "foo/index.html");
    createUISourceCode("https://localhost", "index.html");

    dumpHasMappingForURL("http://www.example.com/index.html");
    dumpHasMappingForURL("http://localhost/index.html");
    dumpHasMappingForURL("http://localhost/foo/index.html");
    dumpHasMappingForURL("https://localhost/index.html");
    InspectorTest.addResult("");

    dumpUISourceCodeForURL("http://www.example.com/index.html");
    dumpUISourceCodeForURL("http://localhost/index.html");
    dumpUISourceCodeForURL("http://localhost/foo/index.html");
    dumpUISourceCodeForURL("https://localhost/index.html");
    InspectorTest.addResult("");

    dumpURLForPath("/home/example.com", "foo/index.html");
    dumpURLForPath("/home/example.com", "index.html");
    dumpURLForPath("/var/www", "localhost/index.html");
    dumpURLForPath("/var/www", "localhost/foo/index.html");
    dumpURLForPath("/home/foo", "index.html");

    InspectorTest.completeTest();
}
</script>
</head>
<body onload="runTest()">
<p>Tests workspace mappings</p>
</body>
</html>
