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/unmount.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 "testing/gtest/include/gtest/gtest.h" 19 20 namespace chromeos { 21 namespace file_system_provider { 22 namespace operations { 23 namespace { 24 25 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; 26 const char kFileSystemId[] = "testing-file-system"; 27 const int kRequestId = 2; 28 29 } // namespace 30 31 class FileSystemProviderOperationsUnmountTest : public testing::Test { 32 protected: 33 FileSystemProviderOperationsUnmountTest() {} 34 virtual ~FileSystemProviderOperationsUnmountTest() {} 35 36 virtual void SetUp() OVERRIDE { 37 file_system_info_ = 38 ProvidedFileSystemInfo(kExtensionId, 39 kFileSystemId, 40 "" /* display_name */, 41 false /* writable */, 42 base::FilePath() /* mount_path */); 43 } 44 45 ProvidedFileSystemInfo file_system_info_; 46 }; 47 48 TEST_F(FileSystemProviderOperationsUnmountTest, Execute) { 49 using extensions::api::file_system_provider::UnmountRequestedOptions; 50 51 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 52 util::StatusCallbackLog callback_log; 53 54 Unmount unmount(NULL, 55 file_system_info_, 56 base::Bind(&util::LogStatusCallback, &callback_log)); 57 unmount.SetDispatchEventImplForTesting( 58 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 59 base::Unretained(&dispatcher))); 60 61 EXPECT_TRUE(unmount.Execute(kRequestId)); 62 63 ASSERT_EQ(1u, dispatcher.events().size()); 64 extensions::Event* event = dispatcher.events()[0]; 65 EXPECT_EQ( 66 extensions::api::file_system_provider::OnUnmountRequested::kEventName, 67 event->event_name); 68 base::ListValue* event_args = event->event_args.get(); 69 ASSERT_EQ(1u, event_args->GetSize()); 70 71 const base::DictionaryValue* options_as_value = NULL; 72 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); 73 74 UnmountRequestedOptions options; 75 ASSERT_TRUE(UnmountRequestedOptions::Populate(*options_as_value, &options)); 76 EXPECT_EQ(kFileSystemId, options.file_system_id); 77 EXPECT_EQ(kRequestId, options.request_id); 78 } 79 80 TEST_F(FileSystemProviderOperationsUnmountTest, Execute_NoListener) { 81 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); 82 util::StatusCallbackLog callback_log; 83 84 Unmount unmount(NULL, 85 file_system_info_, 86 base::Bind(&util::LogStatusCallback, &callback_log)); 87 unmount.SetDispatchEventImplForTesting( 88 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 89 base::Unretained(&dispatcher))); 90 91 EXPECT_FALSE(unmount.Execute(kRequestId)); 92 } 93 94 TEST_F(FileSystemProviderOperationsUnmountTest, OnSuccess) { 95 using extensions::api::file_system_provider_internal:: 96 UnmountRequestedSuccess::Params; 97 98 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 99 util::StatusCallbackLog callback_log; 100 101 Unmount unmount(NULL, 102 file_system_info_, 103 base::Bind(&util::LogStatusCallback, &callback_log)); 104 unmount.SetDispatchEventImplForTesting( 105 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 106 base::Unretained(&dispatcher))); 107 108 EXPECT_TRUE(unmount.Execute(kRequestId)); 109 110 unmount.OnSuccess(kRequestId, 111 scoped_ptr<RequestValue>(new RequestValue()), 112 false /* has_more */); 113 ASSERT_EQ(1u, callback_log.size()); 114 base::File::Error event_result = callback_log[0]; 115 EXPECT_EQ(base::File::FILE_OK, event_result); 116 } 117 118 TEST_F(FileSystemProviderOperationsUnmountTest, OnError) { 119 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 120 util::StatusCallbackLog callback_log; 121 122 Unmount unmount(NULL, 123 file_system_info_, 124 base::Bind(&util::LogStatusCallback, &callback_log)); 125 unmount.SetDispatchEventImplForTesting( 126 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 127 base::Unretained(&dispatcher))); 128 129 EXPECT_TRUE(unmount.Execute(kRequestId)); 130 131 unmount.OnError(kRequestId, 132 scoped_ptr<RequestValue>(new RequestValue()), 133 base::File::FILE_ERROR_NOT_FOUND); 134 ASSERT_EQ(1u, callback_log.size()); 135 base::File::Error event_result = callback_log[0]; 136 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, event_result); 137 } 138 139 } // namespace operations 140 } // namespace file_system_provider 141 } // namespace chromeos 142