Commit bb2e7919 authored by Maggie Cai's avatar Maggie Cai Committed by Commit Bot

[IntentHandling] Delete preferred app from preferred app list.

This CL adds the delete functionality for the preferred app list. This
CL also migrate relevant unit tests from the original preferred app
tests.

BUG=853604

Change-Id: Ib16470c0badf60f0da17fa4c3d7dae9594c5623c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2117596
Commit-Queue: Maggie Cai <mxcai@chromium.org>
Reviewed-by: default avatarNancy Wang <nancylingwang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#753075}
parent cbdd127f
......@@ -52,6 +52,24 @@ apps::mojom::ReplacedAppPreferencesPtr PreferredAppsList::AddPreferredApp(
return replaced_app_preferences;
}
// static
void PreferredAppsList::DeletePreferredApp(
const std::string& app_id,
const apps::mojom::IntentFilterPtr& intent_filter,
PreferredApps* preferred_apps) {
// Go through the list and see if there are overlapped intent filters with the
// same app id in the list. If there are, delete the entry.
auto iter = preferred_apps->begin();
while (iter != preferred_apps->end()) {
if ((*iter)->app_id == app_id &&
apps_util::FiltersHaveOverlap((*iter)->intent_filter, intent_filter)) {
iter = preferred_apps->erase(iter);
} else {
iter++;
}
}
}
base::Optional<std::string> PreferredAppsList::FindPreferredAppForIntent(
const apps::mojom::IntentPtr& intent) {
base::Optional<std::string> best_match_app_id = base::nullopt;
......@@ -82,4 +100,10 @@ apps::mojom::ReplacedAppPreferencesPtr PreferredAppsList::AddPreferredApp(
return AddPreferredApp(app_id, intent_filter, &preferred_apps_);
}
void PreferredAppsList::DeletePreferredApp(
const std::string& app_id,
const apps::mojom::IntentFilterPtr& intent_filter) {
DeletePreferredApp(app_id, intent_filter, &preferred_apps_);
}
} // namespace apps
......@@ -27,6 +27,7 @@ class PreferredAppsList {
PreferredAppsList& operator=(const PreferredAppsList&) = delete;
using PreferredApps = std::vector<apps::mojom::PreferredAppPtr>;
// Add a preferred app for an |intent_filter| for |preferred_apps|.
// Returns the preferred app and their corresponding |intent_filters| that are
// replaced by this action.
......@@ -35,6 +36,13 @@ class PreferredAppsList {
const apps::mojom::IntentFilterPtr& intent_filter,
PreferredApps* preferred_apps);
// Delete a preferred app for an |intent_filter| with the same |app_id| for
// |preferred_apps|.
static void DeletePreferredApp(
const std::string& app_id,
const apps::mojom::IntentFilterPtr& intent_filter,
PreferredApps* preferred_apps);
// Find preferred app id for an |intent|.
base::Optional<std::string> FindPreferredAppForIntent(
const apps::mojom::IntentPtr& intent);
......@@ -49,6 +57,10 @@ class PreferredAppsList {
const std::string& app_id,
const apps::mojom::IntentFilterPtr& intent_filter);
// Delete a preferred app for an |intent_filter| with the same |app_id|.
void DeletePreferredApp(const std::string& app_id,
const apps::mojom::IntentFilterPtr& intent_filter);
private:
PreferredApps preferred_apps_;
};
......
......@@ -295,3 +295,110 @@ TEST_F(PreferredAppListTest, ReplacedAppPreference) {
EXPECT_TRUE(entry != replaced_app_preferences->replaced_preference.end());
EXPECT_EQ(1u, entry->second.size());
}
// Test that for a single preferred app with URL filter, we can delete
// the preferred app id.
TEST_F(PreferredAppListTest, DeletePreferredAppForURL) {
GURL filter_url = GURL("https://www.google.com/abc");
auto intent_filter = apps_util::CreateIntentFilterForUrlScope(filter_url);
preferred_apps_.AddPreferredApp(kAppId1, intent_filter);
EXPECT_EQ(kAppId1, preferred_apps_.FindPreferredAppForUrl(filter_url));
// If try to delete with wrong ID, won't delete.
preferred_apps_.DeletePreferredApp(kAppId2, intent_filter);
EXPECT_EQ(kAppId1, preferred_apps_.FindPreferredAppForUrl(filter_url));
preferred_apps_.DeletePreferredApp(kAppId1, intent_filter);
EXPECT_EQ(base::nullopt, preferred_apps_.FindPreferredAppForUrl(filter_url));
}
// Test for preferred app with filter that does not have all condition
// types. E.g. delete preferred app with intent filter that only have scheme.
TEST_F(PreferredAppListTest, DeleteForTopLayerFilters) {
auto intent_filter = apps_util::CreateSchemeOnlyFilter("tel");
preferred_apps_.AddPreferredApp(kAppId1, intent_filter);
GURL url_in_scope = GURL("tel://1234556/");
EXPECT_EQ(kAppId1, preferred_apps_.FindPreferredAppForUrl(url_in_scope));
preferred_apps_.DeletePreferredApp(kAppId1, intent_filter);
EXPECT_EQ(base::nullopt,
preferred_apps_.FindPreferredAppForUrl(url_in_scope));
}
// Test that we can properly delete for filters that has multiple
// condition values for a condition type.
TEST_F(PreferredAppListTest, DeleteMultipleConditionValues) {
auto intent_filter =
apps_util::CreateIntentFilterForUrlScope(GURL("https://www.google.com/"));
intent_filter->conditions[0]->condition_values.push_back(
apps_util::MakeConditionValue("http",
apps::mojom::PatternMatchType::kNone));
preferred_apps_.AddPreferredApp(kAppId1, intent_filter);
GURL url_https = GURL("https://www.google.com/");
GURL url_http = GURL("http://www.google.com/");
EXPECT_EQ(kAppId1, preferred_apps_.FindPreferredAppForUrl(url_https));
EXPECT_EQ(kAppId1, preferred_apps_.FindPreferredAppForUrl(url_http));
preferred_apps_.DeletePreferredApp(kAppId1, intent_filter);
EXPECT_EQ(base::nullopt, preferred_apps_.FindPreferredAppForUrl(url_https));
EXPECT_EQ(base::nullopt, preferred_apps_.FindPreferredAppForUrl(url_http));
}
// Test for more than one pattern available, we can delete the filter.
TEST_F(PreferredAppListTest, DeleteDifferentPatterns) {
auto intent_filter_literal =
CreatePatternFilter("/bc", apps::mojom::PatternMatchType::kLiteral);
auto intent_filter_prefix =
CreatePatternFilter("/a", apps::mojom::PatternMatchType::kPrefix);
auto intent_filter_glob =
CreatePatternFilter("/c.*d", apps::mojom::PatternMatchType::kGlob);
preferred_apps_.AddPreferredApp(kAppId1, intent_filter_literal);
preferred_apps_.AddPreferredApp(kAppId2, intent_filter_prefix);
preferred_apps_.AddPreferredApp(kAppId3, intent_filter_glob);
GURL url_1 = GURL("https://www.google.com/bc");
GURL url_2 = GURL("https://www.google.com/abbb");
GURL url_3 = GURL("https://www.google.com/ccccccd");
EXPECT_EQ(kAppId1, preferred_apps_.FindPreferredAppForUrl(url_1));
EXPECT_EQ(kAppId2, preferred_apps_.FindPreferredAppForUrl(url_2));
EXPECT_EQ(kAppId3, preferred_apps_.FindPreferredAppForUrl(url_3));
preferred_apps_.DeletePreferredApp(kAppId1, intent_filter_literal);
EXPECT_EQ(base::nullopt, preferred_apps_.FindPreferredAppForUrl(url_1));
EXPECT_EQ(kAppId2, preferred_apps_.FindPreferredAppForUrl(url_2));
EXPECT_EQ(kAppId3, preferred_apps_.FindPreferredAppForUrl(url_3));
preferred_apps_.DeletePreferredApp(kAppId2, intent_filter_prefix);
EXPECT_EQ(base::nullopt, preferred_apps_.FindPreferredAppForUrl(url_2));
EXPECT_EQ(kAppId3, preferred_apps_.FindPreferredAppForUrl(url_3));
preferred_apps_.DeletePreferredApp(kAppId3, intent_filter_glob);
EXPECT_EQ(base::nullopt, preferred_apps_.FindPreferredAppForUrl(url_3));
}
// Test that can delete properly for super set filters. E.g. the filter
// to delete has more condition values compare with filter that was set.
TEST_F(PreferredAppListTest, DeleteForNotCompletedFilter) {
auto intent_filter_set =
apps_util::CreateIntentFilterForUrlScope(GURL("https://www.google.com/"));
auto intent_filter_to_delete =
apps_util::CreateIntentFilterForUrlScope(GURL("http://www.google.com/"));
intent_filter_to_delete->conditions[0]->condition_values.push_back(
apps_util::MakeConditionValue("https",
apps::mojom::PatternMatchType::kNone));
preferred_apps_.AddPreferredApp(kAppId1, intent_filter_set);
GURL url = GURL("https://www.google.com/");
EXPECT_EQ(kAppId1, preferred_apps_.FindPreferredAppForUrl(url));
preferred_apps_.DeletePreferredApp(kAppId1, intent_filter_to_delete);
EXPECT_EQ(base::nullopt, preferred_apps_.FindPreferredAppForUrl(url));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment