tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019, 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 | */ |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 16 | #include "state/StateTracker.h" |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 17 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
| 19 | |
| 20 | #include "state/StateListener.h" |
| 21 | #include "state/StateManager.h" |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 22 | #include "state/StateTracker.h" |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 23 | #include "stats_event.h" |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 24 | #include "tests/statsd_test_util.h" |
| 25 | |
| 26 | #ifdef __ANDROID__ |
| 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 32 | const int32_t timestampNs = 1000; |
| 33 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Mock StateListener class for testing. |
| 36 | * Stores primary key and state pairs. |
| 37 | */ |
| 38 | class TestStateListener : public virtual StateListener { |
| 39 | public: |
| 40 | TestStateListener(){}; |
| 41 | |
| 42 | virtual ~TestStateListener(){}; |
| 43 | |
| 44 | struct Update { |
| 45 | Update(const HashableDimensionKey& key, int state) : mKey(key), mState(state){}; |
| 46 | HashableDimensionKey mKey; |
| 47 | int mState; |
| 48 | }; |
| 49 | |
| 50 | std::vector<Update> updates; |
| 51 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 52 | void onStateChanged(const int64_t eventTimeNs, const int32_t atomId, |
| 53 | const HashableDimensionKey& primaryKey, int oldState, int newState) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 54 | updates.emplace_back(primaryKey, newState); |
| 55 | } |
| 56 | }; |
| 57 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 58 | int getStateInt(StateManager& mgr, int atomId, const HashableDimensionKey& queryKey) { |
| 59 | FieldValue output; |
| 60 | mgr.getStateValue(atomId, queryKey, &output); |
| 61 | return output.mValue.int_value; |
| 62 | } |
| 63 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 64 | // START: build event functions. |
| 65 | // Incorrect event - missing fields |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 66 | std::unique_ptr<LogEvent> buildIncorrectOverlayEvent(int uid, const std::string& packageName, |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 67 | int state) { |
| 68 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 69 | AStatsEvent_setAtomId(statsEvent, util::OVERLAY_STATE_CHANGED); |
| 70 | AStatsEvent_overwriteTimestamp(statsEvent, 1000); |
| 71 | |
| 72 | AStatsEvent_writeInt32(statsEvent, uid); |
| 73 | AStatsEvent_writeString(statsEvent, packageName.c_str()); |
| 74 | // Missing field 3 - using_alert_window. |
| 75 | AStatsEvent_writeInt32(statsEvent, state); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 76 | |
| 77 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 78 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 79 | return logEvent; |
| 80 | } |
| 81 | |
| 82 | // Incorrect event - exclusive state has wrong type |
| 83 | std::unique_ptr<LogEvent> buildOverlayEventBadStateType(int uid, const std::string& packageName) { |
| 84 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 85 | AStatsEvent_setAtomId(statsEvent, util::OVERLAY_STATE_CHANGED); |
| 86 | AStatsEvent_overwriteTimestamp(statsEvent, 1000); |
| 87 | |
| 88 | AStatsEvent_writeInt32(statsEvent, uid); |
| 89 | AStatsEvent_writeString(statsEvent, packageName.c_str()); |
| 90 | AStatsEvent_writeInt32(statsEvent, true); // using_alert_window |
| 91 | AStatsEvent_writeString(statsEvent, "string"); // exclusive state: string instead of int |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 92 | |
| 93 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 94 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 95 | return logEvent; |
| 96 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 97 | // END: build event functions. |
| 98 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 99 | TEST(StateListenerTest, TestStateListenerWeakPointer) { |
| 100 | sp<TestStateListener> listener = new TestStateListener(); |
| 101 | wp<TestStateListener> wListener = listener; |
| 102 | listener = nullptr; // let go of listener |
| 103 | EXPECT_TRUE(wListener.promote() == nullptr); |
| 104 | } |
| 105 | |
| 106 | TEST(StateManagerTest, TestStateManagerGetInstance) { |
| 107 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 108 | StateManager& mgr = StateManager::getInstance(); |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 109 | mgr.clear(); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 110 | |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 111 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener1); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 112 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
| 113 | EXPECT_EQ(1, StateManager::getInstance().getStateTrackersCount()); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Test registering listeners to StateTrackers |
| 118 | * |
| 119 | * - StateManager will create a new StateTracker if it doesn't already exist |
| 120 | * and then register the listener to the StateTracker. |
| 121 | * - If a listener is already registered to a StateTracker, it is not added again. |
| 122 | * - StateTrackers are only created for atoms that are state atoms. |
| 123 | */ |
| 124 | TEST(StateTrackerTest, TestRegisterListener) { |
| 125 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 126 | sp<TestStateListener> listener2 = new TestStateListener(); |
| 127 | StateManager mgr; |
| 128 | |
| 129 | // Register listener to non-existing StateTracker |
| 130 | EXPECT_EQ(0, mgr.getStateTrackersCount()); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 131 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener1); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 132 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 133 | EXPECT_EQ(1, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 134 | |
| 135 | // Register listener to existing StateTracker |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 136 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener2); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 137 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 138 | EXPECT_EQ(2, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 139 | |
| 140 | // Register already registered listener to existing StateTracker |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 141 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener2); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 142 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 143 | EXPECT_EQ(2, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 144 | |
| 145 | // Register listener to non-state atom |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 146 | mgr.registerListener(util::BATTERY_LEVEL_CHANGED, listener2); |
| 147 | EXPECT_EQ(2, mgr.getStateTrackersCount()); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Test unregistering listeners from StateTrackers |
| 152 | * |
| 153 | * - StateManager will unregister listeners from a StateTracker only if the |
| 154 | * StateTracker exists and the listener is registered to the StateTracker. |
| 155 | * - Once all listeners are removed from a StateTracker, the StateTracker |
| 156 | * is also removed. |
| 157 | */ |
| 158 | TEST(StateTrackerTest, TestUnregisterListener) { |
| 159 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 160 | sp<TestStateListener> listener2 = new TestStateListener(); |
| 161 | StateManager mgr; |
| 162 | |
| 163 | // Unregister listener from non-existing StateTracker |
| 164 | EXPECT_EQ(0, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 165 | mgr.unregisterListener(util::SCREEN_STATE_CHANGED, listener1); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 166 | EXPECT_EQ(0, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 167 | EXPECT_EQ(-1, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 168 | |
| 169 | // Unregister non-registered listener from existing StateTracker |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 170 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener1); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 171 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 172 | EXPECT_EQ(1, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
| 173 | mgr.unregisterListener(util::SCREEN_STATE_CHANGED, listener2); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 174 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 175 | EXPECT_EQ(1, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 176 | |
| 177 | // Unregister second-to-last listener from StateTracker |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 178 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener2); |
| 179 | mgr.unregisterListener(util::SCREEN_STATE_CHANGED, listener1); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 180 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 181 | EXPECT_EQ(1, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 182 | |
| 183 | // Unregister last listener from StateTracker |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 184 | mgr.unregisterListener(util::SCREEN_STATE_CHANGED, listener2); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 185 | EXPECT_EQ(0, mgr.getStateTrackersCount()); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 186 | EXPECT_EQ(-1, mgr.getListenersCount(util::SCREEN_STATE_CHANGED)); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 187 | } |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Test a binary state atom with nested counting. |
| 191 | * |
| 192 | * To go from an "ON" state to an "OFF" state with nested counting, we must see |
| 193 | * an equal number of "OFF" events as "ON" events. |
| 194 | * For example, ACQUIRE, ACQUIRE, RELEASE will still be in the ACQUIRE state. |
| 195 | * ACQUIRE, ACQUIRE, RELEASE, RELEASE will be in the RELEASE state. |
| 196 | */ |
| 197 | TEST(StateTrackerTest, TestStateChangeNested) { |
| 198 | sp<TestStateListener> listener = new TestStateListener(); |
| 199 | StateManager mgr; |
| 200 | mgr.registerListener(util::WAKELOCK_STATE_CHANGED, listener); |
| 201 | |
| 202 | std::vector<int> attributionUids1 = {1000}; |
| 203 | std::vector<string> attributionTags1 = {"tag"}; |
| 204 | |
| 205 | std::unique_ptr<LogEvent> event1 = CreateAcquireWakelockEvent(timestampNs, attributionUids1, |
| 206 | attributionTags1, "wakelockName"); |
| 207 | mgr.onLogEvent(*event1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 208 | ASSERT_EQ(1, listener->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 209 | EXPECT_EQ(1000, listener->updates[0].mKey.getValues()[0].mValue.int_value); |
| 210 | EXPECT_EQ(1, listener->updates[0].mState); |
| 211 | listener->updates.clear(); |
| 212 | |
| 213 | std::unique_ptr<LogEvent> event2 = CreateAcquireWakelockEvent( |
| 214 | timestampNs + 1000, attributionUids1, attributionTags1, "wakelockName"); |
| 215 | mgr.onLogEvent(*event2); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 216 | ASSERT_EQ(0, listener->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 217 | |
| 218 | std::unique_ptr<LogEvent> event3 = CreateReleaseWakelockEvent( |
| 219 | timestampNs + 2000, attributionUids1, attributionTags1, "wakelockName"); |
| 220 | mgr.onLogEvent(*event3); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 221 | ASSERT_EQ(0, listener->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 222 | |
| 223 | std::unique_ptr<LogEvent> event4 = CreateReleaseWakelockEvent( |
| 224 | timestampNs + 3000, attributionUids1, attributionTags1, "wakelockName"); |
| 225 | mgr.onLogEvent(*event4); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 226 | ASSERT_EQ(1, listener->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 227 | EXPECT_EQ(1000, listener->updates[0].mKey.getValues()[0].mValue.int_value); |
| 228 | EXPECT_EQ(0, listener->updates[0].mState); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Test a state atom with a reset state. |
| 233 | * |
| 234 | * If the reset state value is seen, every state in the map is set to the default |
| 235 | * state and every listener is notified. |
| 236 | */ |
| 237 | TEST(StateTrackerTest, TestStateChangeReset) { |
| 238 | sp<TestStateListener> listener = new TestStateListener(); |
| 239 | StateManager mgr; |
| 240 | mgr.registerListener(util::BLE_SCAN_STATE_CHANGED, listener); |
| 241 | |
| 242 | std::vector<int> attributionUids1 = {1000}; |
| 243 | std::vector<string> attributionTags1 = {"tag1"}; |
| 244 | std::vector<int> attributionUids2 = {2000}; |
| 245 | |
| 246 | std::unique_ptr<LogEvent> event1 = |
| 247 | CreateBleScanStateChangedEvent(timestampNs, attributionUids1, attributionTags1, |
| 248 | BleScanStateChanged::ON, false, false, false); |
| 249 | mgr.onLogEvent(*event1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 250 | ASSERT_EQ(1, listener->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 251 | EXPECT_EQ(1000, listener->updates[0].mKey.getValues()[0].mValue.int_value); |
| 252 | EXPECT_EQ(BleScanStateChanged::ON, listener->updates[0].mState); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 253 | FieldValue stateFieldValue; |
| 254 | mgr.getStateValue(util::BLE_SCAN_STATE_CHANGED, listener->updates[0].mKey, &stateFieldValue); |
| 255 | EXPECT_EQ(BleScanStateChanged::ON, stateFieldValue.mValue.int_value); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 256 | listener->updates.clear(); |
| 257 | |
| 258 | std::unique_ptr<LogEvent> event2 = |
| 259 | CreateBleScanStateChangedEvent(timestampNs + 1000, attributionUids2, attributionTags1, |
| 260 | BleScanStateChanged::ON, false, false, false); |
| 261 | mgr.onLogEvent(*event2); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 262 | ASSERT_EQ(1, listener->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 263 | EXPECT_EQ(2000, listener->updates[0].mKey.getValues()[0].mValue.int_value); |
| 264 | EXPECT_EQ(BleScanStateChanged::ON, listener->updates[0].mState); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 265 | mgr.getStateValue(util::BLE_SCAN_STATE_CHANGED, listener->updates[0].mKey, &stateFieldValue); |
| 266 | EXPECT_EQ(BleScanStateChanged::ON, stateFieldValue.mValue.int_value); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 267 | listener->updates.clear(); |
| 268 | |
| 269 | std::unique_ptr<LogEvent> event3 = |
| 270 | CreateBleScanStateChangedEvent(timestampNs + 2000, attributionUids2, attributionTags1, |
| 271 | BleScanStateChanged::RESET, false, false, false); |
| 272 | mgr.onLogEvent(*event3); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 273 | ASSERT_EQ(2, listener->updates.size()); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 274 | for (const TestStateListener::Update& update : listener->updates) { |
| 275 | EXPECT_EQ(BleScanStateChanged::OFF, update.mState); |
| 276 | |
| 277 | mgr.getStateValue(util::BLE_SCAN_STATE_CHANGED, update.mKey, &stateFieldValue); |
| 278 | EXPECT_EQ(BleScanStateChanged::OFF, stateFieldValue.mValue.int_value); |
| 279 | } |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Test StateManager's onLogEvent and StateListener's onStateChanged correctly |
| 284 | * updates listener for states without primary keys. |
| 285 | */ |
| 286 | TEST(StateTrackerTest, TestStateChangeNoPrimaryFields) { |
| 287 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 288 | StateManager mgr; |
| 289 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener1); |
| 290 | |
| 291 | // log event |
| 292 | std::unique_ptr<LogEvent> event = CreateScreenStateChangedEvent( |
| 293 | timestampNs, android::view::DisplayStateEnum::DISPLAY_STATE_ON); |
| 294 | mgr.onLogEvent(*event); |
| 295 | |
| 296 | // check listener was updated |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 297 | ASSERT_EQ(1, listener1->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 298 | EXPECT_EQ(DEFAULT_DIMENSION_KEY, listener1->updates[0].mKey); |
| 299 | EXPECT_EQ(2, listener1->updates[0].mState); |
| 300 | |
| 301 | // check StateTracker was updated by querying for state |
| 302 | HashableDimensionKey queryKey = DEFAULT_DIMENSION_KEY; |
| 303 | EXPECT_EQ(android::view::DisplayStateEnum::DISPLAY_STATE_ON, |
| 304 | getStateInt(mgr, util::SCREEN_STATE_CHANGED, queryKey)); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Test StateManager's onLogEvent and StateListener's onStateChanged correctly |
| 309 | * updates listener for states with one primary key. |
| 310 | */ |
| 311 | TEST(StateTrackerTest, TestStateChangeOnePrimaryField) { |
| 312 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 313 | StateManager mgr; |
| 314 | mgr.registerListener(util::UID_PROCESS_STATE_CHANGED, listener1); |
| 315 | |
| 316 | // log event |
| 317 | std::unique_ptr<LogEvent> event = CreateUidProcessStateChangedEvent( |
| 318 | timestampNs, 1000 /*uid*/, android::app::ProcessStateEnum::PROCESS_STATE_TOP); |
| 319 | mgr.onLogEvent(*event); |
| 320 | |
| 321 | // check listener was updated |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 322 | ASSERT_EQ(1, listener1->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 323 | EXPECT_EQ(1000, listener1->updates[0].mKey.getValues()[0].mValue.int_value); |
| 324 | EXPECT_EQ(1002, listener1->updates[0].mState); |
| 325 | |
| 326 | // check StateTracker was updated by querying for state |
| 327 | HashableDimensionKey queryKey; |
| 328 | getUidProcessKey(1000 /* uid */, &queryKey); |
| 329 | EXPECT_EQ(android::app::ProcessStateEnum::PROCESS_STATE_TOP, |
| 330 | getStateInt(mgr, util::UID_PROCESS_STATE_CHANGED, queryKey)); |
| 331 | } |
| 332 | |
| 333 | TEST(StateTrackerTest, TestStateChangePrimaryFieldAttrChain) { |
| 334 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 335 | StateManager mgr; |
| 336 | mgr.registerListener(util::WAKELOCK_STATE_CHANGED, listener1); |
| 337 | |
| 338 | // Log event. |
| 339 | std::vector<int> attributionUids = {1001}; |
| 340 | std::vector<string> attributionTags = {"tag1"}; |
| 341 | |
| 342 | std::unique_ptr<LogEvent> event = CreateAcquireWakelockEvent(timestampNs, attributionUids, |
| 343 | attributionTags, "wakelockName"); |
| 344 | mgr.onLogEvent(*event); |
| 345 | EXPECT_EQ(1, mgr.getStateTrackersCount()); |
| 346 | EXPECT_EQ(1, mgr.getListenersCount(util::WAKELOCK_STATE_CHANGED)); |
| 347 | |
| 348 | // Check listener was updated. |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 349 | ASSERT_EQ(1, listener1->updates.size()); |
| 350 | ASSERT_EQ(3, listener1->updates[0].mKey.getValues().size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 351 | EXPECT_EQ(1001, listener1->updates[0].mKey.getValues()[0].mValue.int_value); |
| 352 | EXPECT_EQ(1, listener1->updates[0].mKey.getValues()[1].mValue.int_value); |
| 353 | EXPECT_EQ("wakelockName", listener1->updates[0].mKey.getValues()[2].mValue.str_value); |
| 354 | EXPECT_EQ(WakelockStateChanged::ACQUIRE, listener1->updates[0].mState); |
| 355 | |
| 356 | // Check StateTracker was updated by querying for state. |
| 357 | HashableDimensionKey queryKey; |
| 358 | getPartialWakelockKey(1001 /* uid */, "wakelockName", &queryKey); |
| 359 | EXPECT_EQ(WakelockStateChanged::ACQUIRE, |
| 360 | getStateInt(mgr, util::WAKELOCK_STATE_CHANGED, queryKey)); |
| 361 | |
| 362 | // No state stored for this query key. |
| 363 | HashableDimensionKey queryKey2; |
| 364 | getPartialWakelockKey(1002 /* uid */, "tag1", &queryKey2); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 365 | EXPECT_EQ(-1 /*StateTracker::kStateUnknown*/, |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 366 | getStateInt(mgr, util::WAKELOCK_STATE_CHANGED, queryKey2)); |
| 367 | |
| 368 | // Partial query fails. |
| 369 | HashableDimensionKey queryKey3; |
| 370 | getPartialWakelockKey(1001 /* uid */, &queryKey3); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 371 | EXPECT_EQ(-1 /*StateTracker::kStateUnknown*/, |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 372 | getStateInt(mgr, util::WAKELOCK_STATE_CHANGED, queryKey3)); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Test StateManager's onLogEvent and StateListener's onStateChanged correctly |
| 377 | * updates listener for states with multiple primary keys. |
| 378 | */ |
| 379 | TEST(StateTrackerTest, TestStateChangeMultiplePrimaryFields) { |
| 380 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 381 | StateManager mgr; |
| 382 | mgr.registerListener(util::OVERLAY_STATE_CHANGED, listener1); |
| 383 | |
| 384 | // log event |
| 385 | std::unique_ptr<LogEvent> event = CreateOverlayStateChangedEvent( |
| 386 | timestampNs, 1000 /* uid */, "package1", true /*using_alert_window*/, |
| 387 | OverlayStateChanged::ENTERED); |
| 388 | mgr.onLogEvent(*event); |
| 389 | |
| 390 | // check listener was updated |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 391 | ASSERT_EQ(1, listener1->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 392 | EXPECT_EQ(1000, listener1->updates[0].mKey.getValues()[0].mValue.int_value); |
| 393 | EXPECT_EQ(1, listener1->updates[0].mState); |
| 394 | |
| 395 | // check StateTracker was updated by querying for state |
| 396 | HashableDimensionKey queryKey; |
| 397 | getOverlayKey(1000 /* uid */, "package1", &queryKey); |
| 398 | EXPECT_EQ(OverlayStateChanged::ENTERED, |
| 399 | getStateInt(mgr, util::OVERLAY_STATE_CHANGED, queryKey)); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Test StateManager's onLogEvent and StateListener's onStateChanged |
| 404 | * when there is an error extracting state from log event. Listener is not |
| 405 | * updated of state change. |
| 406 | */ |
| 407 | TEST(StateTrackerTest, TestStateChangeEventError) { |
| 408 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 409 | StateManager mgr; |
| 410 | mgr.registerListener(util::OVERLAY_STATE_CHANGED, listener1); |
| 411 | |
| 412 | // log event |
| 413 | std::shared_ptr<LogEvent> event1 = |
| 414 | buildIncorrectOverlayEvent(1000 /* uid */, "package1", 1 /* state */); |
| 415 | std::shared_ptr<LogEvent> event2 = buildOverlayEventBadStateType(1001 /* uid */, "package2"); |
| 416 | |
| 417 | // check listener was updated |
| 418 | mgr.onLogEvent(*event1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 419 | ASSERT_EQ(0, listener1->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 420 | mgr.onLogEvent(*event2); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 421 | ASSERT_EQ(0, listener1->updates.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST(StateTrackerTest, TestStateQuery) { |
| 425 | sp<TestStateListener> listener1 = new TestStateListener(); |
| 426 | sp<TestStateListener> listener2 = new TestStateListener(); |
| 427 | sp<TestStateListener> listener3 = new TestStateListener(); |
| 428 | sp<TestStateListener> listener4 = new TestStateListener(); |
| 429 | StateManager mgr; |
| 430 | mgr.registerListener(util::SCREEN_STATE_CHANGED, listener1); |
| 431 | mgr.registerListener(util::UID_PROCESS_STATE_CHANGED, listener2); |
| 432 | mgr.registerListener(util::OVERLAY_STATE_CHANGED, listener3); |
| 433 | mgr.registerListener(util::WAKELOCK_STATE_CHANGED, listener4); |
| 434 | |
| 435 | std::unique_ptr<LogEvent> event1 = CreateUidProcessStateChangedEvent( |
| 436 | timestampNs, 1000 /*uid*/, |
| 437 | android::app::ProcessStateEnum::PROCESS_STATE_TOP); // state value: 1002 |
| 438 | std::unique_ptr<LogEvent> event2 = CreateUidProcessStateChangedEvent( |
| 439 | timestampNs + 1000, 1001 /*uid*/, |
| 440 | android::app::ProcessStateEnum::PROCESS_STATE_FOREGROUND_SERVICE); // state value: |
| 441 | // 1003 |
| 442 | std::unique_ptr<LogEvent> event3 = CreateUidProcessStateChangedEvent( |
| 443 | timestampNs + 2000, 1002 /*uid*/, |
| 444 | android::app::ProcessStateEnum::PROCESS_STATE_PERSISTENT); // state value: 1000 |
| 445 | std::unique_ptr<LogEvent> event4 = CreateUidProcessStateChangedEvent( |
| 446 | timestampNs + 3000, 1001 /*uid*/, |
| 447 | android::app::ProcessStateEnum::PROCESS_STATE_TOP); // state value: 1002 |
| 448 | std::unique_ptr<LogEvent> event5 = CreateScreenStateChangedEvent( |
| 449 | timestampNs + 4000, android::view::DisplayStateEnum::DISPLAY_STATE_ON); |
| 450 | std::unique_ptr<LogEvent> event6 = CreateOverlayStateChangedEvent( |
| 451 | timestampNs + 5000, 1000 /*uid*/, "package1", true /*using_alert_window*/, |
| 452 | OverlayStateChanged::ENTERED); |
| 453 | std::unique_ptr<LogEvent> event7 = CreateOverlayStateChangedEvent( |
| 454 | timestampNs + 6000, 1000 /*uid*/, "package2", true /*using_alert_window*/, |
| 455 | OverlayStateChanged::EXITED); |
| 456 | |
| 457 | std::vector<int> attributionUids = {1005}; |
| 458 | std::vector<string> attributionTags = {"tag"}; |
| 459 | |
| 460 | std::unique_ptr<LogEvent> event8 = CreateAcquireWakelockEvent( |
| 461 | timestampNs + 7000, attributionUids, attributionTags, "wakelock1"); |
| 462 | std::unique_ptr<LogEvent> event9 = CreateReleaseWakelockEvent( |
| 463 | timestampNs + 8000, attributionUids, attributionTags, "wakelock2"); |
| 464 | |
| 465 | mgr.onLogEvent(*event1); |
| 466 | mgr.onLogEvent(*event2); |
| 467 | mgr.onLogEvent(*event3); |
| 468 | mgr.onLogEvent(*event5); |
| 469 | mgr.onLogEvent(*event5); |
| 470 | mgr.onLogEvent(*event6); |
| 471 | mgr.onLogEvent(*event7); |
| 472 | mgr.onLogEvent(*event8); |
| 473 | mgr.onLogEvent(*event9); |
| 474 | |
| 475 | // Query for UidProcessState of uid 1001 |
| 476 | HashableDimensionKey queryKey1; |
| 477 | getUidProcessKey(1001, &queryKey1); |
| 478 | EXPECT_EQ(android::app::ProcessStateEnum::PROCESS_STATE_FOREGROUND_SERVICE, |
| 479 | getStateInt(mgr, util::UID_PROCESS_STATE_CHANGED, queryKey1)); |
| 480 | |
| 481 | // Query for UidProcessState of uid 1004 - not in state map |
| 482 | HashableDimensionKey queryKey2; |
| 483 | getUidProcessKey(1004, &queryKey2); |
| 484 | EXPECT_EQ(-1, getStateInt(mgr, util::UID_PROCESS_STATE_CHANGED, |
| 485 | queryKey2)); // default state |
| 486 | |
| 487 | // Query for UidProcessState of uid 1001 - after change in state |
| 488 | mgr.onLogEvent(*event4); |
| 489 | EXPECT_EQ(android::app::ProcessStateEnum::PROCESS_STATE_TOP, |
| 490 | getStateInt(mgr, util::UID_PROCESS_STATE_CHANGED, queryKey1)); |
| 491 | |
| 492 | // Query for ScreenState |
| 493 | EXPECT_EQ(android::view::DisplayStateEnum::DISPLAY_STATE_ON, |
| 494 | getStateInt(mgr, util::SCREEN_STATE_CHANGED, DEFAULT_DIMENSION_KEY)); |
| 495 | |
| 496 | // Query for OverlayState of uid 1000, package name "package2" |
| 497 | HashableDimensionKey queryKey3; |
| 498 | getOverlayKey(1000, "package2", &queryKey3); |
| 499 | EXPECT_EQ(OverlayStateChanged::EXITED, |
| 500 | getStateInt(mgr, util::OVERLAY_STATE_CHANGED, queryKey3)); |
| 501 | |
| 502 | // Query for WakelockState of uid 1005, tag 2 |
| 503 | HashableDimensionKey queryKey4; |
| 504 | getPartialWakelockKey(1005, "wakelock2", &queryKey4); |
| 505 | EXPECT_EQ(WakelockStateChanged::RELEASE, |
| 506 | getStateInt(mgr, util::WAKELOCK_STATE_CHANGED, queryKey4)); |
| 507 | |
| 508 | // Query for WakelockState of uid 1005, tag 1 |
| 509 | HashableDimensionKey queryKey5; |
| 510 | getPartialWakelockKey(1005, "wakelock1", &queryKey5); |
| 511 | EXPECT_EQ(WakelockStateChanged::ACQUIRE, |
| 512 | getStateInt(mgr, util::WAKELOCK_STATE_CHANGED, queryKey5)); |
| 513 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 514 | |
| 515 | } // namespace statsd |
| 516 | } // namespace os |
| 517 | } // namespace android |
| 518 | #else |
| 519 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 520 | #endif |