Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system_util_unittest.cc
index 8c28c90..89db541 100644 (file)
@@ -9,7 +9,11 @@
 #include "base/files/scoped_temp_dir.h"
 #include "base/message_loop/message_loop.h"
 #include "base/strings/utf_string_conversions.h"
+#include "chrome/common/chrome_constants.h"
+#include "chrome/test/base/testing_browser_process.h"
 #include "chrome/test/base/testing_profile.h"
+#include "chrome/test/base/testing_profile_manager.h"
+#include "content/public/test/test_browser_thread_bundle.h"
 #include "content/public/test/test_file_system_options.h"
 #include "google_apis/drive/test_util.h"
 #include "testing/gtest/include/gtest/gtest.h"
 namespace drive {
 namespace util {
 
+namespace {
+
+// Sets up ProfileManager for testing and marks the current thread as UI by
+// TestBrowserThreadBundle. We need the thread since Profile objects must be
+// touched from UI and hence has CHECK/DCHECKs for it.
+class ProfileRelatedFileSystemUtilTest : public testing::Test {
+ protected:
+  ProfileRelatedFileSystemUtilTest()
+      : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {
+  }
+
+  virtual void SetUp() OVERRIDE {
+    ASSERT_TRUE(testing_profile_manager_.SetUp());
+  }
+
+  Profile* CreateProfileWithName(const std::string& name) {
+    return testing_profile_manager_.CreateTestingProfile(
+        chrome::kProfileDirPrefix + name);
+  }
+
+ private:
+  content::TestBrowserThreadBundle thread_bundle_;
+  TestingProfileManager testing_profile_manager_;
+};
+
+}  // namespace
+
 TEST(FileSystemUtilTest, FilePathToDriveURL) {
   base::FilePath path;
 
@@ -47,15 +78,30 @@ TEST(FileSystemUtilTest, FilePathToDriveURL) {
   EXPECT_EQ(path, DriveURLToFilePath(FilePathToDriveURL(path)));
 }
 
+TEST_F(ProfileRelatedFileSystemUtilTest, GetDriveMountPointPath) {
+  Profile* profile = CreateProfileWithName("hash1");
+  EXPECT_EQ(base::FilePath::FromUTF8Unsafe("/special/drive-hash1"),
+            GetDriveMountPointPath(profile));
+}
+
+TEST_F(ProfileRelatedFileSystemUtilTest, ExtractProfileFromPath) {
+  Profile* profile1 = CreateProfileWithName("hash1");
+  Profile* profile2 = CreateProfileWithName("hash2");
+  EXPECT_EQ(profile1, ExtractProfileFromPath(
+      base::FilePath::FromUTF8Unsafe("/special/drive-hash1")));
+  EXPECT_EQ(profile2, ExtractProfileFromPath(
+      base::FilePath::FromUTF8Unsafe("/special/drive-hash2/root/xxx")));
+  EXPECT_EQ(NULL, ExtractProfileFromPath(
+      base::FilePath::FromUTF8Unsafe("/special/non-drive-path")));
+}
+
 TEST(FileSystemUtilTest, IsUnderDriveMountPoint) {
   EXPECT_FALSE(IsUnderDriveMountPoint(
       base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
   EXPECT_FALSE(IsUnderDriveMountPoint(
       base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
   EXPECT_FALSE(IsUnderDriveMountPoint(
-      base::FilePath::FromUTF8Unsafe("/special/drivex/foo.txt")));
-  EXPECT_FALSE(IsUnderDriveMountPoint(
-      base::FilePath::FromUTF8Unsafe("special/drivex/foo.txt")));
+      base::FilePath::FromUTF8Unsafe("special/drive/foo.txt")));
 
   EXPECT_TRUE(IsUnderDriveMountPoint(
       base::FilePath::FromUTF8Unsafe("/special/drive")));
@@ -63,6 +109,8 @@ TEST(FileSystemUtilTest, IsUnderDriveMountPoint) {
       base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
   EXPECT_TRUE(IsUnderDriveMountPoint(
       base::FilePath::FromUTF8Unsafe("/special/drive/subdir/foo.txt")));
+  EXPECT_TRUE(IsUnderDriveMountPoint(
+      base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
 }
 
 TEST(FileSystemUtilTest, ExtractDrivePath) {
@@ -72,9 +120,6 @@ TEST(FileSystemUtilTest, ExtractDrivePath) {
   EXPECT_EQ(base::FilePath(),
             ExtractDrivePath(
                 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
-  EXPECT_EQ(base::FilePath(),
-            ExtractDrivePath(
-                base::FilePath::FromUTF8Unsafe("/special/drivex/foo.txt")));
 
   EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive"),
             ExtractDrivePath(
@@ -85,9 +130,14 @@ TEST(FileSystemUtilTest, ExtractDrivePath) {
   EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/subdir/foo.txt"),
             ExtractDrivePath(base::FilePath::FromUTF8Unsafe(
                 "/special/drive/subdir/foo.txt")));
+  EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
+            ExtractDrivePath(
+                base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
 }
 
 TEST(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
+  TestingProfile profile;
+
   // Set up file system context for testing.
   base::ScopedTempDir temp_dir_;
   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
@@ -108,14 +158,14 @@ TEST(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
 
   // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar".
   const std::string& drive_mount_name =
-      GetDriveMountPointPath().BaseName().AsUTF8Unsafe();
+      GetDriveMountPointPath(&profile).BaseName().AsUTF8Unsafe();
   mount_points->RegisterFileSystem(
       drive_mount_name,
       fileapi::kFileSystemTypeDrive,
       fileapi::FileSystemMountOption(),
-      GetDriveMountPointPath());
+      GetDriveMountPointPath(&profile));
   EXPECT_EQ(
-      base::FilePath::FromUTF8Unsafe(drive_mount_name + "/foo/bar"),
+      base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
       ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
           "filesystem:chrome-extension://dummy-id/external/" +
           drive_mount_name + "/foo/bar"))));
@@ -126,9 +176,9 @@ TEST(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
       "drive2",
       fileapi::kFileSystemTypeDrive,
       fileapi::FileSystemMountOption(),
-      GetDriveMountPointPath());
+      GetDriveMountPointPath(&profile));
   EXPECT_EQ(
-      base::FilePath::FromUTF8Unsafe(drive_mount_name + "/foo/bar"),
+      base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
       ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
           "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar"))));
 
@@ -148,10 +198,10 @@ TEST(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
   std::string isolated_id =
       fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
           fileapi::kFileSystemTypeNativeForPlatformApp,
-          GetDriveMountPointPath().AppendASCII("bar/buz"),
+          GetDriveMountPointPath(&profile).AppendASCII("bar/buz"),
           &isolated_name);
   EXPECT_EQ(
-      base::FilePath::FromUTF8Unsafe(drive_mount_name + "/bar/buz"),
+      base::FilePath::FromUTF8Unsafe("drive/bar/buz"),
       ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
           "filesystem:chrome-extension://dummy-id/isolated/" +
           isolated_id + "/" + isolated_name))));