OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/file_system_provider/operations/close_file.h" | 5 #include "chrome/browser/chromeos/file_system_provider/operations/create_file.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" |
13 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" | 14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" |
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
15 #include "chrome/common/extensions/api/file_system_provider.h" | 16 #include "chrome/common/extensions/api/file_system_provider.h" |
16 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 17 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
17 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "webkit/browser/fileapi/async_file_util.h" | 20 #include "webkit/browser/fileapi/async_file_util.h" |
20 | 21 |
21 namespace chromeos { | 22 namespace chromeos { |
22 namespace file_system_provider { | 23 namespace file_system_provider { |
23 namespace operations { | 24 namespace operations { |
24 namespace { | 25 namespace { |
25 | 26 |
26 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
27 const char kFileSystemId[] = "testing-file-system"; | 28 const char kFileSystemId[] = "testing-file-system"; |
28 const int kRequestId = 2; | 29 const int kRequestId = 2; |
29 const int kOpenRequestId = 3; | 30 const base::FilePath::CharType kFilePath[] = "/kitty/and/puppy/happy"; |
30 | 31 |
31 } // namespace | 32 } // namespace |
32 | 33 |
33 class FileSystemProviderOperationsCloseFileTest : public testing::Test { | 34 class FileSystemProviderOperationsCreateFileTest : public testing::Test { |
34 protected: | 35 protected: |
35 FileSystemProviderOperationsCloseFileTest() {} | 36 FileSystemProviderOperationsCreateFileTest() {} |
36 virtual ~FileSystemProviderOperationsCloseFileTest() {} | 37 virtual ~FileSystemProviderOperationsCreateFileTest() {} |
37 | 38 |
38 virtual void SetUp() OVERRIDE { | 39 virtual void SetUp() OVERRIDE { |
39 file_system_info_ = | 40 file_system_info_ = |
40 ProvidedFileSystemInfo(kExtensionId, | 41 ProvidedFileSystemInfo(kExtensionId, |
41 kFileSystemId, | 42 kFileSystemId, |
42 "" /* display_name */, | 43 "" /* file_system_name */, |
43 false /* writable */, | 44 true /* writable */, |
44 base::FilePath() /* mount_path */); | 45 base::FilePath() /* mount_path */); |
45 } | 46 } |
46 | 47 |
47 ProvidedFileSystemInfo file_system_info_; | 48 ProvidedFileSystemInfo file_system_info_; |
48 }; | 49 }; |
49 | 50 |
50 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) { | 51 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute) { |
51 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 52 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
52 util::StatusCallbackLog callback_log; | 53 util::StatusCallbackLog callback_log; |
53 | 54 |
54 CloseFile close_file(NULL, | 55 CreateFile create_file(NULL, |
55 file_system_info_, | 56 file_system_info_, |
56 kOpenRequestId, | 57 base::FilePath::FromUTF8Unsafe(kFilePath), |
57 base::Bind(&util::LogStatusCallback, &callback_log)); | 58 base::Bind(&util::LogStatusCallback, &callback_log)); |
58 close_file.SetDispatchEventImplForTesting( | 59 create_file.SetDispatchEventImplForTesting( |
59 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 60 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
60 base::Unretained(&dispatcher))); | 61 base::Unretained(&dispatcher))); |
61 | 62 |
62 EXPECT_TRUE(close_file.Execute(kRequestId)); | 63 EXPECT_TRUE(create_file.Execute(kRequestId)); |
63 | 64 |
64 ASSERT_EQ(1u, dispatcher.events().size()); | 65 ASSERT_EQ(1u, dispatcher.events().size()); |
65 extensions::Event* event = dispatcher.events()[0]; | 66 extensions::Event* event = dispatcher.events()[0]; |
66 EXPECT_EQ( | 67 EXPECT_EQ( |
67 extensions::api::file_system_provider::OnCloseFileRequested::kEventName, | 68 extensions::api::file_system_provider::OnCreateFileRequested::kEventName, |
68 event->event_name); | 69 event->event_name); |
69 base::ListValue* event_args = event->event_args.get(); | 70 base::ListValue* event_args = event->event_args.get(); |
70 ASSERT_EQ(1u, event_args->GetSize()); | 71 ASSERT_EQ(1u, event_args->GetSize()); |
71 | 72 |
72 base::DictionaryValue* options = NULL; | 73 base::DictionaryValue* options = NULL; |
73 ASSERT_TRUE(event_args->GetDictionary(0, &options)); | 74 ASSERT_TRUE(event_args->GetDictionary(0, &options)); |
74 | 75 |
75 std::string event_file_system_id; | 76 std::string event_file_system_id; |
76 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); | 77 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); |
77 EXPECT_EQ(kFileSystemId, event_file_system_id); | 78 EXPECT_EQ(kFileSystemId, event_file_system_id); |
78 | 79 |
79 int event_request_id = -1; | 80 int event_request_id = -1; |
80 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); | 81 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); |
81 EXPECT_EQ(kRequestId, event_request_id); | 82 EXPECT_EQ(kRequestId, event_request_id); |
82 | 83 |
83 int event_open_request_id = -1; | 84 std::string event_file_path; |
84 EXPECT_TRUE(options->GetInteger("openRequestId", &event_open_request_id)); | 85 EXPECT_TRUE(options->GetString("filePath", &event_file_path)); |
85 EXPECT_EQ(kOpenRequestId, event_open_request_id); | 86 EXPECT_EQ(kFilePath, event_file_path); |
86 } | 87 } |
87 | 88 |
88 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) { | 89 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) { |
89 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 90 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
90 util::StatusCallbackLog callback_log; | 91 util::StatusCallbackLog callback_log; |
91 | 92 |
92 CloseFile close_file(NULL, | 93 CreateFile create_file(NULL, |
93 file_system_info_, | 94 file_system_info_, |
94 kOpenRequestId, | 95 base::FilePath::FromUTF8Unsafe(kFilePath), |
95 base::Bind(&util::LogStatusCallback, &callback_log)); | 96 base::Bind(&util::LogStatusCallback, &callback_log)); |
96 close_file.SetDispatchEventImplForTesting( | 97 create_file.SetDispatchEventImplForTesting( |
97 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 98 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
98 base::Unretained(&dispatcher))); | 99 base::Unretained(&dispatcher))); |
99 | 100 |
100 EXPECT_FALSE(close_file.Execute(kRequestId)); | 101 EXPECT_FALSE(create_file.Execute(kRequestId)); |
101 } | 102 } |
102 | 103 |
103 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) { | 104 TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) { |
104 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 105 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
105 util::StatusCallbackLog callback_log; | 106 util::StatusCallbackLog callback_log; |
106 | 107 |
107 CloseFile close_file(NULL, | 108 CreateFile create_file(NULL, |
108 file_system_info_, | 109 file_system_info_, |
109 kOpenRequestId, | 110 base::FilePath::FromUTF8Unsafe(kFilePath), |
110 base::Bind(&util::LogStatusCallback, &callback_log)); | 111 base::Bind(&util::LogStatusCallback, &callback_log)); |
111 close_file.SetDispatchEventImplForTesting( | 112 create_file.SetDispatchEventImplForTesting( |
112 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 113 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
113 base::Unretained(&dispatcher))); | 114 base::Unretained(&dispatcher))); |
114 | 115 |
115 EXPECT_TRUE(close_file.Execute(kRequestId)); | 116 EXPECT_TRUE(create_file.Execute(kRequestId)); |
116 | 117 |
117 close_file.OnSuccess(kRequestId, | 118 create_file.OnSuccess(kRequestId, |
118 scoped_ptr<RequestValue>(new RequestValue()), | 119 scoped_ptr<RequestValue>(new RequestValue()), |
119 false /* has_more */); | 120 false /* has_more */); |
120 ASSERT_EQ(1u, callback_log.size()); | 121 ASSERT_EQ(1u, callback_log.size()); |
121 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); | 122 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); |
122 } | 123 } |
123 | 124 |
124 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) { | 125 TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) { |
125 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 126 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
126 util::StatusCallbackLog callback_log; | 127 util::StatusCallbackLog callback_log; |
127 | 128 |
128 CloseFile close_file(NULL, | 129 CreateFile create_file(NULL, |
129 file_system_info_, | 130 file_system_info_, |
130 kOpenRequestId, | 131 base::FilePath::FromUTF8Unsafe(kFilePath), |
131 base::Bind(&util::LogStatusCallback, &callback_log)); | 132 base::Bind(&util::LogStatusCallback, &callback_log)); |
132 close_file.SetDispatchEventImplForTesting( | 133 create_file.SetDispatchEventImplForTesting( |
133 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 134 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
134 base::Unretained(&dispatcher))); | 135 base::Unretained(&dispatcher))); |
135 | 136 |
136 EXPECT_TRUE(close_file.Execute(kRequestId)); | 137 EXPECT_TRUE(create_file.Execute(kRequestId)); |
137 | 138 |
138 close_file.OnError(kRequestId, | 139 create_file.OnError(kRequestId, |
139 scoped_ptr<RequestValue>(new RequestValue()), | 140 scoped_ptr<RequestValue>(new RequestValue()), |
140 base::File::FILE_ERROR_TOO_MANY_OPENED); | 141 base::File::FILE_ERROR_TOO_MANY_OPENED); |
141 ASSERT_EQ(1u, callback_log.size()); | 142 ASSERT_EQ(1u, callback_log.size()); |
142 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); | 143 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); |
143 } | 144 } |
144 | 145 |
145 } // namespace operations | 146 } // namespace operations |
146 } // namespace file_system_provider | 147 } // namespace file_system_provider |
147 } // namespace chromeos | 148 } // namespace chromeos |
OLD | NEW |