| OLD | NEW | 
|    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 <set> |    5 #include <set> | 
|    6 #include <string> |    6 #include <string> | 
|    7  |    7  | 
|    8 #include "base/bind.h" |    8 #include "base/bind.h" | 
|    9 #include "base/files/scoped_temp_dir.h" |    9 #include "base/files/scoped_temp_dir.h" | 
|   10 #include "base/format_macros.h" |   10 #include "base/format_macros.h" | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
|   25  |   25  | 
|   26 #define FPL(x) FILE_PATH_LITERAL(x) |   26 #define FPL(x) FILE_PATH_LITERAL(x) | 
|   27  |   27  | 
|   28 namespace fileapi { |   28 namespace fileapi { | 
|   29  |   29  | 
|   30 namespace { |   30 namespace { | 
|   31  |   31  | 
|   32 typedef FileSystemOperation::FileEntryList FileEntryList; |   32 typedef FileSystemOperation::FileEntryList FileEntryList; | 
|   33  |   33  | 
|   34 struct FilteringTestCase { |   34 struct FilteringTestCase { | 
|   35   const FilePath::CharType* path; |   35   const base::FilePath::CharType* path; | 
|   36   bool is_directory; |   36   bool is_directory; | 
|   37   bool visible; |   37   bool visible; | 
|   38 }; |   38 }; | 
|   39  |   39  | 
|   40 const FilteringTestCase kFilteringTestCases[] = { |   40 const FilteringTestCase kFilteringTestCases[] = { | 
|   41   // Directory should always be visible. |   41   // Directory should always be visible. | 
|   42   { FPL("hoge"), true, true }, |   42   { FPL("hoge"), true, true }, | 
|   43   { FPL("fuga.jpg"), true, true }, |   43   { FPL("fuga.jpg"), true, true }, | 
|   44   { FPL("piyo.txt"), true, true }, |   44   { FPL("piyo.txt"), true, true }, | 
|   45   { FPL("moga.cod"), true, true }, |   45   { FPL("moga.cod"), true, true }, | 
|   46  |   46  | 
|   47   // File should be visible if it's a supported media file. |   47   // File should be visible if it's a supported media file. | 
|   48   { FPL("foo"), false, false },  // File without extension. |   48   { FPL("foo"), false, false },  // File without extension. | 
|   49   { FPL("bar.jpg"), false, true },  // Supported media file. |   49   { FPL("bar.jpg"), false, true },  // Supported media file. | 
|   50   { FPL("baz.txt"), false, false },  // Non-media file. |   50   { FPL("baz.txt"), false, false },  // Non-media file. | 
|   51   { FPL("foobar.cod"), false, false },  // Unsupported media file. |   51   { FPL("foobar.cod"), false, false },  // Unsupported media file. | 
|   52 }; |   52 }; | 
|   53  |   53  | 
|   54 void ExpectEqHelper(const std::string& test_name, |   54 void ExpectEqHelper(const std::string& test_name, | 
|   55                     base::PlatformFileError expected, |   55                     base::PlatformFileError expected, | 
|   56                     base::PlatformFileError actual) { |   56                     base::PlatformFileError actual) { | 
|   57   EXPECT_EQ(expected, actual) << test_name; |   57   EXPECT_EQ(expected, actual) << test_name; | 
|   58 } |   58 } | 
|   59  |   59  | 
|   60 void ExpectMetadataEqHelper(const std::string& test_name, |   60 void ExpectMetadataEqHelper(const std::string& test_name, | 
|   61                             base::PlatformFileError expected, |   61                             base::PlatformFileError expected, | 
|   62                             bool expected_is_directory, |   62                             bool expected_is_directory, | 
|   63                             base::PlatformFileError actual, |   63                             base::PlatformFileError actual, | 
|   64                             const base::PlatformFileInfo& file_info, |   64                             const base::PlatformFileInfo& file_info, | 
|   65                             const FilePath& /*platform_path*/) { |   65                             const base::FilePath& /*platform_path*/) { | 
|   66   EXPECT_EQ(expected, actual) << test_name; |   66   EXPECT_EQ(expected, actual) << test_name; | 
|   67   if (actual == base::PLATFORM_FILE_OK) |   67   if (actual == base::PLATFORM_FILE_OK) | 
|   68     EXPECT_EQ(expected_is_directory, file_info.is_directory) << test_name; |   68     EXPECT_EQ(expected_is_directory, file_info.is_directory) << test_name; | 
|   69 } |   69 } | 
|   70  |   70  | 
|   71 void DidReadDirectory(std::set<FilePath::StringType>* content, |   71 void DidReadDirectory(std::set<base::FilePath::StringType>* content, | 
|   72                       bool* completed, |   72                       bool* completed, | 
|   73                       base::PlatformFileError error, |   73                       base::PlatformFileError error, | 
|   74                       const FileEntryList& file_list, |   74                       const FileEntryList& file_list, | 
|   75                       bool has_more) { |   75                       bool has_more) { | 
|   76   EXPECT_TRUE(!*completed); |   76   EXPECT_TRUE(!*completed); | 
|   77   *completed = !has_more; |   77   *completed = !has_more; | 
|   78   for (FileEntryList::const_iterator itr = file_list.begin(); |   78   for (FileEntryList::const_iterator itr = file_list.begin(); | 
|   79        itr != file_list.end(); ++itr) |   79        itr != file_list.end(); ++itr) | 
|   80     EXPECT_TRUE(content->insert(itr->name).second); |   80     EXPECT_TRUE(content->insert(itr->name).second); | 
|   81 } |   81 } | 
|   82  |   82  | 
|   83 void PopulateDirectoryWithTestCases(const FilePath& dir, |   83 void PopulateDirectoryWithTestCases(const base::FilePath& dir, | 
|   84                                     const FilteringTestCase* test_cases, |   84                                     const FilteringTestCase* test_cases, | 
|   85                                     size_t n) { |   85                                     size_t n) { | 
|   86   for (size_t i = 0; i < n; ++i) { |   86   for (size_t i = 0; i < n; ++i) { | 
|   87     FilePath path = dir.Append(test_cases[i].path); |   87     base::FilePath path = dir.Append(test_cases[i].path); | 
|   88     if (test_cases[i].is_directory) { |   88     if (test_cases[i].is_directory) { | 
|   89       ASSERT_TRUE(file_util::CreateDirectory(path)); |   89       ASSERT_TRUE(file_util::CreateDirectory(path)); | 
|   90     } else { |   90     } else { | 
|   91       bool created = false; |   91       bool created = false; | 
|   92       ASSERT_EQ(base::PLATFORM_FILE_OK, |   92       ASSERT_EQ(base::PLATFORM_FILE_OK, | 
|   93                 NativeFileUtil::EnsureFileExists(path, &created)); |   93                 NativeFileUtil::EnsureFileExists(path, &created)); | 
|   94       ASSERT_TRUE(created); |   94       ASSERT_TRUE(created); | 
|   95     } |   95     } | 
|   96   } |   96   } | 
|   97 } |   97 } | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  131   void TearDown() { |  131   void TearDown() { | 
|  132     isolated_context()->RemoveReference(filesystem_id_); |  132     isolated_context()->RemoveReference(filesystem_id_); | 
|  133     file_system_context_ = NULL; |  133     file_system_context_ = NULL; | 
|  134   } |  134   } | 
|  135  |  135  | 
|  136  protected: |  136  protected: | 
|  137   FileSystemContext* file_system_context() { |  137   FileSystemContext* file_system_context() { | 
|  138     return file_system_context_.get(); |  138     return file_system_context_.get(); | 
|  139   } |  139   } | 
|  140  |  140  | 
|  141   FileSystemURL CreateURL(const FilePath::CharType* test_case_path) { |  141   FileSystemURL CreateURL(const base::FilePath::CharType* test_case_path) { | 
|  142     return file_system_context_->CreateCrackedFileSystemURL( |  142     return file_system_context_->CreateCrackedFileSystemURL( | 
|  143         origin(), |  143         origin(), | 
|  144         fileapi::kFileSystemTypeIsolated, |  144         fileapi::kFileSystemTypeIsolated, | 
|  145         GetVirtualPath(test_case_path)); |  145         GetVirtualPath(test_case_path)); | 
|  146   } |  146   } | 
|  147  |  147  | 
|  148   IsolatedContext* isolated_context() { |  148   IsolatedContext* isolated_context() { | 
|  149     return IsolatedContext::GetInstance(); |  149     return IsolatedContext::GetInstance(); | 
|  150   } |  150   } | 
|  151  |  151  | 
|  152   FilePath root_path() { |  152   base::FilePath root_path() { | 
|  153     return data_dir_.path().Append(FPL("Media Directory")); |  153     return data_dir_.path().Append(FPL("Media Directory")); | 
|  154   } |  154   } | 
|  155  |  155  | 
|  156   FilePath GetVirtualPath(const FilePath::CharType* test_case_path) { |  156   base::FilePath GetVirtualPath(const base::FilePath::CharType* test_case_path) 
     { | 
|  157     return FilePath::FromUTF8Unsafe(filesystem_id_). |  157     return base::FilePath::FromUTF8Unsafe(filesystem_id_). | 
|  158                Append(FPL("Media Directory")). |  158                Append(FPL("Media Directory")). | 
|  159                Append(FilePath(test_case_path)); |  159                Append(base::FilePath(test_case_path)); | 
|  160   } |  160   } | 
|  161  |  161  | 
|  162   FileSystemFileUtil* file_util() { |  162   FileSystemFileUtil* file_util() { | 
|  163     return file_util_; |  163     return file_util_; | 
|  164   } |  164   } | 
|  165  |  165  | 
|  166   GURL origin() { |  166   GURL origin() { | 
|  167     return GURL("http://example.com"); |  167     return GURL("http://example.com"); | 
|  168   } |  168   } | 
|  169  |  169  | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  212     } |  212     } | 
|  213     MessageLoop::current()->RunUntilIdle(); |  213     MessageLoop::current()->RunUntilIdle(); | 
|  214   } |  214   } | 
|  215 } |  215 } | 
|  216  |  216  | 
|  217 TEST_F(NativeMediaFileUtilTest, ReadDirectoryFiltering) { |  217 TEST_F(NativeMediaFileUtilTest, ReadDirectoryFiltering) { | 
|  218   PopulateDirectoryWithTestCases(root_path(), |  218   PopulateDirectoryWithTestCases(root_path(), | 
|  219                                  kFilteringTestCases, |  219                                  kFilteringTestCases, | 
|  220                                  arraysize(kFilteringTestCases)); |  220                                  arraysize(kFilteringTestCases)); | 
|  221  |  221  | 
|  222   std::set<FilePath::StringType> content; |  222   std::set<base::FilePath::StringType> content; | 
|  223   FileSystemURL url = CreateURL(FPL("")); |  223   FileSystemURL url = CreateURL(FPL("")); | 
|  224   bool completed = false; |  224   bool completed = false; | 
|  225   NewOperation(url)->ReadDirectory( |  225   NewOperation(url)->ReadDirectory( | 
|  226       url, base::Bind(&DidReadDirectory, &content, &completed)); |  226       url, base::Bind(&DidReadDirectory, &content, &completed)); | 
|  227   MessageLoop::current()->RunUntilIdle(); |  227   MessageLoop::current()->RunUntilIdle(); | 
|  228   EXPECT_TRUE(completed); |  228   EXPECT_TRUE(completed); | 
|  229   EXPECT_EQ(5u, content.size()); |  229   EXPECT_EQ(5u, content.size()); | 
|  230  |  230  | 
|  231   for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |  231   for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 
|  232     FilePath::StringType name = |  232     base::FilePath::StringType name = | 
|  233         FilePath(kFilteringTestCases[i].path).BaseName().value(); |  233         base::FilePath(kFilteringTestCases[i].path).BaseName().value(); | 
|  234     std::set<FilePath::StringType>::const_iterator found = content.find(name); |  234     std::set<base::FilePath::StringType>::const_iterator found = content.find(na
     me); | 
|  235     EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); |  235     EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | 
|  236   } |  236   } | 
|  237 } |  237 } | 
|  238  |  238  | 
|  239 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { |  239 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { | 
|  240   // Run the loop twice. The second loop attempts to create files that are |  240   // Run the loop twice. The second loop attempts to create files that are | 
|  241   // pre-existing. Though the result should be the same. |  241   // pre-existing. Though the result should be the same. | 
|  242   for (int loop_count = 0; loop_count < 2; ++loop_count) { |  242   for (int loop_count = 0; loop_count < 2; ++loop_count) { | 
|  243     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |  243     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 
|  244       FileSystemURL root_url = CreateURL(FPL("")); |  244       FileSystemURL root_url = CreateURL(FPL("")); | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
|  260       } else { |  260       } else { | 
|  261         operation->CreateFile( |  261         operation->CreateFile( | 
|  262             url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); |  262             url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); | 
|  263       } |  263       } | 
|  264       MessageLoop::current()->RunUntilIdle(); |  264       MessageLoop::current()->RunUntilIdle(); | 
|  265     } |  265     } | 
|  266   } |  266   } | 
|  267 } |  267 } | 
|  268  |  268  | 
|  269 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { |  269 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { | 
|  270   FilePath dest_path = root_path().AppendASCII("dest"); |  270   base::FilePath dest_path = root_path().AppendASCII("dest"); | 
|  271   FileSystemURL dest_url = CreateURL(FPL("dest")); |  271   FileSystemURL dest_url = CreateURL(FPL("dest")); | 
|  272  |  272  | 
|  273   // Run the loop twice. The first run has no source files. The second run does. |  273   // Run the loop twice. The first run has no source files. The second run does. | 
|  274   for (int loop_count = 0; loop_count < 2; ++loop_count) { |  274   for (int loop_count = 0; loop_count < 2; ++loop_count) { | 
|  275     if (loop_count == 1) { |  275     if (loop_count == 1) { | 
|  276       PopulateDirectoryWithTestCases(root_path(), |  276       PopulateDirectoryWithTestCases(root_path(), | 
|  277                                      kFilteringTestCases, |  277                                      kFilteringTestCases, | 
|  278                                      arraysize(kFilteringTestCases)); |  278                                      arraysize(kFilteringTestCases)); | 
|  279     } |  279     } | 
|  280     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |  280     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  313       // Reset the test directory between the two loops to remove old |  313       // Reset the test directory between the two loops to remove old | 
|  314       // directories and create new ones that should pre-exist. |  314       // directories and create new ones that should pre-exist. | 
|  315       ASSERT_TRUE(file_util::Delete(root_path(), true)); |  315       ASSERT_TRUE(file_util::Delete(root_path(), true)); | 
|  316       ASSERT_TRUE(file_util::CreateDirectory(root_path())); |  316       ASSERT_TRUE(file_util::CreateDirectory(root_path())); | 
|  317       PopulateDirectoryWithTestCases(root_path(), |  317       PopulateDirectoryWithTestCases(root_path(), | 
|  318                                      kFilteringTestCases, |  318                                      kFilteringTestCases, | 
|  319                                      arraysize(kFilteringTestCases)); |  319                                      arraysize(kFilteringTestCases)); | 
|  320     } |  320     } | 
|  321  |  321  | 
|  322     // Always create a dummy source data file. |  322     // Always create a dummy source data file. | 
|  323     FilePath src_path = root_path().AppendASCII("foo.jpg"); |  323     base::FilePath src_path = root_path().AppendASCII("foo.jpg"); | 
|  324     FileSystemURL src_url = CreateURL(FPL("foo.jpg")); |  324     FileSystemURL src_url = CreateURL(FPL("foo.jpg")); | 
|  325     static const char kDummyData[] = "dummy"; |  325     static const char kDummyData[] = "dummy"; | 
|  326     ASSERT_TRUE(file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); |  326     ASSERT_TRUE(file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); | 
|  327  |  327  | 
|  328     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |  328     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 
|  329       if (loop_count == 0 && kFilteringTestCases[i].is_directory) { |  329       if (loop_count == 0 && kFilteringTestCases[i].is_directory) { | 
|  330         // These directories do not exist in this case, so Copy() will not |  330         // These directories do not exist in this case, so Copy() will not | 
|  331         // treat them as directories. Thus invalidating these test cases. |  331         // treat them as directories. Thus invalidating these test cases. | 
|  332         // Continue now to avoid creating a new |operation| below that goes |  332         // Continue now to avoid creating a new |operation| below that goes | 
|  333         // unused. |  333         // unused. | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  365         } |  365         } | 
|  366       } |  366       } | 
|  367       operation->Copy( |  367       operation->Copy( | 
|  368           src_url, url, base::Bind(&ExpectEqHelper, test_name, expectation)); |  368           src_url, url, base::Bind(&ExpectEqHelper, test_name, expectation)); | 
|  369       MessageLoop::current()->RunUntilIdle(); |  369       MessageLoop::current()->RunUntilIdle(); | 
|  370     } |  370     } | 
|  371   } |  371   } | 
|  372 } |  372 } | 
|  373  |  373  | 
|  374 TEST_F(NativeMediaFileUtilTest, MoveSourceFiltering) { |  374 TEST_F(NativeMediaFileUtilTest, MoveSourceFiltering) { | 
|  375   FilePath dest_path = root_path().AppendASCII("dest"); |  375   base::FilePath dest_path = root_path().AppendASCII("dest"); | 
|  376   FileSystemURL dest_url = CreateURL(FPL("dest")); |  376   FileSystemURL dest_url = CreateURL(FPL("dest")); | 
|  377  |  377  | 
|  378   // Run the loop twice. The first run has no source files. The second run does. |  378   // Run the loop twice. The first run has no source files. The second run does. | 
|  379   for (int loop_count = 0; loop_count < 2; ++loop_count) { |  379   for (int loop_count = 0; loop_count < 2; ++loop_count) { | 
|  380     if (loop_count == 1) { |  380     if (loop_count == 1) { | 
|  381       PopulateDirectoryWithTestCases(root_path(), |  381       PopulateDirectoryWithTestCases(root_path(), | 
|  382                                      kFilteringTestCases, |  382                                      kFilteringTestCases, | 
|  383                                      arraysize(kFilteringTestCases)); |  383                                      arraysize(kFilteringTestCases)); | 
|  384     } |  384     } | 
|  385     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |  385     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  427     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { |  427     for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | 
|  428       if (loop_count == 0 && kFilteringTestCases[i].is_directory) { |  428       if (loop_count == 0 && kFilteringTestCases[i].is_directory) { | 
|  429         // These directories do not exist in this case, so Copy() will not |  429         // These directories do not exist in this case, so Copy() will not | 
|  430         // treat them as directories. Thus invalidating these test cases. |  430         // treat them as directories. Thus invalidating these test cases. | 
|  431         // Continue now to avoid creating a new |operation| below that goes |  431         // Continue now to avoid creating a new |operation| below that goes | 
|  432         // unused. |  432         // unused. | 
|  433         continue; |  433         continue; | 
|  434       } |  434       } | 
|  435  |  435  | 
|  436       // Create the source file for every test case because it might get moved. |  436       // Create the source file for every test case because it might get moved. | 
|  437       FilePath src_path = root_path().AppendASCII("foo.jpg"); |  437       base::FilePath src_path = root_path().AppendASCII("foo.jpg"); | 
|  438       FileSystemURL src_url = CreateURL(FPL("foo.jpg")); |  438       FileSystemURL src_url = CreateURL(FPL("foo.jpg")); | 
|  439       static const char kDummyData[] = "dummy"; |  439       static const char kDummyData[] = "dummy"; | 
|  440       ASSERT_TRUE( |  440       ASSERT_TRUE( | 
|  441           file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); |  441           file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); | 
|  442  |  442  | 
|  443       FileSystemURL root_url = CreateURL(FPL("")); |  443       FileSystemURL root_url = CreateURL(FPL("")); | 
|  444       FileSystemOperation* operation = NewOperation(root_url); |  444       FileSystemOperation* operation = NewOperation(root_url); | 
|  445  |  445  | 
|  446       FileSystemURL url = CreateURL(kFilteringTestCases[i].path); |  446       FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | 
|  447  |  447  | 
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  592         expectation = base::PLATFORM_FILE_ERROR_FAILED; |  592         expectation = base::PLATFORM_FILE_ERROR_FAILED; | 
|  593       } |  593       } | 
|  594       operation->TouchFile( |  594       operation->TouchFile( | 
|  595           url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); |  595           url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); | 
|  596       MessageLoop::current()->RunUntilIdle(); |  596       MessageLoop::current()->RunUntilIdle(); | 
|  597     } |  597     } | 
|  598   } |  598   } | 
|  599 } |  599 } | 
|  600  |  600  | 
|  601 }  // namespace fileapi |  601 }  // namespace fileapi | 
| OLD | NEW |