#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/file_select_helper.h"
#include <stddef.h>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/file_select_listener.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/shell_dialogs/selected_file_info.h"
FileChooserParams;
#if BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS)
namespace {
class TestFileSelectListener : public content::FileSelectListener { … };
void PrepareContentAnalysisCompletionCallbackArgs(
std::vector<base::FilePath> paths,
std::vector<bool> status,
std::vector<blink::mojom::FileChooserFileInfoPtr>* orig_files,
enterprise_connectors::ContentAnalysisDelegate::Data* data,
enterprise_connectors::ContentAnalysisDelegate::Result* result) { … }
}
#endif
class FileSelectHelperTest : public testing::Test { … };
TEST_F(FileSelectHelperTest, IsAcceptTypeValid) { … }
#if BUILDFLAG(IS_MAC)
TEST_F(FileSelectHelperTest, ZipPackage) {
const char app_name[] = "CalculatorFake.app";
base::FilePath src = data_dir_.Append(app_name);
base::FilePath dest = FileSelectHelper::ZipPackage(src);
ASSERT_FALSE(dest.empty());
ASSERT_TRUE(base::PathExists(dest));
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::CommandLine cl(base::FilePath("/usr/bin/unzip"));
cl.AppendArg(dest.value().c_str());
cl.AppendArg("-d");
cl.AppendArg(temp_dir.GetPath().value().c_str());
std::string output;
EXPECT_TRUE(base::GetAppOutput(cl, &output));
const char* files_to_verify[] = {"Contents/Info.plist",
"Contents/MacOS/Calculator",
"Contents/_CodeSignature/CodeResources"};
size_t file_count = std::size(files_to_verify);
for (size_t i = 0; i < file_count; i++) {
const char* relative_path = files_to_verify[i];
base::FilePath orig_file = src.Append(relative_path);
base::FilePath final_file =
temp_dir.GetPath().Append(app_name).Append(relative_path);
EXPECT_TRUE(base::ContentsEqual(orig_file, final_file));
}
}
#endif
TEST_F(FileSelectHelperTest, GetSanitizedFileName) { … }
TEST_F(FileSelectHelperTest, LastSelectedDirectory) { … }
#if BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS)
TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_NoFiles) { … }
TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_OneOKFile) { … }
TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_TwoOKFiles) { … }
TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_TwoBadFiles) { … }
TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_OKBadFiles) { … }
TEST_F(FileSelectHelperTest,
ContentAnalysisCompletionCallback_SystemFilesSkipped) { … }
TEST_F(FileSelectHelperTest,
ContentAnalysisCompletionCallback_SystemOKBadFiles) { … }
TEST_F(FileSelectHelperTest,
ContentAnalysisCompletionCallback_FolderUpload_OK) { … }
TEST_F(FileSelectHelperTest,
ContentAnalysisCompletionCallback_FolderUpload_Bad) { … }
TEST_F(FileSelectHelperTest,
ContentAnalysisCompletionCallback_FolderUpload_OKBad) { … }
TEST_F(FileSelectHelperTest, GetFileTypesFromAcceptType) { … }
#if BUILDFLAG(IS_MAC)
TEST_F(FileSelectHelperTest, MultipleFileExtensionsForMime) {
content::BrowserTaskEnvironment task_environment;
TestingProfile profile;
scoped_refptr<FileSelectHelper> file_select_helper =
new FileSelectHelper(&profile);
std::vector<std::u16string> accept_types{u"application/vnd.ms-powerpoint"};
std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> file_type_info =
file_select_helper->GetFileTypesFromAcceptType(accept_types);
std::vector<base::FilePath::StringType> expected_extensions {
#if BUILDFLAG(IS_WIN)
L"ppt", L"pot", L"pps"
};
#else
"ppt", "pot", "pps"
};
#endif
std::sort(expected_extensions.begin(), expected_extensions.end());
ASSERT_EQ(file_type_info->extensions.size(), 1u);
std::vector<base::FilePath::StringType> actual_extensions =
file_type_info->extensions[0];
std::sort(actual_extensions.begin(), actual_extensions.end());
EXPECT_EQ(expected_extensions, actual_extensions);
}
#endif
#endif