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

[IntentHandling] Parse base::Value to preferred apps list.

This CL parses the base::Value read from JSON file back
to preferred apps list.
This CL also adds some unit tests.

BUG=853604

Change-Id: I8724784f5ab91e19614c1f66f45f1aff4fef0d80
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2141754
Commit-Queue: Maggie Cai <mxcai@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759189}
parent 9f0e99aa
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <utility>
#include "chrome/services/app_service/public/cpp/preferred_apps_converter.h"
#include "chrome/services/app_service/public/mojom/types.mojom.h"
......@@ -40,6 +42,62 @@ base::Value ConvertIntentFilterToValue(
return intent_filter_value;
}
apps::mojom::ConditionValuePtr ParseValueToConditionValue(
const base::Value& value) {
auto* value_string = value.FindStringKey(apps::kValueKey);
if (!value_string) {
return nullptr;
}
auto condition_value = apps::mojom::ConditionValue::New();
condition_value->value = *value_string;
auto match_type = value.FindIntKey(apps::kMatchTypeKey);
if (!match_type.has_value()) {
return nullptr;
}
condition_value->match_type =
static_cast<apps::mojom::PatternMatchType>(match_type.value());
return condition_value;
}
apps::mojom::ConditionPtr ParseValueToCondition(const base::Value& value) {
auto condition_type = value.FindIntKey(apps::kConditionTypeKey);
if (!condition_type.has_value()) {
return nullptr;
}
auto condition = apps::mojom::Condition::New();
condition->condition_type =
static_cast<apps::mojom::ConditionType>(condition_type.value());
auto* condition_values = value.FindKey(apps::kConditionValuesKey);
if (!condition_values || !condition_values->is_list()) {
return nullptr;
}
for (auto& condition_value : condition_values->GetList()) {
auto parsed_condition_value = ParseValueToConditionValue(condition_value);
if (!parsed_condition_value) {
return nullptr;
}
condition->condition_values.push_back(std::move(parsed_condition_value));
}
return condition;
}
apps::mojom::IntentFilterPtr ParseValueToIntentFilter(
const base::Value* value) {
if (!value || !value->is_list()) {
return nullptr;
}
auto intent_filter = apps::mojom::IntentFilter::New();
for (auto& condition : value->GetList()) {
auto parsed_condition = ParseValueToCondition(condition);
if (!parsed_condition) {
return nullptr;
}
intent_filter->conditions.push_back(std::move(parsed_condition));
}
return intent_filter;
}
} // namespace
namespace apps {
......@@ -65,4 +123,27 @@ base::Value ConvertPreferredAppsToValue(
return preferred_apps_value;
}
PreferredAppsList::PreferredApps ParseValueToPreferredApps(
const base::Value& preferred_apps_value) {
if (!preferred_apps_value.is_list()) {
return PreferredAppsList::PreferredApps();
}
PreferredAppsList::PreferredApps preferred_apps;
for (auto& entry : preferred_apps_value.GetList()) {
auto* app_id = entry.FindStringKey(kAppIdKey);
if (!app_id) {
return PreferredAppsList::PreferredApps();
}
auto parsed_intent_filter =
ParseValueToIntentFilter(entry.FindKey(kIntentFilterKey));
if (!parsed_intent_filter) {
return PreferredAppsList::PreferredApps();
}
auto new_preferred_app = apps::mojom::PreferredApp::New(
std::move(parsed_intent_filter), *app_id);
preferred_apps.push_back(std::move(new_preferred_app));
}
return preferred_apps;
}
} // namespace apps
......@@ -45,6 +45,10 @@ extern const char kIntentFilterKey[];
base::Value ConvertPreferredAppsToValue(
const PreferredAppsList::PreferredApps& preferred_apps);
// Parse the base::Value read from JSON file back to preferred apps struct.
PreferredAppsList::PreferredApps ParseValueToPreferredApps(
const base::Value& preferred_apps_value);
} // namespace apps
#endif // CHROME_SERVICES_APP_SERVICE_PUBLIC_CPP_PREFERRED_APPS_CONVERTER_H_
......@@ -17,19 +17,18 @@ const char kAppId1[] = "abcdefg";
} // namespace
class PreferredAppsConverterTest : public testing::Test {
protected:
apps::PreferredAppsList preferred_apps_;
};
class PreferredAppsConverterTest : public testing::Test {};
// Test one simple entry with simple filter.
TEST_F(PreferredAppsConverterTest, ConvertSimpleEntry) {
GURL filter_url = GURL("https://www.google.com/abc");
auto intent_filter = apps_util::CreateIntentFilterForUrlScope(filter_url);
preferred_apps_.AddPreferredApp(kAppId1, intent_filter);
apps::PreferredAppsList preferred_apps;
preferred_apps.Init();
preferred_apps.AddPreferredApp(kAppId1, intent_filter);
auto converted_value =
apps::ConvertPreferredAppsToValue(preferred_apps_.GetReference());
apps::ConvertPreferredAppsToValue(preferred_apps.GetReference());
// Check that each entry is correct.
ASSERT_EQ(1u, converted_value.GetList().size());
......@@ -55,16 +54,27 @@ TEST_F(PreferredAppsConverterTest, ConvertSimpleEntry) {
EXPECT_EQ(static_cast<int>(condition_values[0]->match_type),
converted_condition_values[0].FindIntKey(apps::kMatchTypeKey));
}
auto preferred_apps_list = apps::ParseValueToPreferredApps(converted_value);
preferred_apps.Init();
EXPECT_EQ(base::nullopt, preferred_apps.FindPreferredAppForUrl(filter_url));
preferred_apps.Init(preferred_apps_list);
EXPECT_EQ(kAppId1, preferred_apps.FindPreferredAppForUrl(filter_url));
GURL url_wrong_host = GURL("https://www.hahaha.com/");
EXPECT_EQ(base::nullopt,
preferred_apps.FindPreferredAppForUrl(url_wrong_host));
}
// Test one simple entry with json string.
TEST_F(PreferredAppsConverterTest, ConvertSimpleEntryJson) {
GURL filter_url = GURL("https://www.google.com/abc");
auto intent_filter = apps_util::CreateIntentFilterForUrlScope(filter_url);
preferred_apps_.AddPreferredApp(kAppId1, intent_filter);
apps::PreferredAppsList preferred_apps;
preferred_apps.Init();
preferred_apps.AddPreferredApp(kAppId1, intent_filter);
auto converted_value =
apps::ConvertPreferredAppsToValue(preferred_apps_.GetReference());
apps::ConvertPreferredAppsToValue(preferred_apps.GetReference());
const char expected_output_string[] =
"[ {\"app_id\": \"abcdefg\","
......@@ -93,3 +103,369 @@ TEST_F(PreferredAppsConverterTest, ConvertSimpleEntryJson) {
ASSERT_TRUE(expected_output);
EXPECT_EQ(expected_output.value(), converted_value);
}
// Test parse simple entry from json string.
TEST_F(PreferredAppsConverterTest, ParseSimpleEntryJson) {
const char test_string[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> test_value = base::JSONReader::Read(test_string);
ASSERT_TRUE(test_value);
auto parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
GURL filter_url = GURL("https://www.google.com/abc");
auto intent_filter = apps_util::CreateIntentFilterForUrlScope(filter_url);
apps::PreferredAppsList preferred_apps;
preferred_apps.Init();
preferred_apps.AddPreferredApp(kAppId1, intent_filter);
auto& expected_entry = preferred_apps.GetReference();
EXPECT_EQ(expected_entry, parsed_entry);
}
TEST_F(PreferredAppsConverterTest, ParseJsonWithInvalidAppId) {
// Invalid key.
const char test_key[] =
"[ {\"app_idd\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> test_value = base::JSONReader::Read(test_key);
ASSERT_TRUE(test_value);
auto parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
// Invalid value.
const char test_string[] =
"[ {\"app_id\": 0,"
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
test_value = base::JSONReader::Read(test_string);
ASSERT_TRUE(test_value);
parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
}
TEST_F(PreferredAppsConverterTest, ParseJsonWithInvalidIntentFilter) {
// Invalid key.
const char test_key[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filterrr\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> test_value = base::JSONReader::Read(test_key);
ASSERT_TRUE(test_value);
auto parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
// Invalid value.
const char test_string[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": \"not_list\""
"} ]";
test_value = base::JSONReader::Read(test_string);
ASSERT_TRUE(test_value);
parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
}
TEST_F(PreferredAppsConverterTest, ParseJsonWithInvalidConditionType) {
// Invalid key.
const char test_key[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_typeeee\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> test_value = base::JSONReader::Read(test_key);
ASSERT_TRUE(test_value);
auto parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
// Invalid value.
const char test_string[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": \"not_int\","
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
test_value = base::JSONReader::Read(test_string);
ASSERT_TRUE(test_value);
parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
}
TEST_F(PreferredAppsConverterTest, ParseJsonWithInvalidValues) {
// Invalid key.
const char test_key[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_valuessss\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> test_value = base::JSONReader::Read(test_key);
ASSERT_TRUE(test_value);
auto parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
// Invalid value.
const char test_string[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": \"not_list\""
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
test_value = base::JSONReader::Read(test_string);
ASSERT_TRUE(test_value);
parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
}
TEST_F(PreferredAppsConverterTest, ParseJsonWithInvalidMatchType) {
// Invalid key.
const char test_key[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_typeeeee\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> test_value = base::JSONReader::Read(test_key);
ASSERT_TRUE(test_value);
auto parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
// Invalid value.
const char test_string[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": \"not_int\","
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
test_value = base::JSONReader::Read(test_string);
ASSERT_TRUE(test_value);
parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
}
TEST_F(PreferredAppsConverterTest, ParseJsonWithInvalidValue) {
// Invalid key.
const char test_key[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"valueeeee\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> test_value = base::JSONReader::Read(test_key);
ASSERT_TRUE(test_value);
auto parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
// Invalid value.
const char test_string[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": {}"
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
test_value = base::JSONReader::Read(test_string);
ASSERT_TRUE(test_value);
parsed_entry = apps::ParseValueToPreferredApps(test_value.value());
EXPECT_TRUE(parsed_entry.empty());
}
......@@ -137,6 +137,7 @@ void PreferredAppsList::DeleteAppId(const std::string& app_id) {
}
void PreferredAppsList::Init() {
preferred_apps_ = PreferredApps();
initialized_ = true;
}
......
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