Home | History | Annotate | Download | only in drive
      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(DriveAPIUtilTest, GetMd5Digest) {
    157   base::ScopedTempDir temp_dir;
    158   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
    159 
    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));
    163 
    164   EXPECT_EQ(base::MD5String(kTestData), GetMd5Digest(path));
    165 }
    166 
    167 TEST(DriveAPIUtilTest, HasHostedDocumentExtension) {
    168   EXPECT_TRUE(
    169       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdoc")));
    170   EXPECT_TRUE(
    171       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gsheet")));
    172   EXPECT_TRUE(
    173       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gslides")));
    174   EXPECT_TRUE(
    175       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdraw")));
    176   EXPECT_TRUE(
    177       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gtable")));
    178   EXPECT_TRUE(
    179       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gform")));
    180   EXPECT_TRUE(
    181       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.glink")));
    182 
    183   EXPECT_FALSE(
    184       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.gdocs")));
    185   EXPECT_FALSE(
    186       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.docx")));
    187   EXPECT_FALSE(
    188       HasHostedDocumentExtension(base::FilePath::FromUTF8Unsafe("xx.jpg")));
    189 }
    190 
    191 }  // namespace util
    192 }  // namespace drive
    193