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.
5 #include "chrome/browser/chromeos/file_system_provider/operations/close_file.h"
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"
22 namespace file_system_provider {
23 namespace operations {
26 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
27 const char kFileSystemId[] = "testing-file-system";
28 const int kRequestId = 2;
29 const int kOpenRequestId = 3;
33 class FileSystemProviderOperationsCloseFileTest : public testing::Test {
35 FileSystemProviderOperationsCloseFileTest() {}
36 virtual ~FileSystemProviderOperationsCloseFileTest() {}
38 virtual void SetUp() OVERRIDE {
40 ProvidedFileSystemInfo(kExtensionId,
42 "" /* display_name */,
44 base::FilePath() /* mount_path */);
47 ProvidedFileSystemInfo file_system_info_;
50 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) {
51 using extensions::api::file_system_provider::CloseFileRequestedOptions;
53 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
54 util::StatusCallbackLog callback_log;
56 CloseFile close_file(NULL,
59 base::Bind(&util::LogStatusCallback, &callback_log));
60 close_file.SetDispatchEventImplForTesting(
61 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
62 base::Unretained(&dispatcher)));
64 EXPECT_TRUE(close_file.Execute(kRequestId));
66 ASSERT_EQ(1u, dispatcher.events().size());
67 extensions::Event* event = dispatcher.events()[0];
69 extensions::api::file_system_provider::OnCloseFileRequested::kEventName,
71 base::ListValue* event_args = event->event_args.get();
72 ASSERT_EQ(1u, event_args->GetSize());
74 const base::DictionaryValue* options_as_value = NULL;
75 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
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);
84 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) {
85 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
86 util::StatusCallbackLog callback_log;
88 CloseFile close_file(NULL,
91 base::Bind(&util::LogStatusCallback, &callback_log));
92 close_file.SetDispatchEventImplForTesting(
93 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
94 base::Unretained(&dispatcher)));
96 EXPECT_FALSE(close_file.Execute(kRequestId));
99 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) {
100 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
101 util::StatusCallbackLog callback_log;
103 CloseFile close_file(NULL,
106 base::Bind(&util::LogStatusCallback, &callback_log));
107 close_file.SetDispatchEventImplForTesting(
108 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
109 base::Unretained(&dispatcher)));
111 EXPECT_TRUE(close_file.Execute(kRequestId));
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]);
120 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) {
121 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
122 util::StatusCallbackLog callback_log;
124 CloseFile close_file(NULL,
127 base::Bind(&util::LogStatusCallback, &callback_log));
128 close_file.SetDispatchEventImplForTesting(
129 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
130 base::Unretained(&dispatcher)));
132 EXPECT_TRUE(close_file.Execute(kRequestId));
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]);
141 } // namespace operations
142 } // namespace file_system_provider
143 } // namespace chromeos