Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: chrome/browser/extensions/api/media_galleries/media_galleries_apitest.cc

Issue 24242010: Media Galleries: Refactor test to not need RunSecondPhase(). (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 return RunMediaGalleriesTestWithArg(extension_name, NULL);
84 void RunSecondTestPhase(const std::string& command) { 80 }
85 const extensions::Extension* extension = GetSingleLoadedExtension();
86 extensions::ExtensionHost* host =
87 extensions::ExtensionSystem::Get(browser()->profile())->
88 process_manager()->GetBackgroundHostForExtension(extension->id());
89 ASSERT_TRUE(host);
90 81
91 ResultCatcher catcher; 82 bool RunMediaGalleriesTestWithArg(const std::string& extension_name,
tommycli 2013/09/24 00:34:08 Since everywhere you use this, you are effectively
Lei Zhang 2013/09/24 01:37:46 Done. I made RunMediaGalleriesTest() and RunMedia
92 host->render_view_host()->ExecuteJavascriptInWebFrame( 83 const char* custom_arg) {
93 base::string16(), base::UTF8ToUTF16(command)); 84 const char kTestDir[] = "api_test/media_galleries/";
94 EXPECT_TRUE(catcher.GetNextResult()) << message_; 85 return RunPlatformAppTestWithArg(kTestDir + extension_name, custom_arg);
95 } 86 }
96 87
97 void AttachFakeDevice() { 88 void AttachFakeDevice() {
98 device_id_ = StorageInfo::MakeDeviceId( 89 device_id_ = StorageInfo::MakeDeviceId(
99 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId); 90 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId);
100 91
101 StorageMonitor::GetInstance()->receiver()->ProcessAttach( 92 StorageMonitor::GetInstance()->receiver()->ProcessAttach(
102 StorageInfo(device_id_, base::string16(), kDevicePath, 93 StorageInfo(device_id_, base::string16(), kDevicePath,
103 ASCIIToUTF16(kDeviceName), base::string16(), 94 ASCIIToUTF16(kDeviceName), base::string16(),
104 base::string16(), 0)); 95 base::string16(), 0));
(...skipping 16 matching lines...) Expand all
121 base::FilePath write_path; 112 base::FilePath write_path;
122 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path)); 113 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path));
123 114
124 // Valid file, should show up in JS as a FileEntry. 115 // Valid file, should show up in JS as a FileEntry.
125 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"), 116 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"),
126 write_path.AppendASCII("test.jpg"))); 117 write_path.AppendASCII("test.jpg")));
127 118
128 // Invalid file, should not show up as a FileEntry in JS at all. 119 // Invalid file, should not show up as a FileEntry in JS at all.
129 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"), 120 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"),
130 write_path.AppendASCII("test.txt"))); 121 write_path.AppendASCII("test.txt")));
122
123 int64 file_size;
124 ASSERT_TRUE(file_util::GetFileSize(test_data_path.AppendASCII("test.jpg"),
125 &file_size));
126 test_jpg_size_ = base::checked_numeric_cast<int>(file_size);
131 } 127 }
132 128
133 int num_galleries() const { 129 int num_galleries() const {
134 return ensure_media_directories_exist_->num_galleries(); 130 return ensure_media_directories_exist_->num_galleries();
135 } 131 }
136 132
133 int test_jpg_size() const { return test_jpg_size_; }
134
137 private: 135 private:
138 std::string device_id_; 136 std::string device_id_;
137 int test_jpg_size_;
139 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_; 138 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_;
140 }; 139 };
141 140
142 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 141 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
143 MediaGalleriesNoAccess) { 142 MediaGalleriesNoAccess) {
144 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_access")) 143 base::ListValue custom_args;
144 custom_args.AppendInteger(num_galleries());
145
146 std::string json_string;
147 base::JSONWriter::Write(&custom_args, &json_string);
148 ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", json_string.c_str()))
145 << message_; 149 << message_;
146 RunSecondTestPhase(base::StringPrintf(kTestGalleries, num_galleries()));
147 } 150 }
148 151
149 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) { 152 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) {
150 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/no_galleries")) 153 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_;
154 }
155
156 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
157 NoGalleriesCopyTo) {
158 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries_copy_to")) << message_;
159 }
160
161 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
162 MediaGalleriesRead) {
163 base::ListValue custom_args;
164 custom_args.AppendInteger(num_galleries());
165 custom_args.AppendInteger(test_jpg_size());
166
167 std::string json_string;
168 base::JSONWriter::Write(&custom_args, &json_string);
169 ASSERT_TRUE(RunMediaGalleriesTestWithArg("read_access", json_string.c_str()))
151 << message_; 170 << message_;
152 } 171 }
153 172
154 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 173 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) { 174 MediaGalleriesCopyTo) {
169 base::ScopedTempDir temp_dir; 175 base::ScopedTempDir temp_dir;
170 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 176 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
171 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); 177 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path());
172 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/copy_to_access")) 178 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_;
173 << message_;
174 } 179 }
175 180
176 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 181 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
177 MediaGalleriesCopyToNoAccess) { 182 MediaGalleriesCopyToNoAccess) {
178 base::ScopedTempDir temp_dir; 183 base::ScopedTempDir temp_dir;
179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 184 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
180 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); 185 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path());
181 ASSERT_TRUE(RunPlatformAppTest( 186 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access/no_access")) << message_;
182 "api_test/media_galleries/copy_to_access/no_access"))
183 << message_;
184 } 187 }
185 188
186 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 189 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
187 MediaGalleriesAccessAttached) { 190 MediaGalleriesAccessAttached) {
188 AttachFakeDevice(); 191 AttachFakeDevice();
189 192
190 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/access_attached")) 193 base::ListValue custom_args;
191 << message_; 194 custom_args.AppendInteger(num_galleries() + 1);
195 custom_args.AppendString(kDeviceName);
192 196
193 RunSecondTestPhase(base::StringPrintf( 197 std::string json_string;
194 "testGalleries(%d, \"%s\")", num_galleries() + 1, kDeviceName)); 198 base::JSONWriter::Write(&custom_args, &json_string);
199 ASSERT_TRUE(RunMediaGalleriesTestWithArg(
200 "access_attached", json_string.c_str())) << message_;
195 201
196 DetachFakeDevice(); 202 DetachFakeDevice();
197 } 203 }
198 204
199 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 205 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
200 GetFilesystemMetadata) { 206 GetFilesystemMetadata) {
201 ASSERT_TRUE(RunPlatformAppTest("api_test/media_galleries/metadata")) 207 ASSERT_TRUE(RunMediaGalleriesTest("metadata")) << message_;
202 << message_;
203 } 208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698