Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 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 "ash/wallpaper/wallpaper_utils/sea_pen_metadata_utils.h" |
| 6 | |
| 7 | #include "ash/test/ash_test_base.h" |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 8 | #include "base/i18n/rtl.h" |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 9 | #include "base/json/values_util.h" |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 10 | #include "base/test/icu_test_util.h" |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 11 | #include "base/time/time_override.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace ash { |
| 15 | namespace { |
| 16 | |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 17 | std::string user_search_query = "search query"; |
| 18 | std::string user_visible_query_text = "test template query text"; |
| 19 | std::string user_visible_query_template = "test template title"; |
| 20 | |
| 21 | base::Value::Dict GetTestTemplateQueryDict( |
| 22 | base::Time time = base::Time::Now()) { |
| 23 | return base::Value::Dict() |
| 24 | .Set("creation_time", base::TimeToValue(time)) |
| 25 | .Set("template_id", |
| 26 | base::NumberToString(static_cast<int32_t>( |
| 27 | ash::personalization_app::mojom::SeaPenTemplateId::kFlower))) |
| 28 | .Set("options", base::Value::Dict() |
| 29 | .Set(base::NumberToString(static_cast<int32_t>( |
| 30 | ash::personalization_app::mojom:: |
| 31 | SeaPenTemplateChip::kFlowerColor)), |
| 32 | base::NumberToString(static_cast<int32_t>( |
| 33 | ash::personalization_app::mojom:: |
| 34 | SeaPenTemplateOption::kFlowerColorBlue))) |
| 35 | .Set(base::NumberToString(static_cast<int32_t>( |
| 36 | ash::personalization_app::mojom:: |
| 37 | SeaPenTemplateChip::kFlowerType)), |
| 38 | base::NumberToString(static_cast<int32_t>( |
| 39 | ash::personalization_app::mojom:: |
| 40 | SeaPenTemplateOption::kFlowerTypeRose)))) |
| 41 | .Set("user_visible_query_text", user_visible_query_text) |
| 42 | .Set("user_visible_query_template", user_visible_query_template); |
| 43 | } |
| 44 | |
| 45 | base::Value::Dict GetTestFreeformQueryDict( |
| 46 | base::Time time = base::Time::Now()) { |
| 47 | return base::Value::Dict() |
| 48 | .Set("creation_time", base::TimeToValue(time)) |
| 49 | .Set("freeform_query", user_search_query); |
| 50 | } |
| 51 | |
| 52 | base::Value::Dict GetTestInvalidTemplateQueryDict( |
| 53 | const std::string& missing_field) { |
| 54 | base::Value::Dict template_query_dict = GetTestTemplateQueryDict(); |
| 55 | if (template_query_dict.contains(missing_field)) { |
| 56 | template_query_dict.Remove(missing_field); |
| 57 | } |
| 58 | return template_query_dict; |
| 59 | } |
| 60 | |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 61 | base::subtle::ScopedTimeClockOverrides CreateScopedTimeNowOverride() { |
| 62 | return base::subtle::ScopedTimeClockOverrides( |
| 63 | []() -> base::Time { |
| 64 | base::Time fake_now; |
| 65 | bool success = |
| 66 | base::Time::FromString("2023-04-05T01:23:45Z", &fake_now); |
| 67 | DCHECK(success); |
| 68 | return fake_now; |
| 69 | }, |
| 70 | nullptr, nullptr); |
| 71 | } |
| 72 | |
| 73 | TEST(SeaPenMetadataUtilsTest, SeaPenTextQueryToDict) { |
| 74 | auto time_override = CreateScopedTimeNowOverride(); |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 75 | |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 76 | ash::personalization_app::mojom::SeaPenQueryPtr search_query = |
| 77 | ash::personalization_app::mojom::SeaPenQuery::NewTextQuery( |
| 78 | user_search_query); |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 79 | |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 80 | base::Value::Dict result = SeaPenQueryToDict(search_query); |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 81 | |
| 82 | EXPECT_EQ(GetTestFreeformQueryDict(), result); |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(SeaPenMetadataUtilsTest, SeaPenTemplateQueryToDict) { |
| 86 | auto time_override = CreateScopedTimeNowOverride(); |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 87 | |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 88 | base::flat_map<ash::personalization_app::mojom::SeaPenTemplateChip, |
| 89 | ash::personalization_app::mojom::SeaPenTemplateOption> |
| 90 | options( |
| 91 | {{ash::personalization_app::mojom::SeaPenTemplateChip::kFlowerColor, |
| 92 | ash::personalization_app::mojom::SeaPenTemplateOption:: |
| 93 | kFlowerColorBlue}, |
| 94 | {ash::personalization_app::mojom::SeaPenTemplateChip::kFlowerType, |
| 95 | ash::personalization_app::mojom::SeaPenTemplateOption:: |
| 96 | kFlowerTypeRose}}); |
| 97 | ash::personalization_app::mojom::SeaPenQueryPtr search_query = |
| 98 | ash::personalization_app::mojom::SeaPenQuery::NewTemplateQuery( |
| 99 | ash::personalization_app::mojom::SeaPenTemplateQuery::New( |
| 100 | ash::personalization_app::mojom::SeaPenTemplateId::kFlower, |
| 101 | options, |
| 102 | ash::personalization_app::mojom::SeaPenUserVisibleQuery::New( |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 103 | user_visible_query_text, user_visible_query_template))); |
| 104 | |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 105 | base::Value::Dict result = SeaPenQueryToDict(search_query); |
Thuong Phan | 3f64c24 | 2024-02-15 19:43:36 | [diff] [blame] | 106 | |
| 107 | EXPECT_EQ(GetTestTemplateQueryDict(), result); |
| 108 | } |
| 109 | |
| 110 | TEST(SeaPenMetadataUtilsTest, |
| 111 | SeaPenQueryDictToRecentImageInfoVerifyCreationTime) { |
| 112 | const base::test::ScopedRestoreICUDefaultLocale locale("en_US"); |
| 113 | const base::test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles"); |
| 114 | |
| 115 | base::Time test_creation_time; |
| 116 | ASSERT_TRUE(base::Time::FromString("Fri, 01 Dec 2023 00:00:00 GMT", |
| 117 | &test_creation_time)); |
| 118 | |
| 119 | auto recent_image_info = SeaPenQueryDictToRecentImageInfo( |
| 120 | GetTestFreeformQueryDict(test_creation_time)); |
| 121 | |
| 122 | // creation time in recent_image_info should be "Nov 30, 2023" which is |
| 123 | // converted from "Fri, 01 Dec 2023 00:00:00 GMT" to America/Los_Angeles time. |
| 124 | EXPECT_EQ(u"Nov 30, 2023", recent_image_info->creation_time); |
| 125 | } |
| 126 | |
| 127 | TEST(SeaPenMetadataUtilsTest, |
| 128 | SeaPenQueryDictToRecentImageInfoValidTemplateData) { |
| 129 | auto expected_user_visible_query = |
| 130 | ash::personalization_app::mojom::SeaPenUserVisibleQuery::New( |
| 131 | user_visible_query_text, user_visible_query_template); |
| 132 | |
| 133 | auto recent_image_info = |
| 134 | SeaPenQueryDictToRecentImageInfo(GetTestTemplateQueryDict()); |
| 135 | |
| 136 | EXPECT_TRUE(recent_image_info->user_visible_query.Equals( |
| 137 | expected_user_visible_query)); |
| 138 | } |
| 139 | |
| 140 | TEST(SeaPenMetadataUtilsTest, |
| 141 | SeaPenQueryDictToRecentImageInfoValidFreeformData) { |
| 142 | auto expected_user_visible_query = |
| 143 | ash::personalization_app::mojom::SeaPenUserVisibleQuery::New( |
| 144 | user_search_query, std::string()); |
| 145 | |
| 146 | auto recent_image_info = |
| 147 | SeaPenQueryDictToRecentImageInfo(GetTestFreeformQueryDict()); |
| 148 | |
| 149 | EXPECT_TRUE(recent_image_info->user_visible_query.Equals( |
| 150 | expected_user_visible_query)); |
| 151 | } |
| 152 | |
| 153 | TEST(SeaPenMetadataUtilsTest, |
| 154 | SeaPenQueryDictToRecentImageInfoMissingCreationTime) { |
| 155 | base::Value::Dict invalid_template_query_dict = |
| 156 | GetTestInvalidTemplateQueryDict(/*missing_field=*/"creation_time"); |
| 157 | |
| 158 | auto recent_image_info = |
| 159 | SeaPenQueryDictToRecentImageInfo(invalid_template_query_dict); |
| 160 | |
| 161 | EXPECT_FALSE(recent_image_info); |
| 162 | } |
| 163 | |
| 164 | TEST(SeaPenMetadataUtilsTest, |
| 165 | SeaPenQueryDictToRecentImageInfoMissingUserVisibleQueryText) { |
| 166 | base::Value::Dict invalid_template_query_dict = |
| 167 | GetTestInvalidTemplateQueryDict( |
| 168 | /*missing_field=*/"user_visible_query_text"); |
| 169 | |
| 170 | auto recent_image_info = |
| 171 | SeaPenQueryDictToRecentImageInfo(invalid_template_query_dict); |
| 172 | |
| 173 | EXPECT_FALSE(recent_image_info); |
| 174 | } |
| 175 | |
| 176 | TEST(SeaPenMetadataUtilsTest, |
| 177 | SeaPenQueryDictToRecentImageInfoMissingUserVisibleQueryTemplate) { |
| 178 | base::Value::Dict invalid_template_query_dict = |
| 179 | GetTestInvalidTemplateQueryDict( |
| 180 | /*missing_field=*/"user_visible_query_template"); |
| 181 | |
| 182 | auto recent_image_info = |
| 183 | SeaPenQueryDictToRecentImageInfo(invalid_template_query_dict); |
| 184 | |
| 185 | EXPECT_FALSE(recent_image_info); |
Thuong Phan | 01675005 | 2024-02-05 19:55:38 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace |
| 189 | } // namespace ash |