Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / drive / drive_api_util_unittest.cc
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.
4
5 #include "chrome/browser/drive/drive_api_util.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/md5.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"
13 #include "url/gurl.h"
14
15 namespace drive {
16 namespace util {
17
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("\\'"));
24 }
25
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\""));
33
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"));
39
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"));
43
44   // For quoted text with leading '-'.
45   EXPECT_EQ("not fullText contains 'dog cat'", TranslateQuery("-\"dog cat\""));
46
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 \"\""));
54 }
55
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")));
62
63   // The resource ID cannot be extracted, hence empty.
64   EXPECT_EQ("", ExtractResourceIdFromUrl(GURL("https://www.example.com/")));
65 }
66
67 TEST(FileSystemUtilTest, CanonicalizeResourceId) {
68   std::string resource_id("1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug");
69
70   // New style ID is unchanged.
71   EXPECT_EQ(resource_id, CanonicalizeResourceId(resource_id));
72
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));
80 }
81
82 TEST(FileSystemUtilTest, ConvertFileResourceToResource_Parents) {
83   google_apis::FileResource file_resource;
84
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"));
89
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);
94   }
95
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());
102   }
103
104   EXPECT_EQ(expected_links, actual_links);
105 }
106
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;
112   {
113     {
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);
119     }
120     {
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);
126     }
127   }
128
129   // Verify the converted values.
130   {
131     scoped_ptr<google_apis::ResourceEntry> resource_entry(
132         ConvertFileResourceToResourceEntry(file_resource_all_fields));
133
134     EXPECT_EQ(640, resource_entry->image_width());
135     EXPECT_EQ(480, resource_entry->image_height());
136     EXPECT_EQ(90, resource_entry->image_rotation());
137   }
138   {
139     scoped_ptr<google_apis::ResourceEntry> resource_entry(
140         ConvertFileResourceToResourceEntry(file_resource_zero_fields));
141
142     EXPECT_EQ(0, resource_entry->image_width());
143     EXPECT_EQ(0, resource_entry->image_height());
144     EXPECT_EQ(0, resource_entry->image_rotation());
145   }
146   {
147     scoped_ptr<google_apis::ResourceEntry> resource_entry(
148         ConvertFileResourceToResourceEntry(file_resource_no_fields));
149
150     EXPECT_EQ(-1, resource_entry->image_width());
151     EXPECT_EQ(-1, resource_entry->image_height());
152     EXPECT_EQ(-1, resource_entry->image_rotation());
153   }
154 }
155
156 TEST(FileSystemUtilTest, ConvertResourceEntryToFileResourceImageMediaMetadata) {
157   google_apis::ResourceEntry resource_entry_all_fields;
158   google_apis::ResourceEntry resource_entry_zero_fields;
159   google_apis::ResourceEntry resource_entry_no_fields;
160   // Set up FileResource instances;
161   {
162     resource_entry_all_fields.set_image_width(640);
163     resource_entry_all_fields.set_image_height(480);
164     resource_entry_all_fields.set_image_rotation(90);
165
166     resource_entry_zero_fields.set_image_width(0);
167     resource_entry_zero_fields.set_image_height(0);
168     resource_entry_zero_fields.set_image_rotation(0);
169   }
170
171   // Verify the converted values.
172   {
173     scoped_ptr<google_apis::FileResource> file_resource(
174         ConvertResourceEntryToFileResource(resource_entry_all_fields));
175     const google_apis::ImageMediaMetadata& image_media_metadata =
176       file_resource->image_media_metadata();
177     EXPECT_EQ(640, image_media_metadata.width());
178     EXPECT_EQ(480, image_media_metadata.height());
179     EXPECT_EQ(90, image_media_metadata.rotation());
180   }
181   {
182     scoped_ptr<google_apis::FileResource> file_resource(
183         ConvertResourceEntryToFileResource(resource_entry_zero_fields));
184     const google_apis::ImageMediaMetadata& image_media_metadata =
185       file_resource->image_media_metadata();
186     EXPECT_EQ(0, image_media_metadata.width());
187     EXPECT_EQ(0, image_media_metadata.height());
188     EXPECT_EQ(0, image_media_metadata.rotation());
189   }
190   {
191     scoped_ptr<google_apis::FileResource> file_resource(
192         ConvertResourceEntryToFileResource(resource_entry_no_fields));
193     const google_apis::ImageMediaMetadata& image_media_metadata =
194       file_resource->image_media_metadata();
195     EXPECT_EQ(-1, image_media_metadata.width());
196     EXPECT_EQ(-1, image_media_metadata.height());
197     EXPECT_EQ(-1, image_media_metadata.rotation());
198   }
199 }
200
201 TEST(DriveAPIUtilTest, GetMd5Digest) {
202   base::ScopedTempDir temp_dir;
203   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
204
205   base::FilePath path = temp_dir.path().AppendASCII("test.txt");
206   const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789";
207   ASSERT_TRUE(google_apis::test_util::WriteStringToFile(path, kTestData));
208
209   EXPECT_EQ(base::MD5String(kTestData), GetMd5Digest(path));
210 }
211
212 }  // namespace util
213 }  // namespace drive