1ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com/*
2ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com * Copyright 2013 Google Inc.
3ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com *
4ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com * Use of this source code is governed by a BSD-style license that can be
5ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com * found in the LICENSE file.
6ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com */
7ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
8bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner#include "SkOSPath.h"
98f6884aab8aecd7657cf3f9cdbc682f0deca29c5tfarina@chromium.org#include "SkString.h"
108f6884aab8aecd7657cf3f9cdbc682f0deca29c5tfarina@chromium.org#include "Test.h"
11ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
12ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com/**
136eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon *  Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname.
14a8e2e1504b9af6ba791637f228debaa23953064atfarina *  Will use SkOSPath::Join to append filename to dir, test that it works correctly,
15a8e2e1504b9af6ba791637f228debaa23953064atfarina *  and tests using SkOSPath::Basename on the result.
16ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com *  @param reporter Reporter for test conditions.
17ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com *  @param dir String representing the path to a folder. May or may not
18bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner *      end with SkOSPath::SEPARATOR.
19ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com *  @param filename String representing the basename of a file. Must NOT
20bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner *      contain SkOSPath::SEPARATOR.
21ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com */
22ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.comstatic void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
23ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com                               SkString filename) {
24bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    // If filename contains SkOSPath::SEPARATOR, the tests will fail.
25bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    SkASSERT(!filename.contains(SkOSPath::SEPARATOR));
26ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
27a8e2e1504b9af6ba791637f228debaa23953064atfarina    // Tests for SkOSPath::Join and SkOSPath::Basename
28ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
29bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    // fullName should be "dir<SkOSPath::SEPARATOR>file"
30a8e2e1504b9af6ba791637f228debaa23953064atfarina    SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str());
31ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
32ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // fullName should be the combined size of dir and file, plus one if
33ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // dir did not include the final path separator.
34ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    size_t expectedSize = dir.size() + filename.size();
35bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    if (!dir.endsWith(SkOSPath::SEPARATOR) && !dir.isEmpty()) {
36ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com        expectedSize++;
37ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    }
38ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
39ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
40a8e2e1504b9af6ba791637f228debaa23953064atfarina    SkString basename = SkOSPath::Basename(fullName.c_str());
416eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    SkString dirname = SkOSPath::Dirname(fullName.c_str());
42ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
43ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // basename should be the same as filename
44ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    REPORTER_ASSERT(reporter, basename.equals(filename));
45ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
466eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    // dirname should be the same as dir with any trailing seperators removed.
476eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    // Except when the the string is just "/".
486eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    SkString strippedDir = dir;
49bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkOSPath::SEPARATOR) {
506eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon        strippedDir.remove(strippedDir.size() - 1, 1);
516eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    }
526eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    if (!dirname.equals(strippedDir)) {
536eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon        SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c_str());
546eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    }
556eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    REPORTER_ASSERT(reporter, dirname.equals(strippedDir));
566eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon
57ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // basename will not contain a path separator
58bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    REPORTER_ASSERT(reporter, !basename.contains(SkOSPath::SEPARATOR));
59ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
60ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // Now take the basename of filename, which should be the same as filename.
61a8e2e1504b9af6ba791637f228debaa23953064atfarina    basename = SkOSPath::Basename(filename.c_str());
62ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    REPORTER_ASSERT(reporter, basename.equals(filename));
63ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com}
64ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
65e4fafb146e85cdfcf9d5418597b6818aa0754adatfarina@chromium.orgDEF_TEST(OSPath, reporter) {
66ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    SkString dir("dir");
67ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    SkString filename("file");
68ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    test_dir_with_file(reporter, dir, filename);
69ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
70ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // Now make sure this works with a path separator at the end of dir.
71bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    dir.appendUnichar(SkOSPath::SEPARATOR);
72ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    test_dir_with_file(reporter, dir, filename);
73ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
74c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com    // Test using no filename.
75c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com    test_dir_with_file(reporter, dir, SkString());
76c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com
77c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com    // Testing using no directory.
78c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com    test_dir_with_file(reporter, SkString(), filename);
79c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com
80ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // Test with a sub directory.
81ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    dir.append("subDir");
82ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    test_dir_with_file(reporter, dir, filename);
83ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
84ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    // Basename of a directory with a path separator at the end is empty.
85bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    dir.appendUnichar(SkOSPath::SEPARATOR);
86a8e2e1504b9af6ba791637f228debaa23953064atfarina    SkString baseOfDir = SkOSPath::Basename(dir.c_str());
87ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
88ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
8996fcdcc219d2a0d3579719b84b28bede76efba64halcanary    // Basename of nullptr is an empty string.
9096fcdcc219d2a0d3579719b84b28bede76efba64halcanary    SkString empty = SkOSPath::Basename(nullptr);
91ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    REPORTER_ASSERT(reporter, empty.size() == 0);
92c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com
936eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    // File in root dir
94bf111d7bc9ba3857433e30eae27f0272c34ed0fbBen Wagner    dir.printf("%c", SkOSPath::SEPARATOR);
956eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    filename.set("file");
966eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    test_dir_with_file(reporter, dir, filename);
976eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon
986eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    // Just the root dir
996eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    filename.reset();
1006eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    test_dir_with_file(reporter, dir, filename);
1016eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon
10296fcdcc219d2a0d3579719b84b28bede76efba64halcanary    // Test that nullptr can be used for the directory and filename.
10396fcdcc219d2a0d3579719b84b28bede76efba64halcanary    SkString emptyPath = SkOSPath::Join(nullptr, nullptr);
1046eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    REPORTER_ASSERT(reporter, emptyPath.isEmpty());
105ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com}
106