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/bind.h" | 5 #include "base/file_util.h" |
6 #include "base/bind_helpers.h" | |
7 #include "base/command_line.h" | |
8 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/json/json_writer.h" |
9 #include "base/path_service.h" | 8 #include "base/path_service.h" |
10 #include "base/strings/stringprintf.h" | 9 #include "base/safe_numerics.h" |
11 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" |
12 #include "chrome/browser/apps/app_browsertest_util.h" | 12 #include "chrome/browser/apps/app_browsertest_util.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/extensions/extension_apitest.h" | |
15 #include "chrome/browser/extensions/extension_process_manager.h" | |
16 #include "chrome/browser/extensions/extension_system.h" | 14 #include "chrome/browser/extensions/extension_system.h" |
17 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 15 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
18 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 16 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
19 #include "chrome/browser/media_galleries/media_galleries_test_util.h" | 17 #include "chrome/browser/media_galleries/media_galleries_test_util.h" |
20 #include "chrome/browser/storage_monitor/storage_info.h" | 18 #include "chrome/browser/storage_monitor/storage_info.h" |
21 #include "chrome/browser/storage_monitor/storage_monitor.h" | 19 #include "chrome/browser/storage_monitor/storage_monitor.h" |
22 #include "chrome/browser/ui/browser.h" | |
23 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
24 #include "content/public/browser/render_view_host.h" | |
25 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
26 #include "extensions/common/switches.h" | |
27 | 22 |
28 using extensions::PlatformAppBrowserTest; | 23 using extensions::PlatformAppBrowserTest; |
29 | 24 |
30 namespace { | 25 namespace { |
31 | 26 |
32 // Dummy device properties. | 27 // Dummy device properties. |
33 const char kDeviceId[] = "testDeviceId"; | 28 const char kDeviceId[] = "testDeviceId"; |
34 const char kDeviceName[] = "foobar"; | 29 const char kDeviceName[] = "foobar"; |
35 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | 30 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
36 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("C:\\qux"); | 31 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("C:\\qux"); |
37 #else | 32 #else |
38 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux"); | 33 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux"); |
39 #endif | 34 #endif |
40 | 35 |
41 const char kTestGalleries[] = "testGalleries(%d)"; | |
42 | |
43 } // namespace | 36 } // namespace |
44 | 37 |
45 // This function is to ensure at least one (fake) media gallery exists for | 38 // This function is to ensure at least one (fake) media gallery exists for |
46 // testing platforms with no default media galleries, such as CHROMEOS. | 39 // testing platforms with no default media galleries, such as CHROMEOS. |
47 void MakeFakeMediaGalleryForTest(Profile* profile, const base::FilePath& path) { | 40 void MakeFakeMediaGalleryForTest(Profile* profile, const base::FilePath& path) { |
48 base::RunLoop runloop; | 41 base::RunLoop runloop; |
49 StorageMonitor::GetInstance()->EnsureInitialized(runloop.QuitClosure()); | 42 StorageMonitor::GetInstance()->EnsureInitialized(runloop.QuitClosure()); |
50 runloop.Run(); | 43 runloop.Run(); |
51 | 44 |
52 MediaGalleriesPreferences* preferences = | 45 MediaGalleriesPreferences* preferences = |
53 g_browser_process->media_file_system_registry()->GetPreferences(profile); | 46 g_browser_process->media_file_system_registry()->GetPreferences(profile); |
54 | 47 |
55 MediaGalleryPrefInfo gallery_info; | 48 MediaGalleryPrefInfo gallery_info; |
56 ASSERT_FALSE(preferences->LookUpGalleryByPath(path, &gallery_info)); | 49 ASSERT_FALSE(preferences->LookUpGalleryByPath(path, &gallery_info)); |
57 preferences->AddGallery(gallery_info.device_id, | 50 preferences->AddGallery(gallery_info.device_id, |
58 gallery_info.path, | 51 gallery_info.path, |
59 false /* user_added */, | 52 false /* user_added */, |
60 gallery_info.volume_label, | 53 gallery_info.volume_label, |
61 gallery_info.vendor_name, | 54 gallery_info.vendor_name, |
62 gallery_info.model_name, | 55 gallery_info.model_name, |
63 gallery_info.total_size_in_bytes, | 56 gallery_info.total_size_in_bytes, |
64 gallery_info.last_attach_time); | 57 gallery_info.last_attach_time); |
65 | 58 |
66 content::RunAllPendingInMessageLoop(); | 59 content::RunAllPendingInMessageLoop(); |
67 } | 60 } |
68 | 61 |
69 class MediaGalleriesPlatformAppBrowserTest : public PlatformAppBrowserTest { | 62 class MediaGalleriesPlatformAppBrowserTest : public PlatformAppBrowserTest { |
70 protected: | 63 protected: |
| 64 MediaGalleriesPlatformAppBrowserTest() : test_jpg_size_(0) {} |
| 65 virtual ~MediaGalleriesPlatformAppBrowserTest() {} |
| 66 |
71 virtual void SetUpOnMainThread() OVERRIDE { | 67 virtual void SetUpOnMainThread() OVERRIDE { |
72 PlatformAppBrowserTest::SetUpOnMainThread(); | 68 PlatformAppBrowserTest::SetUpOnMainThread(); |
73 ensure_media_directories_exist_.reset(new EnsureMediaDirectoriesExists); | 69 ensure_media_directories_exist_.reset(new EnsureMediaDirectoriesExists); |
74 PopulatePicturesDirectoryTestData(); | 70 PopulatePicturesDirectoryTestData(); |
75 } | 71 } |
76 | 72 |
77 virtual void TearDownOnMainThread() OVERRIDE { | 73 virtual void TearDownOnMainThread() OVERRIDE { |
78 ensure_media_directories_exist_.reset(); | 74 ensure_media_directories_exist_.reset(); |
79 PlatformAppBrowserTest::TearDownOnMainThread(); | 75 PlatformAppBrowserTest::TearDownOnMainThread(); |
80 } | 76 } |
81 | 77 |
82 // Since ExtensionTestMessageListener does not work with RunPlatformAppTest(), | 78 bool RunMediaGalleriesTest(const std::string& extension_name) { |
83 // This helper method can be used to run additional media gallery tests. | 79 base::ListValue empty_list_value; |
84 void RunSecondTestPhase(const std::string& command) { | 80 return RunMediaGalleriesTestWithArg(extension_name, empty_list_value); |
85 const extensions::Extension* extension = GetSingleLoadedExtension(); | 81 } |
86 extensions::ExtensionHost* host = | |
87 extensions::ExtensionSystem::Get(browser()->profile())-> | |
88 process_manager()->GetBackgroundHostForExtension(extension->id()); | |
89 ASSERT_TRUE(host); | |
90 | 82 |
91 ResultCatcher catcher; | 83 bool RunMediaGalleriesTestWithArg(const std::string& extension_name, |
92 host->render_view_host()->ExecuteJavascriptInWebFrame( | 84 const base::ListValue& custom_arg_value) { |
93 base::string16(), base::UTF8ToUTF16(command)); | 85 const char* custom_arg = NULL; |
94 EXPECT_TRUE(catcher.GetNextResult()) << message_; | 86 std::string json_string; |
| 87 if (!custom_arg_value.empty()) { |
| 88 base::JSONWriter::Write(&custom_arg_value, &json_string); |
| 89 custom_arg = json_string.c_str(); |
| 90 } |
| 91 |
| 92 const char kTestDir[] = "api_test/media_galleries/"; |
| 93 return RunPlatformAppTestWithArg(kTestDir + extension_name, custom_arg); |
95 } | 94 } |
96 | 95 |
97 void AttachFakeDevice() { | 96 void AttachFakeDevice() { |
98 device_id_ = StorageInfo::MakeDeviceId( | 97 device_id_ = StorageInfo::MakeDeviceId( |
99 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId); | 98 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId); |
100 | 99 |
101 StorageMonitor::GetInstance()->receiver()->ProcessAttach( | 100 StorageMonitor::GetInstance()->receiver()->ProcessAttach( |
102 StorageInfo(device_id_, base::string16(), kDevicePath, | 101 StorageInfo(device_id_, base::string16(), kDevicePath, |
103 ASCIIToUTF16(kDeviceName), base::string16(), | 102 ASCIIToUTF16(kDeviceName), base::string16(), |
104 base::string16(), 0)); | 103 base::string16(), 0)); |
(...skipping 16 matching lines...) Expand all Loading... |
121 base::FilePath write_path; | 120 base::FilePath write_path; |
122 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path)); | 121 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path)); |
123 | 122 |
124 // Valid file, should show up in JS as a FileEntry. | 123 // Valid file, should show up in JS as a FileEntry. |
125 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"), | 124 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"), |
126 write_path.AppendASCII("test.jpg"))); | 125 write_path.AppendASCII("test.jpg"))); |
127 | 126 |
128 // Invalid file, should not show up as a FileEntry in JS at all. | 127 // Invalid file, should not show up as a FileEntry in JS at all. |
129 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"), | 128 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"), |
130 write_path.AppendASCII("test.txt"))); | 129 write_path.AppendASCII("test.txt"))); |
| 130 |
| 131 int64 file_size; |
| 132 ASSERT_TRUE(file_util::GetFileSize(test_data_path.AppendASCII("test.jpg"), |
| 133 &file_size)); |
| 134 test_jpg_size_ = base::checked_numeric_cast<int>(file_size); |
131 } | 135 } |
132 | 136 |
133 int num_galleries() const { | 137 int num_galleries() const { |
134 return ensure_media_directories_exist_->num_galleries(); | 138 return ensure_media_directories_exist_->num_galleries(); |
135 } | 139 } |
136 | 140 |
| 141 int test_jpg_size() const { return test_jpg_size_; } |
| 142 |
137 private: | 143 private: |
138 std::string device_id_; | 144 std::string device_id_; |
| 145 int test_jpg_size_; |
139 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_; | 146 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_; |
140 }; | 147 }; |
141 | 148 |
142 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 149 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
143 MediaGalleriesNoAccess) { | 150 MediaGalleriesNoAccess) { |
144 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_access")) | 151 base::ListValue custom_args; |
| 152 custom_args.AppendInteger(num_galleries()); |
| 153 |
| 154 ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", custom_args)) |
145 << message_; | 155 << message_; |
146 RunSecondTestPhase(base::StringPrintf(kTestGalleries, num_galleries())); | |
147 } | 156 } |
148 | 157 |
149 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) { | 158 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) { |
150 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_galleries")) | 159 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_; |
| 160 } |
| 161 |
| 162 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
| 163 NoGalleriesCopyTo) { |
| 164 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries_copy_to")) << message_; |
| 165 } |
| 166 |
| 167 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
| 168 MediaGalleriesRead) { |
| 169 base::ListValue custom_args; |
| 170 custom_args.AppendInteger(num_galleries()); |
| 171 custom_args.AppendInteger(test_jpg_size()); |
| 172 |
| 173 ASSERT_TRUE(RunMediaGalleriesTestWithArg("read_access", custom_args)) |
151 << message_; | 174 << message_; |
152 } | 175 } |
153 | 176 |
154 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 177 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
155 NoGalleriesCopyTo) { | |
156 ASSERT_TRUE(RunPlatformAppTest( | |
157 "api_test/media_galleries/no_galleries_copy_to")) << message_; | |
158 } | |
159 | |
160 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | |
161 MediaGalleriesRead) { | |
162 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/read_access")) | |
163 << message_; | |
164 RunSecondTestPhase(base::StringPrintf(kTestGalleries, num_galleries())); | |
165 } | |
166 | |
167 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | |
168 MediaGalleriesCopyTo) { | 178 MediaGalleriesCopyTo) { |
169 base::ScopedTempDir temp_dir; | 179 base::ScopedTempDir temp_dir; |
170 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 180 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
171 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); | 181 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); |
172 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/copy_to_access")) | 182 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_; |
173 << message_; | |
174 } | 183 } |
175 | 184 |
176 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 185 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
177 MediaGalleriesCopyToNoAccess) { | 186 MediaGalleriesCopyToNoAccess) { |
178 base::ScopedTempDir temp_dir; | 187 base::ScopedTempDir temp_dir; |
179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 188 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
180 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); | 189 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); |
181 ASSERT_TRUE(RunPlatformAppTest( | 190 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access/no_access")) << message_; |
182 "api_test/media_galleries/copy_to_access/no_access")) | |
183 << message_; | |
184 } | 191 } |
185 | 192 |
186 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 193 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
187 MediaGalleriesAccessAttached) { | 194 MediaGalleriesAccessAttached) { |
188 AttachFakeDevice(); | 195 AttachFakeDevice(); |
189 | 196 |
190 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/access_attached")) | 197 base::ListValue custom_args; |
| 198 custom_args.AppendInteger(num_galleries() + 1); |
| 199 custom_args.AppendString(kDeviceName); |
| 200 |
| 201 ASSERT_TRUE(RunMediaGalleriesTestWithArg("access_attached", custom_args)) |
191 << message_; | 202 << message_; |
192 | 203 |
193 RunSecondTestPhase(base::StringPrintf( | |
194 "testGalleries(%d, \"%s\")", num_galleries() + 1, kDeviceName)); | |
195 | |
196 DetachFakeDevice(); | 204 DetachFakeDevice(); |
197 } | 205 } |
198 | 206 |
199 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, | 207 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, |
200 GetFilesystemMetadata) { | 208 GetFilesystemMetadata) { |
201 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/metadata")) | 209 ASSERT_TRUE(RunMediaGalleriesTest("metadata")) << message_; |
202 << message_; | |
203 } | 210 } |
OLD | NEW |