This source file includes following definitions.
- ExpectFileContentEquals
- selected_path_
- SelectFile
- selected_path_
- CreateFileSelector
- SetUp
- AddTmpMountPoint
- GetFullPathOnTmpMountPoint
- TestSelectFileFunctionFactory
- SetTestCases
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "chrome/browser/chromeos/extensions/file_manager/file_browser_handler_api.h"
#include <vector>
#include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/browser_context.h"
#include "extensions/common/extension.h"
#include "webkit/browser/fileapi/external_mount_points.h"
#include "webkit/common/fileapi/file_system_types.h"
namespace utils = extension_function_test_utils;
using content::BrowserContext;
using extensions::Extension;
namespace {
struct TestCase {
TestCase(const base::FilePath& suggested_name,
const std::vector<std::string>& allowed_extensions,
bool success,
const base::FilePath& selected_path)
: suggested_name(suggested_name),
allowed_extensions(allowed_extensions),
success(success),
selected_path(selected_path) {
}
~TestCase() {}
base::FilePath suggested_name;
std::vector<std::string> allowed_extensions;
bool success;
base::FilePath selected_path;
};
void ExpectFileContentEquals(const base::FilePath& selected_path,
const std::string& expected_contents) {
std::string test_file_contents;
ASSERT_TRUE(base::ReadFileToString(selected_path, &test_file_contents));
EXPECT_EQ(expected_contents, test_file_contents);
}
class MockFileSelector : public file_manager::FileSelector {
public:
MockFileSelector(const base::FilePath& suggested_name,
const std::vector<std::string>& allowed_extensions,
bool success,
const base::FilePath& selected_path)
: suggested_name_(suggested_name),
allowed_extensions_(allowed_extensions),
success_(success),
selected_path_(selected_path) {
}
virtual ~MockFileSelector() {}
virtual void SelectFile(
const base::FilePath& suggested_name,
const std::vector<std::string>& allowed_extensions,
Browser* browser,
FileBrowserHandlerInternalSelectFileFunction* function) OVERRIDE {
EXPECT_EQ(suggested_name_, suggested_name);
EXPECT_EQ(allowed_extensions_.size(), allowed_extensions.size());
if (allowed_extensions_.size() == allowed_extensions.size()) {
for (size_t i = 0; i < allowed_extensions_.size(); ++i) {
EXPECT_EQ(allowed_extensions_[i], allowed_extensions[i]);
}
}
base::MessageLoopProxy::current()->PostTask(FROM_HERE,
base::Bind(&FileBrowserHandlerInternalSelectFileFunction::
OnFilePathSelected,
function, success_, selected_path_));
delete this;
}
private:
base::FilePath suggested_name_;
std::vector<std::string> allowed_extensions_;
bool success_;
base::FilePath selected_path_;
DISALLOW_COPY_AND_ASSIGN(MockFileSelector);
};
class MockFileSelectorFactory : public file_manager::FileSelectorFactory {
public:
explicit MockFileSelectorFactory(const TestCase& test_case)
: suggested_name_(test_case.suggested_name),
allowed_extensions_(test_case.allowed_extensions),
success_(test_case.success),
selected_path_(test_case.selected_path) {
}
virtual ~MockFileSelectorFactory() {}
virtual file_manager::FileSelector* CreateFileSelector() const OVERRIDE {
return new MockFileSelector(suggested_name_,
allowed_extensions_,
success_,
selected_path_);
}
private:
base::FilePath suggested_name_;
std::vector<std::string> allowed_extensions_;
bool success_;
base::FilePath selected_path_;
DISALLOW_COPY_AND_ASSIGN(MockFileSelectorFactory);
};
class FileBrowserHandlerExtensionTest : public ExtensionApiTest {
protected:
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(scoped_tmp_dir_.CreateUniqueTempDir());
tmp_mount_point_ = scoped_tmp_dir_.path().Append("tmp");
base::CreateDirectory(tmp_mount_point_);
ExtensionApiTest::SetUp();
}
void AddTmpMountPoint(const std::string& extension_id) {
BrowserContext::GetMountPoints(browser()->profile())->RegisterFileSystem(
"tmp",
fileapi::kFileSystemTypeNativeLocal,
fileapi::FileSystemMountOption(),
tmp_mount_point_);
}
base::FilePath GetFullPathOnTmpMountPoint(
const base::FilePath& relative_path) {
return tmp_mount_point_.Append(relative_path);
}
static ExtensionFunction* TestSelectFileFunctionFactory() {
EXPECT_TRUE(test_cases_);
EXPECT_TRUE(!test_cases_ || current_test_case_ < test_cases_->size());
if (!test_cases_ || current_test_case_ >= test_cases_->size())
return new FileBrowserHandlerInternalSelectFileFunction();
MockFileSelectorFactory* mock_factory =
new MockFileSelectorFactory(test_cases_->at(current_test_case_));
current_test_case_++;
return new FileBrowserHandlerInternalSelectFileFunction(
mock_factory, false);
}
void SetTestCases(const std::vector<TestCase>* test_cases) {
test_cases_ = test_cases;
current_test_case_ = 0;
}
private:
static const std::vector<TestCase>* test_cases_;
static size_t current_test_case_;
base::ScopedTempDir scoped_tmp_dir_;
base::FilePath tmp_mount_point_;
};
const std::vector<TestCase>* FileBrowserHandlerExtensionTest::test_cases_ =
NULL;
size_t FileBrowserHandlerExtensionTest::current_test_case_ = 0;
IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, EndToEnd) {
const base::FilePath selected_path =
GetFullPathOnTmpMountPoint(base::FilePath("test_file.txt"));
std::vector<std::string> allowed_extensions;
allowed_extensions.push_back("txt");
allowed_extensions.push_back("html");
std::vector<TestCase> test_cases;
test_cases.push_back(
TestCase(base::FilePath("some_file_name.txt"),
allowed_extensions,
true,
selected_path));
test_cases.push_back(
TestCase(base::FilePath("fail"),
std::vector<std::string>(),
false,
base::FilePath()));
SetTestCases(&test_cases);
ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
"fileBrowserHandlerInternal.selectFile",
FileBrowserHandlerExtensionTest::TestSelectFileFunctionFactory));
ASSERT_FALSE(base::PathExists(selected_path));
const Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("file_browser/filehandler_create"));
ASSERT_TRUE(extension) << message_;
AddTmpMountPoint(extension->id());
ResultCatcher catcher;
GURL url = extension->GetResourceURL("test.html");
ui_test_utils::NavigateToURL(browser(), url);
ASSERT_TRUE(catcher.GetNextResult()) << message_;
ASSERT_TRUE(base::PathExists(selected_path));
const std::string expected_contents = "hello from test extension.";
content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
base::Bind(&ExpectFileContentEquals, selected_path, expected_contents));
content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
SetTestCases(NULL);
}
IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, NoUserGesture) {
scoped_refptr<FileBrowserHandlerInternalSelectFileFunction>
select_file_function(
new FileBrowserHandlerInternalSelectFileFunction());
std::string error =
utils::RunFunctionAndReturnError(
select_file_function.get(),
"[{\"suggestedName\": \"foo\"}]",
browser());
const std::string expected_error =
"This method can only be called in response to user gesture, such as a "
"mouse click or key press.";
EXPECT_EQ(expected_error, error);
}
IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, SelectionFailed) {
TestCase test_case(base::FilePath("some_file_name.txt"),
std::vector<std::string>(),
false,
base::FilePath());
scoped_refptr<FileBrowserHandlerInternalSelectFileFunction>
select_file_function(
new FileBrowserHandlerInternalSelectFileFunction(
new MockFileSelectorFactory(test_case),
false));
select_file_function->set_has_callback(true);
select_file_function->set_user_gesture(true);
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
select_file_function.get(),
"[{\"suggestedName\": \"some_file_name.txt\"}]",
browser())));
EXPECT_FALSE(utils::GetBoolean(result.get(), "success"));
base::DictionaryValue* entry_info;
EXPECT_FALSE(result->GetDictionary("entry", &entry_info));
}
IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, SuggestedFullPath) {
TestCase test_case(base::FilePath("some_file_name.txt"),
std::vector<std::string>(),
false,
base::FilePath());
scoped_refptr<FileBrowserHandlerInternalSelectFileFunction>
select_file_function(
new FileBrowserHandlerInternalSelectFileFunction(
new MockFileSelectorFactory(test_case),
false));
select_file_function->set_has_callback(true);
select_file_function->set_user_gesture(true);
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
select_file_function.get(),
"[{\"suggestedName\": \"/path_to_file/some_file_name.txt\"}]",
browser())));
EXPECT_FALSE(utils::GetBoolean(result.get(), "success"));
base::DictionaryValue* entry_info;
EXPECT_FALSE(result->GetDictionary("entry", &entry_info));
}
}