David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 15 | #include "packages/UidMap.h" |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 16 | #include "config/ConfigKey.h" |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 17 | |
| 18 | #include <gtest/gtest.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 19 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | |
| 22 | using namespace android; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 27 | |
| 28 | #ifdef __ANDROID__ |
| 29 | const string kApp1 = "app1.sharing.1"; |
| 30 | const string kApp2 = "app2.sharing.1"; |
| 31 | |
| 32 | TEST(UidMapTest, TestMatching) { |
| 33 | UidMap m; |
| 34 | vector<int32_t> uids; |
| 35 | vector<int32_t> versions; |
| 36 | vector<String16> apps; |
| 37 | |
| 38 | uids.push_back(1000); |
| 39 | uids.push_back(1000); |
| 40 | apps.push_back(String16(kApp1.c_str())); |
| 41 | apps.push_back(String16(kApp2.c_str())); |
| 42 | versions.push_back(4); |
| 43 | versions.push_back(5); |
| 44 | m.updateMap(uids, versions, apps); |
| 45 | EXPECT_TRUE(m.hasApp(1000, kApp1)); |
| 46 | EXPECT_TRUE(m.hasApp(1000, kApp2)); |
| 47 | EXPECT_FALSE(m.hasApp(1000, "not.app")); |
| 48 | } |
| 49 | |
| 50 | TEST(UidMapTest, TestAddAndRemove) { |
| 51 | UidMap m; |
| 52 | vector<int32_t> uids; |
| 53 | vector<int32_t> versions; |
| 54 | vector<String16> apps; |
| 55 | |
| 56 | uids.push_back(1000); |
| 57 | uids.push_back(1000); |
| 58 | apps.push_back(String16(kApp1.c_str())); |
| 59 | apps.push_back(String16(kApp2.c_str())); |
| 60 | versions.push_back(4); |
| 61 | versions.push_back(5); |
| 62 | m.updateMap(uids, versions, apps); |
| 63 | |
| 64 | m.updateApp(String16(kApp1.c_str()), 1000, 40); |
| 65 | EXPECT_EQ(40, m.getAppVersion(1000, kApp1)); |
| 66 | |
| 67 | m.removeApp(String16(kApp1.c_str()), 1000); |
| 68 | EXPECT_FALSE(m.hasApp(1000, kApp1)); |
| 69 | EXPECT_TRUE(m.hasApp(1000, kApp2)); |
| 70 | } |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 71 | |
| 72 | TEST(UidMapTest, TestClearingOutput) { |
| 73 | UidMap m; |
| 74 | |
| 75 | ConfigKey config1(1, "config1"); |
| 76 | ConfigKey config2(1, "config2"); |
| 77 | |
| 78 | m.OnConfigUpdated(config1); |
| 79 | |
| 80 | vector<int32_t> uids; |
| 81 | vector<int32_t> versions; |
| 82 | vector<String16> apps; |
| 83 | uids.push_back(1000); |
| 84 | uids.push_back(1000); |
| 85 | apps.push_back(String16(kApp1.c_str())); |
| 86 | apps.push_back(String16(kApp2.c_str())); |
| 87 | versions.push_back(4); |
| 88 | versions.push_back(5); |
| 89 | m.updateMap(1, uids, versions, apps); |
| 90 | |
| 91 | UidMapping results = m.getOutput(2, config1); |
| 92 | EXPECT_EQ(1, results.snapshots_size()); |
| 93 | |
| 94 | // It should be cleared now |
| 95 | results = m.getOutput(3, config1); |
| 96 | EXPECT_EQ(0, results.snapshots_size()); |
| 97 | |
| 98 | // Now add another configuration. |
| 99 | m.OnConfigUpdated(config2); |
| 100 | m.updateApp(5, String16(kApp1.c_str()), 1000, 40); |
| 101 | results = m.getOutput(6, config1); |
| 102 | EXPECT_EQ(0, results.snapshots_size()); |
| 103 | EXPECT_EQ(1, results.changes_size()); |
| 104 | |
| 105 | // Now we still haven't been able to delete anything |
| 106 | m.updateApp(7, String16(kApp2.c_str()), 1001, 41); |
| 107 | results = m.getOutput(8, config1); |
| 108 | EXPECT_EQ(0, results.snapshots_size()); |
| 109 | EXPECT_EQ(2, results.changes_size()); |
| 110 | |
| 111 | results = m.getOutput(9, config2); |
| 112 | EXPECT_EQ(0, results.snapshots_size()); |
| 113 | EXPECT_EQ(2, results.changes_size()); |
| 114 | // At this point both should be cleared. |
| 115 | EXPECT_EQ(0, m.mOutput.snapshots_size()); |
| 116 | EXPECT_EQ(0, m.mOutput.changes_size()); |
| 117 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 118 | #else |
| 119 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 120 | #endif |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 121 | |
| 122 | } // namespace statsd |
| 123 | } // namespace os |
| 124 | } // namespace android |