| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/extensions/file_manager/file_tasks.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/file_tasks.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
| 11 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/chromeos/drive/drive_app_registry.h" | 13 #include "chrome/browser/chromeos/drive/drive_app_registry.h" |
| 14 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 14 #include "chrome/browser/chromeos/extensions/file_manager/app_id.h" | 15 #include "chrome/browser/chromeos/extensions/file_manager/app_id.h" |
| 15 #include "chrome/browser/google_apis/drive_api_parser.h" | 16 #include "chrome/browser/google_apis/drive_api_parser.h" |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace file_manager { | 21 namespace file_manager { |
| 21 namespace file_tasks { | 22 namespace file_tasks { |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 EXPECT_EQ("app-id|file|action-id", task_id); | 63 EXPECT_EQ("app-id|file|action-id", task_id); |
| 63 | 64 |
| 64 std::string icon_url; | 65 std::string icon_url; |
| 65 EXPECT_TRUE(dictionary->GetString("iconUrl", &icon_url)); | 66 EXPECT_TRUE(dictionary->GetString("iconUrl", &icon_url)); |
| 66 EXPECT_EQ("http://example.com/icon.png", icon_url); | 67 EXPECT_EQ("http://example.com/icon.png", icon_url); |
| 67 | 68 |
| 68 std::string title; | 69 std::string title; |
| 69 EXPECT_TRUE(dictionary->GetString("title", &title)); | 70 EXPECT_TRUE(dictionary->GetString("title", &title)); |
| 70 EXPECT_EQ("task title", title); | 71 EXPECT_EQ("task title", title); |
| 71 | 72 |
| 72 bool is_drive_app = false; | |
| 73 EXPECT_TRUE(dictionary->GetBoolean("driveApp", &is_drive_app)); | |
| 74 EXPECT_FALSE(is_drive_app); | |
| 75 | |
| 76 bool is_default = false; | 73 bool is_default = false; |
| 77 EXPECT_TRUE(dictionary->GetBoolean("isDefault", &is_default)); | 74 EXPECT_TRUE(dictionary->GetBoolean("isDefault", &is_default)); |
| 78 EXPECT_TRUE(is_default); | 75 EXPECT_TRUE(is_default); |
| 79 } | 76 } |
| 80 | 77 |
| 81 TEST(FileManagerFileTasksTest, | 78 TEST(FileManagerFileTasksTest, |
| 82 FullTaskDescriptor_DriveAppWithoutIconAndNotDefault) { | 79 FullTaskDescriptor_DriveAppWithoutIconAndNotDefault) { |
| 83 FullTaskDescriptor full_descriptor( | 80 FullTaskDescriptor full_descriptor( |
| 84 TaskDescriptor("app-id", | 81 TaskDescriptor("app-id", |
| 85 TASK_TYPE_DRIVE_APP, | 82 TASK_TYPE_DRIVE_APP, |
| 86 "action-id"), | 83 "action-id"), |
| 87 "task title", | 84 "task title", |
| 88 GURL(), // No icon URL. | 85 GURL(), // No icon URL. |
| 89 false /* is_default */); | 86 false /* is_default */); |
| 90 | 87 |
| 91 scoped_ptr<base::DictionaryValue> dictionary( | 88 scoped_ptr<base::DictionaryValue> dictionary( |
| 92 full_descriptor.AsDictionaryValue()); | 89 full_descriptor.AsDictionaryValue()); |
| 93 std::string task_id; | 90 std::string task_id; |
| 94 EXPECT_TRUE(dictionary->GetString("taskId", &task_id)); | 91 EXPECT_TRUE(dictionary->GetString("taskId", &task_id)); |
| 95 EXPECT_EQ("app-id|drive|action-id", task_id); | 92 EXPECT_EQ("app-id|drive|action-id", task_id); |
| 96 | 93 |
| 97 std::string icon_url; | 94 std::string icon_url; |
| 98 EXPECT_FALSE(dictionary->GetString("iconUrl", &icon_url)); | 95 EXPECT_FALSE(dictionary->GetString("iconUrl", &icon_url)); |
| 99 | 96 |
| 100 std::string title; | 97 std::string title; |
| 101 EXPECT_TRUE(dictionary->GetString("title", &title)); | 98 EXPECT_TRUE(dictionary->GetString("title", &title)); |
| 102 EXPECT_EQ("task title", title); | 99 EXPECT_EQ("task title", title); |
| 103 | 100 |
| 104 bool is_drive_app = false; | |
| 105 EXPECT_TRUE(dictionary->GetBoolean("driveApp", &is_drive_app)); | |
| 106 EXPECT_TRUE(is_drive_app); | |
| 107 | |
| 108 bool is_default = false; | 101 bool is_default = false; |
| 109 EXPECT_TRUE(dictionary->GetBoolean("isDefault", &is_default)); | 102 EXPECT_TRUE(dictionary->GetBoolean("isDefault", &is_default)); |
| 110 EXPECT_FALSE(is_default); | 103 EXPECT_FALSE(is_default); |
| 111 } | 104 } |
| 112 | 105 |
| 113 TEST(FileManagerFileTasksTest, MakeTaskID) { | 106 TEST(FileManagerFileTasksTest, MakeTaskID) { |
| 114 EXPECT_EQ("app-id|file|action-id", | 107 EXPECT_EQ("app-id|file|action-id", |
| 115 MakeTaskID("app-id", TASK_TYPE_FILE_BROWSER_HANDLER, "action-id")); | 108 MakeTaskID("app-id", TASK_TYPE_FILE_BROWSER_HANDLER, "action-id")); |
| 116 EXPECT_EQ("app-id|app|action-id", | 109 EXPECT_EQ("app-id|app|action-id", |
| 117 MakeTaskID("app-id", TASK_TYPE_FILE_HANDLER, "action-id")); | 110 MakeTaskID("app-id", TASK_TYPE_FILE_HANDLER, "action-id")); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 app_resources.push_back(foo_app.release()); | 209 app_resources.push_back(foo_app.release()); |
| 217 app_resources.push_back(bar_app.release()); | 210 app_resources.push_back(bar_app.release()); |
| 218 google_apis::AppList app_list; | 211 google_apis::AppList app_list; |
| 219 app_list.set_items(&app_resources); | 212 app_list.set_items(&app_resources); |
| 220 drive::DriveAppRegistry drive_app_registry(NULL); | 213 drive::DriveAppRegistry drive_app_registry(NULL); |
| 221 drive_app_registry.UpdateFromAppList(app_list); | 214 drive_app_registry.UpdateFromAppList(app_list); |
| 222 | 215 |
| 223 // Find apps for a "text/plain" file. Foo.app and Bar.app should be found. | 216 // Find apps for a "text/plain" file. Foo.app and Bar.app should be found. |
| 224 PathAndMimeTypeSet path_mime_set; | 217 PathAndMimeTypeSet path_mime_set; |
| 225 path_mime_set.insert( | 218 path_mime_set.insert( |
| 226 std::make_pair(base::FilePath::FromUTF8Unsafe("foo.txt"), "text/plain")); | 219 std::make_pair( |
| 220 drive::util::GetDriveMountPointPath().AppendASCII("foo.txt"), |
| 221 "text/plain")); |
| 227 std::vector<FullTaskDescriptor> tasks; | 222 std::vector<FullTaskDescriptor> tasks; |
| 228 FindDriveAppTasks(drive_app_registry, | 223 FindDriveAppTasks(drive_app_registry, |
| 229 path_mime_set, | 224 path_mime_set, |
| 230 &tasks); | 225 &tasks); |
| 231 ASSERT_EQ(2U, tasks.size()); | 226 ASSERT_EQ(2U, tasks.size()); |
| 232 // Sort the app IDs, as the order is not guaranteed. | 227 // Sort the app IDs, as the order is not guaranteed. |
| 233 std::vector<std::string> app_ids; | 228 std::vector<std::string> app_ids; |
| 234 app_ids.push_back(tasks[0].task_descriptor().app_id); | 229 app_ids.push_back(tasks[0].task_descriptor().app_id); |
| 235 app_ids.push_back(tasks[1].task_descriptor().app_id); | 230 app_ids.push_back(tasks[1].task_descriptor().app_id); |
| 236 std::sort(app_ids.begin(), app_ids.end()); | 231 std::sort(app_ids.begin(), app_ids.end()); |
| 237 // Confirm that both Foo.app and Bar.app are found. | 232 // Confirm that both Foo.app and Bar.app are found. |
| 238 EXPECT_EQ("bar_app_id", app_ids[0]); | 233 EXPECT_EQ("bar_app_id", app_ids[0]); |
| 239 EXPECT_EQ("foo_app_id", app_ids[1]); | 234 EXPECT_EQ("foo_app_id", app_ids[1]); |
| 240 | 235 |
| 241 // Find apps for "text/plain" and "text/html" files. Only Foo.app should be | 236 // Find apps for "text/plain" and "text/html" files. Only Foo.app should be |
| 242 // found. | 237 // found. |
| 243 path_mime_set.clear(); | 238 path_mime_set.clear(); |
| 244 path_mime_set.insert( | 239 path_mime_set.insert( |
| 245 std::make_pair(base::FilePath::FromUTF8Unsafe("foo.txt"), "text/plain")); | 240 std::make_pair( |
| 241 drive::util::GetDriveMountPointPath().AppendASCII("foo.txt"), |
| 242 "text/plain")); |
| 246 path_mime_set.insert( | 243 path_mime_set.insert( |
| 247 std::make_pair(base::FilePath::FromUTF8Unsafe("foo.html"), "text/html")); | 244 std::make_pair( |
| 245 drive::util::GetDriveMountPointPath().AppendASCII("foo.html"), |
| 246 "text/html")); |
| 248 tasks.clear(); | 247 tasks.clear(); |
| 249 FindDriveAppTasks(drive_app_registry, | 248 FindDriveAppTasks(drive_app_registry, |
| 250 path_mime_set, | 249 path_mime_set, |
| 251 &tasks); | 250 &tasks); |
| 252 ASSERT_EQ(1U, tasks.size()); | 251 ASSERT_EQ(1U, tasks.size()); |
| 253 // Confirm that both Foo.app is found. | 252 // Confirm that both Foo.app is found. |
| 254 EXPECT_EQ("foo_app_id", tasks[0].task_descriptor().app_id); | 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()); |
| 255 } | 265 } |
| 256 | 266 |
| 257 // Test that the right task is chosen from multiple choices per mime types | 267 // Test that the right task is chosen from multiple choices per mime types |
| 258 // and file extensions. | 268 // and file extensions. |
| 259 TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_MultipleTasks) { | 269 TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_MultipleTasks) { |
| 260 TestingPrefServiceSimple pref_service; | 270 TestingPrefServiceSimple pref_service; |
| 261 RegisterDefaultTaskPreferences(&pref_service); | 271 RegisterDefaultTaskPreferences(&pref_service); |
| 262 | 272 |
| 263 // Text.app and Nice.app were found for "foo.txt". | 273 // Text.app and Nice.app were found for "foo.txt". |
| 264 TaskDescriptor text_app_task("text-app-id", | 274 TaskDescriptor text_app_task("text-app-id", |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 "text/plain")); | 356 "text/plain")); |
| 347 | 357 |
| 348 // The internal file browser handler should be chosen as default, as it's a | 358 // The internal file browser handler should be chosen as default, as it's a |
| 349 // fallback file browser handler. | 359 // fallback file browser handler. |
| 350 ChooseAndSetDefaultTask(pref_service, path_mime_set, &tasks); | 360 ChooseAndSetDefaultTask(pref_service, path_mime_set, &tasks); |
| 351 EXPECT_TRUE(tasks[0].is_default()); | 361 EXPECT_TRUE(tasks[0].is_default()); |
| 352 } | 362 } |
| 353 | 363 |
| 354 } // namespace file_tasks | 364 } // namespace file_tasks |
| 355 } // namespace file_manager. | 365 } // namespace file_manager. |
| OLD | NEW |