Chromium Code Reviews| 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 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/macros.h" | 13 #include "base/macros.h" |
| 12 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 13 #include "base/process/launch.h" | 15 #include "base/process/launch.h" |
| 14 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 15 #include "chrome/browser/file_select_helper.h" | 17 #include "chrome/browser/file_select_helper.h" |
| 16 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/test/base/testing_profile.h" | |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 22 |
| 23 using content::FileChooserParams; | |
| 24 | |
| 19 class FileSelectHelperTest : public testing::Test { | 25 class FileSelectHelperTest : public testing::Test { |
| 20 public: | 26 public: |
| 21 FileSelectHelperTest() {} | 27 FileSelectHelperTest() {} |
| 22 | 28 |
| 23 protected: | 29 protected: |
| 24 void SetUp() override { | 30 void SetUp() override { |
| 25 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | 31 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
| 26 data_dir_ = data_dir_.AppendASCII("file_select_helper"); | 32 data_dir_ = data_dir_.AppendASCII("file_select_helper"); |
| 27 ASSERT_TRUE(base::PathExists(data_dir_)); | 33 ASSERT_TRUE(base::PathExists(data_dir_)); |
| 28 } | 34 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 base::FilePath::CharType kBadName[] = {0xe3, 0x81, 0x81, 0x81, 0x82, 0}; | 112 base::FilePath::CharType kBadName[] = {0xe3, 0x81, 0x81, 0x81, 0x82, 0}; |
| 107 #endif | 113 #endif |
| 108 base::FilePath bad_filename(kBadName); | 114 base::FilePath bad_filename(kBadName); |
| 109 ASSERT_FALSE(bad_filename.empty()); | 115 ASSERT_FALSE(bad_filename.empty()); |
| 110 // The only thing we are testing is that if the source filename was non-empty, | 116 // The only thing we are testing is that if the source filename was non-empty, |
| 111 // the resulting filename is also not empty. Invalid encoded filenames can | 117 // the resulting filename is also not empty. Invalid encoded filenames can |
| 112 // cause conversions to fail. Such failures shouldn't cause the resulting | 118 // cause conversions to fail. Such failures shouldn't cause the resulting |
| 113 // filename to disappear. | 119 // filename to disappear. |
| 114 EXPECT_FALSE(FileSelectHelper::GetSanitizedFileName(bad_filename).empty()); | 120 EXPECT_FALSE(FileSelectHelper::GetSanitizedFileName(bad_filename).empty()); |
| 115 } | 121 } |
| 122 | |
| 123 TEST_F(FileSelectHelperTest, LastSelectedDirectory) { | |
| 124 content::TestBrowserThreadBundle browser_thread_bundle; | |
| 125 TestingProfile profile; | |
| 126 scoped_refptr<FileSelectHelper> file_select_helper = | |
| 127 new FileSelectHelper(&profile); | |
| 128 | |
| 129 const int index = 0; | |
| 130 void* params = nullptr; | |
| 131 | |
| 132 const base::FilePath dir_path_1 = data_dir_.AppendASCII("dir1"); | |
| 133 const base::FilePath dir_path_2 = data_dir_.AppendASCII("dir2"); | |
| 134 const base::FilePath file_path_1 = dir_path_1.AppendASCII("file1.txt"); | |
| 135 const base::FilePath file_path_2 = dir_path_2.AppendASCII("file2.txt"); | |
|
nasko
2016/10/27 22:58:24
Shouldn't the two files be in the same directory?
jsbell
2016/10/28 00:21:03
Hrm, good point. Doesn't really matter for the tes
| |
| 136 std::vector<base::FilePath> files; | |
| 137 files.push_back(file_path_1); | |
| 138 files.push_back(file_path_2); | |
| 139 std::vector<base::FilePath> dirs; | |
| 140 dirs.push_back(dir_path_1); | |
| 141 dirs.push_back(dir_path_2); | |
| 142 | |
| 143 // Modes where the parent of the selection is remembered. | |
| 144 const std::vector<FileChooserParams::Mode> modes = { | |
| 145 FileChooserParams::Open, | |
| 146 FileChooserParams::OpenMultiple, | |
| 147 FileChooserParams::Save, | |
| 148 }; | |
| 149 | |
| 150 for (const auto& mode : modes) { | |
| 151 file_select_helper->dialog_mode_ = mode; | |
| 152 | |
| 153 file_select_helper->AddRef(); // Normally called by RunFileChooser(). | |
| 154 file_select_helper->FileSelected(file_path_1, index, params); | |
| 155 EXPECT_EQ(dir_path_1, profile.last_selected_directory()); | |
| 156 | |
| 157 file_select_helper->AddRef(); // Normally called by RunFileChooser(). | |
| 158 file_select_helper->FileSelected(file_path_2, index, params); | |
| 159 EXPECT_EQ(dir_path_2, profile.last_selected_directory()); | |
| 160 | |
| 161 file_select_helper->AddRef(); // Normally called by RunFileChooser(). | |
| 162 file_select_helper->MultiFilesSelected(files, params); | |
| 163 EXPECT_EQ(dir_path_1, profile.last_selected_directory()); | |
| 164 } | |
| 165 | |
| 166 // Type where the selected folder itself is remembered. | |
| 167 file_select_helper->dialog_mode_ = FileChooserParams::UploadFolder; | |
| 168 | |
| 169 file_select_helper->AddRef(); // Normally called by RunFileChooser(). | |
| 170 file_select_helper->FileSelected(dir_path_1, index, params); | |
| 171 EXPECT_EQ(dir_path_1, profile.last_selected_directory()); | |
| 172 | |
| 173 file_select_helper->AddRef(); // Normally called by RunFileChooser(). | |
| 174 file_select_helper->FileSelected(dir_path_2, index, params); | |
| 175 EXPECT_EQ(dir_path_2, profile.last_selected_directory()); | |
| 176 | |
| 177 file_select_helper->AddRef(); // Normally called by RunFileChooser(). | |
| 178 file_select_helper->MultiFilesSelected(dirs, params); | |
| 179 EXPECT_EQ(dir_path_1, profile.last_selected_directory()); | |
| 180 } | |
| OLD | NEW |