| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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/extensions/file_manager/file_tasks.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/prefs/pref_registry_simple.h" | |
| 11 #include "base/prefs/testing_pref_service.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/chromeos/drive/drive_app_registry.h" | |
| 14 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
| 15 #include "chrome/browser/chromeos/extensions/file_manager/app_id.h" | |
| 16 #include "chrome/browser/google_apis/drive_api_parser.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace file_manager { | |
| 22 namespace file_tasks { | |
| 23 namespace { | |
| 24 | |
| 25 // Registers the default task preferences. Used for testing | |
| 26 // ChooseAndSetDefaultTask(). | |
| 27 void RegisterDefaultTaskPreferences(TestingPrefServiceSimple* pref_service) { | |
| 28 DCHECK(pref_service); | |
| 29 | |
| 30 pref_service->registry()->RegisterDictionaryPref( | |
| 31 prefs::kDefaultTasksByMimeType); | |
| 32 pref_service->registry()->RegisterDictionaryPref( | |
| 33 prefs::kDefaultTasksBySuffix); | |
| 34 } | |
| 35 | |
| 36 // Updates the default task preferences per the given dictionary values. Used | |
| 37 // for testing ChooseAndSetDefaultTask. | |
| 38 void UpdateDefaultTaskPreferences(TestingPrefServiceSimple* pref_service, | |
| 39 const DictionaryValue& mime_types, | |
| 40 const DictionaryValue& suffixes) { | |
| 41 DCHECK(pref_service); | |
| 42 | |
| 43 pref_service->Set(prefs::kDefaultTasksByMimeType, mime_types); | |
| 44 pref_service->Set(prefs::kDefaultTasksBySuffix, suffixes); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 TEST(FileManagerFileTasksTest, | |
| 50 FullTaskDescriptor_NonDriveAppWithIconAndDefault) { | |
| 51 FullTaskDescriptor full_descriptor( | |
| 52 TaskDescriptor("app-id", | |
| 53 TASK_TYPE_FILE_BROWSER_HANDLER, | |
| 54 "action-id"), | |
| 55 "task title", | |
| 56 GURL("http://example.com/icon.png"), | |
| 57 true /* is_default */); | |
| 58 | |
| 59 scoped_ptr<base::DictionaryValue> dictionary( | |
| 60 full_descriptor.AsDictionaryValue()); | |
| 61 std::string task_id; | |
| 62 EXPECT_TRUE(dictionary->GetString("taskId", &task_id)); | |
| 63 EXPECT_EQ("app-id|file|action-id", task_id); | |
| 64 | |
| 65 std::string icon_url; | |
| 66 EXPECT_TRUE(dictionary->GetString("iconUrl", &icon_url)); | |
| 67 EXPECT_EQ("http://example.com/icon.png", icon_url); | |
| 68 | |
| 69 std::string title; | |
| 70 EXPECT_TRUE(dictionary->GetString("title", &title)); | |
| 71 EXPECT_EQ("task title", title); | |
| 72 | |
| 73 bool is_default = false; | |
| 74 EXPECT_TRUE(dictionary->GetBoolean("isDefault", &is_default)); | |
| 75 EXPECT_TRUE(is_default); | |
| 76 } | |
| 77 | |
| 78 TEST(FileManagerFileTasksTest, | |
| 79 FullTaskDescriptor_DriveAppWithoutIconAndNotDefault) { | |
| 80 FullTaskDescriptor full_descriptor( | |
| 81 TaskDescriptor("app-id", | |
| 82 TASK_TYPE_DRIVE_APP, | |
| 83 "action-id"), | |
| 84 "task title", | |
| 85 GURL(), // No icon URL. | |
| 86 false /* is_default */); | |
| 87 | |
| 88 scoped_ptr<base::DictionaryValue> dictionary( | |
| 89 full_descriptor.AsDictionaryValue()); | |
| 90 std::string task_id; | |
| 91 EXPECT_TRUE(dictionary->GetString("taskId", &task_id)); | |
| 92 EXPECT_EQ("app-id|drive|action-id", task_id); | |
| 93 | |
| 94 std::string icon_url; | |
| 95 EXPECT_FALSE(dictionary->GetString("iconUrl", &icon_url)); | |
| 96 | |
| 97 std::string title; | |
| 98 EXPECT_TRUE(dictionary->GetString("title", &title)); | |
| 99 EXPECT_EQ("task title", title); | |
| 100 | |
| 101 bool is_default = false; | |
| 102 EXPECT_TRUE(dictionary->GetBoolean("isDefault", &is_default)); | |
| 103 EXPECT_FALSE(is_default); | |
| 104 } | |
| 105 | |
| 106 TEST(FileManagerFileTasksTest, MakeTaskID) { | |
| 107 EXPECT_EQ("app-id|file|action-id", | |
| 108 MakeTaskID("app-id", TASK_TYPE_FILE_BROWSER_HANDLER, "action-id")); | |
| 109 EXPECT_EQ("app-id|app|action-id", | |
| 110 MakeTaskID("app-id", TASK_TYPE_FILE_HANDLER, "action-id")); | |
| 111 EXPECT_EQ("app-id|drive|action-id", | |
| 112 MakeTaskID("app-id", TASK_TYPE_DRIVE_APP, "action-id")); | |
| 113 } | |
| 114 | |
| 115 TEST(FileManagerFileTasksTest, MakeDriveAppTaskId) { | |
| 116 EXPECT_EQ("app-id|drive|open-with", MakeDriveAppTaskId("app-id")); | |
| 117 } | |
| 118 | |
| 119 TEST(FileManagerFileTasksTest, TaskDescriptorToId) { | |
| 120 EXPECT_EQ("app-id|file|action-id", | |
| 121 TaskDescriptorToId(TaskDescriptor("app-id", | |
| 122 TASK_TYPE_FILE_BROWSER_HANDLER, | |
| 123 "action-id"))); | |
| 124 } | |
| 125 | |
| 126 TEST(FileManagerFileTasksTest, ParseTaskID_FileBrowserHandler) { | |
| 127 TaskDescriptor task; | |
| 128 EXPECT_TRUE(ParseTaskID("app-id|file|action-id", &task)); | |
| 129 EXPECT_EQ("app-id", task.app_id); | |
| 130 EXPECT_EQ(TASK_TYPE_FILE_BROWSER_HANDLER, task.task_type); | |
| 131 EXPECT_EQ("action-id", task.action_id); | |
| 132 } | |
| 133 | |
| 134 TEST(FileManagerFileTasksTest, ParseTaskID_FileHandler) { | |
| 135 TaskDescriptor task; | |
| 136 EXPECT_TRUE(ParseTaskID("app-id|app|action-id", &task)); | |
| 137 EXPECT_EQ("app-id", task.app_id); | |
| 138 EXPECT_EQ(TASK_TYPE_FILE_HANDLER, task.task_type); | |
| 139 EXPECT_EQ("action-id", task.action_id); | |
| 140 } | |
| 141 | |
| 142 TEST(FileManagerFileTasksTest, ParseTaskID_DriveApp) { | |
| 143 TaskDescriptor task; | |
| 144 EXPECT_TRUE(ParseTaskID("app-id|drive|action-id", &task)); | |
| 145 EXPECT_EQ("app-id", task.app_id); | |
| 146 EXPECT_EQ(TASK_TYPE_DRIVE_APP, task.task_type); | |
| 147 EXPECT_EQ("action-id", task.action_id); | |
| 148 } | |
| 149 | |
| 150 TEST(FileManagerFileTasksTest, ParseTaskID_Legacy) { | |
| 151 TaskDescriptor task; | |
| 152 // A legacy task ID only has two parts. The task type should be | |
| 153 // TASK_TYPE_FILE_BROWSER_HANDLER. | |
| 154 EXPECT_TRUE(ParseTaskID("app-id|action-id", &task)); | |
| 155 EXPECT_EQ("app-id", task.app_id); | |
| 156 EXPECT_EQ(TASK_TYPE_FILE_BROWSER_HANDLER, task.task_type); | |
| 157 EXPECT_EQ("action-id", task.action_id); | |
| 158 } | |
| 159 | |
| 160 TEST(FileManagerFileTasksTest, ParseTaskID_LegacyDrive) { | |
| 161 TaskDescriptor task; | |
| 162 // A legacy task ID only has two parts. For Drive app, the app ID is | |
| 163 // prefixed with "drive-app:". | |
| 164 EXPECT_TRUE(ParseTaskID("drive-app:app-id|action-id", &task)); | |
| 165 EXPECT_EQ("app-id", task.app_id); | |
| 166 EXPECT_EQ(TASK_TYPE_DRIVE_APP, task.task_type); | |
| 167 EXPECT_EQ("action-id", task.action_id); | |
| 168 } | |
| 169 | |
| 170 TEST(FileManagerFileTasksTest, ParseTaskID_Invalid) { | |
| 171 TaskDescriptor task; | |
| 172 EXPECT_FALSE(ParseTaskID("invalid", &task)); | |
| 173 } | |
| 174 | |
| 175 TEST(FileManagerFileTasksTest, ParseTaskID_UnknownTaskType) { | |
| 176 TaskDescriptor task; | |
| 177 EXPECT_FALSE(ParseTaskID("app-id|unknown|action-id", &task)); | |
| 178 } | |
| 179 | |
| 180 TEST(FileManagerFileTasksTest, FindDriveAppTasks) { | |
| 181 // For DriveAppRegistry, which checks CurrentlyOn(BrowserThread::UI). | |
| 182 content::TestBrowserThreadBundle thread_bundle; | |
| 183 | |
| 184 // Foo.app can handle "text/plain" and "text/html" | |
| 185 scoped_ptr<google_apis::AppResource> foo_app(new google_apis::AppResource); | |
| 186 foo_app->set_product_url( | |
| 187 GURL("https://chrome.google.com/webstore/detail/foo_app_id")); | |
| 188 foo_app->set_application_id("foo_app_id"); | |
| 189 foo_app->set_name("Foo"); | |
| 190 foo_app->set_object_type("foo_object_type"); | |
| 191 ScopedVector<std::string> foo_mime_types; | |
| 192 foo_mime_types.push_back(new std::string("text/plain")); | |
| 193 foo_mime_types.push_back(new std::string("text/html")); | |
| 194 foo_app->set_primary_mimetypes(&foo_mime_types); | |
| 195 | |
| 196 // Bar.app can only handle "text/plain". | |
| 197 scoped_ptr<google_apis::AppResource> bar_app(new google_apis::AppResource); | |
| 198 bar_app->set_product_url( | |
| 199 GURL("https://chrome.google.com/webstore/detail/bar_app_id")); | |
| 200 bar_app->set_application_id("bar_app_id"); | |
| 201 bar_app->set_name("Bar"); | |
| 202 bar_app->set_object_type("bar_object_type"); | |
| 203 ScopedVector<std::string> bar_mime_types; | |
| 204 bar_mime_types.push_back(new std::string("text/plain")); | |
| 205 bar_app->set_primary_mimetypes(&bar_mime_types); | |
| 206 | |
| 207 // Prepare DriveAppRegistry from Foo.app and Bar.app. | |
| 208 ScopedVector<google_apis::AppResource> app_resources; | |
| 209 app_resources.push_back(foo_app.release()); | |
| 210 app_resources.push_back(bar_app.release()); | |
| 211 google_apis::AppList app_list; | |
| 212 app_list.set_items(&app_resources); | |
| 213 drive::DriveAppRegistry drive_app_registry(NULL); | |
| 214 drive_app_registry.UpdateFromAppList(app_list); | |
| 215 | |
| 216 // Find apps for a "text/plain" file. Foo.app and Bar.app should be found. | |
| 217 PathAndMimeTypeSet path_mime_set; | |
| 218 path_mime_set.insert( | |
| 219 std::make_pair( | |
| 220 drive::util::GetDriveMountPointPath().AppendASCII("foo.txt"), | |
| 221 "text/plain")); | |
| 222 std::vector<FullTaskDescriptor> tasks; | |
| 223 FindDriveAppTasks(drive_app_registry, | |
| 224 path_mime_set, | |
| 225 &tasks); | |
| 226 ASSERT_EQ(2U, tasks.size()); | |
| 227 // Sort the app IDs, as the order is not guaranteed. | |
| 228 std::vector<std::string> app_ids; | |
| 229 app_ids.push_back(tasks[0].task_descriptor().app_id); | |
| 230 app_ids.push_back(tasks[1].task_descriptor().app_id); | |
| 231 std::sort(app_ids.begin(), app_ids.end()); | |
| 232 // Confirm that both Foo.app and Bar.app are found. | |
| 233 EXPECT_EQ("bar_app_id", app_ids[0]); | |
| 234 EXPECT_EQ("foo_app_id", app_ids[1]); | |
| 235 | |
| 236 // Find apps for "text/plain" and "text/html" files. Only Foo.app should be | |
| 237 // found. | |
| 238 path_mime_set.clear(); | |
| 239 path_mime_set.insert( | |
| 240 std::make_pair( | |
| 241 drive::util::GetDriveMountPointPath().AppendASCII("foo.txt"), | |
| 242 "text/plain")); | |
| 243 path_mime_set.insert( | |
| 244 std::make_pair( | |
| 245 drive::util::GetDriveMountPointPath().AppendASCII("foo.html"), | |
| 246 "text/html")); | |
| 247 tasks.clear(); | |
| 248 FindDriveAppTasks(drive_app_registry, | |
| 249 path_mime_set, | |
| 250 &tasks); | |
| 251 ASSERT_EQ(1U, tasks.size()); | |
| 252 // Confirm that both Foo.app is found. | |
| 253 EXPECT_EQ("foo_app_id", tasks[0].task_descriptor().app_id); | |
| 254 | |
| 255 // Add a "text/plain" file not on Drive. No tasks should be found. | |
| 256 path_mime_set.insert( | |
| 257 std::make_pair(base::FilePath::FromUTF8Unsafe("not_on_drive.txt"), | |
| 258 "text/plain")); | |
| 259 tasks.clear(); | |
| 260 FindDriveAppTasks(drive_app_registry, | |
| 261 path_mime_set, | |
| 262 &tasks); | |
| 263 // Confirm no tasks are found. | |
| 264 ASSERT_TRUE(tasks.empty()); | |
| 265 } | |
| 266 | |
| 267 // Test that the right task is chosen from multiple choices per mime types | |
| 268 // and file extensions. | |
| 269 TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_MultipleTasks) { | |
| 270 TestingPrefServiceSimple pref_service; | |
| 271 RegisterDefaultTaskPreferences(&pref_service); | |
| 272 | |
| 273 // Text.app and Nice.app were found for "foo.txt". | |
| 274 TaskDescriptor text_app_task("text-app-id", | |
| 275 TASK_TYPE_FILE_HANDLER, | |
| 276 "action-id"); | |
| 277 TaskDescriptor nice_app_task("nice-app-id", | |
| 278 TASK_TYPE_FILE_HANDLER, | |
| 279 "action-id"); | |
| 280 std::vector<FullTaskDescriptor> tasks; | |
| 281 tasks.push_back(FullTaskDescriptor( | |
| 282 text_app_task, | |
| 283 "Text.app", | |
| 284 GURL("http://example.com/text_app.png"), | |
| 285 false /* is_default */)); | |
| 286 tasks.push_back(FullTaskDescriptor( | |
| 287 nice_app_task, | |
| 288 "Nice.app", | |
| 289 GURL("http://example.com/nice_app.png"), | |
| 290 false /* is_default */)); | |
| 291 PathAndMimeTypeSet path_mime_set; | |
| 292 path_mime_set.insert(std::make_pair( | |
| 293 base::FilePath::FromUTF8Unsafe("foo.txt"), | |
| 294 "text/plain")); | |
| 295 | |
| 296 // None of them should be chosen as default, as nothing is set in the | |
| 297 // preferences. | |
| 298 ChooseAndSetDefaultTask(pref_service, path_mime_set, &tasks); | |
| 299 EXPECT_FALSE(tasks[0].is_default()); | |
| 300 EXPECT_FALSE(tasks[1].is_default()); | |
| 301 | |
| 302 // Set Text.app as default for "text/plain" in the preferences. | |
| 303 DictionaryValue empty; | |
| 304 DictionaryValue mime_types; | |
| 305 mime_types.SetStringWithoutPathExpansion( | |
| 306 "text/plain", | |
| 307 TaskDescriptorToId(text_app_task)); | |
| 308 UpdateDefaultTaskPreferences(&pref_service, mime_types, empty); | |
| 309 | |
| 310 // Text.app should be chosen as default. | |
| 311 ChooseAndSetDefaultTask(pref_service, path_mime_set, &tasks); | |
| 312 EXPECT_TRUE(tasks[0].is_default()); | |
| 313 EXPECT_FALSE(tasks[1].is_default()); | |
| 314 | |
| 315 // Change it back to non-default for testing further. | |
| 316 tasks[0].set_is_default(false); | |
| 317 | |
| 318 // Clear the preferences and make sure none of them are default. | |
| 319 UpdateDefaultTaskPreferences(&pref_service, empty, empty); | |
| 320 ChooseAndSetDefaultTask(pref_service, path_mime_set, &tasks); | |
| 321 EXPECT_FALSE(tasks[0].is_default()); | |
| 322 EXPECT_FALSE(tasks[1].is_default()); | |
| 323 | |
| 324 // Set Nice.app as default for ".txt" in the preferences. | |
| 325 DictionaryValue suffixes; | |
| 326 suffixes.SetStringWithoutPathExpansion( | |
| 327 ".txt", | |
| 328 TaskDescriptorToId(nice_app_task)); | |
| 329 UpdateDefaultTaskPreferences(&pref_service, empty, suffixes); | |
| 330 | |
| 331 // Now Nice.app should be chosen as default. | |
| 332 ChooseAndSetDefaultTask(pref_service, path_mime_set, &tasks); | |
| 333 EXPECT_FALSE(tasks[0].is_default()); | |
| 334 EXPECT_TRUE(tasks[1].is_default()); | |
| 335 } | |
| 336 | |
| 337 // Test that Files.app's internal file browser handler is chosen as default | |
| 338 // even if nothing is set in the preferences. | |
| 339 TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_FallbackFileBrowser) { | |
| 340 TestingPrefServiceSimple pref_service; | |
| 341 RegisterDefaultTaskPreferences(&pref_service); | |
| 342 | |
| 343 // Files.app's internal file browser handler was found for "foo.txt". | |
| 344 TaskDescriptor files_app_task(kFileManagerAppId, | |
| 345 TASK_TYPE_FILE_BROWSER_HANDLER, | |
| 346 "view-in-browser"); | |
| 347 std::vector<FullTaskDescriptor> tasks; | |
| 348 tasks.push_back(FullTaskDescriptor( | |
| 349 files_app_task, | |
| 350 "View in browser", | |
| 351 GURL("http://example.com/some_icon.png"), | |
| 352 false /* is_default */)); | |
| 353 PathAndMimeTypeSet path_mime_set; | |
| 354 path_mime_set.insert(std::make_pair( | |
| 355 base::FilePath::FromUTF8Unsafe("foo.txt"), | |
| 356 "text/plain")); | |
| 357 | |
| 358 // The internal file browser handler should be chosen as default, as it's a | |
| 359 // fallback file browser handler. | |
| 360 ChooseAndSetDefaultTask(pref_service, path_mime_set, &tasks); | |
| 361 EXPECT_TRUE(tasks[0].is_default()); | |
| 362 } | |
| 363 | |
| 364 } // namespace file_tasks | |
| 365 } // namespace file_manager. | |
| OLD | NEW |