Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 1 | /* |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 17 | #include "androidfw/AttributeFinder.h" |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 19 | #include "android-base/macros.h" |
| 20 | #include "gtest/gtest.h" |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 21 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 22 | namespace android { |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 23 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 24 | class MockAttributeFinder |
| 25 | : public BackTrackingAttributeFinder<MockAttributeFinder, int> { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 26 | public: |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 27 | MockAttributeFinder(const uint32_t* attrs, int len) |
| 28 | : BackTrackingAttributeFinder(0, len) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 29 | attrs_ = new uint32_t[len]; |
| 30 | memcpy(attrs_, attrs, sizeof(*attrs) * len); |
| 31 | } |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 33 | ~MockAttributeFinder() { delete attrs_; } |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 35 | inline uint32_t GetAttribute(const int index) const { return attrs_[index]; } |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 37 | private: |
| 38 | uint32_t* attrs_; |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 41 | static const uint32_t kSortedAttributes[] = {0x01010000, 0x01010001, 0x01010002, |
| 42 | 0x01010004, 0x02010001, 0x02010010, |
| 43 | 0x7f010001}; |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 45 | static const uint32_t kPackageUnsortedAttributes[] = { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 46 | 0x02010001, 0x02010010, 0x01010000, 0x01010001, |
| 47 | 0x01010002, 0x01010004, 0x7f010001}; |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 49 | static const uint32_t kSinglePackageAttributes[] = {0x7f010007, 0x7f01000a, |
| 50 | 0x7f01000d, 0x00000000}; |
Adam Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 51 | |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 52 | TEST(AttributeFinderTest, IteratesSequentially) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 53 | const int end = arraysize(kSortedAttributes); |
| 54 | MockAttributeFinder finder(kSortedAttributes, end); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 56 | EXPECT_EQ(0, finder.Find(0x01010000)); |
| 57 | EXPECT_EQ(1, finder.Find(0x01010001)); |
| 58 | EXPECT_EQ(2, finder.Find(0x01010002)); |
| 59 | EXPECT_EQ(3, finder.Find(0x01010004)); |
| 60 | EXPECT_EQ(4, finder.Find(0x02010001)); |
| 61 | EXPECT_EQ(5, finder.Find(0x02010010)); |
| 62 | EXPECT_EQ(6, finder.Find(0x7f010001)); |
| 63 | EXPECT_EQ(end, finder.Find(0x7f010002)); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(AttributeFinderTest, PackagesAreOutOfOrder) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 67 | const int end = arraysize(kSortedAttributes); |
| 68 | MockAttributeFinder finder(kSortedAttributes, end); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 70 | EXPECT_EQ(6, finder.Find(0x7f010001)); |
| 71 | EXPECT_EQ(end, finder.Find(0x7f010002)); |
| 72 | EXPECT_EQ(4, finder.Find(0x02010001)); |
| 73 | EXPECT_EQ(5, finder.Find(0x02010010)); |
| 74 | EXPECT_EQ(0, finder.Find(0x01010000)); |
| 75 | EXPECT_EQ(1, finder.Find(0x01010001)); |
| 76 | EXPECT_EQ(2, finder.Find(0x01010002)); |
| 77 | EXPECT_EQ(3, finder.Find(0x01010004)); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(AttributeFinderTest, SomeAttributesAreNotFound) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 81 | const int end = arraysize(kSortedAttributes); |
| 82 | MockAttributeFinder finder(kSortedAttributes, end); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 84 | EXPECT_EQ(0, finder.Find(0x01010000)); |
| 85 | EXPECT_EQ(1, finder.Find(0x01010001)); |
| 86 | EXPECT_EQ(2, finder.Find(0x01010002)); |
| 87 | EXPECT_EQ(end, finder.Find(0x01010003)); |
| 88 | EXPECT_EQ(3, finder.Find(0x01010004)); |
| 89 | EXPECT_EQ(end, finder.Find(0x01010005)); |
| 90 | EXPECT_EQ(end, finder.Find(0x01010006)); |
| 91 | EXPECT_EQ(4, finder.Find(0x02010001)); |
| 92 | EXPECT_EQ(end, finder.Find(0x02010002)); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TEST(AttributeFinderTest, FindAttributesInPackageUnsortedAttributeList) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 96 | const int end = arraysize(kPackageUnsortedAttributes); |
| 97 | MockAttributeFinder finder(kPackageUnsortedAttributes, end); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 98 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 99 | EXPECT_EQ(2, finder.Find(0x01010000)); |
| 100 | EXPECT_EQ(3, finder.Find(0x01010001)); |
| 101 | EXPECT_EQ(4, finder.Find(0x01010002)); |
| 102 | EXPECT_EQ(end, finder.Find(0x01010003)); |
| 103 | EXPECT_EQ(5, finder.Find(0x01010004)); |
| 104 | EXPECT_EQ(end, finder.Find(0x01010005)); |
| 105 | EXPECT_EQ(end, finder.Find(0x01010006)); |
| 106 | EXPECT_EQ(0, finder.Find(0x02010001)); |
| 107 | EXPECT_EQ(end, finder.Find(0x02010002)); |
| 108 | EXPECT_EQ(1, finder.Find(0x02010010)); |
| 109 | EXPECT_EQ(6, finder.Find(0x7f010001)); |
Adam Lesinski | a7d1d73 | 2014-10-01 18:24:54 -0700 | [diff] [blame] | 110 | } |
Adam Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 111 | |
| 112 | TEST(AttributeFinderTest, FindAttributesInSinglePackageAttributeList) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 113 | const int end = arraysize(kSinglePackageAttributes); |
| 114 | MockAttributeFinder finder(kSinglePackageAttributes, end); |
Adam Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 115 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 116 | EXPECT_EQ(end, finder.Find(0x010100f4)); |
| 117 | EXPECT_EQ(end, finder.Find(0x010100f5)); |
| 118 | EXPECT_EQ(end, finder.Find(0x010100f6)); |
| 119 | EXPECT_EQ(end, finder.Find(0x010100f7)); |
| 120 | EXPECT_EQ(end, finder.Find(0x010100f8)); |
| 121 | EXPECT_EQ(end, finder.Find(0x010100fa)); |
| 122 | EXPECT_EQ(0, finder.Find(0x7f010007)); |
Adam Lesinski | 5dce5e6 | 2014-12-10 10:47:53 -0800 | [diff] [blame] | 123 | } |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 124 | |
| 125 | } // namespace android |