Home | History | Annotate | Download | only in drive
      1 // Copyright (c) 2012 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 "google_apis/drive/gdata_wapi_parser.h"
      6 
      7 #include <string>
      8 
      9 #include "base/files/file_path.h"
     10 #include "base/json/json_file_value_serializer.h"
     11 #include "base/logging.h"
     12 #include "base/time/time.h"
     13 #include "base/values.h"
     14 #include "google_apis/drive/test_util.h"
     15 #include "google_apis/drive/time_util.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 namespace google_apis {
     19 
     20 // Test document feed parsing.
     21 TEST(GDataWAPIParserTest, ResourceListJsonParser) {
     22   std::string error;
     23   scoped_ptr<base::Value> document =
     24       test_util::LoadJSONFile("gdata/basic_feed.json");
     25   ASSERT_TRUE(document.get());
     26   ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
     27   scoped_ptr<ResourceList> feed(ResourceList::ExtractAndParse(*document));
     28   ASSERT_TRUE(feed.get());
     29 
     30   base::Time update_time;
     31   ASSERT_TRUE(util::GetTimeFromString("2011-12-14T01:03:21.151Z",
     32                                                    &update_time));
     33 
     34   EXPECT_EQ(1, feed->start_index());
     35   EXPECT_EQ(1000, feed->items_per_page());
     36   EXPECT_EQ(update_time, feed->updated_time());
     37 
     38   // Check authors.
     39   ASSERT_EQ(1U, feed->authors().size());
     40   EXPECT_EQ("tester", feed->authors()[0]->name());
     41   EXPECT_EQ("tester (at) testing.com", feed->authors()[0]->email());
     42 
     43   // Check links.
     44   ASSERT_EQ(6U, feed->links().size());
     45   const Link* self_link = feed->GetLinkByType(Link::LINK_SELF);
     46   ASSERT_TRUE(self_link);
     47   EXPECT_EQ("https://self_link/", self_link->href().spec());
     48   EXPECT_EQ("application/atom+xml", self_link->mime_type());
     49 
     50   const Link* resumable_link =
     51       feed->GetLinkByType(Link::LINK_RESUMABLE_CREATE_MEDIA);
     52   ASSERT_TRUE(resumable_link);
     53   EXPECT_EQ("https://resumable_create_media_link/",
     54             resumable_link->href().spec());
     55   EXPECT_EQ("application/atom+xml", resumable_link->mime_type());
     56 
     57   // Check entries.
     58   ASSERT_EQ(4U, feed->entries().size());
     59 
     60   // Check a folder entry.
     61   const ResourceEntry* folder_entry = feed->entries()[0];
     62   ASSERT_TRUE(folder_entry);
     63   EXPECT_EQ(ENTRY_KIND_FOLDER, folder_entry->kind());
     64   EXPECT_EQ("\"HhMOFgcNHSt7ImBr\"", folder_entry->etag());
     65   EXPECT_EQ("folder:sub_sub_directory_folder_id", folder_entry->resource_id());
     66   EXPECT_EQ("https://1_folder_id", folder_entry->id());
     67   EXPECT_EQ("Entry 1 Title", folder_entry->title());
     68   base::Time entry1_update_time;
     69   base::Time entry1_publish_time;
     70   ASSERT_TRUE(util::GetTimeFromString("2011-04-01T18:34:08.234Z",
     71                                                    &entry1_update_time));
     72   ASSERT_TRUE(util::GetTimeFromString("2010-11-07T05:03:54.719Z",
     73                                                    &entry1_publish_time));
     74   EXPECT_EQ(entry1_update_time, folder_entry->updated_time());
     75   EXPECT_EQ(entry1_publish_time, folder_entry->published_time());
     76 
     77   ASSERT_EQ(1U, folder_entry->authors().size());
     78   EXPECT_EQ("entry_tester", folder_entry->authors()[0]->name());
     79   EXPECT_EQ("entry_tester (at) testing.com", folder_entry->authors()[0]->email());
     80   EXPECT_EQ("https://1_folder_content_url/",
     81             folder_entry->download_url().spec());
     82   EXPECT_EQ("application/atom+xml;type=feed",
     83             folder_entry->content_mime_type());
     84 
     85   ASSERT_EQ(1U, folder_entry->resource_links().size());
     86   const ResourceLink* feed_link = folder_entry->resource_links()[0];
     87   ASSERT_TRUE(feed_link);
     88   ASSERT_EQ(ResourceLink::FEED_LINK_ACL, feed_link->type());
     89 
     90   const Link* entry1_alternate_link =
     91       folder_entry->GetLinkByType(Link::LINK_ALTERNATE);
     92   ASSERT_TRUE(entry1_alternate_link);
     93   EXPECT_EQ("https://1_folder_alternate_link/",
     94             entry1_alternate_link->href().spec());
     95   EXPECT_EQ("text/html", entry1_alternate_link->mime_type());
     96 
     97   const Link* entry1_edit_link = folder_entry->GetLinkByType(Link::LINK_EDIT);
     98   ASSERT_TRUE(entry1_edit_link);
     99   EXPECT_EQ("https://1_edit_link/", entry1_edit_link->href().spec());
    100   EXPECT_EQ("application/atom+xml", entry1_edit_link->mime_type());
    101 
    102   // Check a file entry.
    103   const ResourceEntry* file_entry = feed->entries()[1];
    104   ASSERT_TRUE(file_entry);
    105   EXPECT_EQ(ENTRY_KIND_FILE, file_entry->kind());
    106   EXPECT_EQ("filename.m4a", file_entry->filename());
    107   EXPECT_EQ("sugg_file_name.m4a", file_entry->suggested_filename());
    108   EXPECT_EQ("3b4382ebefec6e743578c76bbd0575ce", file_entry->file_md5());
    109   EXPECT_EQ(892721, file_entry->file_size());
    110   const Link* file_parent_link = file_entry->GetLinkByType(Link::LINK_PARENT);
    111   ASSERT_TRUE(file_parent_link);
    112   EXPECT_EQ("https://file_link_parent/", file_parent_link->href().spec());
    113   EXPECT_EQ("application/atom+xml", file_parent_link->mime_type());
    114   EXPECT_EQ("Medical", file_parent_link->title());
    115   const Link* file_open_with_link =
    116     file_entry->GetLinkByType(Link::LINK_OPEN_WITH);
    117   ASSERT_TRUE(file_open_with_link);
    118   EXPECT_EQ("https://xml_file_entry_open_with_link/",
    119             file_open_with_link->href().spec());
    120   EXPECT_EQ("application/atom+xml", file_open_with_link->mime_type());
    121   EXPECT_EQ("the_app_id", file_open_with_link->app_id());
    122   EXPECT_EQ(654321, file_entry->changestamp());
    123 
    124   const Link* file_unknown_link = file_entry->GetLinkByType(Link::LINK_UNKNOWN);
    125   ASSERT_TRUE(file_unknown_link);
    126   EXPECT_EQ("https://xml_file_fake_entry_open_with_link/",
    127             file_unknown_link->href().spec());
    128   EXPECT_EQ("application/atom+xml", file_unknown_link->mime_type());
    129   EXPECT_EQ("", file_unknown_link->app_id());
    130 
    131   // Check a file entry.
    132   const ResourceEntry* resource_entry = feed->entries()[2];
    133   ASSERT_TRUE(resource_entry);
    134   EXPECT_EQ(ENTRY_KIND_DOCUMENT, resource_entry->kind());
    135   EXPECT_TRUE(resource_entry->is_hosted_document());
    136   EXPECT_TRUE(resource_entry->is_google_document());
    137   EXPECT_FALSE(resource_entry->is_external_document());
    138 
    139   // Check an external document entry.
    140   const ResourceEntry* app_entry = feed->entries()[3];
    141   ASSERT_TRUE(app_entry);
    142   EXPECT_EQ(ENTRY_KIND_EXTERNAL_APP, app_entry->kind());
    143   EXPECT_TRUE(app_entry->is_hosted_document());
    144   EXPECT_TRUE(app_entry->is_external_document());
    145   EXPECT_FALSE(app_entry->is_google_document());
    146 }
    147 
    148 
    149 // Test document feed parsing.
    150 TEST(GDataWAPIParserTest, ResourceEntryJsonParser) {
    151   std::string error;
    152   scoped_ptr<base::Value> document =
    153       test_util::LoadJSONFile("gdata/file_entry.json");
    154   ASSERT_TRUE(document.get());
    155   ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
    156   scoped_ptr<ResourceEntry> entry(ResourceEntry::ExtractAndParse(*document));
    157   ASSERT_TRUE(entry.get());
    158 
    159   EXPECT_EQ(ENTRY_KIND_FILE, entry->kind());
    160   EXPECT_EQ("\"HhMOFgxXHit7ImBr\"", entry->etag());
    161   EXPECT_EQ("file:2_file_resource_id", entry->resource_id());
    162   EXPECT_EQ("2_file_id", entry->id());
    163   EXPECT_EQ("File 1.mp3", entry->title());
    164   base::Time entry1_update_time;
    165   base::Time entry1_publish_time;
    166   ASSERT_TRUE(util::GetTimeFromString("2011-12-14T00:40:47.330Z",
    167                                       &entry1_update_time));
    168   ASSERT_TRUE(util::GetTimeFromString("2011-12-13T00:40:47.330Z",
    169                                       &entry1_publish_time));
    170   EXPECT_EQ(entry1_update_time, entry->updated_time());
    171   EXPECT_EQ(entry1_publish_time, entry->published_time());
    172 
    173   EXPECT_EQ(1U, entry->authors().size());
    174   EXPECT_EQ("tester", entry->authors()[0]->name());
    175   EXPECT_EQ("tester (at) testing.com", entry->authors()[0]->email());
    176   EXPECT_EQ("https://file_content_url/",
    177             entry->download_url().spec());
    178   EXPECT_EQ("audio/mpeg",
    179             entry->content_mime_type());
    180 
    181   // Check feed links.
    182   ASSERT_EQ(1U, entry->resource_links().size());
    183   const ResourceLink* feed_link_1 = entry->resource_links()[0];
    184   ASSERT_TRUE(feed_link_1);
    185   EXPECT_EQ(ResourceLink::FEED_LINK_REVISIONS, feed_link_1->type());
    186 
    187   // Check links.
    188   ASSERT_EQ(8U, entry->links().size());
    189   const Link* entry1_alternate_link =
    190       entry->GetLinkByType(Link::LINK_ALTERNATE);
    191   ASSERT_TRUE(entry1_alternate_link);
    192   EXPECT_EQ("https://file_link_alternate/",
    193             entry1_alternate_link->href().spec());
    194   EXPECT_EQ("text/html", entry1_alternate_link->mime_type());
    195 
    196   const Link* entry1_edit_link = entry->GetLinkByType(Link::LINK_EDIT_MEDIA);
    197   ASSERT_TRUE(entry1_edit_link);
    198   EXPECT_EQ("https://file_edit_media/",
    199             entry1_edit_link->href().spec());
    200   EXPECT_EQ("audio/mpeg", entry1_edit_link->mime_type());
    201 
    202   const Link* entry1_self_link = entry->GetLinkByType(Link::LINK_SELF);
    203   ASSERT_TRUE(entry1_self_link);
    204   EXPECT_EQ("https://file1_link_self/file%3A2_file_resource_id",
    205             entry1_self_link->href().spec());
    206   EXPECT_EQ("application/atom+xml", entry1_self_link->mime_type());
    207   EXPECT_EQ("", entry1_self_link->app_id());
    208 
    209   const Link* entry1_open_with_link =
    210       entry->GetLinkByType(Link::LINK_OPEN_WITH);
    211   ASSERT_TRUE(entry1_open_with_link);
    212   EXPECT_EQ("https://entry1_open_with_link/",
    213             entry1_open_with_link->href().spec());
    214   EXPECT_EQ("application/atom+xml", entry1_open_with_link->mime_type());
    215   EXPECT_EQ("the_app_id", entry1_open_with_link->app_id());
    216 
    217   const Link* entry1_unknown_link = entry->GetLinkByType(Link::LINK_UNKNOWN);
    218   ASSERT_TRUE(entry1_unknown_link);
    219   EXPECT_EQ("https://entry1_fake_entry_open_with_link/",
    220             entry1_unknown_link->href().spec());
    221   EXPECT_EQ("application/atom+xml", entry1_unknown_link->mime_type());
    222   EXPECT_EQ("", entry1_unknown_link->app_id());
    223 
    224   // Check a file properties.
    225   EXPECT_EQ(ENTRY_KIND_FILE, entry->kind());
    226   EXPECT_EQ("File 1.mp3", entry->filename());
    227   EXPECT_EQ("File 1.mp3", entry->suggested_filename());
    228   EXPECT_EQ("3b4382ebefec6e743578c76bbd0575ce", entry->file_md5());
    229   EXPECT_EQ(892721, entry->file_size());
    230 
    231   // WAPI doesn't provide image metadata, but these fields are available
    232   // since this class can wrap data received from Drive API (via a converter).
    233   EXPECT_EQ(-1, entry->image_width());
    234   EXPECT_EQ(-1, entry->image_height());
    235   EXPECT_EQ(-1, entry->image_rotation());
    236 }
    237 
    238 TEST(GDataWAPIParserTest, ClassifyEntryKindByFileExtension) {
    239   EXPECT_EQ(
    240       ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    241       ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    242       ResourceEntry::ClassifyEntryKindByFileExtension(
    243           base::FilePath(FILE_PATH_LITERAL("Test.gdoc"))));
    244   EXPECT_EQ(
    245       ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    246       ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    247       ResourceEntry::ClassifyEntryKindByFileExtension(
    248           base::FilePath(FILE_PATH_LITERAL("Test.gsheet"))));
    249   EXPECT_EQ(
    250       ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    251       ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    252       ResourceEntry::ClassifyEntryKindByFileExtension(
    253           base::FilePath(FILE_PATH_LITERAL("Test.gslides"))));
    254   EXPECT_EQ(
    255       ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    256       ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    257       ResourceEntry::ClassifyEntryKindByFileExtension(
    258           base::FilePath(FILE_PATH_LITERAL("Test.gdraw"))));
    259   EXPECT_EQ(
    260       ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    261       ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    262       ResourceEntry::ClassifyEntryKindByFileExtension(
    263           base::FilePath(FILE_PATH_LITERAL("Test.gtable"))));
    264   EXPECT_EQ(
    265       ResourceEntry::KIND_OF_EXTERNAL_DOCUMENT |
    266       ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    267       ResourceEntry::ClassifyEntryKindByFileExtension(
    268           base::FilePath(FILE_PATH_LITERAL("Test.glink"))));
    269   EXPECT_EQ(
    270       ResourceEntry::KIND_OF_NONE,
    271       ResourceEntry::ClassifyEntryKindByFileExtension(
    272           base::FilePath(FILE_PATH_LITERAL("Test.tar.gz"))));
    273   EXPECT_EQ(
    274       ResourceEntry::KIND_OF_NONE,
    275       ResourceEntry::ClassifyEntryKindByFileExtension(
    276           base::FilePath(FILE_PATH_LITERAL("Test.txt"))));
    277   EXPECT_EQ(
    278       ResourceEntry::KIND_OF_NONE,
    279       ResourceEntry::ClassifyEntryKindByFileExtension(
    280           base::FilePath(FILE_PATH_LITERAL("Test"))));
    281   EXPECT_EQ(
    282       ResourceEntry::KIND_OF_NONE,
    283       ResourceEntry::ClassifyEntryKindByFileExtension(
    284           base::FilePath()));
    285 }
    286 
    287 TEST(GDataWAPIParserTest, ResourceEntryClassifyEntryKind) {
    288   EXPECT_EQ(ResourceEntry::KIND_OF_NONE,
    289             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_UNKNOWN));
    290   EXPECT_EQ(ResourceEntry::KIND_OF_NONE,
    291             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_ITEM));
    292   EXPECT_EQ(ResourceEntry::KIND_OF_NONE,
    293             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_SITE));
    294   EXPECT_EQ(ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    295             ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    296             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_DOCUMENT));
    297   EXPECT_EQ(ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    298             ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    299             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_SPREADSHEET));
    300   EXPECT_EQ(ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    301             ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    302             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_PRESENTATION));
    303   EXPECT_EQ(ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    304             ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    305             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_DRAWING));
    306   EXPECT_EQ(ResourceEntry::KIND_OF_GOOGLE_DOCUMENT |
    307             ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    308             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_TABLE));
    309   EXPECT_EQ(ResourceEntry::KIND_OF_EXTERNAL_DOCUMENT |
    310             ResourceEntry::KIND_OF_HOSTED_DOCUMENT,
    311             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_EXTERNAL_APP));
    312   EXPECT_EQ(ResourceEntry::KIND_OF_FOLDER,
    313             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_FOLDER));
    314   EXPECT_EQ(ResourceEntry::KIND_OF_FILE,
    315             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_FILE));
    316   EXPECT_EQ(ResourceEntry::KIND_OF_FILE,
    317             ResourceEntry::ClassifyEntryKind(ENTRY_KIND_PDF));
    318 }
    319 
    320 }  // namespace google_apis
    321