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