1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/core/SkString.h"
9 #include "include/core/SkTypes.h"
10 #include "include/private/base/SkDebug.h"
11 #include "src/utils/SkOSPath.h"
12 #include "tests/Test.h"
13
14 #include <cstddef>
15
16 /**
17 * Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname.
18 * Will use SkOSPath::Join to append filename to dir, test that it works correctly,
19 * and tests using SkOSPath::Basename on the result.
20 * @param reporter Reporter for test conditions.
21 * @param dir String representing the path to a folder. May or may not
22 * end with SkOSPath::SEPARATOR.
23 * @param filename String representing the basename of a file. Must NOT
24 * contain SkOSPath::SEPARATOR.
25 */
test_dir_with_file(skiatest::Reporter * reporter,const SkString & dir,const SkString & filename)26 static void test_dir_with_file(skiatest::Reporter* reporter, const SkString& dir,
27 const SkString& filename) {
28 // If filename contains SkOSPath::SEPARATOR, the tests will fail.
29 SkASSERT(!filename.contains(SkOSPath::SEPARATOR));
30
31 // Tests for SkOSPath::Join and SkOSPath::Basename
32
33 // fullName should be "dir<SkOSPath::SEPARATOR>file"
34 SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str());
35
36 // fullName should be the combined size of dir and file, plus one if
37 // dir did not include the final path separator.
38 size_t expectedSize = dir.size() + filename.size();
39 if (!dir.endsWith(SkOSPath::SEPARATOR) && !dir.isEmpty()) {
40 expectedSize++;
41 }
42 REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
43
44 SkString basename = SkOSPath::Basename(fullName.c_str());
45 SkString dirname = SkOSPath::Dirname(fullName.c_str());
46
47 // basename should be the same as filename
48 REPORTER_ASSERT(reporter, basename.equals(filename));
49
50 // dirname should be the same as dir with any trailing seperators removed.
51 // Except when the the string is just "/".
52 SkString strippedDir = dir;
53 while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkOSPath::SEPARATOR) {
54 strippedDir.remove(strippedDir.size() - 1, 1);
55 }
56 if (!dirname.equals(strippedDir)) {
57 SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c_str());
58 }
59 REPORTER_ASSERT(reporter, dirname.equals(strippedDir));
60
61 // basename will not contain a path separator
62 REPORTER_ASSERT(reporter, !basename.contains(SkOSPath::SEPARATOR));
63
64 // Now take the basename of filename, which should be the same as filename.
65 basename = SkOSPath::Basename(filename.c_str());
66 REPORTER_ASSERT(reporter, basename.equals(filename));
67 }
68
DEF_TEST(OSPath,reporter)69 DEF_TEST(OSPath, reporter) {
70 SkString dir("dir");
71 SkString filename("file");
72 test_dir_with_file(reporter, dir, filename);
73
74 // Now make sure this works with a path separator at the end of dir.
75 dir.appendUnichar(SkOSPath::SEPARATOR);
76 test_dir_with_file(reporter, dir, filename);
77
78 // Test using no filename.
79 test_dir_with_file(reporter, dir, SkString());
80
81 // Testing using no directory.
82 test_dir_with_file(reporter, SkString(), filename);
83
84 // Test with a sub directory.
85 dir.append("subDir");
86 test_dir_with_file(reporter, dir, filename);
87
88 // Basename of a directory with a path separator at the end is empty.
89 dir.appendUnichar(SkOSPath::SEPARATOR);
90 SkString baseOfDir = SkOSPath::Basename(dir.c_str());
91 REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
92
93 // Basename of nullptr is an empty string.
94 SkString empty = SkOSPath::Basename(nullptr);
95 REPORTER_ASSERT(reporter, empty.size() == 0);
96
97 // File in root dir
98 dir.printf("%c", SkOSPath::SEPARATOR);
99 filename.set("file");
100 test_dir_with_file(reporter, dir, filename);
101
102 // Just the root dir
103 filename.reset();
104 test_dir_with_file(reporter, dir, filename);
105
106 // Test that nullptr can be used for the directory and filename.
107 SkString emptyPath = SkOSPath::Join(nullptr, nullptr);
108 REPORTER_ASSERT(reporter, emptyPath.isEmpty());
109 }
110