blob: 9a39428b4d363d194cf4a770840f93e3391e469e [file] [log] [blame]
scroggo@google.comccd7afb2013-05-28 16:45:07 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkString.h"
9#include "src/utils/SkOSPath.h"
10#include "tests/Test.h"
scroggo@google.comccd7afb2013-05-28 16:45:07 +000011
12/**
bsalomon6eb03cc2014-08-07 14:28:50 -070013 * Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname.
tfarinaa8e2e152014-07-28 19:26:58 -070014 * Will use SkOSPath::Join to append filename to dir, test that it works correctly,
15 * and tests using SkOSPath::Basename on the result.
scroggo@google.comccd7afb2013-05-28 16:45:07 +000016 * @param reporter Reporter for test conditions.
17 * @param dir String representing the path to a folder. May or may not
Ben Wagnerbf111d72016-11-07 18:05:29 -050018 * end with SkOSPath::SEPARATOR.
scroggo@google.comccd7afb2013-05-28 16:45:07 +000019 * @param filename String representing the basename of a file. Must NOT
Ben Wagnerbf111d72016-11-07 18:05:29 -050020 * contain SkOSPath::SEPARATOR.
scroggo@google.comccd7afb2013-05-28 16:45:07 +000021 */
22static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
23 SkString filename) {
Ben Wagnerbf111d72016-11-07 18:05:29 -050024 // If filename contains SkOSPath::SEPARATOR, the tests will fail.
25 SkASSERT(!filename.contains(SkOSPath::SEPARATOR));
scroggo@google.comccd7afb2013-05-28 16:45:07 +000026
tfarinaa8e2e152014-07-28 19:26:58 -070027 // Tests for SkOSPath::Join and SkOSPath::Basename
scroggo@google.comccd7afb2013-05-28 16:45:07 +000028
Ben Wagnerbf111d72016-11-07 18:05:29 -050029 // fullName should be "dir<SkOSPath::SEPARATOR>file"
tfarinaa8e2e152014-07-28 19:26:58 -070030 SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str());
scroggo@google.comccd7afb2013-05-28 16:45:07 +000031
32 // fullName should be the combined size of dir and file, plus one if
33 // dir did not include the final path separator.
34 size_t expectedSize = dir.size() + filename.size();
Cary Clark5ef4ebf2018-01-05 18:21:48 +000035 if (!dir.endsWith(SkOSPath::SEPARATOR) && !dir.isEmpty()) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000036 expectedSize++;
37 }
38 REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
39
tfarinaa8e2e152014-07-28 19:26:58 -070040 SkString basename = SkOSPath::Basename(fullName.c_str());
bsalomon6eb03cc2014-08-07 14:28:50 -070041 SkString dirname = SkOSPath::Dirname(fullName.c_str());
scroggo@google.comccd7afb2013-05-28 16:45:07 +000042
43 // basename should be the same as filename
44 REPORTER_ASSERT(reporter, basename.equals(filename));
45
bsalomon6eb03cc2014-08-07 14:28:50 -070046 // dirname should be the same as dir with any trailing seperators removed.
47 // Except when the the string is just "/".
48 SkString strippedDir = dir;
Cary Clark5ef4ebf2018-01-05 18:21:48 +000049 while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkOSPath::SEPARATOR) {
bsalomon6eb03cc2014-08-07 14:28:50 -070050 strippedDir.remove(strippedDir.size() - 1, 1);
51 }
52 if (!dirname.equals(strippedDir)) {
53 SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c_str());
54 }
55 REPORTER_ASSERT(reporter, dirname.equals(strippedDir));
56
scroggo@google.comccd7afb2013-05-28 16:45:07 +000057 // basename will not contain a path separator
Ben Wagnerbf111d72016-11-07 18:05:29 -050058 REPORTER_ASSERT(reporter, !basename.contains(SkOSPath::SEPARATOR));
scroggo@google.comccd7afb2013-05-28 16:45:07 +000059
60 // Now take the basename of filename, which should be the same as filename.
tfarinaa8e2e152014-07-28 19:26:58 -070061 basename = SkOSPath::Basename(filename.c_str());
scroggo@google.comccd7afb2013-05-28 16:45:07 +000062 REPORTER_ASSERT(reporter, basename.equals(filename));
63}
64
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000065DEF_TEST(OSPath, reporter) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000066 SkString dir("dir");
67 SkString filename("file");
68 test_dir_with_file(reporter, dir, filename);
69
70 // Now make sure this works with a path separator at the end of dir.
Ben Wagnerbf111d72016-11-07 18:05:29 -050071 dir.appendUnichar(SkOSPath::SEPARATOR);
scroggo@google.comccd7afb2013-05-28 16:45:07 +000072 test_dir_with_file(reporter, dir, filename);
73
scroggo@google.comc76218d2013-06-06 14:59:56 +000074 // Test using no filename.
75 test_dir_with_file(reporter, dir, SkString());
76
77 // Testing using no directory.
78 test_dir_with_file(reporter, SkString(), filename);
79
scroggo@google.comccd7afb2013-05-28 16:45:07 +000080 // Test with a sub directory.
81 dir.append("subDir");
82 test_dir_with_file(reporter, dir, filename);
83
84 // Basename of a directory with a path separator at the end is empty.
Ben Wagnerbf111d72016-11-07 18:05:29 -050085 dir.appendUnichar(SkOSPath::SEPARATOR);
tfarinaa8e2e152014-07-28 19:26:58 -070086 SkString baseOfDir = SkOSPath::Basename(dir.c_str());
scroggo@google.comccd7afb2013-05-28 16:45:07 +000087 REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
88
halcanary96fcdcc2015-08-27 07:41:13 -070089 // Basename of nullptr is an empty string.
90 SkString empty = SkOSPath::Basename(nullptr);
scroggo@google.comccd7afb2013-05-28 16:45:07 +000091 REPORTER_ASSERT(reporter, empty.size() == 0);
scroggo@google.comc76218d2013-06-06 14:59:56 +000092
bsalomon6eb03cc2014-08-07 14:28:50 -070093 // File in root dir
Ben Wagnerbf111d72016-11-07 18:05:29 -050094 dir.printf("%c", SkOSPath::SEPARATOR);
bsalomon6eb03cc2014-08-07 14:28:50 -070095 filename.set("file");
96 test_dir_with_file(reporter, dir, filename);
97
98 // Just the root dir
99 filename.reset();
100 test_dir_with_file(reporter, dir, filename);
101
halcanary96fcdcc2015-08-27 07:41:13 -0700102 // Test that nullptr can be used for the directory and filename.
103 SkString emptyPath = SkOSPath::Join(nullptr, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -0700104 REPORTER_ASSERT(reporter, emptyPath.isEmpty());
scroggo@google.comccd7afb2013-05-28 16:45:07 +0000105}