summaryrefslogtreecommitdiffstats
path: root/chrome/browser/drive/drive_api_util_unittest.cc
blob: 201fba76ee4d69f2f1c4b5dd29b646ef3b594b68 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2013 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 "chrome/browser/drive/drive_api_util.h"

#include "base/files/scoped_temp_dir.h"
#include "base/md5.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/gdata_wapi_parser.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace drive {
namespace util {

TEST(DriveApiUtilTest, EscapeQueryStringValue) {
  EXPECT_EQ("abcde", EscapeQueryStringValue("abcde"));
  EXPECT_EQ("\\'", EscapeQueryStringValue("'"));
  EXPECT_EQ("\\'abcde\\'", EscapeQueryStringValue("'abcde'"));
  EXPECT_EQ("\\\\", EscapeQueryStringValue("\\"));
  EXPECT_EQ("\\\\\\'", EscapeQueryStringValue("\\'"));
}

TEST(DriveApiUtilTest, TranslateQuery) {
  EXPECT_EQ("", TranslateQuery(""));
  EXPECT_EQ("fullText contains 'dog'", TranslateQuery("dog"));
  EXPECT_EQ("fullText contains 'dog' and fullText contains 'cat'",
            TranslateQuery("dog cat"));
  EXPECT_EQ("not fullText contains 'cat'", TranslateQuery("-cat"));
  EXPECT_EQ("fullText contains 'dog cat'", TranslateQuery("\"dog cat\""));

  // Should handles full-width white space correctly.
  // Note: \xE3\x80\x80 (\u3000) is Ideographic Space (a.k.a. Japanese
  //   full-width whitespace).
  EXPECT_EQ("fullText contains 'dog' and fullText contains 'cat'",
            TranslateQuery("dog" "\xE3\x80\x80" "cat"));

  // If the quoted token is not closed (i.e. the last '"' is missing),
  // we handle the remaining string is one token, as a fallback.
  EXPECT_EQ("fullText contains 'dog cat'", TranslateQuery("\"dog cat"));

  // For quoted text with leading '-'.
  EXPECT_EQ("not fullText contains 'dog cat'", TranslateQuery("-\"dog cat\""));

  // Empty tokens should be simply ignored.
  EXPECT_EQ("", TranslateQuery("-"));
  EXPECT_EQ("", TranslateQuery("\"\""));
  EXPECT_EQ("", TranslateQuery("-\"\""));
  EXPECT_EQ("", TranslateQuery("\"\"\"\""));
  EXPECT_EQ("", TranslateQuery("\"\" \"\""));
  EXPECT_EQ("fullText contains 'dog'", TranslateQuery("\"\" dog \"\""));
}

TEST(FileSystemUtilTest, ExtractResourceIdFromUrl) {
  EXPECT_EQ("file:2_file_resource_id", ExtractResourceIdFromUrl(
      GURL("https://file1_link_self/file:2_file_resource_id")));
  // %3A should be unescaped.
  EXPECT_EQ("file:2_file_resource_id", ExtractResourceIdFromUrl(
      GURL("https://file1_link_self/file%3A2_file_resource_id")));

  // The resource ID cannot be extracted, hence empty.
  EXPECT_EQ("", ExtractResourceIdFromUrl(GURL("https://www.example.com/")));
}

TEST(FileSystemUtilTest, CanonicalizeResourceId) {
  std::string resource_id("1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug");

  // New style ID is unchanged.
  EXPECT_EQ(resource_id, CanonicalizeResourceId(resource_id));

  // Drop prefixes from old style IDs.
  EXPECT_EQ(resource_id, CanonicalizeResourceId("document:" + resource_id));
  EXPECT_EQ(resource_id, CanonicalizeResourceId("spreadsheet:" + resource_id));
  EXPECT_EQ(resource_id, CanonicalizeResourceId("presentation:" + resource_id));
  EXPECT_EQ(resource_id, CanonicalizeResourceId("drawing:" + resource_id));
  EXPECT_EQ(resource_id, CanonicalizeResourceId("table:" + resource_id));
  EXPECT_EQ(resource_id, CanonicalizeResourceId("externalapp:" + resource_id));
}

TEST(FileSystemUtilTest, ConvertFileResourceToResource_Parents) {
  google_apis::FileResource file_resource;

  std::vector<GURL> expected_links;
  expected_links.push_back(GURL("http://server/id1"));
  expected_links.push_back(GURL("http://server/id2"));
  expected_links.push_back(GURL("http://server/id3"));

  for (size_t i = 0; i < expected_links.size(); ++i) {
    google_apis::ParentReference parent;
    parent.set_parent_link(expected_links[i]);
    file_resource.mutable_parents()->push_back(parent);
  }

  scoped_ptr<google_apis::ResourceEntry> entry(
      ConvertFileResourceToResourceEntry(file_resource));
  std::vector<GURL> actual_links;
  for (size_t i = 0; i < entry->links().size(); ++i) {
    if (entry->links()[i]->type() == google_apis::Link::LINK_PARENT)
      actual_links.push_back(entry->links()[i]->href());
  }

  EXPECT_EQ(expected_links, actual_links);
}

TEST(FileSystemUtilTest, ConvertFileResourceToResourceEntryImageMediaMetadata) {
  google_apis::FileResource file_resource_all_fields;
  google_apis::FileResource file_resource_zero_fields;
  google_apis::FileResource file_resource_no_fields;
  // Set up FileResource instances;
  {
    {
      google_apis::ImageMediaMetadata* image_media_metadata =
        file_resource_all_fields.mutable_image_media_metadata();
      image_media_metadata->set_width(640);
      image_media_metadata->set_height(480);
      image_media_metadata->set_rotation(90);
    }
    {
      google_apis::ImageMediaMetadata* image_media_metadata =
        file_resource_zero_fields.mutable_image_media_metadata();
      image_media_metadata->set_width(0);
      image_media_metadata->set_height(0);
      image_media_metadata->set_rotation(0);
    }
  }

  // Verify the converted values.
  {
    scoped_ptr<google_apis::ResourceEntry> resource_entry(
        ConvertFileResourceToResourceEntry(file_resource_all_fields));

    EXPECT_EQ(640, resource_entry->image_width());
    EXPECT_EQ(480, resource_entry->image_height());
    EXPECT_EQ(90, resource_entry->image_rotation());
  }
  {
    scoped_ptr<google_apis::ResourceEntry> resource_entry(
        ConvertFileResourceToResourceEntry(file_resource_zero_fields));

    EXPECT_EQ(0, resource_entry->image_width());
    EXPECT_EQ(0, resource_entry->image_height());
    EXPECT_EQ(0, resource_entry->image_rotation());
  }
  {
    scoped_ptr<google_apis::ResourceEntry> resource_entry(
        ConvertFileResourceToResourceEntry(file_resource_no_fields));

    EXPECT_EQ(-1, resource_entry->image_width());
    EXPECT_EQ(-1, resource_entry->image_height());
    EXPECT_EQ(-1, resource_entry->image_rotation());
  }
}

TEST(DriveAPIUtilTest, GetMd5Digest) {
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  base::FilePath path = temp_dir.path().AppendASCII("test.txt");
  const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789";
  ASSERT_TRUE(google_apis::test_util::WriteStringToFile(path, kTestData));

  EXPECT_EQ(base::MD5String(kTestData), GetMd5Digest(path));
}

TEST(DriveAPIUtilTest, HasHostedDocumentExtension) {
  EXPECT_TRUE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdoc")));
  EXPECT_TRUE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gsheet")));
  EXPECT_TRUE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gslides")));
  EXPECT_TRUE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdraw")));
  EXPECT_TRUE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gtable")));
  EXPECT_TRUE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gform")));
  EXPECT_TRUE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.glink")));

  EXPECT_FALSE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdocs")));
  EXPECT_FALSE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.docx")));
  EXPECT_FALSE(
      HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.jpg")));
}

}  // namespace util
}  // namespace drive