1 // Copyright (c) 2020 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_manager/app_service_file_tasks.h"
6 
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 #include "base/test/scoped_feature_list.h"
12 #include "chrome/browser/apps/app_service/app_service_proxy.h"
13 #include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
14 #include "chrome/browser/apps/app_service/app_service_test.h"
15 #include "chrome/browser/chromeos/file_manager/file_tasks.h"
16 #include "chrome/browser/chromeos/file_manager/path_util.h"
17 #include "chrome/common/chrome_features.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/services/app_service/public/cpp/intent_test_util.h"
20 #include "components/services/app_service/public/mojom/types.mojom.h"
21 #include "content/public/test/browser_task_environment.h"
22 #include "extensions/browser/entry_info.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
25 
26 using extensions::api::file_manager_private::Verb;
27 
28 namespace {
29 const char kAppIdText[] = "abcdefg";
30 const char kAppIdImage[] = "gfedcba";
31 const char kAppIdAny[] = "hijklmn";
32 const char kMimeTypeText[] = "text/plain";
33 const char kMimeTypeImage[] = "image/jpeg";
34 const char kMimeTypeAny[] = "*/*";
35 const char kActivityLabelText[] = "some_text_activity";
36 const char kActivityLabelImage[] = "some_image_activity";
37 const char kActivityLabelAny[] = "some_any_file";
38 }  // namespace
39 
40 namespace file_manager {
41 namespace file_tasks {
42 
43 class AppServiceFileTasksTest : public testing::Test {
44  protected:
AppServiceFileTasksTest()45   AppServiceFileTasksTest() {
46     scoped_feature_list_.InitAndEnableFeature(features::kIntentHandlingSharing);
47   }
SetUp()48   void SetUp() override {
49     profile_ = std::make_unique<TestingProfile>();
50     app_service_test_.SetUp(profile_.get());
51     app_service_proxy_ =
52         apps::AppServiceProxyFactory::GetForProfile(profile_.get());
53     ASSERT_TRUE(app_service_proxy_);
54     AddApps();
55   }
56 
profile()57   Profile* profile() { return profile_.get(); }
58 
AddFakeAppWithIntentFilter(const std::string & app_id,const std::string & mime_type,const std::string & activity_label,bool is_send_multiple)59   void AddFakeAppWithIntentFilter(const std::string& app_id,
60                                   const std::string& mime_type,
61                                   const std::string& activity_label,
62                                   bool is_send_multiple) {
63     std::vector<apps::mojom::AppPtr> apps;
64     auto app = apps::mojom::App::New();
65     app->app_id = app_id;
66     app->app_type = apps::mojom::AppType::kArc;
67     auto intent_filter =
68         is_send_multiple
69             ? apps_util::CreateIntentFilterForSendMultiple(mime_type,
70                                                            activity_label)
71             : apps_util::CreateIntentFilterForSend(mime_type, activity_label);
72     app->intent_filters.push_back(std::move(intent_filter));
73     apps.push_back(std::move(app));
74     app_service_proxy_->AppRegistryCache().OnApps(std::move(apps));
75     app_service_test_.WaitForAppService();
76   }
77 
AddApps()78   void AddApps() {
79     AddFakeAppWithIntentFilter(kAppIdText, kMimeTypeText, kActivityLabelText,
80                                /*is_send_multiple=*/false);
81     AddFakeAppWithIntentFilter(kAppIdImage, kMimeTypeImage, kActivityLabelImage,
82                                /*is_send_multiple=*/false);
83     AddFakeAppWithIntentFilter(kAppIdAny, kMimeTypeAny, kActivityLabelAny,
84                                /*is_send_multiple=*/true);
85   }
86 
87   base::test::ScopedFeatureList scoped_feature_list_;
88   content::BrowserTaskEnvironment task_environment_;
89   std::unique_ptr<TestingProfile> profile_;
90   apps::AppServiceProxy* app_service_proxy_ = nullptr;
91   apps::AppServiceTest app_service_test_;
92 };
93 
94 // Test that between an image app and text app, the text app can be
95 // found for an text file entry.
TEST_F(AppServiceFileTasksTest,FindAppServiceFileTasksText)96 TEST_F(AppServiceFileTasksTest, FindAppServiceFileTasksText) {
97   // Find apps for a "text/plain" file.
98   std::vector<extensions::EntryInfo> entries;
99   entries.emplace_back(
100       util::GetMyFilesFolderForProfile(profile()).AppendASCII("foo.txt"),
101       kMimeTypeText, false);
102 
103   // This test doesn't test file_urls, leave it empty.
104   std::vector<GURL> file_urls{GURL()};
105   std::vector<FullTaskDescriptor> tasks;
106   FindAppServiceTasks(profile(), entries, file_urls, &tasks);
107   ASSERT_EQ(1U, tasks.size());
108   EXPECT_EQ(kAppIdText, tasks[0].task_descriptor().app_id);
109   EXPECT_EQ(kActivityLabelText, tasks[0].task_title());
110 }
111 
112 // Test that between an image app and text app, the image app can be
113 // found for an image file entry.
TEST_F(AppServiceFileTasksTest,FindAppServiceFileTasksImage)114 TEST_F(AppServiceFileTasksTest, FindAppServiceFileTasksImage) {
115   // Find apps for a "image/jpeg" file.
116   std::vector<extensions::EntryInfo> entries;
117   entries.emplace_back(
118       util::GetMyFilesFolderForProfile(profile()).AppendASCII("bar.jpeg"),
119       kMimeTypeImage, false);
120 
121   // This test doesn't test file_urls, leave it empty.
122   std::vector<GURL> file_urls{GURL()};
123   std::vector<FullTaskDescriptor> tasks;
124   FindAppServiceTasks(profile(), entries, file_urls, &tasks);
125   ASSERT_EQ(1U, tasks.size());
126   EXPECT_EQ(kAppIdImage, tasks[0].task_descriptor().app_id);
127   EXPECT_EQ(kActivityLabelImage, tasks[0].task_title());
128 }
129 
130 // Test that between an image app, text app and an app that can handle every
131 // file, the app that can handle every file can be found for an image file entry
132 // and text file entry.
TEST_F(AppServiceFileTasksTest,FindAppServiceFileTasksMultiple)133 TEST_F(AppServiceFileTasksTest, FindAppServiceFileTasksMultiple) {
134   std::vector<extensions::EntryInfo> entries;
135   entries.emplace_back(
136       util::GetMyFilesFolderForProfile(profile()).AppendASCII("foo.txt"),
137       kMimeTypeText, false);
138   entries.emplace_back(
139       util::GetMyFilesFolderForProfile(profile()).AppendASCII("bar.jpeg"),
140       kMimeTypeImage, false);
141 
142   // This test doesn't test file_urls, leave it empty.
143   std::vector<GURL> file_urls{GURL(), GURL()};
144   std::vector<FullTaskDescriptor> tasks;
145   FindAppServiceTasks(profile(), entries, file_urls, &tasks);
146   ASSERT_EQ(1U, tasks.size());
147   EXPECT_EQ(kAppIdAny, tasks[0].task_descriptor().app_id);
148   EXPECT_EQ(kActivityLabelAny, tasks[0].task_title());
149 }
150 
151 }  // namespace file_tasks
152 }  // namespace file_manager.
153