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/remove_watcher.h"
6
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/files/file.h"
13 #include "base/files/file_path.h"
14 #include "chrome/browser/chromeos/file_system_provider/icon_set.h"
15 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
17 #include "chrome/common/extensions/api/file_system_provider.h"
18 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h"
19 #include "chrome/common/extensions/api/file_system_provider_internal.h"
20 #include "extensions/browser/event_router.h"
21 #include "storage/browser/file_system/async_file_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace chromeos {
25 namespace file_system_provider {
26 namespace operations {
27 namespace {
28
29 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
30 const char kFileSystemId[] = "testing-file-system";
31 const int kRequestId = 2;
32 const base::FilePath::CharType kEntryPath[] =
33 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
34
35 } // namespace
36
37 class FileSystemProviderOperationsRemoveWatcherTest : public testing::Test {
38 protected:
FileSystemProviderOperationsRemoveWatcherTest()39 FileSystemProviderOperationsRemoveWatcherTest() {}
~FileSystemProviderOperationsRemoveWatcherTest()40 ~FileSystemProviderOperationsRemoveWatcherTest() override {}
41
SetUp()42 void SetUp() override {
43 file_system_info_ = ProvidedFileSystemInfo(
44 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
45 base::FilePath(), false /* configurable */, true /* watchable */,
46 extensions::SOURCE_FILE, IconSet());
47 }
48
49 ProvidedFileSystemInfo file_system_info_;
50 };
51
TEST_F(FileSystemProviderOperationsRemoveWatcherTest,Execute)52 TEST_F(FileSystemProviderOperationsRemoveWatcherTest, Execute) {
53 using extensions::api::file_system_provider::RemoveWatcherRequestedOptions;
54
55 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
56 util::StatusCallbackLog callback_log;
57
58 RemoveWatcher remove_watcher(
59 NULL, file_system_info_, base::FilePath(kEntryPath), true /* recursive */,
60 base::BindOnce(&util::LogStatusCallback, &callback_log));
61 remove_watcher.SetDispatchEventImplForTesting(
62 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
63 base::Unretained(&dispatcher)));
64
65 EXPECT_TRUE(remove_watcher.Execute(kRequestId));
66
67 ASSERT_EQ(1u, dispatcher.events().size());
68 extensions::Event* event = dispatcher.events()[0].get();
69 EXPECT_EQ(extensions::api::file_system_provider::OnRemoveWatcherRequested::
70 kEventName,
71 event->event_name);
72 base::ListValue* event_args = event->event_args.get();
73 ASSERT_EQ(1u, event_args->GetSize());
74
75 const base::DictionaryValue* options_as_value = NULL;
76 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
77
78 RemoveWatcherRequestedOptions options;
79 ASSERT_TRUE(
80 RemoveWatcherRequestedOptions::Populate(*options_as_value, &options));
81 EXPECT_EQ(kFileSystemId, options.file_system_id);
82 EXPECT_EQ(kRequestId, options.request_id);
83 EXPECT_EQ(kEntryPath, options.entry_path);
84 EXPECT_TRUE(options.recursive);
85 }
86
TEST_F(FileSystemProviderOperationsRemoveWatcherTest,Execute_NoListener)87 TEST_F(FileSystemProviderOperationsRemoveWatcherTest, Execute_NoListener) {
88 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
89 util::StatusCallbackLog callback_log;
90
91 RemoveWatcher remove_watcher(
92 NULL, file_system_info_, base::FilePath(kEntryPath), true /* recursive */,
93 base::BindOnce(&util::LogStatusCallback, &callback_log));
94 remove_watcher.SetDispatchEventImplForTesting(
95 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
96 base::Unretained(&dispatcher)));
97
98 EXPECT_FALSE(remove_watcher.Execute(kRequestId));
99 }
100
TEST_F(FileSystemProviderOperationsRemoveWatcherTest,OnSuccess)101 TEST_F(FileSystemProviderOperationsRemoveWatcherTest, OnSuccess) {
102 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
103 util::StatusCallbackLog callback_log;
104
105 RemoveWatcher remove_watcher(
106 NULL, file_system_info_, base::FilePath(kEntryPath), true /* recursive */,
107 base::BindOnce(&util::LogStatusCallback, &callback_log));
108 remove_watcher.SetDispatchEventImplForTesting(
109 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
110 base::Unretained(&dispatcher)));
111
112 EXPECT_TRUE(remove_watcher.Execute(kRequestId));
113
114 remove_watcher.OnSuccess(kRequestId,
115 std::unique_ptr<RequestValue>(new RequestValue()),
116 false /* has_more */);
117 ASSERT_EQ(1u, callback_log.size());
118 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
119 }
120
TEST_F(FileSystemProviderOperationsRemoveWatcherTest,OnError)121 TEST_F(FileSystemProviderOperationsRemoveWatcherTest, OnError) {
122 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
123 util::StatusCallbackLog callback_log;
124
125 RemoveWatcher remove_watcher(
126 NULL, file_system_info_, base::FilePath(kEntryPath), true /* recursive */,
127 base::BindOnce(&util::LogStatusCallback, &callback_log));
128 remove_watcher.SetDispatchEventImplForTesting(
129 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
130 base::Unretained(&dispatcher)));
131
132 EXPECT_TRUE(remove_watcher.Execute(kRequestId));
133
134 remove_watcher.OnError(kRequestId,
135 std::unique_ptr<RequestValue>(new RequestValue()),
136 base::File::FILE_ERROR_TOO_MANY_OPENED);
137 ASSERT_EQ(1u, callback_log.size());
138 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
139 }
140
141 } // namespace operations
142 } // namespace file_system_provider
143 } // namespace chromeos
144