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
|
// Copyright (c) 2011 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.
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/i18n/file_util_icu.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "net/base/directory_lister.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
class ListerDelegate : public DirectoryLister::DirectoryListerDelegate {
public:
explicit ListerDelegate(bool recursive) : error_(-1), recursive_(recursive) {
}
void OnListFile(const DirectoryLister::DirectoryListerData& data) {
file_list_.push_back(data.info);
paths_.push_back(data.path);
}
void OnListDone(int error) {
error_ = error;
MessageLoop::current()->Quit();
if (recursive_)
CheckRecursiveSort();
else
CheckSort();
}
void CheckRecursiveSort() {
// Check that we got files in the right order.
if (!file_list_.empty()) {
for (size_t previous = 0, current = 1;
current < file_list_.size();
previous++, current++) {
EXPECT_TRUE(file_util::LocaleAwareCompareFilenames(
paths_[previous], paths_[current]));
}
}
}
void CheckSort() {
// Check that we got files in the right order.
if (!file_list_.empty()) {
for (size_t previous = 0, current = 1;
current < file_list_.size();
previous++, current++) {
// Directories should come before files.
if (file_util::FileEnumerator::IsDirectory(file_list_[previous]) &&
!file_util::FileEnumerator::IsDirectory(file_list_[current])) {
continue;
}
EXPECT_FALSE(file_util::IsDotDot(
file_util::FileEnumerator::GetFilename(file_list_[current])));
EXPECT_EQ(file_util::FileEnumerator::IsDirectory(file_list_[previous]),
file_util::FileEnumerator::IsDirectory(file_list_[current]));
EXPECT_TRUE(file_util::LocaleAwareCompareFilenames(
file_util::FileEnumerator::GetFilename(file_list_[previous]),
file_util::FileEnumerator::GetFilename(file_list_[current])));
}
}
}
int error() const { return error_; }
private:
int error_;
bool recursive_;
std::vector<file_util::FileEnumerator::FindInfo> file_list_;
std::vector<FilePath> paths_;
};
TEST(DirectoryListerTest, BigDirTest) {
FilePath path;
ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path));
ListerDelegate delegate(false);
scoped_refptr<DirectoryLister> lister(
new DirectoryLister(path, &delegate));
lister->Start();
MessageLoop::current()->Run();
EXPECT_EQ(delegate.error(), OK);
}
TEST(DirectoryListerTest, BigDirRecursiveTest) {
FilePath path;
ASSERT_TRUE(PathService::Get(base::DIR_EXE, &path));
ListerDelegate delegate(true);
scoped_refptr<DirectoryLister> lister(
new DirectoryLister(path, true, DirectoryLister::FULL_PATH, &delegate));
lister->Start();
MessageLoop::current()->Run();
EXPECT_EQ(delegate.error(), OK);
}
TEST(DirectoryListerTest, CancelTest) {
FilePath path;
ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path));
ListerDelegate delegate(false);
scoped_refptr<DirectoryLister> lister(
new DirectoryLister(path, &delegate));
lister->Start();
lister->Cancel();
MessageLoop::current()->Run();
EXPECT_EQ(delegate.error(), ERR_ABORTED);
}
} // namespace net
|