1 /*
2 * Copyright (C) 2024 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 <android/content/AttributionSourceState.h>
18 #include <media/AttrSourceIter.h>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include <algorithm>
24
25 using ::android::content::AttributionSourceState;
26 using ::android::media::permission::AttrSourceIter::begin;
27 using ::android::media::permission::AttrSourceIter::cbegin;
28 using ::android::media::permission::AttrSourceIter::cend;
29 using ::android::media::permission::AttrSourceIter::end;
30
31 using ::android::media::permission::AttrSourceIter::ConstIter;
32
33 using ::testing::ContainerEq;
34 using ::testing::ElementsAreArray;
35 using ::testing::Eq;
36 using ::testing::Return;
37
38 class AttrSourceIterTest : public ::testing::Test {
39 public:
AttrSourceIterTest()40 AttrSourceIterTest() {
41 mAttr.pid = 1;
42 mAttr.uid = 1;
43 AttributionSourceState next;
44 next.pid = 2;
45 next.uid = 2;
46 AttributionSourceState nextnext;
47 nextnext.pid = 3;
48 nextnext.uid = 3;
49 next.next = {nextnext};
50 mAttr.next = {next};
51 }
52
53 protected:
54 AttributionSourceState mAttr;
55 };
56
TEST_F(AttrSourceIterTest,constIter)57 TEST_F(AttrSourceIterTest, constIter) {
58 const AttributionSourceState& ref = mAttr;
59 std::vector<int> mPids;
60 std::transform(cbegin(ref), cend(), std::back_inserter(mPids),
61 [](const auto& x) { return x.pid; });
62 EXPECT_THAT(mPids, ElementsAreArray({1, 2, 3}));
63 }
64
TEST_F(AttrSourceIterTest,nonConstIter)65 TEST_F(AttrSourceIterTest, nonConstIter) {
66 AttributionSourceState expected;
67 {
68 expected.pid = 2;
69 expected.uid = 1;
70 AttributionSourceState expectedNext;
71 expectedNext.pid = 4;
72 expectedNext.uid = 2;
73 AttributionSourceState expectedNextNext;
74 expectedNextNext.pid = 6;
75 expectedNextNext.uid = 3;
76 expectedNext.next = {expectedNextNext};
77 expected.next = {expectedNext};
78 }
79 std::for_each(begin(mAttr), end(), [](auto& x) { x.pid = x.pid * 2; });
80
81 EXPECT_THAT(mAttr, Eq(expected));
82 }
83
TEST_F(AttrSourceIterTest,nonConstIterReferenceEquals)84 TEST_F(AttrSourceIterTest, nonConstIterReferenceEquals) {
85 const AttributionSourceState& ref = mAttr;
86 std::vector<const AttributionSourceState*> attrs;
87 std::transform(cbegin(ref), cend(), std::back_inserter(attrs),
88 [](const auto& x) { return &x; });
89 std::for_each(begin(mAttr), end(), [](auto& x) { x.pid = x.pid * 2; });
90 std::vector<const AttributionSourceState*> attrsAfter;
91 std::transform(cbegin(ref), cend(), std::back_inserter(attrsAfter),
92 [](const auto& x) { return &x; });
93 EXPECT_THAT(attrs, ContainerEq(attrsAfter));
94 }
95