summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata/gdata_files_unittest.cc
blob: bbfb1c93ea2186a7ff66af27e33b551d8361f45d (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
// 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 "chrome/browser/chromeos/gdata/gdata_files.h"

#include <string>
#include <utility>
#include <vector>
#include "chrome/browser/chromeos/gdata/gdata.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace gdata {

namespace {

const char kResumableEditMediaUrl[] = "http://resumable-edit-media/";

}  // namespace

TEST(GDataFileTest, FromProto_DetectBadUploadUrl) {
  GDataFileProto proto;
  proto.mutable_gdata_entry()->set_title("test.txt");

  GDataFile file(NULL, NULL);
  // This should fail as the upload URL is empty.
  ASSERT_FALSE(file.FromProto(proto));

  // Set a upload URL.
  proto.set_upload_url(kResumableEditMediaUrl);

  // This should succeed as the resource ID is correct.
  ASSERT_TRUE(file.FromProto(proto));
  EXPECT_EQ(kResumableEditMediaUrl, file.upload_url().spec());
}

TEST(GDataRootDirectoryTest, ParseFromString_DetectBadTitle) {
  GDataRootDirectoryProto proto;
  GDataEntryProto* mutable_entry =
      proto.mutable_gdata_directory()->mutable_gdata_entry();
  mutable_entry->mutable_file_info()->set_is_directory(true);
  mutable_entry->set_resource_id(kGDataRootDirectoryResourceId);

  std::string serialized_proto;
  ASSERT_TRUE(proto.SerializeToString(&serialized_proto));

  GDataRootDirectory root;
  // This should fail as the title is empty.
  // root.title() should be unchanged.
  ASSERT_FALSE(root.ParseFromString(serialized_proto));
  ASSERT_EQ(kGDataRootDirectory, root.title());

  // Setting the title to "gdata".
  mutable_entry->set_title("gdata");
  ASSERT_TRUE(proto.SerializeToString(&serialized_proto));

  // This should fail as the title is not kGDataRootDirectory.
  // root.title() should be unchanged.
  ASSERT_FALSE(root.ParseFromString(serialized_proto));
  ASSERT_EQ(kGDataRootDirectory, root.title());

  // Setting the title to kGDataRootDirectory.
  mutable_entry->set_title(kGDataRootDirectory);
  ASSERT_TRUE(proto.SerializeToString(&serialized_proto));

  // This should succeed as the title is kGDataRootDirectory.
  ASSERT_TRUE(root.ParseFromString(serialized_proto));
  ASSERT_EQ(kGDataRootDirectory, root.title());
}

TEST(GDataRootDirectoryTest, ParseFromString_DetectBadResourceID) {
  GDataRootDirectoryProto proto;
  GDataEntryProto* mutable_entry =
      proto.mutable_gdata_directory()->mutable_gdata_entry();
  mutable_entry->mutable_file_info()->set_is_directory(true);
  mutable_entry->set_title(kGDataRootDirectory);

  std::string serialized_proto;
  ASSERT_TRUE(proto.SerializeToString(&serialized_proto));

  GDataRootDirectory root;
  // This should fail as the resource ID is empty.
  // root.resource_id() should be unchanged.
  ASSERT_FALSE(root.ParseFromString(serialized_proto));
  EXPECT_EQ(kGDataRootDirectoryResourceId, root.resource_id());

  // Set the correct resource ID.
  mutable_entry->set_resource_id(kGDataRootDirectoryResourceId);
  ASSERT_TRUE(proto.SerializeToString(&serialized_proto));

  // This should succeed as the resource ID is correct.
  ASSERT_TRUE(root.ParseFromString(serialized_proto));
  EXPECT_EQ(kGDataRootDirectoryResourceId, root.resource_id());
}

// We have a similar test in FromProto_DetectBadUploadUrl, but the test here
// is to ensure that an error in GDataFile::FromProto() is properly
// propagated to GDataRootDirectory::ParseFromString().
TEST(GDataRootDirectoryTest, ParseFromString_DetectNoUploadUrl) {
  // Set up the root directory properly.
  GDataRootDirectoryProto root_directory_proto;
  GDataEntryProto* mutable_entry =
      root_directory_proto.mutable_gdata_directory()->mutable_gdata_entry();
  mutable_entry->mutable_file_info()->set_is_directory(true);
  mutable_entry->set_title(kGDataRootDirectory);
  mutable_entry->set_resource_id(kGDataRootDirectoryResourceId);
  // Set the origin to the server.
  root_directory_proto.mutable_gdata_directory()->set_origin(FROM_SERVER);

  // Add an empty sub directory under the root directory. This directory is
  // added to ensure that nothing is left when the parsing failed.
  GDataDirectoryProto* sub_directory_proto =
      root_directory_proto.mutable_gdata_directory()->add_child_directories();
  sub_directory_proto->mutable_gdata_entry()->mutable_file_info()->
      set_is_directory(true);
  sub_directory_proto->mutable_gdata_entry()->set_title("empty");

  // Add a sub directory under the root directory.
  sub_directory_proto =
      root_directory_proto.mutable_gdata_directory()->add_child_directories();
  sub_directory_proto->mutable_gdata_entry()->mutable_file_info()->
      set_is_directory(true);
  sub_directory_proto->mutable_gdata_entry()->set_title("dir");

  // Add a new file under the sub directory "dir".
  GDataFileProto* file_proto =
      sub_directory_proto->add_child_files();
  file_proto->mutable_gdata_entry()->set_title("test.txt");

  GDataRootDirectory root;
  // The origin is set to UNINITIALIZED by default.
  ASSERT_EQ(UNINITIALIZED, root.origin());
  std::string serialized_proto;
  // Serialize the proto and check if it's loaded.
  // This should fail as the upload URL is not set for |file_proto|.
  ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto));
  ASSERT_FALSE(root.ParseFromString(serialized_proto));
  // Nothing should be added to the root directory if the parse failed.
  ASSERT_TRUE(root.child_files().empty());
  ASSERT_TRUE(root.child_directories().empty());
  // The origin should remain UNINITIALIZED because the loading failed.
  ASSERT_EQ(UNINITIALIZED, root.origin());

  // Set an upload URL.
  file_proto->set_upload_url(kResumableEditMediaUrl);

  // Serialize the proto and check if it's loaded.
  // This should succeed as the upload URL is set for |file_proto|.
  ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto));
  ASSERT_TRUE(root.ParseFromString(serialized_proto));
  // No file should be added to the root directory.
  ASSERT_TRUE(root.child_files().empty());
  // Two directories ("empty", "dir") should be added to the root directory.
  ASSERT_EQ(2U, root.child_directories().size());
  // The origin should change to FROM_CACHE because we loaded from the cache.
  ASSERT_EQ(FROM_CACHE, root.origin());
}

