Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / operations / close_file_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/close_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 "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
15 #include "chrome/common/extensions/api/file_system_provider.h"
16 #include "chrome/common/extensions/api/file_system_provider_internal.h"
17 #include "extensions/browser/event_router.h"
18 #include "storage/browser/fileapi/async_file_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace chromeos {
22 namespace file_system_provider {
23 namespace operations {
24 namespace {
25
26 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
27 const char kFileSystemId[] = "testing-file-system";
28 const int kRequestId = 2;
29 const int kOpenRequestId = 3;
30
31 }  // namespace
32
33 class FileSystemProviderOperationsCloseFileTest : public testing::Test {
34  protected:
35   FileSystemProviderOperationsCloseFileTest() {}
36   virtual ~FileSystemProviderOperationsCloseFileTest() {}
37
38   virtual void SetUp() OVERRIDE {
39     file_system_info_ =
40         ProvidedFileSystemInfo(kExtensionId,
41                                kFileSystemId,
42                                "" /* display_name */,
43                                false /* writable */,
44                                base::FilePath() /* mount_path */);
45   }
46
47   ProvidedFileSystemInfo file_system_info_;
48 };
49
50 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) {
51   using extensions::api::file_system_provider::CloseFileRequestedOptions;
52
53   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
54   util::StatusCallbackLog callback_log;
55
56   CloseFile close_file(NULL,
57                        file_system_info_,
58                        kOpenRequestId,
59                        base::Bind(&util::LogStatusCallback, &callback_log));
60   close_file.SetDispatchEventImplForTesting(
61       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
62                  base::Unretained(&dispatcher)));
63
64   EXPECT_TRUE(close_file.Execute(kRequestId));
65
66   ASSERT_EQ(1u, dispatcher.events().size());
67   extensions::Event* event = dispatcher.events()[0];
68   EXPECT_EQ(
69       extensions::api::file_system_provider::OnCloseFileRequested::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   CloseFileRequestedOptions options;
78   ASSERT_TRUE(CloseFileRequestedOptions::Populate(*options_as_value, &options));
79   EXPECT_EQ(kFileSystemId, options.file_system_id);
80   EXPECT_EQ(kRequestId, options.request_id);
81   EXPECT_EQ(kOpenRequestId, options.open_request_id);
82 }
83
84 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) {
85   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
86   util::StatusCallbackLog callback_log;
87
88   CloseFile close_file(NULL,
89                        file_system_info_,
90                        kOpenRequestId,
91                        base::Bind(&util::LogStatusCallback, &callback_log));
92   close_file.SetDispatchEventImplForTesting(
93       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
94                  base::Unretained(&dispatcher)));
95
96   EXPECT_FALSE(close_file.Execute(kRequestId));
97 }
98
99 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) {
100   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
101   util::StatusCallbackLog callback_log;
102
103   CloseFile close_file(NULL,
104                        file_system_info_,
105                        kOpenRequestId,
106                        base::Bind(&util::LogStatusCallback, &callback_log));
107   close_file.SetDispatchEventImplForTesting(
108       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
109                  base::Unretained(&dispatcher)));
110
111   EXPECT_TRUE(close_file.Execute(kRequestId));
112
113   close_file.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(FileSystemProviderOperationsCloseFileTest, OnError) {
121   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
122   util::StatusCallbackLog callback_log;
123
124   CloseFile close_file(NULL,
125                        file_system_info_,
126                        kOpenRequestId,
127                        base::Bind(&util::LogStatusCallback, &callback_log));
128   close_file.SetDispatchEventImplForTesting(
129       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
130                  base::Unretained(&dispatcher)));
131
132   EXPECT_TRUE(close_file.Execute(kRequestId));
133
134   close_file.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