open_file_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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 "chrome/browser/chromeos/file_system_provider/operations/open_file.h"
6
7#include <string>
8#include <vector>
9
10#include "base/files/file.h"
11#include "base/files/file_path.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/scoped_vector.h"
14#include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
15#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
16#include "chrome/common/extensions/api/file_system_provider.h"
17#include "chrome/common/extensions/api/file_system_provider_internal.h"
18#include "extensions/browser/event_router.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#include "webkit/browser/fileapi/async_file_util.h"
21
22namespace chromeos {
23namespace file_system_provider {
24namespace operations {
25namespace {
26
27const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28const char kFileSystemId[] = "testing-file-system";
29const int kRequestId = 2;
30const base::FilePath::CharType kFilePath[] = "/directory/blueberries.txt";
31
32// Callback invocation logger. Acts as a fileapi end-point.
33class CallbackLogger {
34 public:
35  class Event {
36   public:
37    Event(int file_handle, base::File::Error result)
38        : file_handle_(file_handle), result_(result) {}
39    virtual ~Event() {}
40
41    int file_handle() { return file_handle_; }
42    base::File::Error result() { return result_; }
43
44   private:
45    int file_handle_;
46    base::File::Error result_;
47
48    DISALLOW_COPY_AND_ASSIGN(Event);
49  };
50
51  CallbackLogger() {}
52  virtual ~CallbackLogger() {}
53
54  void OnOpenFile(int file_handle, base::File::Error result) {
55    events_.push_back(new Event(file_handle, result));
56  }
57
58  ScopedVector<Event>& events() { return events_; }
59
60 private:
61  ScopedVector<Event> events_;
62  bool dispatch_reply_;
63
64  DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
65};
66
67}  // namespace
68
69class FileSystemProviderOperationsOpenFileTest : public testing::Test {
70 protected:
71  FileSystemProviderOperationsOpenFileTest() {}
72  virtual ~FileSystemProviderOperationsOpenFileTest() {}
73
74  virtual void SetUp() OVERRIDE {
75    file_system_info_ =
76        ProvidedFileSystemInfo(kExtensionId,
77                               kFileSystemId,
78                               "" /* display_name */,
79                               false /* writable */,
80                               base::FilePath() /* mount_path */);
81  }
82
83  ProvidedFileSystemInfo file_system_info_;
84};
85
86TEST_F(FileSystemProviderOperationsOpenFileTest, Execute) {
87  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
88  CallbackLogger callback_logger;
89
90  OpenFile open_file(NULL,
91                     file_system_info_,
92                     base::FilePath::FromUTF8Unsafe(kFilePath),
93                     ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
94                     base::Bind(&CallbackLogger::OnOpenFile,
95                                base::Unretained(&callback_logger)));
96  open_file.SetDispatchEventImplForTesting(
97      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
98                 base::Unretained(&dispatcher)));
99
100  EXPECT_TRUE(open_file.Execute(kRequestId));
101
102  ASSERT_EQ(1u, dispatcher.events().size());
103  extensions::Event* event = dispatcher.events()[0];
104  EXPECT_EQ(
105      extensions::api::file_system_provider::OnOpenFileRequested::kEventName,
106      event->event_name);
107  base::ListValue* event_args = event->event_args.get();
108  ASSERT_EQ(1u, event_args->GetSize());
109
110  base::DictionaryValue* options = NULL;
111  ASSERT_TRUE(event_args->GetDictionary(0, &options));
112
113  std::string event_file_system_id;
114  EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id));
115  EXPECT_EQ(kFileSystemId, event_file_system_id);
116
117  int event_request_id = -1;
118  EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
119  EXPECT_EQ(kRequestId, event_request_id);
120
121  std::string event_file_path;
122  EXPECT_TRUE(options->GetString("filePath", &event_file_path));
123  EXPECT_EQ(kFilePath, event_file_path);
124
125  std::string event_file_open_mode;
126  EXPECT_TRUE(options->GetString("mode", &event_file_open_mode));
127  const std::string expected_file_open_mode =
128      extensions::api::file_system_provider::ToString(
129          extensions::api::file_system_provider::OPEN_FILE_MODE_READ);
130  EXPECT_EQ(expected_file_open_mode, event_file_open_mode);
131}
132
133TEST_F(FileSystemProviderOperationsOpenFileTest, Execute_NoListener) {
134  util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
135  CallbackLogger callback_logger;
136
137  OpenFile open_file(NULL,
138                     file_system_info_,
139                     base::FilePath::FromUTF8Unsafe(kFilePath),
140                     ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
141                     base::Bind(&CallbackLogger::OnOpenFile,
142                                base::Unretained(&callback_logger)));
143  open_file.SetDispatchEventImplForTesting(
144      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
145                 base::Unretained(&dispatcher)));
146
147  EXPECT_FALSE(open_file.Execute(kRequestId));
148}
149
150TEST_F(FileSystemProviderOperationsOpenFileTest, OnSuccess) {
151  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
152  CallbackLogger callback_logger;
153
154  OpenFile open_file(NULL,
155                     file_system_info_,
156                     base::FilePath::FromUTF8Unsafe(kFilePath),
157                     ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
158                     base::Bind(&CallbackLogger::OnOpenFile,
159                                base::Unretained(&callback_logger)));
160  open_file.SetDispatchEventImplForTesting(
161      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
162                 base::Unretained(&dispatcher)));
163
164  EXPECT_TRUE(open_file.Execute(kRequestId));
165
166  open_file.OnSuccess(kRequestId,
167                      scoped_ptr<RequestValue>(new RequestValue()),
168                      false /* has_more */);
169  ASSERT_EQ(1u, callback_logger.events().size());
170  CallbackLogger::Event* event = callback_logger.events()[0];
171  EXPECT_EQ(base::File::FILE_OK, event->result());
172  EXPECT_LT(0, event->file_handle());
173}
174
175TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) {
176  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
177  CallbackLogger callback_logger;
178
179  OpenFile open_file(NULL,
180                     file_system_info_,
181                     base::FilePath::FromUTF8Unsafe(kFilePath),
182                     ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
183                     base::Bind(&CallbackLogger::OnOpenFile,
184                                base::Unretained(&callback_logger)));
185  open_file.SetDispatchEventImplForTesting(
186      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
187                 base::Unretained(&dispatcher)));
188
189  EXPECT_TRUE(open_file.Execute(kRequestId));
190
191  open_file.OnError(kRequestId,
192                    scoped_ptr<RequestValue>(new RequestValue()),
193                    base::File::FILE_ERROR_TOO_MANY_OPENED);
194  ASSERT_EQ(1u, callback_logger.events().size());
195  CallbackLogger::Event* event = callback_logger.events()[0];
196  EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
197  ASSERT_EQ(0, event->file_handle());
198}
199
200}  // namespace operations
201}  // namespace file_system_provider
202}  // namespace chromeos
203