TEST(GDataRootDirectoryTest, RefreshFile) {
  GDataRootDirectory root;
  // Add a directory to the file system.
  GDataDirectory* directory_entry = new GDataDirectory(&root, &root);
  directory_entry->set_resource_id("folder:directory_resource_id");
  root.AddEntry(directory_entry);

  // Add a new file to the directory.
  GDataFile* initial_file_entry = new GDataFile(NULL, &root);
  initial_file_entry->set_resource_id("file:file_resource_id");
  directory_entry->AddEntry(initial_file_entry);
  ASSERT_EQ(directory_entry, initial_file_entry->parent());

  // Initial file system state set, let's try refreshing entries.

  // New value for the entry with resource id "file:file_resource_id".
  GDataFile* new_file_entry = new GDataFile(NULL, &root);
  new_file_entry->set_resource_id("file:file_resource_id");
  root.RefreshFile(scoped_ptr<GDataFile>(new_file_entry).Pass());
  // Root should have |new_file_entry|, not |initial_file_entry|.
  // If this is not true, |new_file_entry| has probably been destroyed, hence
  // ASSERT (we're trying to access |new_file_entry| later on).
  ASSERT_EQ(new_file_entry, root.GetEntryByResourceId("file:file_resource_id"));
  // We have just verified new_file_entry exists inside root, so accessing
  // |new_file_entry->parent()| should be safe.
  EXPECT_EQ(directory_entry, new_file_entry->parent());

  // Let's try refreshing file that didn't prviously exist.
  GDataFile* non_existent_entry = new GDataFile(NULL, &root);
  non_existent_entry->set_resource_id("file:does_not_exist");
  root.RefreshFile(scoped_ptr<GDataFile>(non_existent_entry).Pass());
  // File with non existent resource id should not be added.
  EXPECT_FALSE(root.GetEntryByResourceId("file:does_not_exist"));
}

TEST(GDataRootDirectoryTest, GetEntryByResourceId_RootDirectory) {
  GDataRootDirectory root;
  // Look up the root directory by its resource ID.
  GDataEntry* entry = root.GetEntryByResourceId(kGDataRootDirectoryResourceId);
  ASSERT_TRUE(entry);
  EXPECT_TRUE(entry->AsGDataRootDirectory());
  EXPECT_EQ(&root, entry->AsGDataRootDirectory());
}

}  // namespace gdata