1 // Copyright 2013 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.
5 #include "chrome/browser/drive/drive_api_util.h"
7 #include "base/files/scoped_temp_dir.h"
9 #include "google_apis/drive/drive_api_parser.h"
10 #include "google_apis/drive/gdata_wapi_parser.h"
11 #include "google_apis/drive/test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 TEST(DriveApiUtilTest
, EscapeQueryStringValue
) {
19 EXPECT_EQ("abcde", EscapeQueryStringValue("abcde"));
20 EXPECT_EQ("\\'", EscapeQueryStringValue("'"));
21 EXPECT_EQ("\\'abcde\\'", EscapeQueryStringValue("'abcde'"));
22 EXPECT_EQ("\\\\", EscapeQueryStringValue("\\"));
23 EXPECT_EQ("\\\\\\'", EscapeQueryStringValue("\\'"));
26 TEST(DriveApiUtilTest
, TranslateQuery
) {
27 EXPECT_EQ("", TranslateQuery(""));
28 EXPECT_EQ("fullText contains 'dog'", TranslateQuery("dog"));
29 EXPECT_EQ("fullText contains 'dog' and fullText contains 'cat'",
30 TranslateQuery("dog cat"));
31 EXPECT_EQ("not fullText contains 'cat'", TranslateQuery("-cat"));
32 EXPECT_EQ("fullText contains 'dog cat'", TranslateQuery("\"dog cat\""));
34 // Should handles full-width white space correctly.
35 // Note: \xE3\x80\x80 (\u3000) is Ideographic Space (a.k.a. Japanese
36 // full-width whitespace).
37 EXPECT_EQ("fullText contains 'dog' and fullText contains 'cat'",
38 TranslateQuery("dog" "\xE3\x80\x80" "cat"));
40 // If the quoted token is not closed (i.e. the last '"' is missing),
41 // we handle the remaining string is one token, as a fallback.
42 EXPECT_EQ("fullText contains 'dog cat'", TranslateQuery("\"dog cat"));
44 // For quoted text with leading '-'.
45 EXPECT_EQ("not fullText contains 'dog cat'", TranslateQuery("-\"dog cat\""));
47 // Empty tokens should be simply ignored.
48 EXPECT_EQ("", TranslateQuery("-"));
49 EXPECT_EQ("", TranslateQuery("\"\""));
50 EXPECT_EQ("", TranslateQuery("-\"\""));
51 EXPECT_EQ("", TranslateQuery("\"\"\"\""));
52 EXPECT_EQ("", TranslateQuery("\"\" \"\""));
53 EXPECT_EQ("fullText contains 'dog'", TranslateQuery("\"\" dog \"\""));
56 TEST(FileSystemUtilTest
, ExtractResourceIdFromUrl
) {
57 EXPECT_EQ("file:2_file_resource_id", ExtractResourceIdFromUrl(
58 GURL("https://file1_link_self/file:2_file_resource_id")));
59 // %3A should be unescaped.
60 EXPECT_EQ("file:2_file_resource_id", ExtractResourceIdFromUrl(
61 GURL("https://file1_link_self/file%3A2_file_resource_id")));
63 // The resource ID cannot be extracted, hence empty.
64 EXPECT_EQ("", ExtractResourceIdFromUrl(GURL("https://www.example.com/")));
67 TEST(FileSystemUtilTest
, CanonicalizeResourceId
) {
68 std::string
resource_id("1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug");
70 // New style ID is unchanged.
71 EXPECT_EQ(resource_id
, CanonicalizeResourceId(resource_id
));
73 // Drop prefixes from old style IDs.
74 EXPECT_EQ(resource_id
, CanonicalizeResourceId("document:" + resource_id
));
75 EXPECT_EQ(resource_id
, CanonicalizeResourceId("spreadsheet:" + resource_id
));
76 EXPECT_EQ(resource_id
, CanonicalizeResourceId("presentation:" + resource_id
));
77 EXPECT_EQ(resource_id
, CanonicalizeResourceId("drawing:" + resource_id
));
78 EXPECT_EQ(resource_id
, CanonicalizeResourceId("table:" + resource_id
));
79 EXPECT_EQ(resource_id
, CanonicalizeResourceId("externalapp:" + resource_id
));
82 TEST(FileSystemUtilTest
, ConvertFileResourceToResource_Parents
) {
83 google_apis::FileResource file_resource
;
85 std::vector
<GURL
> expected_links
;
86 expected_links
.push_back(GURL("http://server/id1"));
87 expected_links
.push_back(GURL("http://server/id2"));
88 expected_links
.push_back(GURL("http://server/id3"));
90 for (size_t i
= 0; i
< expected_links
.size(); ++i
) {
91 google_apis::ParentReference parent
;
92 parent
.set_parent_link(expected_links
[i
]);
93 file_resource
.mutable_parents()->push_back(parent
);
96 scoped_ptr
<google_apis::ResourceEntry
> entry(
97 ConvertFileResourceToResourceEntry(file_resource
));
98 std::vector
<GURL
> actual_links
;
99 for (size_t i
= 0; i
< entry
->links().size(); ++i
) {
100 if (entry
->links()[i
]->type() == google_apis::Link::LINK_PARENT
)
101 actual_links
.push_back(entry
->links()[i
]->href());
104 EXPECT_EQ(expected_links
, actual_links
);
107 TEST(FileSystemUtilTest
, ConvertFileResourceToResourceEntryImageMediaMetadata
) {
108 google_apis::FileResource file_resource_all_fields
;
109 google_apis::FileResource file_resource_zero_fields
;
110 google_apis::FileResource file_resource_no_fields
;
111 // Set up FileResource instances;
114 google_apis::ImageMediaMetadata
* image_media_metadata
=
115 file_resource_all_fields
.mutable_image_media_metadata();
116 image_media_metadata
->set_width(640);
117 image_media_metadata
->set_height(480);
118 image_media_metadata
->set_rotation(90);
121 google_apis::ImageMediaMetadata
* image_media_metadata
=
122 file_resource_zero_fields
.mutable_image_media_metadata();
123 image_media_metadata
->set_width(0);
124 image_media_metadata
->set_height(0);
125 image_media_metadata
->set_rotation(0);
129 // Verify the converted values.
131 scoped_ptr
<google_apis::ResourceEntry
> resource_entry(
132 ConvertFileResourceToResourceEntry(file_resource_all_fields
));
134 EXPECT_EQ(640, resource_entry
->image_width());
135 EXPECT_EQ(480, resource_entry
->image_height());
136 EXPECT_EQ(90, resource_entry
->image_rotation());
139 scoped_ptr
<google_apis::ResourceEntry
> resource_entry(
140 ConvertFileResourceToResourceEntry(file_resource_zero_fields
));
142 EXPECT_EQ(0, resource_entry
->image_width());
143 EXPECT_EQ(0, resource_entry
->image_height());
144 EXPECT_EQ(0, resource_entry
->image_rotation());
147 scoped_ptr
<google_apis::ResourceEntry
> resource_entry(
148 ConvertFileResourceToResourceEntry(file_resource_no_fields
));
150 EXPECT_EQ(-1, resource_entry
->image_width());
151 EXPECT_EQ(-1, resource_entry
->image_height());
152 EXPECT_EQ(-1, resource_entry
->image_rotation());
156 TEST(DriveAPIUtilTest
, GetMd5Digest
) {
157 base::ScopedTempDir temp_dir
;
158 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
160 base::FilePath path
= temp_dir
.path().AppendASCII("test.txt");
161 const char kTestData
[] = "abcdefghijklmnopqrstuvwxyz0123456789";
162 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(path
, kTestData
));
164 EXPECT_EQ(base::MD5String(kTestData
), GetMd5Digest(path
));
167 TEST(DriveAPIUtilTest
, HasHostedDocumentExtension
) {
169 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdoc")));
171 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gsheet")));
173 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gslides")));
175 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdraw")));
177 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gtable")));
179 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gform")));
181 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gmaps")));
183 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.glink")));
186 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdocs")));
188 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.docx")));
190 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.jpg")));
192 HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gmap")));