Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / operations / abort_unittest.cc
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/abort.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 "storage/browser/fileapi/async_file_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace chromeos {
23 namespace file_system_provider {
24 namespace operations {
25 namespace {
26
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2;
30 const int kOperationRequestId = 3;
31
32 }  // namespace
33
34 class FileSystemProviderOperationsAbortTest : public testing::Test {
35  protected:
36   FileSystemProviderOperationsAbortTest() {}
37   virtual ~FileSystemProviderOperationsAbortTest() {}
38
39   virtual void SetUp() OVERRIDE {
40     file_system_info_ =
41         ProvidedFileSystemInfo(kExtensionId,
42                                kFileSystemId,
43                                "" /* file_system_name */,
44                                false /* writable */,
45                                base::FilePath() /* mount_path */);
46   }
47
48   ProvidedFileSystemInfo file_system_info_;
49 };
50
51 TEST_F(FileSystemProviderOperationsAbortTest, Execute) {
52   using extensions::api::file_system_provider::AbortRequestedOptions;
53
54   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
55   util::StatusCallbackLog callback_log;
56
57   Abort abort(NULL,
58               file_system_info_,
59               kOperationRequestId,
60               base::Bind(&util::LogStatusCallback, &callback_log));
61   abort.SetDispatchEventImplForTesting(
62       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
63                  base::Unretained(&dispatcher)));
64
65   EXPECT_TRUE(abort.Execute(kRequestId));
66
67   ASSERT_EQ(1u, dispatcher.events().size());
68   extensions::Event* event = dispatcher.events()[0];
69   EXPECT_EQ(extensions::api::file_system_provider::OnAbortRequested::kEventName,
70             event->event_name);
71   base::ListValue* event_args = event->event_args.get();
72   ASSERT_EQ(1u, event_args->GetSize());
73
74   const base::DictionaryValue* options_as_value = NULL;
75   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
76
77   AbortRequestedOptions options;
78   ASSERT_TRUE(AbortRequestedOptions::Populate(*options_as_value, &options));
79   EXPECT_EQ(kFileSystemId, options.file_system_id);
80   EXPECT_EQ(kRequestId, options.request_id);
81   EXPECT_EQ(kOperationRequestId, options.operation_request_id);
82 }
83
84 TEST_F(FileSystemProviderOperationsAbortTest, Execute_NoListener) {
85   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
86   util::StatusCallbackLog callback_log;
87
88   Abort abort(NULL,
89               file_system_info_,
90               kOperationRequestId,
91               base::Bind(&util::LogStatusCallback, &callback_log));
92   abort.SetDispatchEventImplForTesting(
93       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
94                  base::Unretained(&dispatcher)));
95
96   EXPECT_FALSE(abort.Execute(kRequestId));
97 }
98
99 TEST_F(FileSystemProviderOperationsAbortTest, OnSuccess) {
100   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
101   util::StatusCallbackLog callback_log;
102
103   Abort abort(NULL,
104               file_system_info_,
105               kOperationRequestId,
106               base::Bind(&util::LogStatusCallback, &callback_log));
107   abort.SetDispatchEventImplForTesting(
108       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
109                  base::Unretained(&dispatcher)));
110
111   EXPECT_TRUE(abort.Execute(kRequestId));
112
113   abort.OnSuccess(kRequestId,
114                   scoped_ptr<RequestValue>(new RequestValue()),
115                   false /* has_more */);
116   ASSERT_EQ(1u, callback_log.size());
117   EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
118 }
119
120 TEST_F(FileSystemProviderOperationsAbortTest, OnError) {
121   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
122   util::StatusCallbackLog callback_log;
123
124   Abort abort(NULL,
125               file_system_info_,
126               kOperationRequestId,
127               base::Bind(&util::LogStatusCallback, &callback_log));
128   abort.SetDispatchEventImplForTesting(
129       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
130                  base::Unretained(&dispatcher)));
131
132   EXPECT_TRUE(abort.Execute(kRequestId));
133
134   abort.OnError(kRequestId,
135                 scoped_ptr<RequestValue>(new RequestValue()),
136                 base::File::FILE_ERROR_TOO_MANY_OPENED);
137   ASSERT_EQ(1u, callback_log.size());
138   EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
139 }
140
141 }  // namespace operations
142 }  // namespace file_system_provider
143 }  // namespace chromeos