truncate_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/truncate.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[] = "/kitty/and/puppy/happy";
31const int64 kTruncateLength = 64;
32
33}  // namespace
34
35class FileSystemProviderOperationsTruncateTest : public testing::Test {
36 protected:
37  FileSystemProviderOperationsTruncateTest() {}
38  virtual ~FileSystemProviderOperationsTruncateTest() {}
39
40  virtual void SetUp() OVERRIDE {
41    file_system_info_ =
42        ProvidedFileSystemInfo(kExtensionId,
43                               kFileSystemId,
44                               "" /* file_system_name */,
45                               true /* writable */,
46                               base::FilePath() /* mount_path */);
47  }
48
49  ProvidedFileSystemInfo file_system_info_;
50};
51
52TEST_F(FileSystemProviderOperationsTruncateTest, Execute) {
53  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
54  util::StatusCallbackLog callback_log;
55
56  Truncate truncate(NULL,
57                    file_system_info_,
58                    base::FilePath::FromUTF8Unsafe(kFilePath),
59                    kTruncateLength,
60                    base::Bind(&util::LogStatusCallback, &callback_log));
61  truncate.SetDispatchEventImplForTesting(
62      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
63                 base::Unretained(&dispatcher)));
64
65  EXPECT_TRUE(truncate.Execute(kRequestId));
66
67  ASSERT_EQ(1u, dispatcher.events().size());
68  extensions::Event* event = dispatcher.events()[0];
69  EXPECT_EQ(
70      extensions::api::file_system_provider::OnTruncateRequested::kEventName,
71      event->event_name);
72  base::ListValue* event_args = event->event_args.get();
73  ASSERT_EQ(1u, event_args->GetSize());
74
75  base::DictionaryValue* options = NULL;
76  ASSERT_TRUE(event_args->GetDictionary(0, &options));
77
78  std::string event_file_system_id;
79  EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id));
80  EXPECT_EQ(kFileSystemId, event_file_system_id);
81
82  int event_request_id = -1;
83  EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
84  EXPECT_EQ(kRequestId, event_request_id);
85
86  std::string event_file_path;
87  EXPECT_TRUE(options->GetString("filePath", &event_file_path));
88  EXPECT_EQ(kFilePath, event_file_path);
89
90  double event_length = -1;
91  EXPECT_TRUE(options->GetDouble("length", &event_length));
92  EXPECT_EQ(kTruncateLength, static_cast<double>(event_length));
93}
94
95TEST_F(FileSystemProviderOperationsTruncateTest, Execute_NoListener) {
96  util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
97  util::StatusCallbackLog callback_log;
98
99  Truncate truncate(NULL,
100                    file_system_info_,
101                    base::FilePath::FromUTF8Unsafe(kFilePath),
102                    kTruncateLength,
103                    base::Bind(&util::LogStatusCallback, &callback_log));
104  truncate.SetDispatchEventImplForTesting(
105      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
106                 base::Unretained(&dispatcher)));
107
108  EXPECT_FALSE(truncate.Execute(kRequestId));
109}
110
111TEST_F(FileSystemProviderOperationsTruncateTest, Execute_ReadOnly) {
112  util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
113  util::StatusCallbackLog callback_log;
114
115  const ProvidedFileSystemInfo read_only_file_system_info(
116      kExtensionId,
117      kFileSystemId,
118      "" /* file_system_name */,
119      false /* writable */,
120      base::FilePath() /* mount_path */);
121
122  Truncate truncate(NULL,
123                    file_system_info_,
124                    base::FilePath::FromUTF8Unsafe(kFilePath),
125                    kTruncateLength,
126                    base::Bind(&util::LogStatusCallback, &callback_log));
127  truncate.SetDispatchEventImplForTesting(
128      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
129                 base::Unretained(&dispatcher)));
130
131  EXPECT_FALSE(truncate.Execute(kRequestId));
132}
133
134TEST_F(FileSystemProviderOperationsTruncateTest, OnSuccess) {
135  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
136  util::StatusCallbackLog callback_log;
137
138  Truncate truncate(NULL,
139                    file_system_info_,
140                    base::FilePath::FromUTF8Unsafe(kFilePath),
141                    kTruncateLength,
142                    base::Bind(&util::LogStatusCallback, &callback_log));
143  truncate.SetDispatchEventImplForTesting(
144      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
145                 base::Unretained(&dispatcher)));
146
147  EXPECT_TRUE(truncate.Execute(kRequestId));
148
149  truncate.OnSuccess(kRequestId,
150                     scoped_ptr<RequestValue>(new RequestValue()),
151                     false /* has_more */);
152  ASSERT_EQ(1u, callback_log.size());
153  EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
154}
155
156TEST_F(FileSystemProviderOperationsTruncateTest, OnError) {
157  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
158  util::StatusCallbackLog callback_log;
159
160  Truncate truncate(NULL,
161                    file_system_info_,
162                    base::FilePath::FromUTF8Unsafe(kFilePath),
163                    kTruncateLength,
164                    base::Bind(&util::LogStatusCallback, &callback_log));
165  truncate.SetDispatchEventImplForTesting(
166      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
167                 base::Unretained(&dispatcher)));
168
169  EXPECT_TRUE(truncate.Execute(kRequestId));
170
171  truncate.OnError(kRequestId,
172                   scoped_ptr<RequestValue>(new RequestValue()),
173                   base::File::FILE_ERROR_TOO_MANY_OPENED);
174  ASSERT_EQ(1u, callback_log.size());
175  EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
176}
177
178}  // namespace operations
179}  // namespace file_system_provider
180}  // namespace chromeos
181