Upstream version 11.40.277.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_ = ProvidedFileSystemInfo(
40         kExtensionId,
41         MountOptions(kFileSystemId, "" /* display_name */),
42         base::FilePath());
43   }
44
45   ProvidedFileSystemInfo file_system_info_;
46 };
47
48 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) {
49   using extensions::api::file_system_provider::CloseFileRequestedOptions;
50
51   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
52   util::StatusCallbackLog callback_log;
53
54   CloseFile close_file(NULL,
55                        file_system_info_,
56                        kOpenRequestId,
57                        base::Bind(&util::LogStatusCallback, &callback_log));
58   close_file.SetDispatchEventImplForTesting(
59       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
60                  base::Unretained(&dispatcher)));
61
62   EXPECT_TRUE(close_file.Execute(kRequestId));
63
64   ASSERT_EQ(1u, dispatcher.events().size());
65   extensions::Event* event = dispatcher.events()[0];
66   EXPECT_EQ(
67       extensions::api::file_system_provider::OnCloseFileRequested::kEventName,
68       event->event_name);
69   base::ListValue* event_args = event->event_args.get();
70   ASSERT_EQ(1u, event_args->GetSize());
71
72   const base::DictionaryValue* options_as_value = NULL;
73   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
74
75   CloseFileRequestedOptions options;
76   ASSERT_TRUE(CloseFileRequestedOptions::Populate(*options_as_value, &options));
77   EXPECT_EQ(kFileSystemId, options.file_system_id);
78   EXPECT_EQ(kRequestId, options.request_id);
79   EXPECT_EQ(kOpenRequestId, options.open_request_id);
80 }
81
82 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) {
83   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
84   util::StatusCallbackLog callback_log;
85
86   CloseFile close_file(NULL,
87                        file_system_info_,
88                        kOpenRequestId,
89                        base::Bind(&util::LogStatusCallback, &callback_log));
90   close_file.SetDispatchEventImplForTesting(
91       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
92                  base::Unretained(&dispatcher)));
93
94   EXPECT_FALSE(close_file.Execute(kRequestId));
95 }
96
97 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) {
98   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
99   util::StatusCallbackLog callback_log;
100
101   CloseFile close_file(NULL,
102                        file_system_info_,
103                        kOpenRequestId,
104                        base::Bind(&util::LogStatusCallback, &callback_log));
105   close_file.SetDispatchEventImplForTesting(
106       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
107                  base::Unretained(&dispatcher)));
108
109   EXPECT_TRUE(close_file.Execute(kRequestId));
110
111   close_file.OnSuccess(kRequestId,
112                        scoped_ptr<RequestValue>(new RequestValue()),
113                        false /* has_more */);
114   ASSERT_EQ(1u, callback_log.size());
115   EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
116 }
117
118 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) {
119   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
120   util::StatusCallbackLog callback_log;
121
122   CloseFile close_file(NULL,
123                        file_system_info_,
124                        kOpenRequestId,
125                        base::Bind(&util::LogStatusCallback, &callback_log));
126   close_file.SetDispatchEventImplForTesting(
127       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
128                  base::Unretained(&dispatcher)));
129
130   EXPECT_TRUE(close_file.Execute(kRequestId));
131
132   close_file.OnError(kRequestId,
133                      scoped_ptr<RequestValue>(new RequestValue()),
134                      base::File::FILE_ERROR_TOO_MANY_OPENED);
135   ASSERT_EQ(1u, callback_log.size());
136   EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
137 }
138
139 }  // namespace operations
140 }  // namespace file_system_provider
141 }  // namespace chromeos