Chromium Code Reviews| Index: chrome/browser/chromeos/extensions/file_manager_browsertest.cc |
| diff --git a/chrome/browser/chromeos/extensions/file_manager_browsertest.cc b/chrome/browser/chromeos/extensions/file_manager_browsertest.cc |
| index 4ccf48022e0da8732ef6d92425c05d13cd7bf6a4..ecfa393020491d2d8afe0c2f4ad1778a82b2a2fb 100644 |
| --- a/chrome/browser/chromeos/extensions/file_manager_browsertest.cc |
| +++ b/chrome/browser/chromeos/extensions/file_manager_browsertest.cc |
| @@ -15,14 +15,20 @@ |
| #include "base/file_util.h" |
| #include "base/files/file_path.h" |
| #include "base/files/file_path_watcher.h" |
| +#include "base/path_service.h" |
| #include "base/platform_file.h" |
| #include "base/threading/platform_thread.h" |
| #include "base/time.h" |
| #include "base/utf_string_conversions.h" |
| +#include "chrome/browser/chromeos/drive/drive_file_system.h" |
| +#include "chrome/browser/chromeos/drive/drive_system_service.h" |
| #include "chrome/browser/extensions/component_loader.h" |
| #include "chrome/browser/extensions/extension_apitest.h" |
| #include "chrome/browser/extensions/extension_service.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/browser/ui/browser_window.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| @@ -42,6 +48,26 @@ const char kKeyboardTestFileName[] = "world.mpeg"; |
| const int kKeyboardTestFileSize = 1000; |
| const char kKeyboardTestFileCopyName[] = "world (1).mpeg"; |
| +struct TestFileInfo { |
| + const char* base_name; |
| + int64 file_size; |
| + const char* last_modified_time_as_string; |
| +} kTestFiles[] = { |
| + { "hello.txt", 123, "4 Sep 1998 12:34:56" }, |
| + { "My Desktop Background.png", 1024, "18 Jan 2038 01:02:03" }, |
| + { kKeyboardTestFileName, kKeyboardTestFileSize, "4 July 2012 10:35:00" }, |
| +}; |
| + |
| +struct TestDirectoryInfo { |
| + const char* base_name; |
| + const char* last_modified_time_as_string; |
| +} kTestDirectories[] = { |
| + { "photos", "1 Jan 1980 23:59:59" }, |
| + // Files starting with . are filtered out in |
| + // file_manager/js/directory_contents.js, so this should not be shown. |
| + { ".warez", "26 Oct 1985 13:39" }, |
| +}; |
| + |
| // The base test class. Used by FileManagerBrowserLocalTest and |
| // FileManagerBrowserDriveTest. |
| // TODO(satorux): Add the latter: crbug.com/224534. |
| @@ -55,6 +81,22 @@ class FileManagerBrowserTestBase : public ExtensionApiTest { |
| // Loads our testing extension and sends it a string identifying the current |
| // test. |
| void StartTest(const std::string& test_name); |
| + |
| + // Creates test files and directories. |
| + void CreateTestFilesAndDirectories(); |
| + |
| + // Creates a file with the given |name|, |length|, and |modification_time|. |
| + virtual void CreateTestFile(const std::string& name, |
| + int length, |
|
kinaba
2013/04/04 06:33:17
int64 instead of int is used for file size everywh
satorux1
2013/04/05 02:24:04
Done.
|
| + const std::string& modification_time) = 0; |
| + |
| + // Creates an empty directory with the given |name| and |modification_time|. |
| + virtual void CreateTestDirectory( |
| + const std::string& name, |
| + const std::string& modification_time) = 0; |
| + |
| + // Runs the file display test, shared by sub classes. |
| + void DoTestFileDisplay(); |
| }; |
| void FileManagerBrowserTestBase::StartFileManager( |
| @@ -82,6 +124,31 @@ void FileManagerBrowserTestBase::StartTest(const std::string& test_name) { |
| listener.Reply(test_name); |
| } |
| +void FileManagerBrowserTestBase::CreateTestFilesAndDirectories() { |
| + for (size_t i = 0; i < arraysize(kTestFiles); ++i) { |
| + CreateTestFile(kTestFiles[i].base_name, |
| + kTestFiles[i].file_size, |
| + kTestFiles[i].last_modified_time_as_string); |
| + } |
| + for (size_t i = 0; i < arraysize(kTestDirectories); ++i) { |
| + CreateTestDirectory(kTestDirectories[i].base_name, |
| + kTestDirectories[i].last_modified_time_as_string); |
| + } |
| +} |
| + |
| +void FileManagerBrowserTestBase::DoTestFileDisplay() { |
| + ResultCatcher catcher; |
| + |
| + StartTest("file display"); |
| + |
| + ExtensionTestMessageListener listener("initial check done", true); |
| + ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| + CreateTestFile("newly added file.mp3", 2000, "4 Sep 1998 00:00:00"); |
| + listener.Reply("file added"); |
| + |
| + ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| +} |
| + |
| // The boolean parameter, retrieved by GetParam(), is true if testing in the |
| // guest mode. See SetUpCommandLine() below for details. |
| class FileManagerBrowserLocalTest : public FileManagerBrowserTestBase, |
| @@ -94,14 +161,7 @@ class FileManagerBrowserLocalTest : public FileManagerBrowserTestBase, |
| downloads_path_ = tmp_dir_.path().Append("Downloads"); |
| ASSERT_TRUE(file_util::CreateDirectory(downloads_path_)); |
| - CreateTestFile("hello.txt", 123, "4 Sep 1998 12:34:56"); |
| - CreateTestFile("My Desktop Background.png", 1024, "18 Jan 2038 01:02:03"); |
| - CreateTestFile(kKeyboardTestFileName, kKeyboardTestFileSize, |
| - "4 July 2012 10:35:00"); |
| - CreateTestDirectory("photos", "1 Jan 1980 23:59:59"); |
| - // Files starting with . are filtered out in |
| - // file_manager/js/directory_contents.js, so this should not be shown. |
| - CreateTestDirectory(".warez", "26 Oct 1985 13:39"); |
| + CreateTestFilesAndDirectories(); |
| ExtensionApiTest::SetUp(); |
| } |
| @@ -116,14 +176,13 @@ class FileManagerBrowserLocalTest : public FileManagerBrowserTestBase, |
| } |
| protected: |
| - // Creates a file with the given |name|, |length|, and |modification_time|. |
| - void CreateTestFile(const std::string& name, |
| - int length, |
| - const std::string& modification_time); |
| - |
| - // Creates an empty directory with the given |name| and |modification_time|. |
| - void CreateTestDirectory(const std::string& name, |
| - const std::string& modification_time); |
| + // FileManagerBrowserTestBase overrides. |
| + virtual void CreateTestFile(const std::string& name, |
| + int length, |
| + const std::string& modification_time) OVERRIDE; |
| + virtual void CreateTestDirectory( |
| + const std::string& name, |
| + const std::string& modification_time) OVERRIDE; |
| // Add a mount point to the fake Downloads directory. Should be called |
| // before StartFileManager(). |
| @@ -183,6 +242,133 @@ void FileManagerBrowserLocalTest::AddMountPointToFakeDownloads() { |
| "Downloads", fileapi::kFileSystemTypeNativeLocal, downloads_path_)); |
| } |
| +class FileManagerBrowserDriveTest : public FileManagerBrowserTestBase { |
| + public: |
| + FileManagerBrowserDriveTest() |
| + : fake_drive_service_(NULL), |
| + system_service_(NULL) { |
| + } |
| + |
| + virtual void SetUp() OVERRIDE { |
| + extensions::ComponentLoader::EnableBackgroundExtensionsForTesting(); |
| + |
| + base::FilePath tmp_dir_path; |
| + PathService::Get(base::DIR_TEMP, &tmp_dir_path); |
| + ASSERT_TRUE(test_cache_root_.CreateUniqueTempDirUnderPath(tmp_dir_path)); |
|
kinaba
2013/04/04 06:33:17
test_cache_root_.CreateUniqueTempDir()
should crea
satorux1
2013/04/05 02:24:04
Done. thanks!
|
| + |
| + drive::DriveSystemServiceFactory::SetFactoryForTest( |
| + base::Bind(&FileManagerBrowserDriveTest::CreateDriveSystemService, |
| + base::Unretained(this))); |
| + |
| + ExtensionApiTest::SetUp(); |
| + } |
| + |
| + protected: |
| + // FileManagerBrowserTestBase overrides. |
| + virtual void CreateTestFile(const std::string& name, |
| + int length, |
| + const std::string& modification_time) OVERRIDE; |
| + virtual void CreateTestDirectory( |
| + const std::string& name, |
| + const std::string& modification_time) OVERRIDE; |
| + |
| + // Notifies DriveFileSystem that the contents in FakeDriveService are |
| + // changed, hence the new contents should be fetched. |
| + void CheckForUpdates(); |
| + |
| + // DriveSystemService factory function for this test. |
| + drive::DriveSystemService* CreateDriveSystemService(Profile* profile); |
| + |
| + base::ScopedTempDir test_cache_root_; |
| + google_apis::FakeDriveService* fake_drive_service_; |
| + drive::DriveSystemService* system_service_; |
| +}; |
| + |
| +void FileManagerBrowserDriveTest::CreateTestFile( |
| + const std::string& name, |
| + int length, |
| + const std::string& modification_time) { |
| + google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| + scoped_ptr<google_apis::ResourceEntry> resource_entry; |
| + fake_drive_service_->AddNewFile( |
| + "text/plain", |
| + length, |
| + fake_drive_service_->GetRootResourceId(), |
| + name, |
| + google_apis::test_util::CreateCopyResultCallback(&error, |
| + &resource_entry)); |
| + 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)); |
| + MessageLoop::current()->RunUntilIdle(); |
| + ASSERT_EQ(google_apis::HTTP_SUCCESS, error); |
| + ASSERT_TRUE(resource_entry); |
| + |
| + CheckForUpdates(); |
| +} |
| + |
| +void FileManagerBrowserDriveTest::CreateTestDirectory( |
| + 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)); |
| + 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)); |
| + MessageLoop::current()->RunUntilIdle(); |
| + ASSERT_EQ(google_apis::HTTP_SUCCESS, error); |
| + ASSERT_TRUE(resource_entry); |
| + |
| + CheckForUpdates(); |
| +} |
| + |
| +void FileManagerBrowserDriveTest::CheckForUpdates() { |
| + if (system_service_ && system_service_->file_system()) { |
| + system_service_->file_system()->CheckForUpdates(); |
| + } |
| +} |
| + |
| +drive::DriveSystemService* |
| +FileManagerBrowserDriveTest::CreateDriveSystemService(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"); |
| + |
| + // Create test files and directories inside the fake drive service. |
| + CreateTestFilesAndDirectories(); |
| + |
| + system_service_ = new drive::DriveSystemService(profile, |
| + fake_drive_service_, |
| + test_cache_root_.path(), |
| + NULL); |
| + |
| + return system_service_; |
| +} |
| + |
| // Monitors changes to a single file until the supplied condition callback |
| // returns true. Usage: |
| // TestFilePathWatcher watcher(path_to_file, MyConditionCallback); |
| @@ -301,16 +487,7 @@ IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestFileDisplay) { |
| AddMountPointToFakeDownloads(); |
| StartFileManager("/Downloads"); |
| - ResultCatcher catcher; |
| - |
| - StartTest("file display"); |
| - |
| - ExtensionTestMessageListener listener("initial check done", true); |
| - ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| - CreateTestFile("newly added file.mp3", 2000, "4 Sep 1998 00:00:00"); |
| - listener.Reply("file added"); |
| - |
| - ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| + DoTestFileDisplay(); |
| } |
| IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardCopy) { |
| @@ -352,4 +529,10 @@ IN_PROC_BROWSER_TEST_P(FileManagerBrowserLocalTest, TestKeyboardDelete) { |
| ASSERT_TRUE(watcher.RunMessageLoopUntilConditionSatisfied()); |
| } |
| +IN_PROC_BROWSER_TEST_F(FileManagerBrowserDriveTest, TestFileDisplay) { |
| + StartFileManager("/drive"); |
| + |
| + DoTestFileDisplay(); |
| +} |
| + |
| } // namespace |