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

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

Issue 24420003: Media Galleries: Run API tests in a temp directory with an injected common.js file. Avoid the need … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make it more obvious there's a JS file being injected into the test cases (try 2) Created 7 years, 2 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
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/media_galleries/common/common_injected.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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
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 // Copy the test data for this test into a temporary directory. Then add
87 // a common_injected.js to the temporary copy and run it.
88 const char kTestDir[] = "api_test/media_galleries/";
89 base::FilePath from_dir =
90 test_data_dir_.AppendASCII(kTestDir + extension_name);
91 base::ScopedTempDir temp_dir;
92 if (!temp_dir.CreateUniqueTempDir())
93 return false;
94
95 if (!base::CopyDirectory(from_dir, temp_dir.path(), true))
96 return false;
97
98 base::FilePath common_js_path(
99 GetCommonDataDir().AppendASCII("common_injected.js"));
100 base::FilePath inject_js_path(
101 temp_dir.path().AppendASCII(extension_name)
102 .AppendASCII("common_injected.js"));
103 if (!base::CopyFile(common_js_path, inject_js_path))
104 return false;
105
85 const char* custom_arg = NULL; 106 const char* custom_arg = NULL;
86 std::string json_string; 107 std::string json_string;
87 if (!custom_arg_value.empty()) { 108 if (!custom_arg_value.empty()) {
88 base::JSONWriter::Write(&custom_arg_value, &json_string); 109 base::JSONWriter::Write(&custom_arg_value, &json_string);
89 custom_arg = json_string.c_str(); 110 custom_arg = json_string.c_str();
90 } 111 }
91 112
92 const char kTestDir[] = "api_test/media_galleries/"; 113 base::AutoReset<base::FilePath> reset(&test_data_dir_, temp_dir.path());
93 return RunPlatformAppTestWithArg(kTestDir + extension_name, custom_arg); 114 return RunPlatformAppTestWithArg(extension_name, custom_arg);
94 } 115 }
95 116
96 void AttachFakeDevice() { 117 void AttachFakeDevice() {
97 device_id_ = StorageInfo::MakeDeviceId( 118 device_id_ = StorageInfo::MakeDeviceId(
98 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId); 119 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId);
99 120
100 StorageMonitor::GetInstance()->receiver()->ProcessAttach( 121 StorageMonitor::GetInstance()->receiver()->ProcessAttach(
101 StorageInfo(device_id_, base::string16(), kDevicePath, 122 StorageInfo(device_id_, base::string16(), kDevicePath,
102 ASCIIToUTF16(kDeviceName), base::string16(), 123 ASCIIToUTF16(kDeviceName), base::string16(),
103 base::string16(), 0)); 124 base::string16(), 0));
104 content::RunAllPendingInMessageLoop(); 125 content::RunAllPendingInMessageLoop();
105 } 126 }
106 127
107 void DetachFakeDevice() { 128 void DetachFakeDevice() {
108 StorageMonitor::GetInstance()->receiver()->ProcessDetach(device_id_); 129 StorageMonitor::GetInstance()->receiver()->ProcessDetach(device_id_);
109 content::RunAllPendingInMessageLoop(); 130 content::RunAllPendingInMessageLoop();
110 } 131 }
111 132
112 void PopulatePicturesDirectoryTestData() { 133 void PopulatePicturesDirectoryTestData() {
113 if (ensure_media_directories_exist_->num_galleries() == 0) 134 if (ensure_media_directories_exist_->num_galleries() == 0)
114 return; 135 return;
115 136
116 base::FilePath test_data_path = 137 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; 138 base::FilePath write_path;
121 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path)); 139 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_PICTURES, &write_path));
122 140
123 // Valid file, should show up in JS as a FileEntry. 141 // Valid file, should show up in JS as a FileEntry.
124 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"), 142 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.jpg"),
125 write_path.AppendASCII("test.jpg"))); 143 write_path.AppendASCII("test.jpg")));
126 144
127 // Invalid file, should not show up as a FileEntry in JS at all. 145 // Invalid file, should not show up as a FileEntry in JS at all.
128 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"), 146 ASSERT_TRUE(base::CopyFile(test_data_path.AppendASCII("test.txt"),
129 write_path.AppendASCII("test.txt"))); 147 write_path.AppendASCII("test.txt")));
130 148
131 int64 file_size; 149 int64 file_size;
132 ASSERT_TRUE(file_util::GetFileSize(test_data_path.AppendASCII("test.jpg"), 150 ASSERT_TRUE(file_util::GetFileSize(test_data_path.AppendASCII("test.jpg"),
133 &file_size)); 151 &file_size));
134 test_jpg_size_ = base::checked_numeric_cast<int>(file_size); 152 test_jpg_size_ = base::checked_numeric_cast<int>(file_size);
135 } 153 }
136 154
155 base::FilePath GetCommonDataDir() const {
156 return test_data_dir_.AppendASCII("api_test")
157 .AppendASCII("media_galleries")
158 .AppendASCII("common");
159 }
160
137 int num_galleries() const { 161 int num_galleries() const {
138 return ensure_media_directories_exist_->num_galleries(); 162 return ensure_media_directories_exist_->num_galleries();
139 } 163 }
140 164
141 int test_jpg_size() const { return test_jpg_size_; } 165 int test_jpg_size() const { return test_jpg_size_; }
142 166
143 private: 167 private:
144 std::string device_id_; 168 std::string device_id_;
145 int test_jpg_size_; 169 int test_jpg_size_;
146 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_; 170 scoped_ptr<EnsureMediaDirectoriesExists> ensure_media_directories_exist_;
147 }; 171 };
148 172
149 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 173 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
150 MediaGalleriesNoAccess) { 174 MediaGalleriesNoAccess) {
175 base::ScopedTempDir temp_dir;
176 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
177 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path());
178
151 base::ListValue custom_args; 179 base::ListValue custom_args;
152 custom_args.AppendInteger(num_galleries()); 180 custom_args.AppendInteger(num_galleries() + 1);
153 181
154 ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", custom_args)) 182 ASSERT_TRUE(RunMediaGalleriesTestWithArg("no_access", custom_args))
155 << message_; 183 << message_;
156 } 184 }
157 185
158 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) { 186 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, NoGalleriesRead) {
159 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_; 187 ASSERT_TRUE(RunMediaGalleriesTest("no_galleries")) << message_;
160 } 188 }
161 189
162 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 190 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
(...skipping 13 matching lines...) Expand all
176 204
177 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 205 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
178 MediaGalleriesCopyTo) { 206 MediaGalleriesCopyTo) {
179 base::ScopedTempDir temp_dir; 207 base::ScopedTempDir temp_dir;
180 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 208 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
181 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path()); 209 MakeFakeMediaGalleryForTest(browser()->profile(), temp_dir.path());
182 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_; 210 ASSERT_TRUE(RunMediaGalleriesTest("copy_to_access")) << message_;
183 } 211 }
184 212
185 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 213 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) { 214 MediaGalleriesAccessAttached) {
195 AttachFakeDevice(); 215 AttachFakeDevice();
196 216
197 base::ListValue custom_args; 217 base::ListValue custom_args;
198 custom_args.AppendInteger(num_galleries() + 1); 218 custom_args.AppendInteger(num_galleries() + 1);
199 custom_args.AppendString(kDeviceName); 219 custom_args.AppendString(kDeviceName);
200 220
201 ASSERT_TRUE(RunMediaGalleriesTestWithArg("access_attached", custom_args)) 221 ASSERT_TRUE(RunMediaGalleriesTestWithArg("access_attached", custom_args))
202 << message_; 222 << message_;
203 223
204 DetachFakeDevice(); 224 DetachFakeDevice();
205 } 225 }
206 226
207 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest, 227 IN_PROC_BROWSER_TEST_F(MediaGalleriesPlatformAppBrowserTest,
208 GetFilesystemMetadata) { 228 GetFilesystemMetadata) {
209 ASSERT_TRUE(RunMediaGalleriesTest("metadata")) << message_; 229 ASSERT_TRUE(RunMediaGalleriesTest("metadata")) << message_;
210 } 230 }
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/media_galleries/common/common_injected.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698