1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Browser test for basic Chrome OS file manager functionality:
// - The file list is updated when a file is added externally to the Downloads
// folder.
// - Selecting a file and copy-pasting it with the keyboard copies the file.
// - Selecting a file and pressing delete deletes it.
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/time.h"
#include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "chrome/browser/chromeos/drive/file_system_interface.h"
#include "chrome/browser/chromeos/extensions/file_manager/drive_test_util.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/google_apis/fake_drive_service.h"
#include "chrome/browser/google_apis/gdata_wapi_parser.h"
#include "chrome/browser/google_apis/test_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chromeos/chromeos_switches.h"
#include "content/public/browser/browser_context.h"
#include "webkit/browser/fileapi/external_mount_points.h"
namespace {
enum EntryType {
FILE,
DIRECTORY,
};
enum SharedOption {
NONE,
SHARED,
};
enum GuestMode {
NOT_IN_GUEST_MODE,
IN_GUEST_MODE
};
// This global operator is used from Google Test to format error messages.
std::ostream& operator<<(std::ostream& os, const GuestMode& guest_mode) {
return os << (guest_mode == IN_GUEST_MODE ?
"IN_GUEST_MODE" : "NOT_IN_GUEST_MODE");
}
struct TestEntryInfo {
EntryType type;
const char* source_file_name; // Source file name to be used as a prototype.
const char* target_name; // Target file or directory name.
const char* mime_type;
SharedOption shared_option;
const char* last_modified_time_as_string;
};
TestEntryInfo kTestEntrySetCommon[] = {
{ FILE, "text.txt", "hello.txt", "text/plain", NONE, "4 Sep 1998 12:34:56" },
{ FILE, "image.png", "My Desktop Background.png", "text/plain", NONE,
"18 Jan 2038 01:02:03" },
{ FILE, "music.ogg", "Beautiful Song.ogg", "text/plain", NONE,
"12 Nov 2086 12:00:00" },
{ FILE, "video.ogv", "world.ogv", "text/plain", NONE,
"4 July 2012 10:35:00" },
{ DIRECTORY, "", "photos", NULL, NONE, "1 Jan 1980 23:59:59" },
{ DIRECTORY, "", ".warez", NULL, NONE, "26 Oct 1985 13:39" }
};
TestEntryInfo kTestEntrySetDriveOnly[] = {
{ FILE, "", "Test Document", "application/vnd.google-apps.document", NONE,
"10 Apr 2013 16:20:00" },
{ FILE, "", "Test Shared Document", "application/vnd.google-apps.document",
SHARED, "20 Mar 2013 22:40:00" }
};
// The local volume class for test.
// This class provides the operations for a test volume that simulates local
// drive.
class LocalTestVolume {
public:
// Adds this volume to the file system as a local volume. Returns true on
// success.
bool Mount(Profile* profile) {
const std::string kDownloads = "Downloads";
if (local_path_.empty()) {
if (!tmp_dir_.CreateUniqueTempDir())
return false;
local_path_ = tmp_dir_.path().Append(kDownloads);
}
fileapi::ExternalMountPoints* const mount_points =
content::BrowserContext::GetMountPoints(profile);
mount_points->RevokeFileSystem(kDownloads);
return mount_points->RegisterFileSystem(
kDownloads, fileapi::kFileSystemTypeNativeLocal, local_path_) &&
file_util::CreateDirectory(local_path_);
}
void CreateEntry(const TestEntryInfo& entry) {
base::FilePath target_path = local_path_.AppendASCII(entry.target_name);
switch (entry.type) {
case FILE: {
base::FilePath source_path =
google_apis::test_util::GetTestFilePath("chromeos/file_manager").
AppendASCII(entry.source_file_name);
ASSERT_TRUE(file_util::CopyFile(source_path, target_path))
<< "Copy from " << source_path.value()
<< " to " << target_path.value() << " failed.";
break;
}
case DIRECTORY:
ASSERT_TRUE(file_util::CreateDirectory(target_path)) <<
"Failed to create a directory: " << target_path.value();
break;
}
base::Time time;
ASSERT_TRUE(base::Time::FromString(entry.last_modified_time_as_string,
&time));
ASSERT_TRUE(file_util::SetLastModifiedTime(target_path, time));
}
private:
base::FilePath local_path_;
base::ScopedTempDir tmp_dir_;
};
// The drive volume class for test.
// This class provides the operations for a test volume that simulates Google
// drive.
class DriveTestVolume {
public:
DriveTestVolume() : fake_drive_service_(NULL),
integration_service_(NULL) {
}
// Send request to add this volume to the file system as Google drive.
// This method must be calld at SetUp method of FileManagerBrowserTestBase.
// Returns true on success.
bool SetUp() {
if (!test_cache_root_.CreateUniqueTempDir())
return false;
drive::DriveIntegrationServiceFactory::SetFactoryForTest(
base::Bind(&DriveTestVolume::CreateDriveIntegrationService,
base::Unretained(this)));
return true;
}
void CreateEntry(const TestEntryInfo& entry) {
switch (entry.type) {
case FILE:
CreateFile(entry.source_file_name,
entry.target_name,
entry.mime_type,
entry.shared_option == SHARED,
entry.last_modified_time_as_string);
break;
case DIRECTORY:
CreateDirectory(entry.target_name, entry.last_modified_time_as_string);
break;
}
}
// Creates an empty directory with the given |name| and |modification_time|.
void CreateDirectory(const std::string& name,
const std::string& modification_time) {
google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
scoped_ptr<google_apis::ResourceEntry> resource_entry;
fake_drive_service_->AddNewDirectory(
fake_drive_service_->GetRootResourceId(),
name,
google_apis::test_util::CreateCopyResultCallback(&error,
&resource_entry));
base::MessageLoop::current()->RunUntilIdle();
ASSERT_TRUE(error == google_apis::HTTP_CREATED);
ASSERT_TRUE(resource_entry);
base::Time time;
ASSERT_TRUE(base::Time::FromString(modification_time.c_str(), &time));
fake_drive_service_->SetLastModifiedTime(
resource_entry->resource_id(),
time,
google_apis::test_util::CreateCopyResultCallback(&error,
&resource_entry));
base::MessageLoop::current()->RunUntilIdle();
ASSERT_TRUE(error == google_apis::HTTP_SUCCESS);
ASSERT_TRUE(resource_entry);
CheckForUpdates();
}
// Creates a test file with the given spec.
// Serves |test_file_name| file. Pass an empty string for an empty file.
void CreateFile(const std::string& source_file_name,
const std::string& target_file_name,
const std::string& mime_type,
bool shared_with_me,
const std::string& modification_time) {
google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
std::string content_data;
if (!source_file_name.empty()) {
base::FilePath source_file_path =
google_apis::test_util::GetTestFilePath("chromeos/file_manager").
AppendASCII(source_file_name);
ASSERT_TRUE(file_util::ReadFileToString(source_file_path, &content_data));
}
scoped_ptr<google_apis::ResourceEntry> resource_entry;
fake_drive_service_->AddNewFile(
mime_type,
content_data,
fake_drive_service_->GetRootResourceId(),
target_file_name,
shared_with_me,
google_apis::test_util::CreateCopyResultCallback(&error,
&resource_entry));
base::MessageLoop::current()->RunUntilIdle();
ASSERT_EQ(google_apis::HTTP_CREATED, error);
ASSERT_TRUE(resource_entry);
base::Time time;
ASSERT_TRUE(base::Time::FromString(modification_time.c_str(), &time));
fake_drive_service_->SetLastModifiedTime(
resource_entry->resource_id(),
time,
google_apis::test_util::CreateCopyResultCallback(&error,
&resource_entry));
base::MessageLoop::current()->RunUntilIdle();
ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
ASSERT_TRUE(resource_entry);
CheckForUpdates();
}
// Notifies FileSystem that the contents in FakeDriveService are
// changed, hence the new contents should be fetched.
void CheckForUpdates() {
if (integration_service_ && integration_service_->file_system()) {
integration_service_->file_system()->CheckForUpdates();
}
}
drive::DriveIntegrationService* CreateDriveIntegrationService(
Profile* profile) {
fake_drive_service_ = new google_apis::FakeDriveService;
fake_drive_service_->LoadResourceListForWapi(
"chromeos/gdata/empty_feed.json");
fake_drive_service_->LoadAccountMetadataForWapi(
"chromeos/gdata/account_metadata.json");
fake_drive_service_->LoadAppListForDriveApi("chromeos/drive/applist.json");
integration_service_ = new drive::DriveIntegrationService(
profile,
fake_drive_service_,
test_cache_root_.path(),
NULL);
return integration_service_;
}
private:
base::ScopedTempDir test_cache_root_;
google_apis::FakeDriveService* fake_drive_service_;
drive::DriveIntegrationService* integration_service_;
};
// Parameter of FileManagerBrowserTestBase.
// The second value is the case name of javascript.
typedef std::tr1::tuple<GuestMode, const char*> TestParameter;
// The base test class.
class FileManagerBrowserTestBase :
public ExtensionApiTest,
public ::testing::WithParamInterface<TestParameter> {
protected:
FileManagerBrowserTestBase() :
local_volume_(new LocalTestVolume),
drive_volume_(std::tr1::get<0>(GetParam()) != IN_GUEST_MODE ?
new DriveTestVolume() : NULL),
guest_mode_(std::tr1::get<0>(GetParam())) {}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
virtual void SetUpOnMainThread() OVERRIDE;
// Adds an incognito and guest-mode flags for tests in the guest mode.
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
// Loads our testing extension and sends it a string identifying the current
// test.
void StartTest();
protected:
const scoped_ptr<LocalTestVolume> local_volume_;
const scoped_ptr<DriveTestVolume> drive_volume_;
private:
GuestMode guest_mode_;
};
void FileManagerBrowserTestBase::SetUpInProcessBrowserTestFixture() {
ExtensionApiTest::SetUpInProcessBrowserTestFixture();
extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
if (drive_volume_)
ASSERT_TRUE(drive_volume_->SetUp());
}
void FileManagerBrowserTestBase::SetUpOnMainThread() {
ExtensionApiTest::SetUpOnMainThread();
ASSERT_TRUE(local_volume_->Mount(browser()->profile()));
for (size_t i = 0; i < arraysize(kTestEntrySetCommon); ++i)
local_volume_->CreateEntry(kTestEntrySetCommon[i]);
if (drive_volume_) {
for (size_t i = 0; i < arraysize(kTestEntrySetCommon); ++i)
drive_volume_->CreateEntry(kTestEntrySetCommon[i]);
// For testing Drive, create more entries with Drive specific attributes.
// TODO(haruki): Add a case for an entry cached by DriveCache.
for (size_t i = 0; i < arraysize(kTestEntrySetDriveOnly); ++i)
drive_volume_->CreateEntry(kTestEntrySetDriveOnly[i]);
drive_test_util::WaitUntilDriveMountPointIsAdded(browser()->profile());
}
}
void FileManagerBrowserTestBase::SetUpCommandLine(CommandLine* command_line) {
if (guest_mode_ == IN_GUEST_MODE) {
command_line->AppendSwitch(chromeos::switches::kGuestSession);
command_line->AppendSwitchNative(chromeos::switches::kLoginUser, "");
command_line->AppendSwitch(switches::kIncognito);
}
ExtensionApiTest::SetUpCommandLine(command_line);
}
void FileManagerBrowserTestBase::StartTest() {
base::FilePath path = test_data_dir_.AppendASCII("file_manager_browsertest");
const extensions::Extension* extension = LoadExtensionAsComponent(path);
ASSERT_TRUE(extension);
bool in_guest_mode = guest_mode_ == IN_GUEST_MODE;
ExtensionTestMessageListener listener(
in_guest_mode ? "which test guest" : "which test non-guest", true);
ASSERT_TRUE(listener.WaitUntilSatisfied());
listener.Reply(std::tr1::get<1>(GetParam()));
}
class FileManagerBrowserFileDisplayTest : public FileManagerBrowserTestBase {};
IN_PROC_BROWSER_TEST_P(FileManagerBrowserFileDisplayTest, Test) {
ResultCatcher catcher;
ASSERT_NO_FATAL_FAILURE(StartTest());
ExtensionTestMessageListener listener("initial check done", true);
ASSERT_TRUE(listener.WaitUntilSatisfied());
const TestEntryInfo entry = {
FILE,
"music.ogg", // Prototype file name.
"newly added file.ogg", // Target file name.
"audio/ogg",
NONE,
"4 Sep 1998 00:00:00"
};
if (drive_volume_)
drive_volume_->CreateEntry(entry);
local_volume_->CreateEntry(entry);
listener.Reply("file added");
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}
INSTANTIATE_TEST_CASE_P(
AllTests,
FileManagerBrowserFileDisplayTest,
::testing::Values(TestParameter(NOT_IN_GUEST_MODE, "fileDisplayDownloads"),
TestParameter(IN_GUEST_MODE, "fileDisplayDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "fileDisplayDrive")));
// A test class that just executes JavaScript unit test.
class FileManagerBrowserSimpleTest : public FileManagerBrowserTestBase {};
IN_PROC_BROWSER_TEST_P(FileManagerBrowserSimpleTest, Test) {
ResultCatcher catcher;
ASSERT_NO_FATAL_FAILURE(StartTest());
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}
INSTANTIATE_TEST_CASE_P(
DISABLED_OpenSpecialTypes,
FileManagerBrowserSimpleTest,
::testing::Values(TestParameter(IN_GUEST_MODE, "videoOpenDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "videoOpenDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "videoOpenDrive"),
TestParameter(IN_GUEST_MODE, "audioOpenDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "audioOpenDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "audioOpenDrive"),
TestParameter(IN_GUEST_MODE, "galleryOpenDownloads"),
TestParameter(NOT_IN_GUEST_MODE,
"galleryOpenDownloads")));
// Disabled temporarily since fails on Linux Chromium OS
// ASAN Tests (2). TODO(mtomasz): crbug.com/243611.
// TestParameter(NOT_IN_GUEST_MODE, "galleryOpenDrive")));
INSTANTIATE_TEST_CASE_P(
KeyboardOpeartions,
FileManagerBrowserSimpleTest,
::testing::Values(TestParameter(IN_GUEST_MODE, "keyboardDeleteDownloads"),
TestParameter(NOT_IN_GUEST_MODE,
"keyboardDeleteDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "keyboardDeleteDrive"),
TestParameter(IN_GUEST_MODE, "keyboardCopyDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "keyboardCopyDownloads"),
TestParameter(NOT_IN_GUEST_MODE, "keyboardCopyDrive")));
INSTANTIATE_TEST_CASE_P(
DriveSpecific,
FileManagerBrowserSimpleTest,
::testing::Values(TestParameter(NOT_IN_GUEST_MODE, "openSidebarRecent"),
TestParameter(NOT_IN_GUEST_MODE, "openSidebarOffline"),
TestParameter(NOT_IN_GUEST_MODE,
"openSidebarSharedWithMe"),
TestParameter(NOT_IN_GUEST_MODE, "autocomplete")));
INSTANTIATE_TEST_CASE_P(
Transfer,
FileManagerBrowserSimpleTest,
::testing::Values(TestParameter(NOT_IN_GUEST_MODE,
"transferFromDriveToDownloads"),
TestParameter(NOT_IN_GUEST_MODE,
"transferFromDownloadsToDrive"),
TestParameter(NOT_IN_GUEST_MODE,
"transferFromSharedToDownloads"),
TestParameter(NOT_IN_GUEST_MODE,
"transferFromSharedToDrive"),
TestParameter(NOT_IN_GUEST_MODE,
"transferFromRecentToDownloads"),
TestParameter(NOT_IN_GUEST_MODE,
"transferFromRecentToDrive"),
TestParameter(NOT_IN_GUEST_MODE,
"transferFromOfflineToDownloads"),
TestParameter(NOT_IN_GUEST_MODE,
"transferFromOfflineToDrive")));
// TODO (hirono): Fix this flakiness. crbug.com/247299
INSTANTIATE_TEST_CASE_P(
DISABLED_HideSearchBox,
FileManagerBrowserSimpleTest,
::testing::Values(TestParameter(IN_GUEST_MODE, "hideSearchBox"),
TestParameter(NOT_IN_GUEST_MODE, "hideSearchBox")));
INSTANTIATE_TEST_CASE_P(
RestorePrefs,
FileManagerBrowserSimpleTest,
::testing::Values(TestParameter(IN_GUEST_MODE, "restoreSortColumn"),
TestParameter(NOT_IN_GUEST_MODE, "restoreSortColumn"),
TestParameter(IN_GUEST_MODE, "restoreCurrentView"),
TestParameter(NOT_IN_GUEST_MODE, "restoreCurrentView")));
} // namespace
|