| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // File contains browser tests for the fileBrowserHandler api. | 5 // File contains browser tests for the fileBrowserHandler api. |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/extensions/file_browser_handler_api.h" | 7 #include "chrome/browser/chromeos/extensions/file_browser_handler_api.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 // Checks that file under path |selected_path| contains |expected_contents|. | 58 // Checks that file under path |selected_path| contains |expected_contents|. |
| 59 // Must be called on the file thread. | 59 // Must be called on the file thread. |
| 60 void ExpectFileContentEquals(const FilePath& selected_path, | 60 void ExpectFileContentEquals(const FilePath& selected_path, |
| 61 const std::string& expected_contents) { | 61 const std::string& expected_contents) { |
| 62 std::string test_file_contents; | 62 std::string test_file_contents; |
| 63 ASSERT_TRUE(file_util::ReadFileToString(selected_path, &test_file_contents)); | 63 ASSERT_TRUE(file_util::ReadFileToString(selected_path, &test_file_contents)); |
| 64 EXPECT_EQ(expected_contents, test_file_contents); | 64 EXPECT_EQ(expected_contents, test_file_contents); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Mocks FileSelector used by FileHandlerSelectFileFunction. | 67 // Mocks FileSelector used by FileBrowserHandlerInternalSelectFileFunction. |
| 68 // When |SelectFile| is called, it will check that file name suggestion is as | 68 // When |SelectFile| is called, it will check that file name suggestion is as |
| 69 // expected, and respond to the extension function with specified selection | 69 // expected, and respond to the extension function with specified selection |
| 70 // results. | 70 // results. |
| 71 class MockFileSelector : public file_handler::FileSelector { | 71 class MockFileSelector : public file_handler::FileSelector { |
| 72 public: | 72 public: |
| 73 MockFileSelector(const FilePath& suggested_name, | 73 MockFileSelector(const FilePath& suggested_name, |
| 74 const std::vector<std::string>& allowed_extensions, | 74 const std::vector<std::string>& allowed_extensions, |
| 75 bool success, | 75 bool success, |
| 76 const FilePath& selected_path) | 76 const FilePath& selected_path) |
| 77 : suggested_name_(suggested_name), | 77 : suggested_name_(suggested_name), |
| 78 allowed_extensions_(allowed_extensions), | 78 allowed_extensions_(allowed_extensions), |
| 79 success_(success), | 79 success_(success), |
| 80 selected_path_(selected_path) { | 80 selected_path_(selected_path) { |
| 81 } | 81 } |
| 82 virtual ~MockFileSelector() {} | 82 virtual ~MockFileSelector() {} |
| 83 | 83 |
| 84 // file_handler::FileSelector implementation. | 84 // file_handler::FileSelector implementation. |
| 85 // |browser| is not used. | 85 // |browser| is not used. |
| 86 virtual void SelectFile(const FilePath& suggested_name, | 86 virtual void SelectFile( |
| 87 const std::vector<std::string>& allowed_extensions, | 87 const FilePath& suggested_name, |
| 88 Browser* browser, | 88 const std::vector<std::string>& allowed_extensions, |
| 89 FileHandlerSelectFileFunction* function) OVERRIDE { | 89 Browser* browser, |
| 90 FileBrowserHandlerInternalSelectFileFunction* function) OVERRIDE { |
| 90 // Confirm that the function suggested us the right name. | 91 // Confirm that the function suggested us the right name. |
| 91 EXPECT_EQ(suggested_name_, suggested_name); | 92 EXPECT_EQ(suggested_name_, suggested_name); |
| 92 // Confirm that the function allowed us the right extensions. | 93 // Confirm that the function allowed us the right extensions. |
| 93 EXPECT_EQ(allowed_extensions_.size(), allowed_extensions.size()); | 94 EXPECT_EQ(allowed_extensions_.size(), allowed_extensions.size()); |
| 94 if (allowed_extensions_.size() == allowed_extensions.size()) { | 95 if (allowed_extensions_.size() == allowed_extensions.size()) { |
| 95 for (size_t i = 0; i < allowed_extensions_.size(); ++i) { | 96 for (size_t i = 0; i < allowed_extensions_.size(); ++i) { |
| 96 EXPECT_EQ(allowed_extensions_[i], allowed_extensions[i]); | 97 EXPECT_EQ(allowed_extensions_[i], allowed_extensions[i]); |
| 97 } | 98 } |
| 98 } | 99 } |
| 99 | 100 |
| 100 // Send response to the extension function. | 101 // Send response to the extension function. |
| 101 // The callback will take a reference to the function and keep it alive. | 102 // The callback will take a reference to the function and keep it alive. |
| 102 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 103 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 103 base::Bind(&FileHandlerSelectFileFunction::OnFilePathSelected, | 104 base::Bind(&FileBrowserHandlerInternalSelectFileFunction:: |
| 104 function, success_, selected_path_)); | 105 OnFilePathSelected, |
| 106 function, success_, selected_path_)); |
| 105 delete this; | 107 delete this; |
| 106 } | 108 } |
| 107 | 109 |
| 108 private: | 110 private: |
| 109 // File name that is expected to be suggested by the function. | 111 // File name that is expected to be suggested by the function. |
| 110 FilePath suggested_name_; | 112 FilePath suggested_name_; |
| 111 | 113 |
| 112 // Extensions that is expected to be allowed by the function. | 114 // Extensions that is expected to be allowed by the function. |
| 113 std::vector<std::string> allowed_extensions_; | 115 std::vector<std::string> allowed_extensions_; |
| 114 | 116 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 fileapi::ExternalFileSystemMountPointProvider* provider = | 177 fileapi::ExternalFileSystemMountPointProvider* provider = |
| 176 BrowserContext::GetDefaultStoragePartition(browser()->profile())-> | 178 BrowserContext::GetDefaultStoragePartition(browser()->profile())-> |
| 177 GetFileSystemContext()->external_provider(); | 179 GetFileSystemContext()->external_provider(); |
| 178 provider->AddLocalMountPoint(tmp_mount_point_); | 180 provider->AddLocalMountPoint(tmp_mount_point_); |
| 179 } | 181 } |
| 180 | 182 |
| 181 FilePath GetFullPathOnTmpMountPoint(const FilePath& relative_path) { | 183 FilePath GetFullPathOnTmpMountPoint(const FilePath& relative_path) { |
| 182 return tmp_mount_point_.Append(relative_path); | 184 return tmp_mount_point_.Append(relative_path); |
| 183 } | 185 } |
| 184 | 186 |
| 185 // Creates a new FileHandlerSelectFileFunction to be used in the test. | 187 // Creates a new FileBrowserHandlerInternalSelectFileFunction to be used in |
| 186 // This function will be called from ExtensionFunctinoDispatcher whenever | 188 // the test. This function will be called from ExtensionFunctinoDispatcher |
| 187 // an extension function for fileBrowserHandlerInternal.selectFile will be | 189 // whenever an extension function for fileBrowserHandlerInternal.selectFile |
| 188 // needed. | 190 // will be needed. |
| 189 static ExtensionFunction* TestSelectFileFunctionFactory() { | 191 static ExtensionFunction* TestSelectFileFunctionFactory() { |
| 190 EXPECT_TRUE(test_cases_); | 192 EXPECT_TRUE(test_cases_); |
| 191 EXPECT_TRUE(current_test_case_ < test_cases_->size()); | 193 EXPECT_TRUE(current_test_case_ < test_cases_->size()); |
| 192 | 194 |
| 193 // If this happens, test failed. But, we still don't want to crash, so | 195 // If this happens, test failed. But, we still don't want to crash, so |
| 194 // return valid extension function. | 196 // return valid extension function. |
| 195 if (!test_cases_ && current_test_case_ >= test_cases_->size()) | 197 if (!test_cases_ && current_test_case_ >= test_cases_->size()) |
| 196 return new FileHandlerSelectFileFunction(); | 198 return new FileBrowserHandlerInternalSelectFileFunction(); |
| 197 | 199 |
| 198 // Create file creator factory for the current test case. | 200 // Create file creator factory for the current test case. |
| 199 MockFileSelectorFactory* mock_factory = | 201 MockFileSelectorFactory* mock_factory = |
| 200 new MockFileSelectorFactory(test_cases_->at(current_test_case_)); | 202 new MockFileSelectorFactory(test_cases_->at(current_test_case_)); |
| 201 current_test_case_++; | 203 current_test_case_++; |
| 202 | 204 |
| 203 return new FileHandlerSelectFileFunction(mock_factory, false); | 205 return new FileBrowserHandlerInternalSelectFileFunction( |
| 206 mock_factory, false); |
| 204 } | 207 } |
| 205 | 208 |
| 206 // Sets up test parameters for extension function invocations that will be | 209 // Sets up test parameters for extension function invocations that will be |
| 207 // made during the test. | 210 // made during the test. |
| 208 void SetTestCases(const std::vector<TestCase>* test_cases) { | 211 void SetTestCases(const std::vector<TestCase>* test_cases) { |
| 209 test_cases_ = test_cases; | 212 test_cases_ = test_cases; |
| 210 current_test_case_ = 0; | 213 current_test_case_ = 0; |
| 211 } | 214 } |
| 212 | 215 |
| 213 private: | 216 private: |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 // Make sure test doesn't finish until we check on file thread that the | 285 // Make sure test doesn't finish until we check on file thread that the |
| 283 // selected file's content is as expected. | 286 // selected file's content is as expected. |
| 284 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 287 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
| 285 | 288 |
| 286 SetTestCases(NULL); | 289 SetTestCases(NULL); |
| 287 } | 290 } |
| 288 | 291 |
| 289 // Tests that verifies the fileBrowserHandlerInternal.selectFile function fails | 292 // Tests that verifies the fileBrowserHandlerInternal.selectFile function fails |
| 290 // when invoked without user gesture. | 293 // when invoked without user gesture. |
| 291 IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, NoUserGesture) { | 294 IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, NoUserGesture) { |
| 292 scoped_refptr<FileHandlerSelectFileFunction> select_file_function( | 295 scoped_refptr<FileBrowserHandlerInternalSelectFileFunction> |
| 293 new FileHandlerSelectFileFunction()); | 296 select_file_function( |
| 297 new FileBrowserHandlerInternalSelectFileFunction()); |
| 294 | 298 |
| 295 std::string error = | 299 std::string error = |
| 296 utils::RunFunctionAndReturnError( | 300 utils::RunFunctionAndReturnError( |
| 297 select_file_function.get(), | 301 select_file_function.get(), |
| 298 "[{\"suggestedName\": \"foo\"}]", | 302 "[{\"suggestedName\": \"foo\"}]", |
| 299 browser()); | 303 browser()); |
| 300 | 304 |
| 301 const std::string expected_error = | 305 const std::string expected_error = |
| 302 "This method can only be called in response to user gesture, such as a " | 306 "This method can only be called in response to user gesture, such as a " |
| 303 "mouse click or key press."; | 307 "mouse click or key press."; |
| 304 EXPECT_EQ(expected_error, error); | 308 EXPECT_EQ(expected_error, error); |
| 305 } | 309 } |
| 306 | 310 |
| 307 // Tests that checks that the fileHandlerInternal.selectFile function returns | 311 // Tests that checks that the fileHandlerInternal.selectFile function returns |
| 308 // dictionary with |success == false| and no file entry when user cancels file | 312 // dictionary with |success == false| and no file entry when user cancels file |
| 309 // selection. | 313 // selection. |
| 310 IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, SelectionFailed) { | 314 IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, SelectionFailed) { |
| 311 TestCase test_case(FilePath("some_file_name.txt"), | 315 TestCase test_case(FilePath("some_file_name.txt"), |
| 312 std::vector<std::string>(), | 316 std::vector<std::string>(), |
| 313 false, | 317 false, |
| 314 FilePath()); | 318 FilePath()); |
| 315 | 319 |
| 316 scoped_refptr<FileHandlerSelectFileFunction> select_file_function( | 320 scoped_refptr<FileBrowserHandlerInternalSelectFileFunction> |
| 317 new FileHandlerSelectFileFunction(new MockFileSelectorFactory(test_case), | 321 select_file_function( |
| 318 false)); | 322 new FileBrowserHandlerInternalSelectFileFunction( |
| 323 new MockFileSelectorFactory(test_case), |
| 324 false)); |
| 319 | 325 |
| 320 select_file_function->set_has_callback(true); | 326 select_file_function->set_has_callback(true); |
| 321 select_file_function->set_user_gesture(true); | 327 select_file_function->set_user_gesture(true); |
| 322 | 328 |
| 323 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( | 329 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( |
| 324 utils::RunFunctionAndReturnSingleResult( | 330 utils::RunFunctionAndReturnSingleResult( |
| 325 select_file_function.get(), | 331 select_file_function.get(), |
| 326 "[{\"suggestedName\": \"some_file_name.txt\"}]", | 332 "[{\"suggestedName\": \"some_file_name.txt\"}]", |
| 327 browser()))); | 333 browser()))); |
| 328 | 334 |
| 329 EXPECT_FALSE(utils::GetBoolean(result.get(), "success")); | 335 EXPECT_FALSE(utils::GetBoolean(result.get(), "success")); |
| 330 DictionaryValue* entry_info; | 336 DictionaryValue* entry_info; |
| 331 EXPECT_FALSE(result->GetDictionary("entry", &entry_info)); | 337 EXPECT_FALSE(result->GetDictionary("entry", &entry_info)); |
| 332 } | 338 } |
| 333 | 339 |
| 334 // Tests that user cannot be suggested a full file path when selecting a file, | 340 // Tests that user cannot be suggested a full file path when selecting a file, |
| 335 // only a file name (i.e. that extension function caller has no influence on | 341 // only a file name (i.e. that extension function caller has no influence on |
| 336 // which directory contents will be initially displayed in selection dialog). | 342 // which directory contents will be initially displayed in selection dialog). |
| 337 IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, SuggestedFullPath) { | 343 IN_PROC_BROWSER_TEST_F(FileBrowserHandlerExtensionTest, SuggestedFullPath) { |
| 338 TestCase test_case(FilePath("some_file_name.txt"), | 344 TestCase test_case(FilePath("some_file_name.txt"), |
| 339 std::vector<std::string>(), | 345 std::vector<std::string>(), |
| 340 false, | 346 false, |
| 341 FilePath()); | 347 FilePath()); |
| 342 | 348 |
| 343 scoped_refptr<FileHandlerSelectFileFunction> select_file_function( | 349 scoped_refptr<FileBrowserHandlerInternalSelectFileFunction> |
| 344 new FileHandlerSelectFileFunction(new MockFileSelectorFactory(test_case), | 350 select_file_function( |
| 345 false)); | 351 new FileBrowserHandlerInternalSelectFileFunction( |
| 352 new MockFileSelectorFactory(test_case), |
| 353 false)); |
| 346 | 354 |
| 347 select_file_function->set_has_callback(true); | 355 select_file_function->set_has_callback(true); |
| 348 select_file_function->set_user_gesture(true); | 356 select_file_function->set_user_gesture(true); |
| 349 | 357 |
| 350 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( | 358 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( |
| 351 utils::RunFunctionAndReturnSingleResult( | 359 utils::RunFunctionAndReturnSingleResult( |
| 352 select_file_function.get(), | 360 select_file_function.get(), |
| 353 "[{\"suggestedName\": \"/path_to_file/some_file_name.txt\"}]", | 361 "[{\"suggestedName\": \"/path_to_file/some_file_name.txt\"}]", |
| 354 browser()))); | 362 browser()))); |
| 355 | 363 |
| 356 EXPECT_FALSE(utils::GetBoolean(result.get(), "success")); | 364 EXPECT_FALSE(utils::GetBoolean(result.get(), "success")); |
| 357 DictionaryValue* entry_info; | 365 DictionaryValue* entry_info; |
| 358 EXPECT_FALSE(result->GetDictionary("entry", &entry_info)); | 366 EXPECT_FALSE(result->GetDictionary("entry", &entry_info)); |
| 359 } | 367 } |
| 360 | 368 |
| 361 } // namespace | 369 } // namespace |
| OLD | NEW |