#include "extensions/browser/api/file_handlers/app_file_handler_util.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/run_loop.h"
#include "base/test/gtest_util.h"
#include "base/test/mock_callback.h"
#include "components/file_access/scoped_file_access.h"
#include "components/file_access/scoped_file_access_delegate.h"
#include "components/file_access/test/mock_scoped_file_access_delegate.h"
#include "components/services/app_service/public/cpp/file_handler.h"
#include "components/services/app_service/public/cpp/file_handler_info.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/entry_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace app_file_handler_util {
namespace {
apps::FileHandlerInfo CreateHandlerInfoFromExtension(
const std::string& extension) { … }
apps::FileHandlerInfo CreateHandlerInfoFromIncludeDirectories(
bool include_directories) { … }
apps::FileHandler CreateWebAppFileHandlerFromMimeType(
const std::string& mime_type) { … }
apps::FileHandler CreateWebAppFileHandlerFromFileExtension(
const std::string& file_extension) { … }
}
TEST(FileHandlersAppFileHandlerUtilTest, FileHandlerCanHandleEntry) { … }
TEST(FileHandlersAppFileHandlerUtilTest, WebAppFileHandlerMatch) { … }
TEST(FileHandlersAppFileHandlerUtilTest, WebAppFileHandlerCanHandleEntry) { … }
TEST(FileHandlersAppFileHandlerUtilTest, CreateEntryInfos) { … }
class PrepareFilesForWritableAppTest : public ::testing::Test { … };
TEST_F(PrepareFilesForWritableAppTest, SingleFileThatDoesExists) { … }
TEST_F(PrepareFilesForWritableAppTest, SingleFileThatDoesNotExists) { … }
TEST_F(PrepareFilesForWritableAppTest, SingleFileInDirectoryThatDoesNotExists) { … }
TEST_F(PrepareFilesForWritableAppTest,
OneFileInDirectoryThatDoesNotExistOneFileThatExists) { … }
TEST_F(PrepareFilesForWritableAppTest,
OneFileThatExistsOneFileInDirectoryThatDoesNotExist) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(PrepareFilesForWritableAppTest, SingleFileThatExistsDlpGrantsAccess) {
testing::StrictMock<base::MockOnceCallback<void()>> success_callback;
testing::StrictMock<base::MockOnceCallback<void(const base::FilePath& path)>>
fail_callback;
file_access::MockScopedFileAccessDelegate scoped_file_access_delegate;
base::RunLoop run_loop;
EXPECT_CALL(scoped_file_access_delegate, RequestFilesAccessForSystem)
.WillOnce([this](const std::vector<base::FilePath>& paths,
base::OnceCallback<void(file_access::ScopedFileAccess)>
callback) {
EXPECT_EQ(1ul, paths.size());
EXPECT_EQ(file1, paths[0]);
std::move(callback).Run(file_access::ScopedFileAccess::Allowed());
});
EXPECT_CALL(success_callback, Run).WillOnce([&run_loop] { run_loop.Quit(); });
PrepareFilesForWritableApp({file1}, &context_, {}, success_callback.Get(),
fail_callback.Get());
run_loop.Run();
}
TEST_F(PrepareFilesForWritableAppTest, SingleFileThatExistsDlpDeniesAccess) {
testing::StrictMock<base::MockOnceCallback<void()>> success_callback;
testing::StrictMock<base::MockOnceCallback<void(const base::FilePath& path)>>
fail_callback;
file_access::MockScopedFileAccessDelegate scoped_file_access_delegate;
base::RunLoop run_loop;
EXPECT_CALL(scoped_file_access_delegate, RequestFilesAccessForSystem)
.WillOnce([this](const std::vector<base::FilePath>& paths,
base::OnceCallback<void(file_access::ScopedFileAccess)>
callback) {
EXPECT_EQ(1ul, paths.size());
EXPECT_EQ(file1, paths[0]);
std::move(callback).Run(
file_access::ScopedFileAccess(false, base::ScopedFD()));
});
EXPECT_CALL(success_callback, Run).WillOnce([&run_loop] { run_loop.Quit(); });
PrepareFilesForWritableApp({file1}, &context_, {}, success_callback.Get(),
fail_callback.Get());
run_loop.Run();
}
#endif
}
}