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/json/json_file_value_serializer.h"
     10 #include "base/time/time.h"
     11 #include "base/values.h"
     12 #include "google_apis/drive/test_util.h"
     13 #include "google_apis/drive/time_util.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 namespace google_apis {
     17 
     18 // Test document feed parsing.
     19 TEST(GDataWAPIParserTest, ResourceListJsonParser) {
     20   std::string error;
     21   scoped_ptr<base::Value> document =
     22       test_util::LoadJSONFile("gdata/basic_feed.json");
     23   ASSERT_TRUE(document.get());
     24   ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
     25   scoped_ptr<ResourceList> feed(ResourceList::ExtractAndParse(*document));
     26   ASSERT_TRUE(feed.get());
     27 
     28   base::Time update_time;
     29   ASSERT_TRUE(util::GetTimeFromString("2011-12-14T01:03:21.151Z",
     30                                                    &update_time));
     31 
     32   EXPECT_EQ(1, feed->start_index());
     33   EXPECT_EQ(1000, feed->items_per_page());
     34   EXPECT_EQ(update_time, feed->updated_time());
     35 
     36   // Check authors.
     37   ASSERT_EQ(1U, feed->authors().size());
     38   EXPECT_EQ("tester", feed->authors()[0]->name());
     39   EXPECT_EQ("tester (at) testing.com", feed->authors()[0]->email());
     40 
     41   // Check links.
     42   ASSERT_EQ(6U, feed->links().size());
     43   const Link* self_link = feed->GetLinkByType(Link::LINK_SELF);
     44   ASSERT_TRUE(self_link);
     45   EXPECT_EQ("https://self_link/", self_link->href().spec());
     46   EXPECT_EQ("application/atom+xml", self_link->mime_type());
     47 
     48   const Link* resumable_link =
     49       feed->GetLinkByType(Link::LINK_RESUMABLE_CREATE_MEDIA);
     50   ASSERT_TRUE(resumable_link);
     51   EXPECT_EQ("https://resumable_create_media_link/",
     52             resumable_link->href().spec());
     53   EXPECT_EQ("application/atom+xml", resumable_link->mime_type());
     54 
     55   // Check entries.
     56   ASSERT_EQ(4U, feed->entries().size());
     57 
     58   // Check a folder entry.
     59   const ResourceEntry* folder_entry = feed->entries()[0];
     60   ASSERT_TRUE(folder_entry);
     61   EXPECT_EQ(ResourceEntry::ENTRY_KIND_FOLDER, folder_entry->kind());
     62   EXPECT_EQ("\"HhMOFgcNHSt7ImBr\"", folder_entry->etag());
     63   EXPECT_EQ("folder:sub_sub_directory_folder_id", folder_entry->resource_id());
     64   EXPECT_EQ("https://1_folder_id", folder_entry->id());
     65   EXPECT_EQ("Entry 1 Title", folder_entry->title());
     66   base::Time entry1_update_time;
     67   base::Time entry1_publish_time;
     68   ASSERT_TRUE(util::GetTimeFromString("2011-04-01T18:34:08.234Z",
     69                                                    &entry1_update_time));
     70   ASSERT_TRUE(util::GetTimeFromString("2010-11-07T05:03:54.719Z",
     71                                                    &entry1_publish_time));
     72   EXPECT_EQ(entry1_update_time, folder_entry->updated_time());
     73   EXPECT_EQ(entry1_publish_time, folder_entry->published_time());
     74 
     75   ASSERT_EQ(1U, folder_entry->authors().size());
     76   EXPECT_EQ("entry_tester", folder_entry->authors()[0]->name());
     77   EXPECT_EQ("entry_tester (at) testing.com", folder_entry->authors()[0]->email());
     78   EXPECT_EQ("https://1_folder_content_url/",
     79             folder_entry->download_url().spec());
     80   EXPECT_EQ("application/atom+xml;type=feed",
     81             folder_entry->content_mime_type());
     82 
     83   ASSERT_EQ(1U, folder_entry->resource_links().size());
     84   const ResourceLink* feed_link = folder_entry->resource_links()[0];
     85   ASSERT_TRUE(feed_link);
     86   ASSERT_EQ(ResourceLink::FEED_LINK_ACL, feed_link->type());
     87 
     88   const Link* entry1_alternate_link =
     89       folder_entry->GetLinkByType(Link::LINK_ALTERNATE);
     90   ASSERT_TRUE(entry1_alternate_link);
     91   EXPECT_EQ("https://1_folder_alternate_link/",
     92             entry1_alternate_link->href().spec());
     93   EXPECT_EQ("text/html", entry1_alternate_link->mime_type());
     94 
     95   const Link* entry1_edit_link = folder_entry->GetLinkByType(Link::LINK_EDIT);
     96   ASSERT_TRUE(entry1_edit_link);
     97   EXPECT_EQ("https://1_edit_link/", entry1_edit_link->href().spec());
     98   EXPECT_EQ("application/atom+xml", entry1_edit_link->mime_type());
     99 
    100   // Check a file entry.
    101   const ResourceEntry* file_entry = feed->entries()[1];
    102   ASSERT_TRUE(file_entry);
    103   EXPECT_EQ(ResourceEntry::ENTRY_KIND_FILE, file_entry->kind());
    104   EXPECT_EQ("filename.m4a", file_entry->filename());
    105   EXPECT_EQ("sugg_file_name.m4a", file_entry->suggested_filename());
    106   EXPECT_EQ("3b4382ebefec6e743578c76bbd0575ce", file_entry->file_md5());
    107   EXPECT_EQ(892721, file_entry->file_size());
    108   const Link* file_parent_link = file_entry->GetLinkByType(Link::LINK_PARENT);
    109   ASSERT_TRUE(file_parent_link);
    110   EXPECT_EQ("https://file_link_parent/", file_parent_link->href().spec());
    111   EXPECT_EQ("application/atom+xml", file_parent_link->mime_type());
    112   EXPECT_EQ("Medical", file_parent_link->title());
    113   const Link* file_open_with_link =
    114     file_entry->GetLinkByType(Link::LINK_OPEN_WITH);
    115   ASSERT_TRUE(file_open_with_link);
    116   EXPECT_EQ("https://xml_file_entry_open_with_link/",
    117             file_open_with_link->href().spec());
    118   EXPECT_EQ("application/atom+xml", file_open_with_link->mime_type());
    119   EXPECT_EQ("the_app_id", file_open_with_link->app_id());
    120   EXPECT_EQ(654321, file_entry->changestamp());
    121 
    122   const Link* file_unknown_link = file_entry->GetLinkByType(Link::LINK_UNKNOWN);
    123   ASSERT_TRUE(file_unknown_link);
    124   EXPECT_EQ("https://xml_file_fake_entry_open_with_link/",
    125             file_unknown_link->href().spec());
    126   EXPECT_EQ("application/atom+xml", file_unknown_link->mime_type());
    127   EXPECT_EQ("", file_unknown_link->app_id());
    128 
    129   // Check a file entry.
    130   const ResourceEntry* resource_entry = feed->entries()[2];
    131   ASSERT_TRUE(resource_entry);
    132   // Hosted documents are treated as unknown kind so that sync file system
    133   // doesn't assume them as neither folders nor normal files.
    134   EXPECT_EQ(ResourceEntry::ENTRY_KIND_UNKNOWN, resource_entry->kind());
    135 
    136   // Check an external document entry.
    137   const ResourceEntry* app_entry = feed->entries()[3];
    138   ASSERT_TRUE(app_entry);
    139   // Hosted documents are treated as unknown kind so that sync file system
    140   // doesn't assume them as neither folders nor normal files.
    141   EXPECT_EQ(ResourceEntry::ENTRY_KIND_UNKNOWN, app_entry->kind());
    142 }
    143 
    144 
    145 // Test document feed parsing.
    146 TEST(GDataWAPIParserTest, ResourceEntryJsonParser) {
    147   std::string error;
    148   scoped_ptr<base::Value> document =
    149       test_util::LoadJSONFile("gdata/file_entry.json");
    150   ASSERT_TRUE(document.get());
    151   ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
    152   scoped_ptr<ResourceEntry> entry(ResourceEntry::ExtractAndParse(*document));
    153   ASSERT_TRUE(entry.get());
    154 
    155   EXPECT_EQ(ResourceEntry::ENTRY_KIND_FILE, entry->kind());
    156   EXPECT_EQ("\"HhMOFgxXHit7ImBr\"", entry->etag());
    157   EXPECT_EQ("file:2_file_resource_id", entry->resource_id());
    158   EXPECT_EQ("2_file_id", entry->id());
    159   EXPECT_EQ("File 1.mp3", entry->title());
    160   base::Time entry1_update_time;
    161   base::Time entry1_publish_time;
    162   ASSERT_TRUE(util::GetTimeFromString("2011-12-14T00:40:47.330Z",
    163                                       &entry1_update_time));
    164   ASSERT_TRUE(util::GetTimeFromString("2011-12-13T00:40:47.330Z",
    165                                       &entry1_publish_time));
    166   EXPECT_EQ(entry1_update_time, entry->updated_time());
    167   EXPECT_EQ(entry1_publish_time, entry->published_time());
    168 
    169   EXPECT_EQ(1U, entry->authors().size());
    170   EXPECT_EQ("tester", entry->authors()[0]->name());
    171   EXPECT_EQ("tester (at) testing.com", entry->authors()[0]->email());
    172   EXPECT_EQ("https://file_content_url/",
    173             entry->download_url().spec());
    174   EXPECT_EQ("audio/mpeg",
    175             entry->content_mime_type());
    176 
    177   // Check feed links.
    178   ASSERT_EQ(1U, entry->resource_links().size());
    179   const ResourceLink* feed_link_1 = entry->resource_links()[0];
    180   ASSERT_TRUE(feed_link_1);
    181   EXPECT_EQ(ResourceLink::FEED_LINK_REVISIONS, feed_link_1->type());
    182 
    183   // Check links.
    184   ASSERT_EQ(8U, entry->links().size());
    185   const Link* entry1_alternate_link =
    186       entry->GetLinkByType(Link::LINK_ALTERNATE);
    187   ASSERT_TRUE(entry1_alternate_link);
    188   EXPECT_EQ("https://file_link_alternate/",
    189             entry1_alternate_link->href().spec());
    190   EXPECT_EQ("text/html", entry1_alternate_link->mime_type());
    191 
    192   const Link* entry1_edit_link = entry->GetLinkByType(Link::LINK_EDIT_MEDIA);
    193   ASSERT_TRUE(entry1_edit_link);
    194   EXPECT_EQ("https://file_edit_media/",
    195             entry1_edit_link->href().spec());
    196   EXPECT_EQ("audio/mpeg", entry1_edit_link->mime_type());
    197 
    198   const Link* entry1_self_link = entry->GetLinkByType(Link::LINK_SELF);
    199   ASSERT_TRUE(entry1_self_link);
    200   EXPECT_EQ("https://file1_link_self/file%3A2_file_resource_id",
    201             entry1_self_link->href().spec());
    202   EXPECT_EQ("application/atom+xml", entry1_self_link->mime_type());
    203   EXPECT_EQ("", entry1_self_link->app_id());
    204 
    205   const Link* entry1_open_with_link =
    206       entry->GetLinkByType(Link::LINK_OPEN_WITH);
    207   ASSERT_TRUE(entry1_open_with_link);
    208   EXPECT_EQ("https://entry1_open_with_link/",
    209             entry1_open_with_link->href().spec());
    210   EXPECT_EQ("application/atom+xml", entry1_open_with_link->mime_type());
    211   EXPECT_EQ("the_app_id", entry1_open_with_link->app_id());
    212 
    213   const Link* entry1_unknown_link = entry->GetLinkByType(Link::LINK_UNKNOWN);
    214   ASSERT_TRUE(entry1_unknown_link);
    215   EXPECT_EQ("https://entry1_fake_entry_open_with_link/",
    216             entry1_unknown_link->href().spec());
    217   EXPECT_EQ("application/atom+xml", entry1_unknown_link->mime_type());
    218   EXPECT_EQ("", entry1_unknown_link->app_id());
    219 
    220   // Check a file properties.
    221   EXPECT_EQ(ResourceEntry::ENTRY_KIND_FILE, entry->kind());
    222   EXPECT_EQ("File 1.mp3", entry->filename());
    223   EXPECT_EQ("File 1.mp3", entry->suggested_filename());
    224   EXPECT_EQ("3b4382ebefec6e743578c76bbd0575ce", entry->file_md5());
    225   EXPECT_EQ(892721, entry->file_size());
    226 
    227   // WAPI doesn't provide image metadata, but these fields are available
    228   // since this class can wrap data received from Drive API (via a converter).
    229   EXPECT_EQ(-1, entry->image_width());
    230   EXPECT_EQ(-1, entry->image_height());
    231   EXPECT_EQ(-1, entry->image_rotation());
    232 }
    233 
    234 }  // namespace google_apis
    235