blob: e0d7b9d4100542ba9aaa719ec18884e39bc29c25 [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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "Test.h"
9#include "TestClassDef.h"
scroggo@google.comccd7afb2013-05-28 16:45:07 +000010#include "SkString.h"
11#include "SkOSFile.h"
scroggo@google.comccd7afb2013-05-28 16:45:07 +000012
13/**
14 * Test SkPathJoin and SkBasename.
15 * Will use SkPathJoin to append filename to dir, test that it works correctly,
16 * and tests using SkBasename on the result.
17 * @param reporter Reporter for test conditions.
18 * @param dir String representing the path to a folder. May or may not
19 * end with SkPATH_SEPARATOR.
20 * @param filename String representing the basename of a file. Must NOT
21 * contain SkPATH_SEPARATOR.
22 */
23static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
24 SkString filename) {
25 // If filename contains SkPATH_SEPARATOR, the tests will fail.
26 SkASSERT(!filename.contains(SkPATH_SEPARATOR));
27
28 // Tests for SkOSPath::SkPathJoin and SkOSPath::SkBasename
29
30 // fullName should be "dir<SkPATH_SEPARATOR>file"
31 SkString fullName = SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
32
33 // fullName should be the combined size of dir and file, plus one if
34 // dir did not include the final path separator.
35 size_t expectedSize = dir.size() + filename.size();
36 if (!dir.endsWith(SkPATH_SEPARATOR)) {
37 expectedSize++;
38 }
39 REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
40
41 SkString basename = SkOSPath::SkBasename(fullName.c_str());
42
43 // basename should be the same as filename
44 REPORTER_ASSERT(reporter, basename.equals(filename));
45
46 // basename will not contain a path separator
47 REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR));
48
49 // Now take the basename of filename, which should be the same as filename.
50 basename = SkOSPath::SkBasename(filename.c_str());
51 REPORTER_ASSERT(reporter, basename.equals(filename));
52}
53
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000054DEF_TEST(OSPath, reporter) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000055 SkString dir("dir");
56 SkString filename("file");
57 test_dir_with_file(reporter, dir, filename);
58
59 // Now make sure this works with a path separator at the end of dir.
60 dir.appendUnichar(SkPATH_SEPARATOR);
61 test_dir_with_file(reporter, dir, filename);
62
scroggo@google.comc76218d2013-06-06 14:59:56 +000063 // Test using no filename.
64 test_dir_with_file(reporter, dir, SkString());
65
66 // Testing using no directory.
67 test_dir_with_file(reporter, SkString(), filename);
68
scroggo@google.comccd7afb2013-05-28 16:45:07 +000069 // Test with a sub directory.
70 dir.append("subDir");
71 test_dir_with_file(reporter, dir, filename);
72
73 // Basename of a directory with a path separator at the end is empty.
74 dir.appendUnichar(SkPATH_SEPARATOR);
75 SkString baseOfDir = SkOSPath::SkBasename(dir.c_str());
76 REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
77
78 // Basename of NULL is an empty string.
79 SkString empty = SkOSPath::SkBasename(NULL);
80 REPORTER_ASSERT(reporter, empty.size() == 0);
scroggo@google.comc76218d2013-06-06 14:59:56 +000081
82 // Test that NULL can be used for the directory and filename.
83 SkString emptyPath = SkOSPath::SkPathJoin(NULL, NULL);
84 REPORTER_ASSERT(reporter, emptyPath.size() == 1);
85 REPORTER_ASSERT(reporter, emptyPath.contains(SkPATH_SEPARATOR));
scroggo@google.comccd7afb2013-05-28 16:45:07 +000086}