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