Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/files/file.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" | |
| 12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info .h" | |
| 13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h" | |
| 14 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | |
| 15 #include "chrome/test/base/testing_profile.h" | |
| 16 #include "content/public/test/test_browser_thread_bundle.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 | |
| 23 namespace { | |
| 24 | |
| 25 static const std::string kExtensionId = "mbflcebpggnecokmikipoihdbecnjfoj"; | |
|
hashimoto
2014/03/26 03:03:01
nit: "static" is not needed.
nit: use char[]
mtomasz
2014/03/26 05:45:13
Done.
| |
| 26 static const int kFileSystemId = 1; | |
| 27 static const std::string kFileSystemName = "Camera Pictures"; | |
| 28 static const base::FilePath::CharType kMountPath[] = FILE_PATH_LITERAL( | |
| 29 "/provided/mbflcebpggnecokmikipoihdbecnjfoj-1-testing_profile-hash"); | |
| 30 | |
| 31 class FakeEventRouter : public extensions::EventRouter { | |
| 32 public: | |
| 33 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {} | |
| 34 virtual ~FakeEventRouter() {} | |
| 35 | |
| 36 virtual void DispatchEventToExtension(const std::string& extension_id, | |
| 37 scoped_ptr<extensions::Event> event) | |
| 38 OVERRIDE { | |
| 39 extension_id_ = extension_id; | |
| 40 event_ = event.Pass(); | |
| 41 } | |
| 42 | |
| 43 std::string extension_id() const { return extension_id_; } | |
|
hashimoto
2014/03/26 03:03:01
nit: const std::string&?
mtomasz
2014/03/26 05:45:13
Done.
| |
| 44 | |
| 45 const extensions::Event* event() const { return event_.get(); } | |
| 46 | |
| 47 private: | |
| 48 std::string extension_id_; | |
| 49 scoped_ptr<extensions::Event> event_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter); | |
| 52 }; | |
| 53 | |
| 54 class EventLogger { | |
| 55 public: | |
| 56 EventLogger() : weak_ptr_factory_(this) {} | |
| 57 virtual ~EventLogger() {} | |
| 58 | |
| 59 void OnStatusCallback(base::File::Error error) { | |
| 60 error_.reset(new base::File::Error(error)); | |
| 61 } | |
| 62 | |
| 63 base::File::Error* error() { return error_.get(); } | |
| 64 | |
| 65 base::WeakPtr<EventLogger> GetWeakPtr() { | |
| 66 return weak_ptr_factory_.GetWeakPtr(); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 scoped_ptr<base::File::Error> error_; | |
| 71 | |
| 72 base::WeakPtrFactory<EventLogger> weak_ptr_factory_; | |
| 73 DISALLOW_COPY_AND_ASSIGN(EventLogger); | |
| 74 }; | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 class FileSystemProviderProvidedFileSystemTest : public testing::Test { | |
| 79 protected: | |
| 80 FileSystemProviderProvidedFileSystemTest() {} | |
| 81 virtual ~FileSystemProviderProvidedFileSystemTest() {} | |
| 82 | |
| 83 virtual void SetUp() OVERRIDE { | |
| 84 profile_.reset(new TestingProfile); | |
| 85 event_router_.reset(new FakeEventRouter(profile_.get())); | |
| 86 request_manager_.reset(new RequestManager()); | |
| 87 file_system_info_.reset( | |
| 88 new ProvidedFileSystemInfo(kExtensionId, | |
| 89 kFileSystemId, | |
| 90 kFileSystemName, | |
| 91 base::FilePath(kMountPath))); | |
| 92 provided_file_system_.reset(new ProvidedFileSystem( | |
| 93 event_router_.get(), request_manager_.get(), *file_system_info_.get())); | |
| 94 } | |
| 95 | |
| 96 content::TestBrowserThreadBundle thread_bundle_; | |
| 97 scoped_ptr<TestingProfile> profile_; | |
| 98 scoped_ptr<FakeEventRouter> event_router_; | |
| 99 scoped_ptr<RequestManager> request_manager_; | |
| 100 scoped_ptr<ProvidedFileSystemInfo> file_system_info_; | |
| 101 scoped_ptr<ProvidedFileSystemInterface> provided_file_system_; | |
| 102 }; | |
| 103 | |
| 104 TEST_F(FileSystemProviderProvidedFileSystemTest, RequestUnmount_Success) { | |
| 105 EventLogger logger; | |
| 106 | |
| 107 bool result = provided_file_system_->RequestUnmount( | |
| 108 base::Bind(&EventLogger::OnStatusCallback, logger.GetWeakPtr())); | |
| 109 ASSERT_TRUE(result); | |
| 110 | |
| 111 // Verify that the event has been sent to the providing extension. | |
| 112 EXPECT_EQ(kExtensionId, event_router_->extension_id()); | |
| 113 const extensions::Event* event = event_router_->event(); | |
| 114 ASSERT_TRUE(event); | |
| 115 ASSERT_TRUE(event->event_args); | |
| 116 base::ListValue* event_args = event->event_args.get(); | |
| 117 ASSERT_EQ(2u, event_args->GetSize()); | |
|
hashimoto
2014/03/26 03:03:01
nit: EXPECT instead of ASSERT?
mtomasz
2014/03/26 05:45:13
Good point. Done.
| |
| 118 int file_system_id; | |
| 119 event_args->GetInteger(0, &file_system_id); | |
|
hashimoto
2014/03/26 03:03:01
EXPECT_TRUE?
mtomasz
2014/03/26 05:45:13
Done.
| |
| 120 EXPECT_EQ(kFileSystemId, file_system_id); | |
| 121 | |
| 122 // Remember the request id, and verify it is valid. | |
| 123 int request_id; | |
|
hashimoto
2014/03/26 03:03:01
nit: Please don't leave this variable uninitialize
mtomasz
2014/03/26 05:45:13
Done.
| |
| 124 event_args->GetInteger(0, &request_id); | |
|
hashimoto
2014/03/26 03:03:01
Why are you calling GetInteger for the index=0 twi
mtomasz
2014/03/26 05:45:13
Should be index=1 for the second one. Coincidental
| |
| 125 EXPECT_LT(0, request_id); | |
| 126 | |
| 127 // Callback should not be called, yet. | |
| 128 EXPECT_FALSE(logger.error()); | |
| 129 | |
| 130 // Simulate sending a success response from the providing extension. | |
| 131 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | |
| 132 bool reply_result = request_manager_->FulfillRequest(kExtensionId, | |
| 133 kFileSystemId, | |
| 134 request_id, | |
| 135 response.Pass(), | |
| 136 false /* has_next */); | |
| 137 ASSERT_TRUE(reply_result); | |
| 138 | |
| 139 // Callback should be called. Verify the error code. | |
| 140 ASSERT_TRUE(logger.error()); | |
| 141 EXPECT_EQ(base::File::FILE_OK, *logger.error()); | |
| 142 } | |
| 143 | |
| 144 TEST_F(FileSystemProviderProvidedFileSystemTest, RequestUnmount_Error) { | |
| 145 EventLogger logger; | |
| 146 | |
| 147 bool result = provided_file_system_->RequestUnmount( | |
| 148 base::Bind(&EventLogger::OnStatusCallback, logger.GetWeakPtr())); | |
| 149 ASSERT_TRUE(result); | |
| 150 | |
| 151 // Verify that the event has been sent to the providing extension. | |
| 152 EXPECT_EQ(kExtensionId, event_router_->extension_id()); | |
| 153 const extensions::Event* event = event_router_->event(); | |
| 154 ASSERT_TRUE(event); | |
| 155 ASSERT_TRUE(event->event_args); | |
| 156 base::ListValue* event_args = event->event_args.get(); | |
| 157 ASSERT_EQ(2u, event_args->GetSize()); | |
| 158 int file_system_id; | |
| 159 event_args->GetInteger(0, &file_system_id); | |
| 160 EXPECT_EQ(kFileSystemId, file_system_id); | |
| 161 | |
| 162 // Remember the request id, and verify it is valid. | |
| 163 int request_id; | |
| 164 event_args->GetInteger(0, &request_id); | |
| 165 EXPECT_LT(0, request_id); | |
| 166 | |
| 167 // Simulate sending an error response from the providing extension. | |
| 168 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | |
| 169 bool reply_result = | |
| 170 request_manager_->RejectRequest(kExtensionId, | |
| 171 kFileSystemId, | |
| 172 request_id, | |
| 173 base::File::FILE_ERROR_NOT_FOUND); | |
| 174 ASSERT_TRUE(reply_result); | |
| 175 | |
| 176 // Callback should be called. Verify the error code. | |
| 177 ASSERT_TRUE(logger.error()); | |
| 178 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); | |
| 179 } | |
| 180 | |
| 181 } // namespace file_system_provider | |
| 182 } // namespace chromeos | |
| OLD | NEW |