OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 <string> | |
6 #include <vector> | |
7 | |
8 #include "base/files/file_path.h" | |
9 #include "base/macros.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_document.h" | |
12 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" | |
13 #include "components/arc/common/file_system.mojom.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "url/gurl.h" | |
16 | |
17 namespace arc { | |
18 | |
19 namespace { | |
20 | |
21 class ArcDocumentsProviderDocumentTest : public testing::Test { | |
22 public: | |
23 ArcDocumentsProviderDocumentTest() = default; | |
24 ~ArcDocumentsProviderDocumentTest() override = default; | |
25 | |
26 void SetUp() override { | |
27 root_directory_.reset(new ArcDocumentsProviderDocument("root", true)); | |
Luis Héctor Chávez
2016/12/15 23:54:26
nit: s/.reset(new X(...))/ = base::MakeUnique<X>(.
Shuhei Takahashi
2016/12/16 05:47:14
Done.
| |
28 EXPECT_EQ("root", root_directory_->document_id()); | |
29 EXPECT_TRUE(root_directory_->is_directory()); | |
30 } | |
31 | |
32 protected: | |
33 std::unique_ptr<ArcDocumentsProviderDocument> root_directory_; | |
34 | |
35 private: | |
36 DISALLOW_COPY_AND_ASSIGN(ArcDocumentsProviderDocumentTest); | |
37 }; | |
38 | |
39 mojom::DocumentPtr MakeDocument(const std::string& document_id, | |
40 const std::string& display_name, | |
41 std::string mime_type) { | |
42 mojom::DocumentPtr document = mojom::Document::New(); | |
43 document->document_id = document_id; | |
44 document->display_name = display_name; | |
45 document->mime_type = mime_type; | |
46 return document; | |
47 } | |
48 | |
49 std::string DocumentTreeToDebugString(ArcDocumentsProviderDocument* document, | |
50 int indent_level = 0) { | |
51 const std::string indent(indent_level, ' '); | |
52 std::string text = | |
53 base::StringPrintf("(id=%s, dir=%s)\n", document->document_id().c_str(), | |
54 document->is_directory() ? "true" : "false"); | |
55 if (document->is_directory()) { | |
56 for (const auto& pair : document->children()) { | |
57 text += base::StringPrintf( | |
58 "%s- %s %s", indent.c_str(), pair.first.c_str(), | |
59 DocumentTreeToDebugString(pair.second.get(), indent_level + 2) | |
60 .c_str()); | |
61 } | |
62 } | |
63 return text; | |
64 } | |
65 | |
66 } // namespace | |
67 | |
68 TEST_F(ArcDocumentsProviderDocumentTest, Basic) { | |
69 { | |
70 std::vector<mojom::DocumentPtr> documents; | |
71 documents.emplace_back( | |
72 MakeDocument("pet-id", "pet", kAndroidDirectoryMimeType)); | |
73 root_directory_->UpdateWithChildDocuments(documents); | |
74 } | |
75 | |
76 EXPECT_EQ( | |
77 "(id=root, dir=true)\n" | |
78 "- pet (id=pet-id, dir=true)\n", | |
79 DocumentTreeToDebugString(root_directory_.get())); | |
80 | |
81 ArcDocumentsProviderDocument* pet = | |
82 root_directory_->Lookup(base::FilePath(FILE_PATH_LITERAL("pet"))); | |
83 ASSERT_TRUE(pet); | |
84 | |
85 { | |
86 std::vector<mojom::DocumentPtr> documents; | |
87 documents.emplace_back(MakeDocument("cat-id", "cat.jpg", "image/jpeg")); | |
88 documents.emplace_back(MakeDocument("dog-id", "dog.mp3", "audio/mp3")); | |
89 pet->UpdateWithChildDocuments(documents); | |
90 } | |
91 | |
92 EXPECT_EQ( | |
93 "(id=root, dir=true)\n" | |
94 "- pet (id=pet-id, dir=true)\n" | |
95 " - cat.jpg (id=cat-id, dir=false)\n" | |
96 " - dog.mp3 (id=dog-id, dir=false)\n", | |
97 DocumentTreeToDebugString(root_directory_.get())); | |
98 | |
99 EXPECT_TRUE(root_directory_->Lookup( | |
100 base::FilePath(FILE_PATH_LITERAL("pet/cat.jpg")))); | |
101 EXPECT_TRUE(root_directory_->Lookup( | |
102 base::FilePath(FILE_PATH_LITERAL("pet/dog.mp3")))); | |
103 } | |
104 | |
105 TEST_F(ArcDocumentsProviderDocumentTest, ReplaceSlash) { | |
106 { | |
107 std::vector<mojom::DocumentPtr> documents; | |
108 documents.emplace_back(MakeDocument("test", "\\^o^/.jpg", "image/jpeg")); | |
109 root_directory_->UpdateWithChildDocuments(documents); | |
110 } | |
111 | |
112 EXPECT_EQ( | |
113 "(id=root, dir=true)\n" | |
114 "- \\^o^_.jpg (id=test, dir=false)\n", | |
115 DocumentTreeToDebugString(root_directory_.get())); | |
116 } | |
117 | |
118 TEST_F(ArcDocumentsProviderDocumentTest, ReplaceDots) { | |
119 { | |
120 std::vector<mojom::DocumentPtr> documents; | |
121 documents.emplace_back( | |
122 MakeDocument("dot1", ".", kAndroidDirectoryMimeType)); | |
123 documents.emplace_back( | |
124 MakeDocument("dot2", "..", kAndroidDirectoryMimeType)); | |
125 root_directory_->UpdateWithChildDocuments(documents); | |
126 } | |
127 | |
128 EXPECT_EQ( | |
129 "(id=root, dir=true)\n" | |
130 "- dot (id=dot1, dir=true)\n" | |
131 "- dotdot (id=dot2, dir=true)\n", | |
132 DocumentTreeToDebugString(root_directory_.get())); | |
133 } | |
134 | |
135 TEST_F(ArcDocumentsProviderDocumentTest, AppendExtension) { | |
136 { | |
137 std::vector<mojom::DocumentPtr> documents; | |
138 documents.emplace_back(MakeDocument("cow", "cow.bin", "image/jpeg")); | |
139 documents.emplace_back(MakeDocument("rat", "rat.bin", "image/x-unknown")); | |
140 root_directory_->UpdateWithChildDocuments(documents); | |
141 } | |
142 | |
143 EXPECT_EQ( | |
144 "(id=root, dir=true)\n" | |
145 "- cow.bin.jpg (id=cow, dir=false)\n" | |
146 "- rat.bin (id=rat, dir=false)\n", | |
147 DocumentTreeToDebugString(root_directory_.get())); | |
148 } | |
149 | |
150 TEST_F(ArcDocumentsProviderDocumentTest, DuplicatedNames) { | |
151 { | |
152 std::vector<mojom::DocumentPtr> documents; | |
153 documents.emplace_back(MakeDocument("fox1", "fox.jpg", "image/jpeg")); | |
154 documents.emplace_back(MakeDocument("fox2", "fox.jpg", "image/jpeg")); | |
155 documents.emplace_back(MakeDocument("fox3", "fox.jpg", "image/jpeg")); | |
156 documents.emplace_back(MakeDocument("fox4", "fox 2.jpg", "image/jpeg")); | |
157 root_directory_->UpdateWithChildDocuments(documents); | |
158 } | |
159 | |
160 EXPECT_EQ( | |
161 "(id=root, dir=true)\n" | |
162 "- fox 1.jpg (id=fox2, dir=false)\n" | |
163 "- fox 2.jpg (id=fox4, dir=false)\n" | |
164 "- fox 3.jpg (id=fox3, dir=false)\n" | |
165 "- fox.jpg (id=fox1, dir=false)\n", | |
166 DocumentTreeToDebugString(root_directory_.get())); | |
167 } | |
168 | |
169 TEST_F(ArcDocumentsProviderDocumentTest, KeepMapping) { | |
170 { | |
171 std::vector<mojom::DocumentPtr> documents; | |
172 documents.emplace_back(MakeDocument("fox1", "fox.jpg", "image/jpeg")); | |
173 documents.emplace_back(MakeDocument("fox2", "fox.jpg", "image/jpeg")); | |
174 documents.emplace_back(MakeDocument("fox3", "fox.jpg", "image/jpeg")); | |
175 root_directory_->UpdateWithChildDocuments(documents); | |
176 } | |
177 | |
178 EXPECT_EQ( | |
179 "(id=root, dir=true)\n" | |
180 "- fox 1.jpg (id=fox2, dir=false)\n" | |
181 "- fox 2.jpg (id=fox3, dir=false)\n" | |
182 "- fox.jpg (id=fox1, dir=false)\n", | |
183 DocumentTreeToDebugString(root_directory_.get())); | |
184 | |
185 { | |
186 std::vector<mojom::DocumentPtr> documents; | |
187 documents.emplace_back(MakeDocument("fox2", "fox.jpg", "image/jpeg")); | |
188 documents.emplace_back(MakeDocument("fox3", "fox.jpg", "image/jpeg")); | |
189 root_directory_->UpdateWithChildDocuments(documents); | |
190 } | |
191 | |
192 EXPECT_EQ( | |
193 "(id=root, dir=true)\n" | |
194 "- fox 1.jpg (id=fox2, dir=false)\n" | |
195 "- fox 2.jpg (id=fox3, dir=false)\n", | |
196 DocumentTreeToDebugString(root_directory_.get())); | |
197 | |
198 { | |
199 std::vector<mojom::DocumentPtr> documents; | |
200 documents.emplace_back(MakeDocument("fox2", "fox.jpg", "image/jpeg")); | |
201 documents.emplace_back(MakeDocument("fox3", "fox.jpg", "image/jpeg")); | |
202 documents.emplace_back(MakeDocument("fox4", "fox.jpg", "image/jpeg")); | |
203 documents.emplace_back(MakeDocument("fox5", "fox.jpg", "image/jpeg")); | |
204 root_directory_->UpdateWithChildDocuments(documents); | |
205 } | |
206 | |
207 EXPECT_EQ( | |
208 "(id=root, dir=true)\n" | |
209 "- fox 1.jpg (id=fox2, dir=false)\n" | |
210 "- fox 2.jpg (id=fox3, dir=false)\n" | |
211 "- fox 3.jpg (id=fox5, dir=false)\n" | |
212 "- fox.jpg (id=fox4, dir=false)\n", | |
213 DocumentTreeToDebugString(root_directory_.get())); | |
214 } | |
215 | |
216 } // namespace arc | |
OLD | NEW |