blob: 1452c3852e499e23aa542f5af1e6917ff064cf42 [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
scroggo@google.comccd7afb2013-05-28 16:45:07 +00008#include "SkOSFile.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "SkString.h"
10#include "Test.h"
scroggo@google.comccd7afb2013-05-28 16:45:07 +000011
12/**
tfarinaa8e2e152014-07-28 19:26:58 -070013 * Test SkOSPath::Join and SkOSPath::Basename.
14 * 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
18 * end with SkPATH_SEPARATOR.
19 * @param filename String representing the basename of a file. Must NOT
20 * contain SkPATH_SEPARATOR.
21 */
22static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
23 SkString filename) {
24 // If filename contains SkPATH_SEPARATOR, the tests will fail.
25 SkASSERT(!filename.contains(SkPATH_SEPARATOR));
26
tfarinaa8e2e152014-07-28 19:26:58 -070027 // Tests for SkOSPath::Join and SkOSPath::Basename
scroggo@google.comccd7afb2013-05-28 16:45:07 +000028
29 // fullName should be "dir<SkPATH_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();
35 if (!dir.endsWith(SkPATH_SEPARATOR)) {
36 expectedSize++;
37 }
38 REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
39
tfarinaa8e2e152014-07-28 19:26:58 -070040 SkString basename = SkOSPath::Basename(fullName.c_str());
scroggo@google.comccd7afb2013-05-28 16:45:07 +000041
42 // basename should be the same as filename
43 REPORTER_ASSERT(reporter, basename.equals(filename));
44
45 // basename will not contain a path separator
46 REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR));
47
48 // Now take the basename of filename, which should be the same as filename.
tfarinaa8e2e152014-07-28 19:26:58 -070049 basename = SkOSPath::Basename(filename.c_str());
scroggo@google.comccd7afb2013-05-28 16:45:07 +000050 REPORTER_ASSERT(reporter, basename.equals(filename));
51}
52
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000053DEF_TEST(OSPath, reporter) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000054 SkString dir("dir");
55 SkString filename("file");
56 test_dir_with_file(reporter, dir, filename);
57
58 // Now make sure this works with a path separator at the end of dir.
59 dir.appendUnichar(SkPATH_SEPARATOR);
60 test_dir_with_file(reporter, dir, filename);
61
scroggo@google.comc76218d2013-06-06 14:59:56 +000062 // Test using no filename.
63 test_dir_with_file(reporter, dir, SkString());
64
65 // Testing using no directory.
66 test_dir_with_file(reporter, SkString(), filename);
67
scroggo@google.comccd7afb2013-05-28 16:45:07 +000068 // Test with a sub directory.
69 dir.append("subDir");
70 test_dir_with_file(reporter, dir, filename);
71
72 // Basename of a directory with a path separator at the end is empty.
73 dir.appendUnichar(SkPATH_SEPARATOR);
tfarinaa8e2e152014-07-28 19:26:58 -070074 SkString baseOfDir = SkOSPath::Basename(dir.c_str());
scroggo@google.comccd7afb2013-05-28 16:45:07 +000075 REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
76
77 // Basename of NULL is an empty string.
tfarinaa8e2e152014-07-28 19:26:58 -070078 SkString empty = SkOSPath::Basename(NULL);
scroggo@google.comccd7afb2013-05-28 16:45:07 +000079 REPORTER_ASSERT(reporter, empty.size() == 0);
scroggo@google.comc76218d2013-06-06 14:59:56 +000080
81 // Test that NULL can be used for the directory and filename.
tfarinaa8e2e152014-07-28 19:26:58 -070082 SkString emptyPath = SkOSPath::Join(NULL, NULL);
scroggo@google.comc76218d2013-06-06 14:59:56 +000083 REPORTER_ASSERT(reporter, emptyPath.size() == 1);
84 REPORTER_ASSERT(reporter, emptyPath.contains(SkPATH_SEPARATOR));
scroggo@google.comccd7afb2013-05-28 16:45:07 +000085}