| 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "format/proto/ProtoSerialize.h" |
| 18 | |
| 19 | #include "ResourceUtils.h" |
| 20 | #include "format/proto/ProtoDeserialize.h" |
| 21 | #include "test/Test.h" |
| 22 | |
| 23 | using ::android::ConfigDescription; |
| 24 | using ::android::StringPiece; |
| 25 | using ::testing::Eq; |
| 26 | using ::testing::IsEmpty; |
| 27 | using ::testing::IsNull; |
| 28 | using ::testing::NotNull; |
| 29 | using ::testing::SizeIs; |
| 30 | using ::testing::StrEq; |
| 31 | |
| 32 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 33 | |
| 34 | namespace aapt { |
| 35 | |
| 36 | class MockFileCollection : public io::IFileCollection { |
| 37 | public: |
| 38 | MOCK_METHOD1(FindFile, io::IFile*(const StringPiece& path)); |
| 39 | MOCK_METHOD0(Iterator, std::unique_ptr<io::IFileCollectionIterator>()); |
| 40 | MOCK_METHOD0(GetDirSeparator, char()); |
| 41 | }; |
| 42 | |
| 43 | ResourceEntry* GetEntry(ResourceTable* table, const ResourceNameRef& res_name) { |
| 44 | auto result = table->FindResource(res_name); |
| 45 | return (result) ? result.value().entry : nullptr; |
| 46 | } |
| 47 | |
| 48 | ResourceEntry* GetEntry(ResourceTable* table, const ResourceNameRef& res_name, ResourceId id) { |
| 49 | auto result = table->FindResource(res_name, id); |
| 50 | return (result) ? result.value().entry : nullptr; |
| 51 | } |
| 52 | |
| 53 | TEST(ProtoSerializeTest, SerializeVisibility) { |
| 54 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 55 | std::unique_ptr<ResourceTable> table = |
| 56 | test::ResourceTableBuilder() |
| 57 | .Add(NewResourceBuilder("com.app.a:bool/foo") |
| 58 | .SetVisibility({Visibility::Level::kUndefined}) |
| 59 | .Build()) |
| 60 | .Add(NewResourceBuilder("com.app.a:bool/bar") |
| 61 | .SetVisibility({Visibility::Level::kPrivate}) |
| 62 | .Build()) |
| 63 | .Add(NewResourceBuilder("com.app.a:bool/baz") |
| 64 | .SetVisibility({Visibility::Level::kPublic}) |
| 65 | .Build()) |
| 66 | .Add(NewResourceBuilder("com.app.a:bool/fiz") |
| 67 | .SetVisibility({.level = Visibility::Level::kPublic, .staged_api = true}) |
| 68 | .Build()) |
| 69 | .Build(); |
| 70 | |
| 71 | ResourceTable new_table; |
| 72 | pb::ResourceTable pb_table; |
| 73 | MockFileCollection files; |
| 74 | std::string error; |
| 75 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics()); |
| 76 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)); |
| 77 | EXPECT_THAT(error, IsEmpty()); |
| 78 | |
| 79 | auto search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/foo")); |
| 80 | ASSERT_TRUE(search_result); |
| 81 | EXPECT_THAT(search_result.value().entry->visibility.level, Eq(Visibility::Level::kUndefined)); |
| 82 | EXPECT_FALSE(search_result.value().entry->visibility.staged_api); |
| 83 | |
| 84 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/bar")); |
| 85 | ASSERT_TRUE(search_result); |
| 86 | EXPECT_THAT(search_result.value().entry->visibility.level, Eq(Visibility::Level::kPrivate)); |
| 87 | EXPECT_FALSE(search_result.value().entry->visibility.staged_api); |
| 88 | |
| 89 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/baz")); |
| 90 | ASSERT_TRUE(search_result); |
| 91 | EXPECT_THAT(search_result.value().entry->visibility.level, Eq(Visibility::Level::kPublic)); |
| 92 | EXPECT_FALSE(search_result.value().entry->visibility.staged_api); |
| 93 | |
| 94 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/fiz")); |
| 95 | ASSERT_TRUE(search_result); |
| 96 | EXPECT_THAT(search_result.value().entry->visibility.level, Eq(Visibility::Level::kPublic)); |
| 97 | EXPECT_TRUE(search_result.value().entry->visibility.staged_api); |
| 98 | } |
| 99 | |
| 100 | TEST(ProtoSerializeTest, SerializeSinglePackage) { |
| 101 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 102 | std::unique_ptr<ResourceTable> table = |
| 103 | test::ResourceTableBuilder() |
| 104 | .AddFileReference("com.app.a:layout/main", ResourceId(0x7f020000), "res/layout/main.xml") |
| 105 | .AddReference("com.app.a:layout/other", ResourceId(0x7f020001), "com.app.a:layout/main") |
| 106 | .AddString("com.app.a:string/text", {}, "hi") |
| 107 | .AddValue("com.app.a:id/foo", {}, util::make_unique<Id>()) |
| 108 | .SetSymbolState("com.app.a:bool/foo", {}, Visibility::Level::kUndefined, |
| 109 | true /*allow_new*/) |
| 110 | .Build(); |
| 111 | |
| 112 | ASSERT_TRUE(table->AddResource(NewResourceBuilder(test::ParseNameOrDie("com.app.a:layout/main")) |
| 113 | .SetId(0x7f020000) |
| 114 | .SetVisibility({Visibility::Level::kPublic}) |
| 115 | .Build(), |
| 116 | context->GetDiagnostics())); |
| 117 | |
| 118 | Id* id = test::GetValue<Id>(table.get(), "com.app.a:id/foo"); |
| 119 | ASSERT_THAT(id, NotNull()); |
| 120 | |
| 121 | // Make a plural. |
| 122 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
| 123 | plural->values[Plural::One] = util::make_unique<String>(table->string_pool.MakeRef("one")); |
| 124 | ASSERT_TRUE(table->AddResource(NewResourceBuilder(test::ParseNameOrDie("com.app.a:plurals/hey")) |
| 125 | .SetValue(std::move(plural)) |
| 126 | .Build(), |
| 127 | context->GetDiagnostics())); |
| 128 | |
| 129 | // Make a styled string. |
| 130 | StyleString style_string; |
| 131 | style_string.str = "hello"; |
| 132 | style_string.spans.push_back(Span{"b", 0u, 4u}); |
| 133 | ASSERT_TRUE(table->AddResource( |
| 134 | NewResourceBuilder(test::ParseNameOrDie("com.app.a:string/styled")) |
| 135 | .SetValue(util::make_unique<StyledString>(table->string_pool.MakeRef(style_string))) |
| 136 | .Build(), |
| 137 | context->GetDiagnostics())); |
| 138 | |
| 139 | // Make a resource with different products. |
| 140 | ASSERT_TRUE( |
| 141 | table->AddResource(NewResourceBuilder(test::ParseNameOrDie("com.app.a:integer/one")) |
| 142 | .SetValue(test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 123u), |
| 143 | test::ParseConfigOrDie("land")) |
| 144 | .Build(), |
| 145 | context->GetDiagnostics())); |
| 146 | |
| 147 | ASSERT_TRUE( |
| 148 | table->AddResource(NewResourceBuilder(test::ParseNameOrDie("com.app.a:integer/one")) |
| 149 | .SetValue(test::BuildPrimitive(android::Res_value::TYPE_INT_HEX, 321u), |
| 150 | test::ParseConfigOrDie("land"), "tablet") |
| 151 | .Build(), |
| 152 | context->GetDiagnostics())); |
| 153 | |
| 154 | // Make a reference with both resource name and resource ID. |
| 155 | // The reference should point to a resource outside of this table to test that both name and id |
| 156 | // get serialized. |
| 157 | Reference expected_ref; |
| 158 | expected_ref.name = test::ParseNameOrDie("android:layout/main"); |
| 159 | expected_ref.id = ResourceId(0x01020000); |
| 160 | ASSERT_TRUE(table->AddResource(NewResourceBuilder(test::ParseNameOrDie("com.app.a:layout/abc")) |
| 161 | .SetValue(util::make_unique<Reference>(expected_ref)) |
| 162 | .Build(), |
| 163 | context->GetDiagnostics())); |
| 164 | |
| 165 | // Make an overlayable resource. |
| 166 | OverlayableItem overlayable_item(std::make_shared<Overlayable>( |
| 167 | "OverlayableName", "overlay://theme", Source("res/values/overlayable.xml", 40))); |
| 168 | overlayable_item.source = Source("res/values/overlayable.xml", 42); |
| 169 | ASSERT_TRUE( |
| 170 | table->AddResource(NewResourceBuilder(test::ParseNameOrDie("com.app.a:integer/overlayable")) |
| 171 | .SetOverlayable(overlayable_item) |
| 172 | .Build(), |
| 173 | context->GetDiagnostics())); |
| 174 | |
| 175 | pb::ResourceTable pb_table; |
| 176 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics()); |
| 177 | |
| 178 | test::TestFile file_a("res/layout/main.xml"); |
| 179 | MockFileCollection files; |
| 180 | EXPECT_CALL(files, FindFile(Eq("res/layout/main.xml"))) |
| 181 | .WillRepeatedly(::testing::Return(&file_a)); |
| 182 | |
| 183 | ResourceTable new_table; |
| 184 | std::string error; |
| 185 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)) << error; |
| 186 | EXPECT_THAT(error, IsEmpty()); |
| 187 | |
| 188 | Id* new_id = test::GetValue<Id>(&new_table, "com.app.a:id/foo"); |
| 189 | ASSERT_THAT(new_id, NotNull()); |
| 190 | EXPECT_THAT(new_id->IsWeak(), Eq(id->IsWeak())); |
| 191 | |
| 192 | std::optional<ResourceTable::SearchResult> result = |
| 193 | new_table.FindResource(test::ParseNameOrDie("com.app.a:layout/main")); |
| 194 | ASSERT_TRUE(result); |
| 195 | |
| 196 | EXPECT_THAT(result.value().type->visibility_level, Eq(Visibility::Level::kPublic)); |
| 197 | EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic)); |
| 198 | |
| 199 | result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/foo")); |
| 200 | ASSERT_TRUE(result); |
| 201 | EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kUndefined)); |
| 202 | EXPECT_TRUE(result.value().entry->allow_new); |
| 203 | |
| 204 | // Find the product-dependent values |
| 205 | BinaryPrimitive* prim = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 206 | &new_table, "com.app.a:integer/one", test::ParseConfigOrDie("land"), ""); |
| 207 | ASSERT_THAT(prim, NotNull()); |
| 208 | EXPECT_THAT(prim->value.data, Eq(123u)); |
| 209 | EXPECT_THAT(prim->value.dataType, Eq(0x10)); |
| 210 | |
| 211 | prim = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 212 | &new_table, "com.app.a:integer/one", test::ParseConfigOrDie("land"), "tablet"); |
| 213 | ASSERT_THAT(prim, NotNull()); |
| 214 | EXPECT_THAT(prim->value.data, Eq(321u)); |
| 215 | EXPECT_THAT(prim->value.dataType, Eq(0x11)); |
| 216 | |
| 217 | Reference* actual_ref = test::GetValue<Reference>(&new_table, "com.app.a:layout/abc"); |
| 218 | ASSERT_THAT(actual_ref, NotNull()); |
| 219 | ASSERT_TRUE(actual_ref->name); |
| 220 | ASSERT_TRUE(actual_ref->id); |
| 221 | EXPECT_THAT(*actual_ref, Eq(expected_ref)); |
| 222 | |
| 223 | FileReference* actual_file_ref = |
| 224 | test::GetValue<FileReference>(&new_table, "com.app.a:layout/main"); |
| 225 | ASSERT_THAT(actual_file_ref, NotNull()); |
| 226 | EXPECT_THAT(actual_file_ref->file, Eq(&file_a)); |
| 227 | |
| 228 | StyledString* actual_styled_str = |
| 229 | test::GetValue<StyledString>(&new_table, "com.app.a:string/styled"); |
| 230 | ASSERT_THAT(actual_styled_str, NotNull()); |
| 231 | EXPECT_THAT(actual_styled_str->value->value, Eq("hello")); |
| 232 | ASSERT_THAT(actual_styled_str->value->spans, SizeIs(1u)); |
| 233 | EXPECT_THAT(*actual_styled_str->value->spans[0].name, Eq("b")); |
| 234 | EXPECT_THAT(actual_styled_str->value->spans[0].first_char, Eq(0u)); |
| 235 | EXPECT_THAT(actual_styled_str->value->spans[0].last_char, Eq(4u)); |
| 236 | |
| 237 | std::optional<ResourceTable::SearchResult> search_result = |
| 238 | new_table.FindResource(test::ParseNameOrDie("com.app.a:integer/overlayable")); |
| 239 | ASSERT_TRUE(search_result); |
| 240 | ASSERT_THAT(search_result.value().entry, NotNull()); |
| 241 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 242 | OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 243 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("OverlayableName")); |
| 244 | EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme")); |
| 245 | EXPECT_THAT(result_overlayable_item.overlayable->source.path, Eq("res/values/overlayable.xml")); |
| 246 | EXPECT_THAT(result_overlayable_item.overlayable->source.line, Eq(40)); |
| 247 | EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::NONE)); |
| 248 | EXPECT_THAT(result_overlayable_item.source.path, Eq("res/values/overlayable.xml")); |
| 249 | EXPECT_THAT(result_overlayable_item.source.line, Eq(42)); |
| 250 | } |
| 251 | |
| 252 | TEST(ProtoSerializeTest, SerializeAndDeserializeXml) { |
| 253 | xml::Element element; |
| 254 | element.line_number = 22; |
| 255 | element.column_number = 23; |
| 256 | element.name = "element"; |
| 257 | element.namespace_uri = "uri://"; |
| 258 | |
| 259 | xml::NamespaceDecl decl; |
| 260 | decl.prefix = "android"; |
| 261 | decl.uri = xml::kSchemaAndroid; |
| 262 | decl.line_number = 21; |
| 263 | decl.column_number = 24; |
| 264 | |
| 265 | element.namespace_decls.push_back(decl); |
| 266 | |
| 267 | xml::Attribute attr; |
| 268 | attr.name = "name"; |
| 269 | attr.namespace_uri = xml::kSchemaAndroid; |
| 270 | attr.value = "23dp"; |
| 271 | attr.compiled_attribute = xml::AaptAttribute(Attribute{}, ResourceId(0x01010000)); |
| 272 | attr.compiled_value = |
| 273 | ResourceUtils::TryParseItemForAttribute(attr.value, android::ResTable_map::TYPE_DIMENSION); |
| 274 | attr.compiled_value->SetSource(Source().WithLine(25)); |
| 275 | element.attributes.push_back(std::move(attr)); |
| 276 | |
| 277 | std::unique_ptr<xml::Text> text = util::make_unique<xml::Text>(); |
| 278 | text->line_number = 25; |
| 279 | text->column_number = 3; |
| 280 | text->text = "hey there"; |
| 281 | element.AppendChild(std::move(text)); |
| 282 | |
| 283 | std::unique_ptr<xml::Element> child = util::make_unique<xml::Element>(); |
| 284 | child->name = "child"; |
| 285 | |
| 286 | text = util::make_unique<xml::Text>(); |
| 287 | text->text = "woah there"; |
| 288 | child->AppendChild(std::move(text)); |
| 289 | |
| 290 | element.AppendChild(std::move(child)); |
| 291 | |
| 292 | pb::XmlNode pb_xml; |
| 293 | SerializeXmlToPb(element, &pb_xml); |
| 294 | |
| 295 | StringPool pool; |
| 296 | xml::Element actual_el; |
| 297 | std::string error; |
| 298 | ASSERT_TRUE(DeserializeXmlFromPb(pb_xml, &actual_el, &pool, &error)); |
| 299 | ASSERT_THAT(error, IsEmpty()); |
| 300 | |
| 301 | EXPECT_THAT(actual_el.name, StrEq("element")); |
| 302 | EXPECT_THAT(actual_el.namespace_uri, StrEq("uri://")); |
| 303 | EXPECT_THAT(actual_el.line_number, Eq(22u)); |
| 304 | EXPECT_THAT(actual_el.column_number, Eq(23u)); |
| 305 | |
| 306 | ASSERT_THAT(actual_el.namespace_decls, SizeIs(1u)); |
| 307 | const xml::NamespaceDecl& actual_decl = actual_el.namespace_decls[0]; |
| 308 | EXPECT_THAT(actual_decl.prefix, StrEq("android")); |
| 309 | EXPECT_THAT(actual_decl.uri, StrEq(xml::kSchemaAndroid)); |
| 310 | EXPECT_THAT(actual_decl.line_number, Eq(21u)); |
| 311 | EXPECT_THAT(actual_decl.column_number, Eq(24u)); |
| 312 | |
| 313 | ASSERT_THAT(actual_el.attributes, SizeIs(1u)); |
| 314 | const xml::Attribute& actual_attr = actual_el.attributes[0]; |
| 315 | EXPECT_THAT(actual_attr.name, StrEq("name")); |
| 316 | EXPECT_THAT(actual_attr.namespace_uri, StrEq(xml::kSchemaAndroid)); |
| 317 | EXPECT_THAT(actual_attr.value, StrEq("23dp")); |
| 318 | |
| 319 | ASSERT_THAT(actual_attr.compiled_value, NotNull()); |
| 320 | const BinaryPrimitive* prim = ValueCast<BinaryPrimitive>(actual_attr.compiled_value.get()); |
| 321 | ASSERT_THAT(prim, NotNull()); |
| 322 | EXPECT_THAT(prim->value.dataType, Eq(android::Res_value::TYPE_DIMENSION)); |
| 323 | |
| 324 | ASSERT_TRUE(actual_attr.compiled_attribute); |
| 325 | ASSERT_TRUE(actual_attr.compiled_attribute.value().id); |
| 326 | |
| 327 | ASSERT_THAT(actual_el.children, SizeIs(2u)); |
| 328 | const xml::Text* child_text = xml::NodeCast<xml::Text>(actual_el.children[0].get()); |
| 329 | ASSERT_THAT(child_text, NotNull()); |
| 330 | const xml::Element* child_el = xml::NodeCast<xml::Element>(actual_el.children[1].get()); |
| 331 | ASSERT_THAT(child_el, NotNull()); |
| 332 | |
| 333 | EXPECT_THAT(child_text->line_number, Eq(25u)); |
| 334 | EXPECT_THAT(child_text->column_number, Eq(3u)); |
| 335 | EXPECT_THAT(child_text->text, StrEq("hey there")); |
| 336 | |
| 337 | EXPECT_THAT(child_el->name, StrEq("child")); |
| 338 | ASSERT_THAT(child_el->children, SizeIs(1u)); |
| 339 | |
| 340 | child_text = xml::NodeCast<xml::Text>(child_el->children[0].get()); |
| 341 | ASSERT_THAT(child_text, NotNull()); |
| 342 | EXPECT_THAT(child_text->text, StrEq("woah there")); |
| 343 | } |
| 344 | |
| 345 | TEST(ProtoSerializeTest, SerializeAndDeserializeXmlTrimEmptyWhitepsace) { |
| 346 | xml::Element element; |
| 347 | element.line_number = 22; |
| 348 | element.column_number = 23; |
| 349 | element.name = "element"; |
| 350 | |
| 351 | std::unique_ptr<xml::Text> trim_text = util::make_unique<xml::Text>(); |
| 352 | trim_text->line_number = 25; |
| 353 | trim_text->column_number = 3; |
| 354 | trim_text->text = " \n "; |
| 355 | element.AppendChild(std::move(trim_text)); |
| 356 | |
| 357 | std::unique_ptr<xml::Text> keep_text = util::make_unique<xml::Text>(); |
| 358 | keep_text->line_number = 26; |
| 359 | keep_text->column_number = 3; |
| 360 | keep_text->text = " hello "; |
| 361 | element.AppendChild(std::move(keep_text)); |
| 362 | |
| 363 | pb::XmlNode pb_xml; |
| 364 | SerializeXmlOptions options; |
| 365 | options.remove_empty_text_nodes = true; |
| 366 | SerializeXmlToPb(element, &pb_xml, options); |
| 367 | |
| 368 | StringPool pool; |
| 369 | xml::Element actual_el; |
| 370 | std::string error; |
| 371 | ASSERT_TRUE(DeserializeXmlFromPb(pb_xml, &actual_el, &pool, &error)); |
| 372 | ASSERT_THAT(error, IsEmpty()); |
| 373 | |
| 374 | // Only the child that does not consist of only whitespace should remain |
| 375 | ASSERT_THAT(actual_el.children, SizeIs(1u)); |
| 376 | const xml::Text* child_text_keep = xml::NodeCast<xml::Text>(actual_el.children[0].get()); |
| 377 | ASSERT_THAT(child_text_keep, NotNull()); |
| 378 | EXPECT_THAT(child_text_keep->text, StrEq( " hello ")); |
| 379 | } |
| 380 | |
| 381 | TEST(ProtoSerializeTest, SerializeAndDeserializePrimitives) { |
| 382 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 383 | std::unique_ptr<ResourceTable> table = |
| 384 | test::ResourceTableBuilder() |
| 385 | .AddValue("android:bool/boolean_true", |
| 386 | test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, true)) |
| 387 | .AddValue("android:bool/boolean_false", |
| 388 | test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, false)) |
| 389 | .AddValue("android:color/color_rgb8", ResourceUtils::TryParseColor("#AABBCC")) |
| 390 | .AddValue("android:color/color_argb8", ResourceUtils::TryParseColor("#11223344")) |
| 391 | .AddValue("android:color/color_rgb4", ResourceUtils::TryParseColor("#DEF")) |
| 392 | .AddValue("android:color/color_argb4", ResourceUtils::TryParseColor("#5678")) |
| 393 | .AddValue("android:integer/integer_444", ResourceUtils::TryParseInt("444")) |
| 394 | .AddValue("android:integer/integer_neg_333", ResourceUtils::TryParseInt("-333")) |
| 395 | .AddValue("android:integer/hex_int_abcd", ResourceUtils::TryParseInt("0xABCD")) |
| 396 | .AddValue("android:dimen/dimen_1.39mm", ResourceUtils::TryParseFloat("1.39mm")) |
| 397 | .AddValue("android:fraction/fraction_27", ResourceUtils::TryParseFloat("27%")) |
| 398 | .AddValue("android:dimen/neg_2.3in", ResourceUtils::TryParseFloat("-2.3in")) |
| 399 | .AddValue("android:integer/null", ResourceUtils::MakeEmpty()) |
| 400 | .Build(); |
| 401 | |
| 402 | pb::ResourceTable pb_table; |
| 403 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics()); |
| 404 | |
| 405 | test::TestFile file_a("res/layout/main.xml"); |
| 406 | MockFileCollection files; |
| 407 | EXPECT_CALL(files, FindFile(Eq("res/layout/main.xml"))) |
| 408 | .WillRepeatedly(::testing::Return(&file_a)); |
| 409 | |
| 410 | ResourceTable new_table; |
| 411 | std::string error; |
| 412 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)); |
| 413 | EXPECT_THAT(error, IsEmpty()); |
| 414 | |
| 415 | BinaryPrimitive* bp = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 416 | &new_table, "android:bool/boolean_true", ConfigDescription::DefaultConfig(), ""); |
| 417 | ASSERT_THAT(bp, NotNull()); |
| 418 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_BOOLEAN)); |
| 419 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseBool("true")->value.data)); |
| 420 | |
| 421 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:bool/boolean_false", |
| 422 | ConfigDescription::DefaultConfig(), ""); |
| 423 | ASSERT_THAT(bp, NotNull()); |
| 424 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_BOOLEAN)); |
| 425 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseBool("false")->value.data)); |
| 426 | |
| 427 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:color/color_rgb8", |
| 428 | ConfigDescription::DefaultConfig(), ""); |
| 429 | ASSERT_THAT(bp, NotNull()); |
| 430 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_COLOR_RGB8)); |
| 431 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseColor("#AABBCC")->value.data)); |
| 432 | |
| 433 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:color/color_argb8", |
| 434 | ConfigDescription::DefaultConfig(), ""); |
| 435 | ASSERT_THAT(bp, NotNull()); |
| 436 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_COLOR_ARGB8)); |
| 437 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseColor("#11223344")->value.data)); |
| 438 | |
| 439 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:color/color_rgb4", |
| 440 | ConfigDescription::DefaultConfig(), ""); |
| 441 | ASSERT_THAT(bp, NotNull()); |
| 442 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_COLOR_RGB4)); |
| 443 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseColor("#DEF")->value.data)); |
| 444 | |
| 445 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:color/color_argb4", |
| 446 | ConfigDescription::DefaultConfig(), ""); |
| 447 | ASSERT_THAT(bp, NotNull()); |
| 448 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_COLOR_ARGB4)); |
| 449 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseColor("#5678")->value.data)); |
| 450 | |
| 451 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:integer/integer_444", |
| 452 | ConfigDescription::DefaultConfig(), ""); |
| 453 | ASSERT_THAT(bp, NotNull()); |
| 454 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_DEC)); |
| 455 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseInt("444")->value.data)); |
| 456 | |
| 457 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 458 | &new_table, "android:integer/integer_neg_333", ConfigDescription::DefaultConfig(), ""); |
| 459 | ASSERT_THAT(bp, NotNull()); |
| 460 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_DEC)); |
| 461 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseInt("-333")->value.data)); |
| 462 | |
| 463 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 464 | &new_table, "android:integer/hex_int_abcd", ConfigDescription::DefaultConfig(), ""); |
| 465 | ASSERT_THAT(bp, NotNull()); |
| 466 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_INT_HEX)); |
| 467 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseInt("0xABCD")->value.data)); |
| 468 | |
| 469 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:dimen/dimen_1.39mm", |
| 470 | ConfigDescription::DefaultConfig(), ""); |
| 471 | ASSERT_THAT(bp, NotNull()); |
| 472 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_DIMENSION)); |
| 473 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseFloat("1.39mm")->value.data)); |
| 474 | |
| 475 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>( |
| 476 | &new_table, "android:fraction/fraction_27", ConfigDescription::DefaultConfig(), ""); |
| 477 | ASSERT_THAT(bp, NotNull()); |
| 478 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_FRACTION)); |
| 479 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseFloat("27%")->value.data)); |
| 480 | |
| 481 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:dimen/neg_2.3in", |
| 482 | ConfigDescription::DefaultConfig(), ""); |
| 483 | ASSERT_THAT(bp, NotNull()); |
| 484 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_DIMENSION)); |
| 485 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseFloat("-2.3in")->value.data)); |
| 486 | |
| 487 | bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:integer/null", |
| 488 | ConfigDescription::DefaultConfig(), ""); |
| 489 | ASSERT_THAT(bp, NotNull()); |
| 490 | EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_NULL)); |
| 491 | EXPECT_THAT(bp->value.data, Eq(ResourceUtils::MakeEmpty()->value.data)); |
| 492 | } |
| 493 | |
| 494 | static void ExpectConfigSerializes(const StringPiece& config_str) { |
| 495 | const ConfigDescription expected_config = test::ParseConfigOrDie(config_str); |
| 496 | pb::Configuration pb_config; |
| 497 | SerializeConfig(expected_config, &pb_config); |
| 498 | |
| 499 | ConfigDescription actual_config; |
| 500 | std::string error; |
| 501 | ASSERT_TRUE(DeserializeConfigFromPb(pb_config, &actual_config, &error)); |
| 502 | ASSERT_THAT(error, IsEmpty()); |
| 503 | EXPECT_EQ(expected_config, actual_config); |
| 504 | } |
| 505 | |
| 506 | TEST(ProtoSerializeTest, SerializeDeserializeConfiguration) { |
| 507 | ExpectConfigSerializes(""); |
| 508 | |
| 509 | ExpectConfigSerializes("mcc123"); |
| 510 | |
| 511 | ExpectConfigSerializes("mnc123"); |
| 512 | |
| 513 | ExpectConfigSerializes("en"); |
| 514 | ExpectConfigSerializes("en-rGB"); |
| 515 | ExpectConfigSerializes("b+en+GB"); |
| 516 | |
| 517 | ExpectConfigSerializes("ldltr"); |
| 518 | ExpectConfigSerializes("ldrtl"); |
| 519 | |
| 520 | ExpectConfigSerializes("sw3600dp"); |
| 521 | |
| 522 | ExpectConfigSerializes("w300dp"); |
| 523 | |
| 524 | ExpectConfigSerializes("h400dp"); |
| 525 | |
| 526 | ExpectConfigSerializes("small"); |
| 527 | ExpectConfigSerializes("normal"); |
| 528 | ExpectConfigSerializes("large"); |
| 529 | ExpectConfigSerializes("xlarge"); |
| 530 | |
| 531 | ExpectConfigSerializes("long"); |
| 532 | ExpectConfigSerializes("notlong"); |
| 533 | |
| 534 | ExpectConfigSerializes("round"); |
| 535 | ExpectConfigSerializes("notround"); |
| 536 | |
| 537 | ExpectConfigSerializes("widecg"); |
| 538 | ExpectConfigSerializes("nowidecg"); |
| 539 | |
| 540 | ExpectConfigSerializes("highdr"); |
| 541 | ExpectConfigSerializes("lowdr"); |
| 542 | |
| 543 | ExpectConfigSerializes("port"); |
| 544 | ExpectConfigSerializes("land"); |
| 545 | ExpectConfigSerializes("square"); |
| 546 | |
| 547 | ExpectConfigSerializes("desk"); |
| 548 | ExpectConfigSerializes("car"); |
| 549 | ExpectConfigSerializes("television"); |
| 550 | ExpectConfigSerializes("appliance"); |
| 551 | ExpectConfigSerializes("watch"); |
| 552 | ExpectConfigSerializes("vrheadset"); |
| 553 | |
| 554 | ExpectConfigSerializes("night"); |
| 555 | ExpectConfigSerializes("notnight"); |
| 556 | |
| 557 | ExpectConfigSerializes("300dpi"); |
| 558 | ExpectConfigSerializes("hdpi"); |
| 559 | |
| 560 | ExpectConfigSerializes("notouch"); |
| 561 | ExpectConfigSerializes("stylus"); |
| 562 | ExpectConfigSerializes("finger"); |
| 563 | |
| 564 | ExpectConfigSerializes("keysexposed"); |
| 565 | ExpectConfigSerializes("keyshidden"); |
| 566 | ExpectConfigSerializes("keyssoft"); |
| 567 | |
| 568 | ExpectConfigSerializes("nokeys"); |
| 569 | ExpectConfigSerializes("qwerty"); |
| 570 | ExpectConfigSerializes("12key"); |
| 571 | |
| 572 | ExpectConfigSerializes("navhidden"); |
| 573 | ExpectConfigSerializes("navexposed"); |
| 574 | |
| 575 | ExpectConfigSerializes("nonav"); |
| 576 | ExpectConfigSerializes("dpad"); |
| 577 | ExpectConfigSerializes("trackball"); |
| 578 | ExpectConfigSerializes("wheel"); |
| 579 | |
| 580 | ExpectConfigSerializes("300x200"); |
| 581 | |
| 582 | ExpectConfigSerializes("v8"); |
| 583 | |
| 584 | ExpectConfigSerializes( |
| 585 | "mcc123-mnc456-b+en+GB-ldltr-sw300dp-w300dp-h400dp-large-long-round-widecg-highdr-land-car-" |
| 586 | "night-xhdpi-stylus-keysexposed-qwerty-navhidden-dpad-300x200-v23"); |
| 587 | } |
| 588 | |
| 589 | TEST(ProtoSerializeTest, SerializeAndDeserializeOverlayable) { |
| 590 | OverlayableItem overlayable_item_foo(std::make_shared<Overlayable>( |
| 591 | "CustomizableResources", "overlay://customization")); |
| 592 | overlayable_item_foo.policies |= PolicyFlags::SYSTEM_PARTITION; |
| 593 | overlayable_item_foo.policies |= PolicyFlags::PRODUCT_PARTITION; |
| 594 | |
| 595 | OverlayableItem overlayable_item_bar(std::make_shared<Overlayable>( |
| 596 | "TaskBar", "overlay://theme")); |
| 597 | overlayable_item_bar.policies |= PolicyFlags::PUBLIC; |
| 598 | overlayable_item_bar.policies |= PolicyFlags::VENDOR_PARTITION; |
| 599 | |
| 600 | OverlayableItem overlayable_item_baz(std::make_shared<Overlayable>( |
| 601 | "FontPack", "overlay://theme")); |
| 602 | overlayable_item_baz.policies |= PolicyFlags::PUBLIC; |
| 603 | |
| 604 | OverlayableItem overlayable_item_boz(std::make_shared<Overlayable>( |
| 605 | "IconPack", "overlay://theme")); |
| 606 | overlayable_item_boz.policies |= PolicyFlags::SIGNATURE; |
| 607 | overlayable_item_boz.policies |= PolicyFlags::ODM_PARTITION; |
| 608 | overlayable_item_boz.policies |= PolicyFlags::OEM_PARTITION; |
| 609 | |
| 610 | OverlayableItem overlayable_item_actor_config(std::make_shared<Overlayable>( |
| 611 | "ActorConfig", "overlay://theme")); |
| 612 | overlayable_item_actor_config.policies |= PolicyFlags::SIGNATURE; |
| 613 | overlayable_item_actor_config.policies |= PolicyFlags::ACTOR_SIGNATURE; |
| 614 | |
| 615 | OverlayableItem overlayable_item_biz(std::make_shared<Overlayable>( |
| 616 | "Other", "overlay://customization")); |
| 617 | overlayable_item_biz.comment ="comment"; |
| 618 | |
| 619 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 620 | std::unique_ptr<ResourceTable> table = |
| 621 | test::ResourceTableBuilder() |
| 622 | .SetOverlayable("com.app.a:bool/foo", overlayable_item_foo) |
| 623 | .SetOverlayable("com.app.a:bool/bar", overlayable_item_bar) |
| 624 | .SetOverlayable("com.app.a:bool/baz", overlayable_item_baz) |
| 625 | .SetOverlayable("com.app.a:bool/boz", overlayable_item_boz) |
| 626 | .SetOverlayable("com.app.a:bool/biz", overlayable_item_biz) |
| 627 | .SetOverlayable("com.app.a:bool/actor_config", overlayable_item_actor_config) |
| 628 | .AddValue("com.app.a:bool/fiz", ResourceUtils::TryParseBool("true")) |
| 629 | .Build(); |
| 630 | |
| 631 | pb::ResourceTable pb_table; |
| 632 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics()); |
| 633 | |
| 634 | MockFileCollection files; |
| 635 | ResourceTable new_table; |
| 636 | std::string error; |
| 637 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)); |
| 638 | EXPECT_THAT(error, IsEmpty()); |
| 639 | |
| 640 | std::optional<ResourceTable::SearchResult> search_result = |
| 641 | new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/foo")); |
| 642 | ASSERT_TRUE(search_result); |
| 643 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 644 | OverlayableItem& overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 645 | EXPECT_THAT(overlayable_item.overlayable->name, Eq("CustomizableResources")); |
| 646 | EXPECT_THAT(overlayable_item.overlayable->actor, Eq("overlay://customization")); |
| 647 | EXPECT_THAT(overlayable_item.policies, Eq(PolicyFlags::SYSTEM_PARTITION |
| 648 | | PolicyFlags::PRODUCT_PARTITION)); |
| 649 | |
| 650 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/bar")); |
| 651 | ASSERT_TRUE(search_result); |
| 652 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 653 | overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 654 | EXPECT_THAT(overlayable_item.overlayable->name, Eq("TaskBar")); |
| 655 | EXPECT_THAT(overlayable_item.overlayable->actor, Eq("overlay://theme")); |
| 656 | EXPECT_THAT(overlayable_item.policies, Eq(PolicyFlags::PUBLIC |
| 657 | | PolicyFlags::VENDOR_PARTITION)); |
| 658 | |
| 659 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/baz")); |
| 660 | ASSERT_TRUE(search_result); |
| 661 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 662 | overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 663 | EXPECT_THAT(overlayable_item.overlayable->name, Eq("FontPack")); |
| 664 | EXPECT_THAT(overlayable_item.overlayable->actor, Eq("overlay://theme")); |
| 665 | EXPECT_THAT(overlayable_item.policies, Eq(PolicyFlags::PUBLIC)); |
| 666 | |
| 667 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/boz")); |
| 668 | ASSERT_TRUE(search_result); |
| 669 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 670 | overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 671 | EXPECT_THAT(overlayable_item.overlayable->name, Eq("IconPack")); |
| 672 | EXPECT_THAT(overlayable_item.overlayable->actor, Eq("overlay://theme")); |
| 673 | EXPECT_THAT(overlayable_item.policies, Eq(PolicyFlags::SIGNATURE |
| 674 | | PolicyFlags::ODM_PARTITION |
| 675 | | PolicyFlags::OEM_PARTITION)); |
| 676 | |
| 677 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/actor_config")); |
| 678 | ASSERT_TRUE(search_result); |
| 679 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 680 | overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 681 | EXPECT_THAT(overlayable_item.overlayable->name, Eq("ActorConfig")); |
| 682 | EXPECT_THAT(overlayable_item.overlayable->actor, Eq("overlay://theme")); |
| 683 | EXPECT_THAT(overlayable_item.policies, Eq(PolicyFlags::SIGNATURE |
| 684 | | PolicyFlags::ACTOR_SIGNATURE)); |
| 685 | |
| 686 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/biz")); |
| 687 | ASSERT_TRUE(search_result); |
| 688 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 689 | overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 690 | EXPECT_THAT(overlayable_item.overlayable->name, Eq("Other")); |
| 691 | EXPECT_THAT(overlayable_item.policies, Eq(PolicyFlags::NONE)); |
| 692 | EXPECT_THAT(overlayable_item.comment, Eq("comment")); |
| 693 | |
| 694 | search_result = new_table.FindResource(test::ParseNameOrDie("com.app.a:bool/fiz")); |
| 695 | ASSERT_TRUE(search_result); |
| 696 | ASSERT_FALSE(search_result.value().entry->overlayable_item); |
| 697 | } |
| 698 | |
| 699 | TEST(ProtoSerializeTest, SerializeAndDeserializeDynamicReference) { |
| 700 | Reference ref(ResourceId(0x00010001)); |
| 701 | ref.is_dynamic = true; |
| 702 | |
| 703 | pb::Item pb_item; |
| 704 | SerializeItemToPb(ref, &pb_item); |
| 705 | |
| 706 | ASSERT_TRUE(pb_item.has_ref()); |
| 707 | EXPECT_EQ(pb_item.ref().id(), ref.id.value().id); |
| 708 | EXPECT_TRUE(pb_item.ref().is_dynamic().value()); |
| 709 | |
| 710 | std::unique_ptr<Item> item = DeserializeItemFromPb(pb_item, android::ResStringPool(), |
| 711 | android::ConfigDescription(), nullptr, |
| 712 | nullptr, nullptr); |
| 713 | Reference* actual_ref = ValueCast<Reference>(item.get()); |
| 714 | EXPECT_EQ(actual_ref->id.value().id, ref.id.value().id); |
| 715 | EXPECT_TRUE(actual_ref->is_dynamic); |
| 716 | } |
| 717 | |
| 718 | TEST(ProtoSerializeTest, SerializeAndDeserializeNonDynamicReference) { |
| 719 | Reference ref(ResourceId(0x00010001)); |
| 720 | |
| 721 | pb::Item pb_item; |
| 722 | SerializeItemToPb(ref, &pb_item); |
| 723 | |
| 724 | ASSERT_TRUE(pb_item.has_ref()); |
| 725 | EXPECT_EQ(pb_item.ref().id(), ref.id.value().id); |
| 726 | EXPECT_FALSE(pb_item.ref().has_is_dynamic()); |
| 727 | |
| 728 | std::unique_ptr<Item> item = DeserializeItemFromPb(pb_item, android::ResStringPool(), |
| 729 | android::ConfigDescription(), nullptr, |
| 730 | nullptr, nullptr); |
| 731 | Reference* actual_ref = ValueCast<Reference>(item.get()); |
| 732 | EXPECT_EQ(actual_ref->id.value().id, ref.id.value().id); |
| 733 | EXPECT_FALSE(actual_ref->is_dynamic); |
| 734 | } |
| 735 | |
| 736 | TEST(ProtoSerializeTest, CollapsingResourceNamesNoNameCollapseExemptionsSucceeds) { |
| 737 | const uint32_t id_one_id = 0x7f020000; |
| 738 | const uint32_t id_two_id = 0x7f020001; |
| 739 | const uint32_t id_three_id = 0x7f020002; |
| 740 | const uint32_t integer_three_id = 0x7f030000; |
| 741 | const uint32_t string_test_id = 0x7f040000; |
| 742 | const uint32_t layout_bar_id = 0x7f050000; |
| 743 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 744 | std::unique_ptr<ResourceTable> table = |
| 745 | test::ResourceTableBuilder() |
| 746 | .AddSimple("com.app.test:id/one", ResourceId(id_one_id)) |
| 747 | .AddSimple("com.app.test:id/two", ResourceId(id_two_id)) |
| 748 | .AddValue("com.app.test:id/three", ResourceId(id_three_id), |
| 749 | test::BuildReference("com.app.test:id/one", ResourceId(id_one_id))) |
| 750 | .AddValue("com.app.test:integer/one", ResourceId(integer_three_id), |
| 751 | util::make_unique<BinaryPrimitive>( |
| 752 | uint8_t(android::Res_value::TYPE_INT_DEC), 1u)) |
| 753 | .AddValue("com.app.test:integer/one", test::ParseConfigOrDie("v1"), |
| 754 | ResourceId(integer_three_id), |
| 755 | util::make_unique<BinaryPrimitive>( |
| 756 | uint8_t(android::Res_value::TYPE_INT_DEC), 2u)) |
| 757 | .AddString("com.app.test:string/test", ResourceId(string_test_id), "foo") |
| 758 | .AddFileReference("com.app.test:layout/bar", ResourceId(layout_bar_id), |
| 759 | "res/layout/bar.xml") |
| 760 | .Build(); |
| 761 | |
| 762 | SerializeTableOptions options; |
| 763 | options.collapse_key_stringpool = true; |
| 764 | |
| 765 | pb::ResourceTable pb_table; |
| 766 | |
| 767 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics(), options); |
| 768 | test::TestFile file_a("res/layout/bar.xml"); |
| 769 | MockFileCollection files; |
| 770 | EXPECT_CALL(files, FindFile(Eq("res/layout/bar.xml"))) |
| 771 | .WillRepeatedly(::testing::Return(&file_a)); |
| 772 | ResourceTable new_table; |
| 773 | std::string error; |
| 774 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)) << error; |
| 775 | EXPECT_THAT(error, IsEmpty()); |
| 776 | |
| 777 | ResourceName real_id_resource( |
| 778 | "com.app.test", ResourceType::kId, "one"); |
| 779 | EXPECT_THAT(GetEntry(&new_table, real_id_resource), IsNull()); |
| 780 | |
| 781 | ResourceName obfuscated_id_resource( |
| 782 | "com.app.test", ResourceType::kId, "0_resource_name_obfuscated"); |
| 783 | |
| 784 | EXPECT_THAT(GetEntry(&new_table, obfuscated_id_resource, |
| 785 | id_one_id), NotNull()); |
| 786 | EXPECT_THAT(GetEntry(&new_table, obfuscated_id_resource, |
| 787 | id_two_id), NotNull()); |
| 788 | ResourceEntry* entry = GetEntry(&new_table, obfuscated_id_resource, id_three_id); |
| 789 | EXPECT_THAT(entry, NotNull()); |
| 790 | ResourceConfigValue* config_value = entry->FindValue({}); |
| 791 | Reference* ref = ValueCast<Reference>(config_value->value.get()); |
| 792 | EXPECT_THAT(ref->id.value(), Eq(id_one_id)); |
| 793 | |
| 794 | ResourceName obfuscated_integer_resource( |
| 795 | "com.app.test", ResourceType::kInteger, "0_resource_name_obfuscated"); |
| 796 | entry = GetEntry(&new_table, obfuscated_integer_resource, integer_three_id); |
| 797 | EXPECT_THAT(entry, NotNull()); |
| 798 | config_value = entry->FindValue({}); |
| 799 | BinaryPrimitive* bp = ValueCast<BinaryPrimitive>(config_value->value.get()); |
| 800 | EXPECT_THAT(bp->value.data, Eq(1u)); |
| 801 | |
| 802 | config_value = entry->FindValue(test::ParseConfigOrDie("v1")); |
| 803 | bp = ValueCast<BinaryPrimitive>(config_value->value.get()); |
| 804 | EXPECT_THAT(bp->value.data, Eq(2u)); |
| 805 | |
| 806 | ResourceName obfuscated_string_resource( |
| 807 | "com.app.test", ResourceType::kString, "0_resource_name_obfuscated"); |
| 808 | entry = GetEntry(&new_table, obfuscated_string_resource, string_test_id); |
| 809 | EXPECT_THAT(entry, NotNull()); |
| 810 | config_value = entry->FindValue({}); |
| 811 | String* s = ValueCast<String>(config_value->value.get()); |
| 812 | EXPECT_THAT(*(s->value), Eq("foo")); |
| 813 | |
| 814 | ResourceName obfuscated_layout_resource( |
| 815 | "com.app.test", ResourceType::kLayout, "0_resource_name_obfuscated"); |
| 816 | entry = GetEntry(&new_table, obfuscated_layout_resource, layout_bar_id); |
| 817 | EXPECT_THAT(entry, NotNull()); |
| 818 | config_value = entry->FindValue({}); |
| 819 | FileReference* f = ValueCast<FileReference>(config_value->value.get()); |
| 820 | EXPECT_THAT(*(f->path), Eq("res/layout/bar.xml")); |
| 821 | } |
| 822 | |
| 823 | TEST(ProtoSerializeTest, ObfuscatingResourceNamesWithNameCollapseExemptionsSucceeds) { |
| 824 | const uint32_t id_one_id = 0x7f020000; |
| 825 | const uint32_t id_two_id = 0x7f020001; |
| 826 | const uint32_t id_three_id = 0x7f020002; |
| 827 | const uint32_t integer_three_id = 0x7f030000; |
| 828 | const uint32_t string_test_id = 0x7f040000; |
| 829 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 830 | std::unique_ptr<ResourceTable> table = |
| 831 | test::ResourceTableBuilder() |
| 832 | .AddSimple("com.app.test:id/one", ResourceId(id_one_id)) |
| 833 | .AddSimple("com.app.test:id/two", ResourceId(id_two_id)) |
| 834 | .AddValue("com.app.test:id/three", ResourceId(id_three_id), |
| 835 | test::BuildReference("com.app.test:id/one", ResourceId(id_one_id))) |
| 836 | .AddValue("com.app.test:integer/one", ResourceId(integer_three_id), |
| 837 | util::make_unique<BinaryPrimitive>( |
| 838 | uint8_t(android::Res_value::TYPE_INT_DEC), 1u)) |
| 839 | .AddValue("com.app.test:integer/one", test::ParseConfigOrDie("v1"), |
| 840 | ResourceId(integer_three_id), |
| 841 | util::make_unique<BinaryPrimitive>( |
| 842 | uint8_t(android::Res_value::TYPE_INT_DEC), 2u)) |
| 843 | .AddString("com.app.test:string/test", ResourceId(string_test_id), "foo") |
| 844 | .Build(); |
| 845 | |
| 846 | SerializeTableOptions options; |
| 847 | options.collapse_key_stringpool = true; |
| 848 | options.name_collapse_exemptions.insert(ResourceName({}, ResourceType::kId, "one")); |
| 849 | options.name_collapse_exemptions.insert(ResourceName({}, ResourceType::kString, "test")); |
| 850 | pb::ResourceTable pb_table; |
| 851 | |
| 852 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics(), options); |
| 853 | MockFileCollection files; |
| 854 | ResourceTable new_table; |
| 855 | std::string error; |
| 856 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)) << error; |
| 857 | EXPECT_THAT(error, IsEmpty()); |
| 858 | |
| 859 | EXPECT_THAT(GetEntry(&new_table, ResourceName("com.app.test", ResourceType::kId, "one"), |
| 860 | id_one_id), NotNull()); |
| 861 | ResourceName obfuscated_id_resource( |
| 862 | "com.app.test", ResourceType::kId, "0_resource_name_obfuscated"); |
| 863 | EXPECT_THAT(GetEntry(&new_table, obfuscated_id_resource, id_one_id), IsNull()); |
| 864 | |
| 865 | ResourceName real_id_resource( |
| 866 | "com.app.test", ResourceType::kId, "two"); |
| 867 | EXPECT_THAT(GetEntry(&new_table, real_id_resource, id_two_id), IsNull()); |
| 868 | EXPECT_THAT(GetEntry(&new_table, obfuscated_id_resource, id_two_id), NotNull()); |
| 869 | |
| 870 | ResourceEntry* entry = GetEntry(&new_table, obfuscated_id_resource, id_three_id); |
| 871 | EXPECT_THAT(entry, NotNull()); |
| 872 | ResourceConfigValue* config_value = entry->FindValue({}); |
| 873 | Reference* ref = ValueCast<Reference>(config_value->value.get()); |
| 874 | EXPECT_THAT(ref->id.value(), Eq(id_one_id)); |
| 875 | |
| 876 | // Note that this resource is also named "one", but it's a different type, so gets obfuscated. |
| 877 | ResourceName obfuscated_integer_resource( |
| 878 | "com.app.test", ResourceType::kInteger, "0_resource_name_obfuscated"); |
| 879 | entry = GetEntry(&new_table, obfuscated_integer_resource, integer_three_id); |
| 880 | EXPECT_THAT(entry, NotNull()); |
| 881 | config_value = entry->FindValue({}); |
| 882 | BinaryPrimitive* bp = ValueCast<BinaryPrimitive>(config_value->value.get()); |
| 883 | EXPECT_THAT(bp->value.data, Eq(1u)); |
| 884 | |
| 885 | config_value = entry->FindValue(test::ParseConfigOrDie("v1")); |
| 886 | bp = ValueCast<BinaryPrimitive>(config_value->value.get()); |
| 887 | EXPECT_THAT(bp->value.data, Eq(2u)); |
| 888 | |
| 889 | entry = GetEntry(&new_table, ResourceName("com.app.test", ResourceType::kString, "test"), |
| 890 | string_test_id); |
| 891 | EXPECT_THAT(entry, NotNull()); |
| 892 | config_value = entry->FindValue({}); |
| 893 | String* s = ValueCast<String>(config_value->value.get()); |
| 894 | EXPECT_THAT(*(s->value), Eq("foo")); |
| 895 | } |
| 896 | |
| 897 | TEST(ProtoSerializeTest, SerializeMacro) { |
| 898 | auto original = std::make_unique<Macro>(); |
| 899 | original->raw_value = "\nThis being human is a guest house."; |
| 900 | original->style_string.str = " This being human is a guest house."; |
| 901 | original->style_string.spans.emplace_back(Span{.name = "b", .first_char = 12, .last_char = 16}); |
| 902 | original->untranslatable_sections.emplace_back(UntranslatableSection{.start = 12, .end = 17}); |
| 903 | original->alias_namespaces.emplace_back( |
| 904 | Macro::Namespace{.alias = "prefix", .package_name = "package.name", .is_private = true}); |
| 905 | |
| 906 | CloningValueTransformer cloner(nullptr); |
| 907 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 908 | std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() |
| 909 | .Add(NewResourceBuilder("com.app.a:macro/foo") |
| 910 | .SetValue(original->Transform(cloner)) |
| 911 | .Build()) |
| 912 | .Build(); |
| 913 | |
| 914 | ResourceTable new_table; |
| 915 | pb::ResourceTable pb_table; |
| 916 | MockFileCollection files; |
| 917 | std::string error; |
| 918 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics()); |
| 919 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)); |
| 920 | EXPECT_THAT(error, IsEmpty()); |
| 921 | |
| 922 | Macro* deserialized = test::GetValue<Macro>(&new_table, "com.app.a:macro/foo"); |
| 923 | ASSERT_THAT(deserialized, NotNull()); |
| 924 | EXPECT_THAT(deserialized->raw_value, Eq(original->raw_value)); |
| 925 | EXPECT_THAT(deserialized->style_string.str, Eq(original->style_string.str)); |
| 926 | EXPECT_THAT(deserialized->style_string.spans, Eq(original->style_string.spans)); |
| 927 | EXPECT_THAT(deserialized->untranslatable_sections, Eq(original->untranslatable_sections)); |
| 928 | EXPECT_THAT(deserialized->alias_namespaces, Eq(original->alias_namespaces)); |
| 929 | } |
| 930 | |
| 931 | TEST(ProtoSerializeTest, StagedId) { |
| 932 | CloningValueTransformer cloner(nullptr); |
| 933 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 934 | std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() |
| 935 | .Add(NewResourceBuilder("com.app.a:string/foo") |
| 936 | .SetStagedId(StagedId{.id = 0x01ff0001}) |
| 937 | .Build()) |
| 938 | .Build(); |
| 939 | |
| 940 | ResourceTable new_table; |
| 941 | pb::ResourceTable pb_table; |
| 942 | MockFileCollection files; |
| 943 | std::string error; |
| 944 | SerializeTableToPb(*table, &pb_table, context->GetDiagnostics()); |
| 945 | ASSERT_TRUE(DeserializeTableFromPb(pb_table, &files, &new_table, &error)); |
| 946 | EXPECT_THAT(error, IsEmpty()); |
| 947 | |
| 948 | auto result = new_table.FindResource(test::ParseNameOrDie("com.app.a:string/foo")); |
| 949 | ASSERT_TRUE(result); |
| 950 | ASSERT_TRUE(result.value().entry->staged_id); |
| 951 | EXPECT_THAT(result.value().entry->staged_id.value().id, Eq(ResourceId(0x01ff0001))); |
| 952 | } |
| 953 | |
| 954 | } // namespace aapt |