summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/fake_drive_file_system_unittest.cc
blob: bbc2c91ec1232d1e6dc29f916d8c46c0bf469496 (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
// Copyright 2013 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/drive/fake_drive_file_system.h"

#include "base/file_util.h"
#include "base/message_loop.h"
#include "chrome/browser/chromeos/drive/drive_file_system_util.h"
#include "chrome/browser/google_apis/fake_drive_service.h"
#include "chrome/browser/google_apis/test_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {
namespace test_util {

class FakeDriveFileSystemTest : public ::testing::Test {
 protected:
  FakeDriveFileSystemTest()
      : ui_thread_(content::BrowserThread::UI, &message_loop_) {
  }

  virtual void SetUp() OVERRIDE {
    // Initialize FakeDriveService.
    fake_drive_service_.reset(new google_apis::FakeDriveService);
    fake_drive_service_->LoadResourceListForWapi(
        "chromeos/gdata/root_feed.json");
    fake_drive_service_->LoadAccountMetadataForWapi(
        "chromeos/gdata/account_metadata.json");
    fake_drive_service_->LoadAppListForDriveApi("chromeos/drive/applist.json");

    // Create a testee instance.
    fake_drive_file_system_.reset(
        new FakeDriveFileSystem(fake_drive_service_.get()));
    ASSERT_TRUE(fake_drive_file_system_->InitializeForTesting());
  }

  MessageLoopForUI message_loop_;
  // The order of the test threads is important, do not change the order.
  // See also content/browser/browser_thread_impl.cc.
  content::TestBrowserThread ui_thread_;

  scoped_ptr<google_apis::FakeDriveService> fake_drive_service_;
  scoped_ptr<FakeDriveFileSystem> fake_drive_file_system_;
};

TEST_F(FakeDriveFileSystemTest, GetEntryInfoByResourceId) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<DriveEntryProto> entry;
  base::FilePath file_path;

  fake_drive_file_system_->GetEntryInfoByResourceId(
      "folder:sub_dir_folder_resource_id",
      google_apis::test_util::CreateCopyResultCallback(
          &error, &file_path, &entry));
  google_apis::test_util::RunBlockingPoolTask();

  ASSERT_EQ(DRIVE_FILE_OK, error);
  EXPECT_EQ(
      util::GetDriveMyDriveRootPath().AppendASCII(
          "Directory 1/Sub Directory Folder"),
      file_path);
  EXPECT_TRUE(entry);  // Just make sure something is returned.
}

TEST_F(FakeDriveFileSystemTest,
       GetEntryInfoByResourceId_PathCompatibleWithGetEntryInfoByPath) {
  const std::string document_resource_id = "document:5_document_resource_id";

  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<DriveEntryProto> entry;
  base::FilePath file_path;

  // Get entry info by resource id.
  fake_drive_file_system_->GetEntryInfoByResourceId(
      document_resource_id,
      google_apis::test_util::CreateCopyResultCallback(
          &error, &file_path, &entry));
  google_apis::test_util::RunBlockingPoolTask();

  ASSERT_EQ(DRIVE_FILE_OK, error);
  ASSERT_TRUE(entry);
  EXPECT_TRUE(entry->file_specific_info().is_hosted_document());

  // Get entry info by path given by GetEntryInfoByResourceId.
  error = DRIVE_FILE_ERROR_FAILED;
  entry.reset();
  fake_drive_file_system_->GetEntryInfoByPath(
      file_path,
      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
  google_apis::test_util::RunBlockingPoolTask();

  ASSERT_EQ(DRIVE_FILE_OK, error);
  ASSERT_TRUE(entry);
  EXPECT_EQ(document_resource_id, entry->resource_id());
}

TEST_F(FakeDriveFileSystemTest, GetFileContentByPath) {
  DriveFileError initialize_error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<DriveEntryProto> entry_proto;
  base::FilePath cache_file_path;
  google_apis::test_util::TestGetContentCallback get_content_callback;
  DriveFileError completion_error = DRIVE_FILE_ERROR_FAILED;

  const base::FilePath kDriveFile =
      util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");

  // For the first time, the file should be downloaded from the service.
  fake_drive_file_system_->GetFileContentByPath(
      kDriveFile,
      google_apis::test_util::CreateCopyResultCallback(
          &initialize_error, &entry_proto, &cache_file_path),
      get_content_callback.callback(),
      google_apis::test_util::CreateCopyResultCallback(&completion_error));
  google_apis::test_util::RunBlockingPoolTask();

  EXPECT_EQ(DRIVE_FILE_OK, initialize_error);
  EXPECT_TRUE(entry_proto);

  // No cache file is available yet.
  EXPECT_TRUE(cache_file_path.empty());

  // The download should be happened so the |get_content_callback|
  // should have the actual data.
  std::string content = get_content_callback.GetConcatenatedData();
  EXPECT_EQ(10U, content.size());
  EXPECT_EQ(DRIVE_FILE_OK, completion_error);

  initialize_error = DRIVE_FILE_ERROR_FAILED;
  entry_proto.reset();
  get_content_callback.mutable_data()->clear();
  completion_error = DRIVE_FILE_ERROR_FAILED;

  // For the second time, the cache file should be found.
  fake_drive_file_system_->GetFileContentByPath(
      kDriveFile,
      google_apis::test_util::CreateCopyResultCallback(
          &initialize_error, &entry_proto, &cache_file_path),
      get_content_callback.callback(),
      google_apis::test_util::CreateCopyResultCallback(&completion_error));
  google_apis::test_util::RunBlockingPoolTask();

  EXPECT_EQ(DRIVE_FILE_OK, initialize_error);
  EXPECT_TRUE(entry_proto);

  // Cache file should be available.
  ASSERT_FALSE(cache_file_path.empty());

  // There should be a cache file so no data should be downloaded.
  EXPECT_TRUE(get_content_callback.data().empty());
  EXPECT_EQ(DRIVE_FILE_OK, completion_error);

  // Make sure the cached file's content.
  std::string cache_file_content;
  ASSERT_TRUE(
      file_util::ReadFileToString(cache_file_path, &cache_file_content));
  EXPECT_EQ(content, cache_file_content);
}

TEST_F(FakeDriveFileSystemTest, GetEntryInfoByPath) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<DriveEntryProto> entry;
  fake_drive_file_system_->GetEntryInfoByPath(
      util::GetDriveMyDriveRootPath().AppendASCII(
          "Directory 1/Sub Directory Folder"),
      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
  google_apis::test_util::RunBlockingPoolTask();

  ASSERT_EQ(DRIVE_FILE_OK, error);
  ASSERT_TRUE(entry);
  EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id());
}

TEST_F(FakeDriveFileSystemTest, GetEntryInfoByPath_Root) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<DriveEntryProto> entry;
  fake_drive_file_system_->GetEntryInfoByPath(
      util::GetDriveMyDriveRootPath(),
      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
  google_apis::test_util::RunBlockingPoolTask();

  ASSERT_EQ(DRIVE_FILE_OK, error);
  ASSERT_TRUE(entry);
  EXPECT_TRUE(entry->file_info().is_directory());
  EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id());
  EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title());
}

TEST_F(FakeDriveFileSystemTest, GetEntryInfoByPath_Invalid) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<DriveEntryProto> entry;
  fake_drive_file_system_->GetEntryInfoByPath(
      util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"),
      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
  google_apis::test_util::RunBlockingPoolTask();

  ASSERT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
  ASSERT_FALSE(entry);
}

}  // namespace test_util
}  // namespace drive