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 // TODO(mtomasz): Move these test cases to operations/unmount_unittest.cc.
10 #include "base/files/file.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h"
15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h"
16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
18 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
19 #include "chrome/common/extensions/api/file_system_provider.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "extensions/browser/event_router.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 namespace file_system_provider
{
30 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
31 const int kExpectedRequestId
= 1;
32 const int kFileSystemId
= 2;
33 const char kFileSystemName
[] = "Camera Pictures";
35 class FakeEventRouter
: public extensions::EventRouter
{
37 explicit FakeEventRouter(Profile
* profile
) : EventRouter(profile
, NULL
) {}
38 virtual ~FakeEventRouter() {}
40 virtual void DispatchEventToExtension(const std::string
& extension_id
,
41 scoped_ptr
<extensions::Event
> event
)
43 extension_id_
= extension_id
;
44 event_
= event
.Pass();
47 const std::string
& extension_id() const { return extension_id_
; }
49 const extensions::Event
* event() const { return event_
.get(); }
52 std::string extension_id_
;
53 scoped_ptr
<extensions::Event
> event_
;
55 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter
);
60 EventLogger() : weak_ptr_factory_(this) {}
61 virtual ~EventLogger() {}
63 void OnStatusCallback(base::File::Error error
) {
64 error_
.reset(new base::File::Error(error
));
67 base::File::Error
* error() { return error_
.get(); }
69 base::WeakPtr
<EventLogger
> GetWeakPtr() {
70 return weak_ptr_factory_
.GetWeakPtr();
74 scoped_ptr
<base::File::Error
> error_
;
76 base::WeakPtrFactory
<EventLogger
> weak_ptr_factory_
;
77 DISALLOW_COPY_AND_ASSIGN(EventLogger
);
82 class FileSystemProviderProvidedFileSystemTest
: public testing::Test
{
84 FileSystemProviderProvidedFileSystemTest() {}
85 virtual ~FileSystemProviderProvidedFileSystemTest() {}
87 virtual void SetUp() OVERRIDE
{
88 profile_
.reset(new TestingProfile
);
89 event_router_
.reset(new FakeEventRouter(profile_
.get()));
90 event_router_
->AddEventListener(
91 extensions::api::file_system_provider::OnUnmountRequested::kEventName
,
95 base::FilePath mount_path
=
96 util::GetMountPath(profile_
.get(), kExtensionId
, kFileSystemId
);
97 file_system_info_
.reset(new ProvidedFileSystemInfo(
98 kExtensionId
, kFileSystemId
, kFileSystemName
, mount_path
));
99 provided_file_system_
.reset(
100 new ProvidedFileSystem(event_router_
.get(), *file_system_info_
.get()));
103 content::TestBrowserThreadBundle thread_bundle_
;
104 scoped_ptr
<TestingProfile
> profile_
;
105 scoped_ptr
<FakeEventRouter
> event_router_
;
106 scoped_ptr
<ProvidedFileSystemInfo
> file_system_info_
;
107 scoped_ptr
<ProvidedFileSystemInterface
> provided_file_system_
;
110 TEST_F(FileSystemProviderProvidedFileSystemTest
, RequestUnmount_Success
) {
113 provided_file_system_
->RequestUnmount(
114 base::Bind(&EventLogger::OnStatusCallback
, logger
.GetWeakPtr()));
115 base::RunLoop().RunUntilIdle();
117 // Verify that the event has been sent to the providing extension.
118 EXPECT_EQ(kExtensionId
, event_router_
->extension_id());
119 const extensions::Event
* event
= event_router_
->event();
121 ASSERT_TRUE(event
->event_args
);
122 base::ListValue
* event_args
= event
->event_args
.get();
123 EXPECT_EQ(2u, event_args
->GetSize());
124 int file_system_id
= 0;
125 EXPECT_TRUE(event_args
->GetInteger(0, &file_system_id
));
126 EXPECT_EQ(kFileSystemId
, file_system_id
);
128 // Remember the request id, and verify it is valid.
130 EXPECT_TRUE(event_args
->GetInteger(1, &request_id
));
131 EXPECT_EQ(kExpectedRequestId
, request_id
);
133 // Callback should not be called, yet.
134 EXPECT_FALSE(logger
.error());
136 // Simulate sending a success response from the providing extension.
137 RequestManager
* request_manager
= provided_file_system_
->GetRequestManager();
138 ASSERT_TRUE(request_manager
);
139 scoped_ptr
<RequestValue
> response
;
140 bool reply_result
= request_manager
->FulfillRequest(
141 request_id
, response
.Pass(), false /* has_next */);
142 EXPECT_TRUE(reply_result
);
144 // Callback should be called. Verify the error code.
145 ASSERT_TRUE(logger
.error());
146 EXPECT_EQ(base::File::FILE_OK
, *logger
.error());
149 TEST_F(FileSystemProviderProvidedFileSystemTest
, RequestUnmount_Error
) {
152 provided_file_system_
->RequestUnmount(
153 base::Bind(&EventLogger::OnStatusCallback
, logger
.GetWeakPtr()));
154 base::RunLoop().RunUntilIdle();
156 // Verify that the event has been sent to the providing extension.
157 EXPECT_EQ(kExtensionId
, event_router_
->extension_id());
158 const extensions::Event
* event
= event_router_
->event();
160 ASSERT_TRUE(event
->event_args
);
161 base::ListValue
* event_args
= event
->event_args
.get();
162 EXPECT_EQ(2u, event_args
->GetSize());
163 int file_system_id
= 0;
164 EXPECT_TRUE(event_args
->GetInteger(0, &file_system_id
));
165 EXPECT_EQ(kFileSystemId
, file_system_id
);
167 // Remember the request id, and verify it is valid.
169 EXPECT_TRUE(event_args
->GetInteger(1, &request_id
));
170 EXPECT_EQ(kExpectedRequestId
, request_id
);
172 // Simulate sending an error response from the providing extension.
173 RequestManager
* request_manager
= provided_file_system_
->GetRequestManager();
174 ASSERT_TRUE(request_manager
);
175 bool reply_result
= request_manager
->RejectRequest(
176 request_id
, base::File::FILE_ERROR_NOT_FOUND
);
177 EXPECT_TRUE(reply_result
);
179 // Callback should be called. Verify the error code.
180 ASSERT_TRUE(logger
.error());
181 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, *logger
.error());
184 } // namespace file_system_provider
185 } // namespace chromeos