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