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 "base/auto_reset.h" | |
| 5 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 8 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 9 #include "base/safe_numerics.h" | 10 #include "base/safe_numerics.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/apps/app_browsertest_util.h" | 13 #include "chrome/browser/apps/app_browsertest_util.h" |
| 13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 PlatformAppBrowserTest::TearDownOnMainThread(); | 76 PlatformAppBrowserTest::TearDownOnMainThread(); |
| 76 } | 77 } |
| 77 | 78 |
| 78 bool RunMediaGalleriesTest(const std::string& extension_name) { | 79 bool RunMediaGalleriesTest(const std::string& extension_name) { |
| 79 base::ListValue empty_list_value; | 80 base::ListValue empty_list_value; |
| 80 return RunMediaGalleriesTestWithArg(extension_name, empty_list_value); | 81 return RunMediaGalleriesTestWithArg(extension_name, empty_list_value); |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool RunMediaGalleriesTestWithArg(const std::string& extension_name, | 84 bool RunMediaGalleriesTestWithArg(const std::string& extension_name, |
| 84 const base::ListValue& custom_arg_value) { | 85 const base::ListValue& custom_arg_value) { |
| 86 const char kTestDir[] = "api_test/media_galleries/"; | |
| 87 base::FilePath from_dir = | |
| 88 test_data_dir_.AppendASCII(kTestDir + extension_name); | |
| 89 base::ScopedTempDir temp_dir; | |
| 90 if (!temp_dir.CreateUniqueTempDir()) | |
| 91 return false; | |
| 92 | |
| 93 if (!base::CopyDirectory(from_dir, temp_dir.path(), true)) | |
| 94 return false; | |
| 95 | |
| 96 base::FilePath common_js_path(GetCommonDataDir().AppendASCII("common.js")); | |
| 97 base::FilePath inject_js_path(temp_dir.path().AppendASCII(extension_name) | |
| 98 .AppendASCII("common.js")); | |
| 99 if (!base::CopyFile(common_js_path, inject_js_path)) | |
| 100 return false; | |
| 101 | |
| 85 const char* custom_arg = NULL; | 102 const char* custom_arg = NULL; |
| 86 std::string json_string; | 103 std::string json_string; |
| 87 if (!custom_arg_value.empty()) { | 104 if (!custom_arg_value.empty()) { |
| 88 base::JSONWriter::Write(&custom_arg_value, &json_string); | 105 base::JSONWriter::Write(&custom_arg_value, &json_string); |
| 89 custom_arg = json_string.c_str(); | 106 custom_arg = json_string.c_str(); |
| 90 } | 107 } |
| 91 | 108 |
| 92 const char kTestDir[] = "api_test/media_galleries/"; | 109 base::AutoReset<base::FilePath> reset(&test_data_dir_, temp_dir.path()); |
|
tommycli
2013/09/24 23:26:10
This override and the above copying logic probably
Lei Zhang
2013/09/24 23:41:57
I would like to just make common.js available to a
| |
| 93 return RunPlatformAppTestWithArg(kTestDir + extension_name, custom_arg); | 110 return RunPlatformAppTestWithArg(extension_name, custom_arg); |
| 94 } | 111 } |
| 95 | 112 |
| 96 void AttachFakeDevice() { | 113 void AttachFakeDevice() { |
| 97 device_id_ = StorageInfo::MakeDeviceId( | 114 device_id_ = StorageInfo::MakeDeviceId( |
| 98 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId); | 115 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId); |
| 99 | 116 |
| 100 StorageMonitor::GetInstance()->receiver()->ProcessAttach( | 117 StorageMonitor::GetInstance()->receiver()->ProcessAttach( |
| 101 StorageInfo(device_id_, base::string16(), kDevicePath, | 118 StorageInfo(device_id_, base::string16(), kDevicePath, |
| 102 ASCIIToUTF16(kDeviceName), base::string16(), | 119 ASCIIToUTF16(kDeviceName), base::string16(), |
| 103 base::string16(), 0)); | 120 base::string16(), 0)); |
| 104 content::RunAllPendingInMessageLoop(); | 121 content::RunAllPendingInMessageLoop(); |
| 105 } | 122 } |
| 106 | 123 |
| 107 void DetachFakeDevice() { | 124 void DetachFakeDevice() { |
| 108 StorageMonitor::GetInstance()->receiver()->ProcessDetach(device_id_); | 125 StorageMonitor::GetInstance()->receiver()->ProcessDetach(device_id_); |
| 109 content::RunAllPendingInMessageLoop(); | 126 content::RunAllPendingInMessageLoop(); |
| 110 } | 127 } |
| 111 | 128 |
| 112 void PopulatePicturesDirectoryTestData() { | 129 void PopulatePicturesDirectoryTestData() { |
| 113 if (ensure_media_directories_exist_->num_galleries() == 0) | 130 if (ensure_media_directories_exist_->num_galleries() == 0) |
| 114 return; | 131 return; |
| 115 | 132 |
| 116 base::FilePath test_data_path = | 133 base::FilePath test_data_path(GetCommonDataDir()); |
| 117 test_data_dir_.AppendASCII("api_test") | |
| 118 .AppendASCII("media_galleries") | |
| 119 .AppendASCII("common"); | |
| 120 base::FilePath write_path; | 134 base::FilePath write_path; |
| 121 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path)); | 135 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path)); |
| 122 | 136 |
| 123 // Valid file, should show up in JS as a FileEntry. | 137 // Valid file, should show up in JS as a FileEntry. |
| 124 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"), | 138 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"), |
| 125 write_path.AppendASCII("test.jpg"))); | 139 write_path.AppendASCII("test.jpg"))); |
| 126 | 140 |
| 127 // Invalid file, should not show up as a FileEntry in JS at all. | 141 // Invalid file, should not show up as a FileEntry in JS at all. |
| 128 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"), | 142 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"), |
| 129 write_path.AppendASCII("test.txt"))); | 143 write_path.AppendASCII("test.txt"))); |
| 130 | 144 |
| 131 int64 file_size; | 145 int64 file_size; |
| 132 ASSERT_TRUE(file_util::GetFileSize(test_data_path.AppendASCII("test.jpg"), | 146 ASSERT_TRUE(file_util::GetFileSize(test_data_path.AppendASCII("test.jpg"), |
| 133 &file_size)); | 147 &file_size)); |
| 134 test_jpg_size_ = base::checked_numeric_cast<int>(file_size); | 148 test_jpg_size_ = base::checked_numeric_cast<int>(file_size); |
| 135 } | 149 } |
| 136 | 150 |
| 151 base::FilePath GetCommonDataDir() const { | |
| 152 return test_data_dir_.AppendASCII("api_test") | |
| 153 .AppendASCII("media_galleries") | |
| 154 .AppendASCII("common"); | |
| 155 } | |
| 156 | |
| 137 int num_galleries() const { | 157 int num_galleries() const { |
| 138 return ensure_media_directories_exist_->num_galleries(); | 158 return ensure_media_directories_exist_->num_galleries(); |
| 139 } | 159 } |
| 140 | 160 |
| 141 int test_jpg_size() const { return test_jpg_size_; } | 161 int test_jpg_size() const { return test_jpg_size_; } |
| 142 | 162 |
| 143 private: | 163 private: |
| 144 std::string device_id_; | 164 std::string device_id_; |
| 145 int test_jpg_size_; | 165 int test_jpg_size_; |
| 146 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_; | 166 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_; |
| 147 }; | 167 }; |
| 148 | 168 |
| 149 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 169 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
| 150 MediaGalleriesNoAccess) { | 170 MediaGalleriesNoAccess) { |
| 171 base::ScopedTempDir temp_dir; | |
| 172 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 173 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); | |
| 174 | |
| 151 base::ListValue custom_args; | 175 base::ListValue custom_args; |
| 152 custom_args.AppendInteger(num_galleries()); | 176 custom_args.AppendInteger(num_galleries() + 1); |
| 153 | 177 |
| 154 ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", custom_args)) | 178 ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", custom_args)) |
| 155 << message_; | 179 << message_; |
| 156 } | 180 } |
| 157 | 181 |
| 158 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) { | 182 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) { |
| 159 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_; | 183 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_; |
| 160 } | 184 } |
| 161 | 185 |
| 162 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 186 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 176 | 200 |
| 177 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 201 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
| 178 MediaGalleriesCopyTo) { | 202 MediaGalleriesCopyTo) { |
| 179 base::ScopedTempDir temp_dir; | 203 base::ScopedTempDir temp_dir; |
| 180 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 204 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 181 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); | 205 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); |
| 182 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_; | 206 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_; |
| 183 } | 207 } |
| 184 | 208 |
| 185 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 209 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
| 186 MediaGalleriesCopyToNoAccess) { | |
| 187 base::ScopedTempDir temp_dir; | |
| 188 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 189 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); | |
| 190 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access/no_access")) << message_; | |
| 191 } | |
| 192 | |
| 193 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | |
| 194 MediaGalleriesAccessAttached) { | 210 MediaGalleriesAccessAttached) { |
| 195 AttachFakeDevice(); | 211 AttachFakeDevice(); |
| 196 | 212 |
| 197 base::ListValue custom_args; | 213 base::ListValue custom_args; |
| 198 custom_args.AppendInteger(num_galleries() + 1); | 214 custom_args.AppendInteger(num_galleries() + 1); |
| 199 custom_args.AppendString(kDeviceName); | 215 custom_args.AppendString(kDeviceName); |
| 200 | 216 |
| 201 ASSERT_TRUE(RunMediaGalleriesTestWithArg("access_attached", custom_args)) | 217 ASSERT_TRUE(RunMediaGalleriesTestWithArg("access_attached", custom_args)) |
| 202 << message_; | 218 << message_; |
| 203 | 219 |
| 204 DetachFakeDevice(); | 220 DetachFakeDevice(); |
| 205 } | 221 } |
| 206 | 222 |
| 207 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 223 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
| 208 GetFilesystemMetadata) { | 224 GetFilesystemMetadata) { |
| 209 ASSERT_TRUE(RunMediaGalleriesTest("metadata")) << message_; | 225 ASSERT_TRUE(RunMediaGalleriesTest("metadata")) << message_; |
| 210 } | 226 } |
| OLD | NEW |