| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 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 |  | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 8 | #include "SkOSFile.h" | 
| tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "SkString.h" | 
|  | 10 | #include "Test.h" | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 11 |  | 
|  | 12 | /** | 
| bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 13 | *  Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname. | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 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.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 16 | *  @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 | */ | 
|  | 22 | static 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 |  | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 27 | // Tests for SkOSPath::Join and SkOSPath::Basename | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | // fullName should be "dir<SkPATH_SEPARATOR>file" | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 30 | SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str()); | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 31 |  | 
|  | 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(); | 
| bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 35 | if (!dir.endsWith(SkPATH_SEPARATOR) && !dir.isEmpty()) { | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 36 | expectedSize++; | 
|  | 37 | } | 
|  | 38 | REPORTER_ASSERT(reporter, fullName.size() == expectedSize); | 
|  | 39 |  | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 40 | SkString basename = SkOSPath::Basename(fullName.c_str()); | 
| bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 41 | SkString dirname = SkOSPath::Dirname(fullName.c_str()); | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 42 |  | 
|  | 43 | // basename should be the same as filename | 
|  | 44 | REPORTER_ASSERT(reporter, basename.equals(filename)); | 
|  | 45 |  | 
| bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 46 | // dirname should be the same as dir with any trailing seperators removed. | 
|  | 47 | // Except when the the string is just "/". | 
|  | 48 | SkString strippedDir = dir; | 
|  | 49 | while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkPATH_SEPARATOR) { | 
|  | 50 | 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.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 57 | // basename will not contain a path separator | 
|  | 58 | REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR)); | 
|  | 59 |  | 
|  | 60 | // Now take the basename of filename, which should be the same as filename. | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 61 | basename = SkOSPath::Basename(filename.c_str()); | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, basename.equals(filename)); | 
|  | 63 | } | 
|  | 64 |  | 
| tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 65 | DEF_TEST(OSPath, reporter) { | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 66 | 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. | 
|  | 71 | dir.appendUnichar(SkPATH_SEPARATOR); | 
|  | 72 | test_dir_with_file(reporter, dir, filename); | 
|  | 73 |  | 
| scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 74 | // 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.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 80 | // 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. | 
|  | 85 | dir.appendUnichar(SkPATH_SEPARATOR); | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 86 | SkString baseOfDir = SkOSPath::Basename(dir.c_str()); | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, baseOfDir.size() == 0); | 
|  | 88 |  | 
|  | 89 | // Basename of NULL is an empty string. | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 90 | SkString empty = SkOSPath::Basename(NULL); | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, empty.size() == 0); | 
| scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 92 |  | 
| bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 93 | // File in root dir | 
|  | 94 | dir.printf("%c", SkPATH_SEPARATOR); | 
|  | 95 | 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 |  | 
| scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 102 | // Test that NULL can be used for the directory and filename. | 
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 103 | SkString emptyPath = SkOSPath::Join(NULL, NULL); | 
| bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, emptyPath.isEmpty()); | 
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 105 | } |