Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / close_file_unittest.cc
blobd73f3463262f26ecd5a4d8a3102253c5aac17895
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"
7 #include <string>
8 #include <vector>
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_capabilities/file_system_provider_capabilities_handler.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"
22 namespace chromeos {
23 namespace file_system_provider {
24 namespace operations {
25 namespace {
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2;
30 const int kOpenRequestId = 3;
32 } // namespace
34 class FileSystemProviderOperationsCloseFileTest : public testing::Test {
35 protected:
36 FileSystemProviderOperationsCloseFileTest() {}
37 ~FileSystemProviderOperationsCloseFileTest() override {}
39 void SetUp() override {
40 file_system_info_ = ProvidedFileSystemInfo(
41 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
42 base::FilePath(), false /* configurable */, true /* watchable */,
43 extensions::SOURCE_FILE);
46 ProvidedFileSystemInfo file_system_info_;
49 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) {
50 using extensions::api::file_system_provider::CloseFileRequestedOptions;
52 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
53 util::StatusCallbackLog callback_log;
55 CloseFile close_file(NULL,
56 file_system_info_,
57 kOpenRequestId,
58 base::Bind(&util::LogStatusCallback, &callback_log));
59 close_file.SetDispatchEventImplForTesting(
60 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
61 base::Unretained(&dispatcher)));
63 EXPECT_TRUE(close_file.Execute(kRequestId));
65 ASSERT_EQ(1u, dispatcher.events().size());
66 extensions::Event* event = dispatcher.events()[0];
67 EXPECT_EQ(
68 extensions::api::file_system_provider::OnCloseFileRequested::kEventName,
69 event->event_name);
70 base::ListValue* event_args = event->event_args.get();
71 ASSERT_EQ(1u, event_args->GetSize());
73 const base::DictionaryValue* options_as_value = NULL;
74 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
76 CloseFileRequestedOptions options;
77 ASSERT_TRUE(CloseFileRequestedOptions::Populate(*options_as_value, &options));
78 EXPECT_EQ(kFileSystemId, options.file_system_id);
79 EXPECT_EQ(kRequestId, options.request_id);
80 EXPECT_EQ(kOpenRequestId, options.open_request_id);
83 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) {
84 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
85 util::StatusCallbackLog callback_log;
87 CloseFile close_file(NULL,
88 file_system_info_,
89 kOpenRequestId,
90 base::Bind(&util::LogStatusCallback, &callback_log));
91 close_file.SetDispatchEventImplForTesting(
92 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
93 base::Unretained(&dispatcher)));
95 EXPECT_FALSE(close_file.Execute(kRequestId));
98 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) {
99 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
100 util::StatusCallbackLog callback_log;
102 CloseFile close_file(NULL,
103 file_system_info_,
104 kOpenRequestId,
105 base::Bind(&util::LogStatusCallback, &callback_log));
106 close_file.SetDispatchEventImplForTesting(
107 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
108 base::Unretained(&dispatcher)));
110 EXPECT_TRUE(close_file.Execute(kRequestId));
112 close_file.OnSuccess(kRequestId,
113 scoped_ptr<RequestValue>(new RequestValue()),
114 false /* has_more */);
115 ASSERT_EQ(1u, callback_log.size());
116 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
119 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) {
120 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
121 util::StatusCallbackLog callback_log;
123 CloseFile close_file(NULL,
124 file_system_info_,
125 kOpenRequestId,
126 base::Bind(&util::LogStatusCallback, &callback_log));
127 close_file.SetDispatchEventImplForTesting(
128 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
129 base::Unretained(&dispatcher)));
131 EXPECT_TRUE(close_file.Execute(kRequestId));
133 close_file.OnError(kRequestId,
134 scoped_ptr<RequestValue>(new RequestValue()),
135 base::File::FILE_ERROR_TOO_MANY_OPENED);
136 ASSERT_EQ(1u, callback_log.size());
137 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
140 } // namespace operations
141 } // namespace file_system_provider
142 } // namespace chromeos