Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/file_path.h"
      6 #include "base/file_util.h"
      7 #include "base/i18n/file_util_icu.h"
      8 #include "base/logging.h"
      9 #include "base/message_loop.h"
     10 #include "base/path_service.h"
     11 #include "net/base/directory_lister.h"
     12 #include "net/base/net_errors.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace net {
     16 
     17 class ListerDelegate : public DirectoryLister::DirectoryListerDelegate {
     18  public:
     19   explicit ListerDelegate(bool recursive) : error_(-1), recursive_(recursive) {
     20   }
     21   void OnListFile(const DirectoryLister::DirectoryListerData& data) {
     22     file_list_.push_back(data.info);
     23     paths_.push_back(data.path);
     24   }
     25   void OnListDone(int error) {
     26     error_ = error;
     27     MessageLoop::current()->Quit();
     28     if (recursive_)
     29       CheckRecursiveSort();
     30     else
     31       CheckSort();
     32   }
     33   void CheckRecursiveSort() {
     34     // Check that we got files in the right order.
     35     if (!file_list_.empty()) {
     36       for (size_t previous = 0, current = 1;
     37            current < file_list_.size();
     38            previous++, current++) {
     39         EXPECT_TRUE(file_util::LocaleAwareCompareFilenames(
     40             paths_[previous], paths_[current]));
     41       }
     42     }
     43   }
     44   void CheckSort() {
     45     // Check that we got files in the right order.
     46     if (!file_list_.empty()) {
     47       for (size_t previous = 0, current = 1;
     48            current < file_list_.size();
     49            previous++, current++) {
     50         // Directories should come before files.
     51         if (file_util::FileEnumerator::IsDirectory(file_list_[previous]) &&
     52             !file_util::FileEnumerator::IsDirectory(file_list_[current])) {
     53           continue;
     54         }
     55         EXPECT_FALSE(file_util::IsDotDot(
     56             file_util::FileEnumerator::GetFilename(file_list_[current])));
     57         EXPECT_EQ(file_util::FileEnumerator::IsDirectory(file_list_[previous]),
     58                   file_util::FileEnumerator::IsDirectory(file_list_[current]));
     59         EXPECT_TRUE(file_util::LocaleAwareCompareFilenames(
     60             file_util::FileEnumerator::GetFilename(file_list_[previous]),
     61             file_util::FileEnumerator::GetFilename(file_list_[current])));
     62       }
     63     }
     64   }
     65   int error() const { return error_; }
     66  private:
     67   int error_;
     68   bool recursive_;
     69   std::vector<file_util::FileEnumerator::FindInfo> file_list_;
     70   std::vector<FilePath> paths_;
     71 };
     72 
     73 TEST(DirectoryListerTest, BigDirTest) {
     74   FilePath path;
     75   ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path));
     76 
     77   ListerDelegate delegate(false);
     78   scoped_refptr<DirectoryLister> lister(
     79       new DirectoryLister(path, &delegate));
     80 
     81   lister->Start();
     82 
     83   MessageLoop::current()->Run();
     84 
     85   EXPECT_EQ(delegate.error(), OK);
     86 }
     87 
     88 TEST(DirectoryListerTest, BigDirRecursiveTest) {
     89   FilePath path;
     90   ASSERT_TRUE(PathService::Get(base::DIR_EXE, &path));
     91 
     92   ListerDelegate delegate(true);
     93   scoped_refptr<DirectoryLister> lister(
     94       new DirectoryLister(path, true, DirectoryLister::FULL_PATH, &delegate));
     95 
     96   lister->Start();
     97 
     98   MessageLoop::current()->Run();
     99 
    100   EXPECT_EQ(delegate.error(), OK);
    101 }
    102 
    103 TEST(DirectoryListerTest, CancelTest) {
    104   FilePath path;
    105   ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path));
    106 
    107   ListerDelegate delegate(false);
    108   scoped_refptr<DirectoryLister> lister(
    109       new DirectoryLister(path, &delegate));
    110 
    111   lister->Start();
    112   lister->Cancel();
    113 
    114   MessageLoop::current()->Run();
    115 
    116   EXPECT_EQ(delegate.error(), ERR_ABORTED);
    117 }
    118 
    119 }  // namespace net
    120