summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata/gdata_parser_unittest.cc
blob: 7daa47ba6d5b0017833d0ac96d598d51fe7bda6a (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2012 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 "base/file_path.h"
#include "base/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/path_service.h"
#include "base/string16.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/chromeos/gdata/gdata_parser.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/libxml_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::Value;
using base::DictionaryValue;
using base::ListValue;
using gdata::DocumentEntry;
using gdata::DocumentFeed;
using gdata::AccountMetadataFeed;
using gdata::FeedLink;
using gdata::GDataEntry;
using gdata::Link;

class GDataParserTest : public testing::Test {
 protected:
  static Value* LoadJSONFile(const std::string& filename) {
    FilePath path;
    std::string error;
    // Test files for this unit test are located in
    // src/chrome/test/data/chromeos/gdata/*
    PathService::Get(chrome::DIR_TEST_DATA, &path);
    path = path.AppendASCII("chromeos")
        .AppendASCII("gdata")
        .AppendASCII(filename.c_str());
    EXPECT_TRUE(file_util::PathExists(path)) <<
        "Couldn't find " << path.value();

    JSONFileValueSerializer serializer(path);
    Value* value = serializer.Deserialize(NULL, &error);
    EXPECT_TRUE(value) <<
        "Parse error " << path.value() << ": " << error;
    return value;
  }

  static DocumentEntry* LoadDocumentEntryFromXml(const std::string& filename) {
    FilePath path;
    std::string error;
    PathService::Get(chrome::DIR_TEST_DATA, &path);
    path = path.AppendASCII("chromeos")
        .AppendASCII("gdata")
        .AppendASCII(filename.c_str());
    EXPECT_TRUE(file_util::PathExists(path)) <<
        "Couldn't find " << path.value();
    std::string contents;
    EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
    XmlReader reader;
    if (!reader.Load(contents)) {
      NOTREACHED() << "Invalid xml:\n" << contents;
      return NULL;
    }
    scoped_ptr<DocumentEntry> entry;
    while (reader.Read()) {
      if (reader.NodeName() == "entry") {
        entry.reset(DocumentEntry::CreateFromXml(&reader));
        break;
      }
    }
    return entry.release();
  }
};

// Test document feed parsing.
TEST_F(GDataParserTest, DocumentFeedJsonParser) {
  std::string error;
  scoped_ptr<Value> document(LoadJSONFile("basic_feed.json"));
  ASSERT_TRUE(document.get());
  ASSERT_TRUE(document->GetType() == Value::TYPE_DICTIONARY);
  Value* feed_value;
  ASSERT_TRUE(reinterpret_cast<DictionaryValue*>(document.get())->Get(
      std::string("feed"), &feed_value));
  ASSERT_TRUE(feed_value);
  scoped_ptr<DocumentFeed> feed(DocumentFeed::CreateFrom(feed_value));

  base::Time update_time;
  ASSERT_TRUE(GDataEntry::GetTimeFromString("2011-12-14T01:03:21.151Z",
                                            &update_time));

  EXPECT_EQ(1, feed->start_index());
  EXPECT_EQ(1000, feed->items_per_page());
  EXPECT_EQ(update_time, feed->updated_time());

  // Check authors.
  ASSERT_EQ(1U, feed->authors().size());
  EXPECT_EQ(ASCIIToUTF16("tester"), feed->authors()[0]->name());
  EXPECT_EQ("tester@testing.com", feed->authors()[0]->email());

  // Check links.
  ASSERT_EQ(feed->links().size(), 6U);
  const Link* self_link = feed->GetLinkByType(gdata::Link::SELF);
  ASSERT_TRUE(self_link);
  EXPECT_EQ("https://self_link/", self_link->href().spec());
  EXPECT_EQ("application/atom+xml", self_link->mime_type());

  const Link* resumable_link =
      feed->GetLinkByType(gdata::Link::RESUMABLE_CREATE_MEDIA);
  ASSERT_TRUE(resumable_link);
  EXPECT_EQ("https://resumable_create_media_link/",
            resumable_link->href().spec());
  EXPECT_EQ("application/atom+xml", resumable_link->mime_type());

  // Check entries.
  ASSERT_EQ(3U, feed->entries().size());

  // Check a folder entry.
  const DocumentEntry* folder_entry = feed->entries()[0];
  ASSERT_TRUE(folder_entry);
  EXPECT_EQ(gdata::DocumentEntry::FOLDER, folder_entry->kind());
  EXPECT_EQ("\"HhMOFgcNHSt7ImBr\"", folder_entry->etag());
  EXPECT_EQ("folder:1_folder_resouce_id", folder_entry->resource_id());
  EXPECT_EQ("https://1_folder_id", folder_entry->id());
  EXPECT_EQ(ASCIIToUTF16("Entry 1 Title"), folder_entry->title());
  base::Time entry1_update_time;
  base::Time entry1_publish_time;
  ASSERT_TRUE(GDataEntry::GetTimeFromString("2011-04-01T18:34:08.234Z",
                                              &entry1_update_time));
  ASSERT_TRUE(GDataEntry::GetTimeFromString("2010-11-07T05:03:54.719Z",
                                              &entry1_publish_time));
  ASSERT_EQ(entry1_update_time, folder_entry->updated_time());
  ASSERT_EQ(entry1_publish_time, folder_entry->published_time());

  ASSERT_EQ(1U, folder_entry->authors().size());
  EXPECT_EQ(ASCIIToUTF16("entry_tester"), folder_entry->authors()[0]->name());
  EXPECT_EQ("entry_tester@testing.com", folder_entry->authors()[0]->email());
  EXPECT_EQ("https://1_folder_content_url/",
            folder_entry->content_url().spec());
  EXPECT_EQ("application/atom+xml;type=feed",
            folder_entry->content_mime_type());

  ASSERT_EQ(1U, folder_entry->feed_links().size());
  const FeedLink* feed_link = folder_entry->feed_links()[0];
  ASSERT_TRUE(feed_link);
  ASSERT_EQ(gdata::FeedLink::ACL, feed_link->type());

  const Link* entry1_alternate_link =
      folder_entry->GetLinkByType(gdata::Link::ALTERNATE);
  ASSERT_TRUE(entry1_alternate_link);
  EXPECT_EQ("https://1_folder_alternate_link/",
            entry1_alternate_link->href().spec());
  EXPECT_EQ("text/html", entry1_alternate_link->mime_type());

  const Link* entry1_edit_link =
      folder_entry->GetLinkByType(gdata::Link::EDIT);
  ASSERT_TRUE(entry1_edit_link);
  EXPECT_EQ("https://1_edit_link/", entry1_edit_link->href().spec());
  EXPECT_EQ("application/atom+xml", entry1_edit_link->mime_type());

  // Check a file entry.
  const DocumentEntry* file_entry = feed->entries()[1];
  ASSERT_TRUE(file_entry);
  EXPECT_EQ(gdata::DocumentEntry::FILE, file_entry->kind());
  EXPECT_EQ(ASCIIToUTF16("filename.m4a"), file_entry->filename());
  EXPECT_EQ(ASCIIToUTF16("sugg_file_name.m4a"),
            file_entry->suggested_filename());
  EXPECT_EQ("3b4382ebefec6e743578c76bbd0575ce", file_entry->file_md5());
  EXPECT_EQ(892721, file_entry->file_size());
  const Link* file_parent_link =
      file_entry->GetLinkByType(gdata::Link::PARENT);
  ASSERT_TRUE(file_parent_link);
  EXPECT_EQ("https://file_link_parent/", file_parent_link->href().spec());
  EXPECT_EQ("application/atom+xml", file_parent_link->mime_type());
  EXPECT_EQ(ASCIIToUTF16("Medical"), file_parent_link->title());

  // Check a file entry.
  const DocumentEntry* document_entry = feed->entries()[2];
  ASSERT_TRUE(document_entry);
  EXPECT_EQ(gdata::DocumentEntry::DOCUMENT, document_entry->kind());
}


// Test document feed parsing.
TEST_F(GDataParserTest, DocumentEntryXmlParser) {
  scoped_ptr<DocumentEntry> entry(LoadDocumentEntryFromXml("entry.xml"));
  ASSERT_TRUE(entry.get());

  EXPECT_EQ(gdata::DocumentEntry::FILE, entry->kind());
  EXPECT_EQ("\"HhMOFgcNHSt7ImBr\"", entry->etag());
  EXPECT_EQ("file:xml_file_resouce_id", entry->resource_id());
  EXPECT_EQ("https://xml_file_id", entry->id());
  EXPECT_EQ(ASCIIToUTF16("Xml Entry File Title.tar"), entry->title());
  base::Time entry1_update_time;
  base::Time entry1_publish_time;
  ASSERT_TRUE(GDataEntry::GetTimeFromString("2011-04-01T18:34:08.234Z",
                                              &entry1_update_time));
  ASSERT_TRUE(GDataEntry::GetTimeFromString("2010-11-07T05:03:54.719Z",
                                              &entry1_publish_time));
  ASSERT_EQ(entry1_update_time, entry->updated_time());
  ASSERT_EQ(entry1_publish_time, entry->published_time());

  ASSERT_EQ(1U, entry->authors().size());
  EXPECT_EQ(ASCIIToUTF16("entry_tester"), entry->authors()[0]->name());
  EXPECT_EQ("entry_tester@testing.com", entry->authors()[0]->email());
  EXPECT_EQ("https://1_xml_file_entry_content_url/",
            entry->content_url().spec());
  EXPECT_EQ("application/x-tar",
            entry->content_mime_type());

  // Check feed links.
  ASSERT_EQ(2U, entry->feed_links().size());
  const FeedLink* feed_link_1 = entry->feed_links()[0];
  ASSERT_TRUE(feed_link_1);
  ASSERT_EQ(gdata::FeedLink::ACL, feed_link_1->type());
  const FeedLink* feed_link_2 = entry->feed_links()[1];
  ASSERT_TRUE(feed_link_2);
  ASSERT_EQ(gdata::FeedLink::REVISIONS, feed_link_2->type());

  // Check links.
  ASSERT_EQ(7U, entry->links().size());
  const Link* entry1_alternate_link =
      entry->GetLinkByType(gdata::Link::ALTERNATE);
  ASSERT_TRUE(entry1_alternate_link);
  EXPECT_EQ("https://xml_file_entry_id_alternate_link/",
            entry1_alternate_link->href().spec());
  EXPECT_EQ("text/html", entry1_alternate_link->mime_type());

  const Link* entry1_edit_link =
      entry->GetLinkByType(gdata::Link::EDIT_MEDIA);
  ASSERT_TRUE(entry1_edit_link);
  EXPECT_EQ("https://xml_file_entry_id_edit_media_link/",
            entry1_edit_link->href().spec());
  EXPECT_EQ("application/x-tar", entry1_edit_link->mime_type());

  const Link* entry1_self_link =
      entry->GetLinkByType(gdata::Link::SELF);
  ASSERT_TRUE(entry1_self_link);
  EXPECT_EQ("https://xml_file_entry_id_self_link/",
            entry1_self_link->href().spec());
  EXPECT_EQ("application/atom+xml", entry1_self_link->mime_type());

  // Check a file properties.
  EXPECT_EQ(gdata::DocumentEntry::FILE, entry->kind());
  EXPECT_EQ(ASCIIToUTF16("Xml Entry File Name.tar"), entry->filename());
  EXPECT_EQ(ASCIIToUTF16("Xml Entry Suggested File Name.tar"),
            entry->suggested_filename());
  EXPECT_EQ("e48f4d5c46a778de263e0e3f4b3d2a7d", entry->file_md5());
  EXPECT_EQ(26562560, entry->file_size());
}


TEST_F(GDataParserTest, AccountMetadataFeedParser) {
  scoped_ptr<Value> document(LoadJSONFile("account_metadata.json"));
  ASSERT_TRUE(document.get());
  ASSERT_TRUE(document->GetType() == Value::TYPE_DICTIONARY);
  DictionaryValue* entry_value;
  ASSERT_TRUE(reinterpret_cast<DictionaryValue*>(document.get())->GetDictionary(
      std::string("entry"), &entry_value));
  ASSERT_TRUE(entry_value);

  scoped_ptr<AccountMetadataFeed> feed(
      AccountMetadataFeed::CreateFrom(document.get()));
  EXPECT_EQ(1234, feed->quota_bytes_used());
  EXPECT_EQ(12345, feed->quota_bytes_total());
}