// Copyright (C) 2020 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "subscriber/SubscriberReporter.h" #include #include #include #include "tests/statsd_test_util.h" using namespace testing; using ::ndk::SharedRefBase; using std::unordered_map; #ifdef __ANDROID__ namespace android { namespace os { namespace statsd { namespace { const ConfigKey configKey1(0, 12345); const ConfigKey configKey2(0, 123456); const int64_t subscriptionId1 = 1, subscriptionId2 = 2; } // anonymous namespace class SubscriberReporterTest : public ::testing::Test { public: SubscriberReporterTest() { } const shared_ptr pir1 = SharedRefBase::make>(); const shared_ptr pir2 = SharedRefBase::make>(); const shared_ptr pir3 = SharedRefBase::make>(); // Two subscribers on config key 1, one on config key 2. void SetUp() override { SubscriberReporter::getInstance().setBroadcastSubscriber(configKey1, subscriptionId1, pir1); SubscriberReporter::getInstance().setBroadcastSubscriber(configKey1, subscriptionId2, pir2); SubscriberReporter::getInstance().setBroadcastSubscriber(configKey2, subscriptionId1, pir3); } void TearDown() override { SubscriberReporter::getInstance(); SubscriberReporter::getInstance().unsetBroadcastSubscriber(configKey1, subscriptionId1); SubscriberReporter::getInstance().unsetBroadcastSubscriber(configKey1, subscriptionId2); SubscriberReporter::getInstance().unsetBroadcastSubscriber(configKey2, subscriptionId1); EXPECT_EQ(pir1.use_count(), 1); EXPECT_EQ(pir2.use_count(), 1); EXPECT_EQ(pir3.use_count(), 1); ASSERT_TRUE(SubscriberReporter::getInstance().mIntentMap.empty()); } }; TEST_F(SubscriberReporterTest, TestBroadcastSubscriberDeathRemovesPir) { SubscriberReporter::broadcastSubscriberDied(pir1.get()); EXPECT_EQ(pir1.use_count(), 1); unordered_map>> expectedIntentMap = {{configKey1, {{subscriptionId2, pir2}}}, {configKey2, {{subscriptionId1, pir3}}}}; EXPECT_THAT(SubscriberReporter::getInstance().mIntentMap, ContainerEq(expectedIntentMap)); } TEST_F(SubscriberReporterTest, TestBroadcastSubscriberDeathRemovesPirAndConfigKey) { SubscriberReporter::broadcastSubscriberDied(pir3.get()); EXPECT_EQ(pir3.use_count(), 1); unordered_map>> expectedIntentMap = {{configKey1, {{subscriptionId1, pir1}, {subscriptionId2, pir2}}}}; EXPECT_THAT(SubscriberReporter::getInstance().mIntentMap, ContainerEq(expectedIntentMap)); } TEST_F(SubscriberReporterTest, TestBroadcastSubscriberDeathKeepsReplacedPir) { const shared_ptr pir4 = SharedRefBase::make>(); SubscriberReporter::getInstance().setBroadcastSubscriber(configKey1, subscriptionId1, pir4); // pir1 dies, but pir4 has replaced it with the same keys. The map should remain the same. SubscriberReporter::broadcastSubscriberDied(pir1.get()); unordered_map>> expectedIntentMap = {{configKey1, {{subscriptionId1, pir4}, {subscriptionId2, pir2}}}, {configKey2, {{subscriptionId1, pir3}}}}; EXPECT_THAT(SubscriberReporter::getInstance().mIntentMap, ContainerEq(expectedIntentMap)); } } // namespace statsd } // namespace os } // namespace android #else GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif