- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / media_galleries / media_galleries_apitest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/auto_reset.h"
6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/json/json_writer.h"
9 #include "base/path_service.h"
10 #include "base/safe_numerics.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/apps/app_browsertest_util.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/media_galleries/media_file_system_registry.h"
17 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
18 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
19 #include "chrome/browser/storage_monitor/storage_info.h"
20 #include "chrome/browser/storage_monitor/storage_monitor.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "content/public/test/test_utils.h"
23
24 #if defined(OS_WIN) || defined(OS_MACOSX)
25 #include "chrome/browser/media_galleries/fileapi/picasa_finder.h"
26 #include "chrome/common/media_galleries/picasa_test_util.h"
27 #include "chrome/common/media_galleries/picasa_types.h"
28 #include "chrome/common/media_galleries/pmp_test_util.h"
29 #endif
30
31 using extensions::PlatformAppBrowserTest;
32
33 namespace {
34
35 // Dummy device properties.
36 const char kDeviceId[] = "testDeviceId";
37 const char kDeviceName[] = "foobar";
38 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
39 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("C:\\qux");
40 #else
41 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux");
42 #endif
43
44 // This function is to ensure at least one (fake) media gallery exists for
45 // testing platforms with no default media galleries, such as CHROMEOS.
46 void MakeFakeMediaGalleryForTest(Profile* profile, const base::FilePath& path) {
47   MediaGalleriesPreferences* preferences =
48       g_browser_process->media_file_system_registry()->GetPreferences(profile);
49   base::RunLoop runloop;
50   preferences->EnsureInitialized(runloop.QuitClosure());
51   runloop.Run();
52
53   MediaGalleryPrefInfo gallery_info;
54   ASSERT_FALSE(preferences->LookUpGalleryByPath(path, &gallery_info));
55   preferences->AddGallery(gallery_info.device_id,
56                           gallery_info.path,
57                           false /* user_added */,
58                           gallery_info.volume_label,
59                           gallery_info.vendor_name,
60                           gallery_info.model_name,
61                           gallery_info.total_size_in_bytes,
62                           gallery_info.last_attach_time);
63
64   content::RunAllPendingInMessageLoop();
65 }
66
67 }  // namespace
68
69 class MediaGalleriesPlatformAppBrowserTest : public PlatformAppBrowserTest {
70  protected:
71   MediaGalleriesPlatformAppBrowserTest() : test_jpg_size_(0) {}
72   virtual ~MediaGalleriesPlatformAppBrowserTest() {}
73
74   virtual void SetUpOnMainThread() OVERRIDE {
75     PlatformAppBrowserTest::SetUpOnMainThread();
76     ensure_media_directories_exists_.reset(new EnsureMediaDirectoriesExists);
77     PopulatePicturesDirectoryTestData();
78   }
79
80   virtual void TearDownOnMainThread() OVERRIDE {
81     ensure_media_directories_exists_.reset();
82     PlatformAppBrowserTest::TearDownOnMainThread();
83   }
84
85   bool RunMediaGalleriesTest(const std::string& extension_name) {
86     base::ListValue empty_list_value;
87     return RunMediaGalleriesTestWithArg(extension_name, empty_list_value);
88   }
89
90   bool RunMediaGalleriesTestWithArg(const std::string& extension_name,
91                                     const base::ListValue& custom_arg_value) {
92     // Copy the test data for this test into a temporary directory. Then add
93     // a common_injected.js to the temporary copy and run it.
94     const char kTestDir[] = "api_test/media_galleries/";
95     base::FilePath from_dir =
96         test_data_dir_.AppendASCII(kTestDir + extension_name);
97     from_dir = from_dir.NormalizePathSeparators();
98
99     base::ScopedTempDir temp_dir;
100     if (!temp_dir.CreateUniqueTempDir())
101       return false;
102
103     if (!base::CopyDirectory(from_dir, temp_dir.path(), true))
104       return false;
105
106     base::FilePath common_js_path(
107         GetCommonDataDir().AppendASCII("common_injected.js"));
108     base::FilePath inject_js_path(
109         temp_dir.path().AppendASCII(extension_name)
110                        .AppendASCII("common_injected.js"));
111     if (!base::CopyFile(common_js_path, inject_js_path))
112       return false;
113
114     const char* custom_arg = NULL;
115     std::string json_string;
116     if (!custom_arg_value.empty()) {
117       base::JSONWriter::Write(&custom_arg_value, &json_string);
118       custom_arg = json_string.c_str();
119     }
120
121     base::AutoReset<base::FilePath> reset(&test_data_dir_, temp_dir.path());
122     return RunPlatformAppTestWithArg(extension_name, custom_arg);
123   }
124
125   void AttachFakeDevice() {
126     device_id_ = StorageInfo::MakeDeviceId(
127         StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId);
128
129     StorageMonitor::GetInstance()->receiver()->ProcessAttach(
130         StorageInfo(device_id_, base::string16(), kDevicePath,
131                     ASCIIToUTF16(kDeviceName), base::string16(),
132                     base::string16(), 0));
133     content::RunAllPendingInMessageLoop();
134   }
135
136   void DetachFakeDevice() {
137     StorageMonitor::GetInstance()->receiver()->ProcessDetach(device_id_);
138     content::RunAllPendingInMessageLoop();
139   }
140
141   void PopulatePicturesDirectoryTestData() {
142     if (ensure_media_directories_exists_->num_galleries() == 0)
143       return;
144
145     base::FilePath test_data_path(GetCommonDataDir());
146     base::FilePath write_path;
147     ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path));
148
149     // Valid file, should show up in JS as a FileEntry.
150     ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"),
151                                write_path.AppendASCII("test.jpg")));
152
153     // Invalid file, should not show up as a FileEntry in JS at all.
154     ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"),
155                                write_path.AppendASCII("test.txt")));
156
157     int64 file_size;
158     ASSERT_TRUE(file_util::GetFileSize(test_data_path.AppendASCII("test.jpg"),
159                                        &file_size));
160     test_jpg_size_ = base::checked_numeric_cast<int>(file_size);
161   }
162
163 #if defined(OS_WIN) || defined(OS_MACOSX)
164   void PopulatePicasaTestData(const base::FilePath& picasa_app_data_root) {
165     base::FilePath picasa_database_path =
166         picasa::MakePicasaDatabasePath(picasa_app_data_root);
167     base::FilePath picasa_temp_dir_path =
168         picasa_database_path.DirName().AppendASCII(picasa::kPicasaTempDirName);
169     ASSERT_TRUE(file_util::CreateDirectory(picasa_database_path));
170     ASSERT_TRUE(file_util::CreateDirectory(picasa_temp_dir_path));
171
172     // Create fake folder directories.
173     base::FilePath folders_root =
174         ensure_media_directories_exists_->GetFakePicasaFoldersRootPath();
175     base::FilePath fake_folder_1 = folders_root.AppendASCII("folder1");
176     base::FilePath fake_folder_2 = folders_root.AppendASCII("folder2");
177     ASSERT_TRUE(file_util::CreateDirectory(fake_folder_1));
178     ASSERT_TRUE(file_util::CreateDirectory(fake_folder_2));
179
180     // Write folder and album contents.
181     picasa::WriteTestAlbumTable(
182         picasa_database_path, fake_folder_1, fake_folder_2);
183     picasa::WriteTestAlbumsImagesIndex(fake_folder_1, fake_folder_2);
184
185     base::FilePath test_jpg_path = GetCommonDataDir().AppendASCII("test.jpg");
186     ASSERT_TRUE(base::CopyFile(
187         test_jpg_path, fake_folder_1.AppendASCII("InBoth.jpg")));
188     ASSERT_TRUE(base::CopyFile(
189         test_jpg_path, fake_folder_1.AppendASCII("InSecondAlbumOnly.jpg")));
190     ASSERT_TRUE(base::CopyFile(
191         test_jpg_path, fake_folder_2.AppendASCII("InFirstAlbumOnly.jpg")));
192   }
193 #endif
194
195   base::FilePath GetCommonDataDir() const {
196     return test_data_dir_.AppendASCII("api_test")
197                          .AppendASCII("media_galleries")
198                          .AppendASCII("common");
199   }
200
201   int num_galleries() const {
202     return ensure_media_directories_exists_->num_galleries();
203   }
204
205   int test_jpg_size() const { return test_jpg_size_; }
206
207   EnsureMediaDirectoriesExists* ensure_media_directories_exists() const {
208     return ensure_media_directories_exists_.get();
209   }
210
211  private:
212   std::string device_id_;
213   int test_jpg_size_;
214   scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exists_;
215 };
216
217 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
218                        MediaGalleriesNoAccess) {
219   base::ScopedTempDir temp_dir;
220   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
221   MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path());
222
223   base::ListValue custom_args;
224   custom_args.AppendInteger(num_galleries() + 1);
225
226   ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", custom_args))
227       << message_;
228 }
229
230 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) {
231   ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_;
232 }
233
234 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
235                        NoGalleriesCopyTo) {
236   ASSERT_TRUE(RunMediaGalleriesTest("no_galleries_copy_to")) << message_;
237 }
238
239 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
240                        MediaGalleriesRead) {
241   base::ListValue custom_args;
242   custom_args.AppendInteger(num_galleries());
243   custom_args.AppendInteger(test_jpg_size());
244
245   ASSERT_TRUE(RunMediaGalleriesTestWithArg("read_access", custom_args))
246       << message_;
247 }
248
249 // Flaky: crbug.com/314576
250 #if defined(OS_WIN)
251 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
252                        DISABLED_MediaGalleriesCopyTo) {
253 #else
254 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
255                        MediaGalleriesCopyTo) {
256 #endif
257   base::ScopedTempDir temp_dir;
258   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
259   MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path());
260   ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_;
261 }
262
263 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
264                        MediaGalleriesAccessAttached) {
265   AttachFakeDevice();
266
267   base::ListValue custom_args;
268   custom_args.AppendInteger(num_galleries() + 1);
269   custom_args.AppendString(kDeviceName);
270
271   ASSERT_TRUE(RunMediaGalleriesTestWithArg("access_attached", custom_args))
272       << message_;
273
274   DetachFakeDevice();
275 }
276
277 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
278                        GetFilesystemMetadata) {
279   ASSERT_TRUE(RunMediaGalleriesTest("metadata")) << message_;
280 }
281
282 #if defined(OS_WIN)|| defined(OS_MACOSX)
283 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
284                        PicasaDefaultLocation) {
285 #if defined(OS_WIN)
286   PopulatePicasaTestData(
287       ensure_media_directories_exists()->GetFakeLocalAppDataPath());
288 #elif defined(OS_MACOSX)
289   PopulatePicasaTestData(
290       ensure_media_directories_exists()->GetFakeAppDataPath());
291 #endif
292   ASSERT_TRUE(RunMediaGalleriesTest("picasa")) << message_;
293 }
294
295 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
296                        PicasaCustomLocation) {
297   base::ScopedTempDir custom_picasa_app_data_root;
298   ASSERT_TRUE(custom_picasa_app_data_root.CreateUniqueTempDir());
299   ensure_media_directories_exists()->SetCustomPicasaAppDataPath(
300       custom_picasa_app_data_root.path());
301   PopulatePicasaTestData(custom_picasa_app_data_root.path());
302   ASSERT_TRUE(RunMediaGalleriesTest("picasa")) << message_;
303 }
304 #endif  // defined(OS_WIN) || defined(OS_MACOSX)