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