Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 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 <gtest/gtest.h> |
| 18 | |
| 19 | #include "DrmMetrics.h" |
| 20 | |
| 21 | using ::android::hardware::drm::V1_0::EventType; |
| 22 | using ::android::hardware::drm::V1_0::KeyStatusType; |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for the MediaDrmMetrics class. |
| 28 | */ |
| 29 | class MediaDrmMetricsTest : public ::testing::Test { |
| 30 | }; |
| 31 | |
| 32 | TEST_F(MediaDrmMetricsTest, EmptySuccess) { |
| 33 | MediaDrmMetrics metrics; |
| 34 | MediaAnalyticsItem item; |
| 35 | |
| 36 | metrics.Export(&item); |
| 37 | EXPECT_EQ(0, item.count()); |
| 38 | } |
| 39 | |
| 40 | TEST_F(MediaDrmMetricsTest, AllValuesSuccessCounts) { |
| 41 | MediaDrmMetrics metrics; |
| 42 | |
| 43 | metrics.mOpenSessionCounter.Increment(OK); |
| 44 | metrics.mCloseSessionCounter.Increment(OK); |
| 45 | |
| 46 | { |
| 47 | EventTimer<status_t> get_key_request_timer(&metrics.mGetKeyRequestTiming); |
| 48 | EventTimer<status_t> provide_key_response_timer( |
| 49 | &metrics.mProvideKeyResponseTiming); |
| 50 | get_key_request_timer.SetAttribute(OK); |
| 51 | provide_key_response_timer.SetAttribute(OK); |
| 52 | } |
| 53 | |
| 54 | metrics.mGetProvisionRequestCounter.Increment(OK); |
| 55 | metrics.mProvideProvisionResponseCounter.Increment(OK); |
| 56 | metrics.mGetDeviceUniqueIdCounter.Increment(OK); |
| 57 | |
| 58 | metrics.mKeyStatusChangeCounter.Increment(KeyStatusType::USABLE); |
| 59 | metrics.mEventCounter.Increment(EventType::PROVISION_REQUIRED); |
| 60 | |
| 61 | MediaAnalyticsItem item; |
| 62 | |
| 63 | metrics.Export(&item); |
| 64 | EXPECT_EQ(11, item.count()); |
| 65 | |
| 66 | // Verify the list of pairs of int64 metrics. |
| 67 | std::vector<std::pair<std::string, int64_t>> expected_values = { |
| 68 | { "/drm/mediadrm/open_session/ok/count", 1 }, |
| 69 | { "/drm/mediadrm/close_session/ok/count", 1 }, |
| 70 | { "/drm/mediadrm/get_key_request/ok/count", 1 }, |
| 71 | { "/drm/mediadrm/provide_key_response/ok/count", 1 }, |
| 72 | { "/drm/mediadrm/get_provision_request/ok/count", 1 }, |
| 73 | { "/drm/mediadrm/provide_provision_response/ok/count", 1 }, |
| 74 | { "/drm/mediadrm/key_status_change/USABLE/count", 1 }, |
| 75 | { "/drm/mediadrm/event/PROVISION_REQUIRED/count", 1 }, |
| 76 | { "/drm/mediadrm/get_device_unique_id/ok/count", 1 }}; |
| 77 | for (const auto& expected_pair : expected_values) { |
| 78 | int64_t value = -1; |
| 79 | EXPECT_TRUE(item.getInt64(expected_pair.first.c_str(), &value)) |
| 80 | << "Failed to get " << expected_pair.first; |
| 81 | EXPECT_EQ(expected_pair.second, value) |
| 82 | << "Unexpected value for " << expected_pair.first; |
| 83 | } |
| 84 | |
| 85 | // Validate timing values exist. |
| 86 | int64_t value = -1; |
| 87 | EXPECT_TRUE( |
| 88 | item.getInt64("/drm/mediadrm/get_key_request/ok/average_time_micros", |
| 89 | &value)); |
| 90 | EXPECT_GE(value, 0); |
| 91 | |
| 92 | value = -1; |
| 93 | EXPECT_TRUE( |
| 94 | item.getInt64("/drm/mediadrm/provide_key_response/ok/average_time_micros", |
| 95 | &value)); |
| 96 | EXPECT_GE(value, 0); |
| 97 | } |
| 98 | |
| 99 | TEST_F(MediaDrmMetricsTest, AllValuesFull) { |
| 100 | MediaDrmMetrics metrics; |
| 101 | |
| 102 | metrics.mOpenSessionCounter.Increment(OK); |
| 103 | metrics.mOpenSessionCounter.Increment(UNEXPECTED_NULL); |
| 104 | |
| 105 | metrics.mCloseSessionCounter.Increment(OK); |
| 106 | metrics.mCloseSessionCounter.Increment(UNEXPECTED_NULL); |
| 107 | |
| 108 | for (status_t s : {OK, UNEXPECTED_NULL}) { |
| 109 | { |
| 110 | EventTimer<status_t> get_key_request_timer(&metrics.mGetKeyRequestTiming); |
| 111 | EventTimer<status_t> provide_key_response_timer( |
| 112 | &metrics.mProvideKeyResponseTiming); |
| 113 | get_key_request_timer.SetAttribute(s); |
| 114 | provide_key_response_timer.SetAttribute(s); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | metrics.mGetProvisionRequestCounter.Increment(OK); |
| 119 | metrics.mGetProvisionRequestCounter.Increment(UNEXPECTED_NULL); |
| 120 | metrics.mProvideProvisionResponseCounter.Increment(OK); |
| 121 | metrics.mProvideProvisionResponseCounter.Increment(UNEXPECTED_NULL); |
| 122 | metrics.mGetDeviceUniqueIdCounter.Increment(OK); |
| 123 | metrics.mGetDeviceUniqueIdCounter.Increment(UNEXPECTED_NULL); |
| 124 | |
| 125 | metrics.mKeyStatusChangeCounter.Increment(KeyStatusType::USABLE); |
| 126 | metrics.mKeyStatusChangeCounter.Increment(KeyStatusType::EXPIRED); |
| 127 | metrics.mKeyStatusChangeCounter.Increment(KeyStatusType::OUTPUTNOTALLOWED); |
| 128 | metrics.mKeyStatusChangeCounter.Increment(KeyStatusType::STATUSPENDING); |
| 129 | metrics.mKeyStatusChangeCounter.Increment(KeyStatusType::INTERNALERROR); |
| 130 | metrics.mEventCounter.Increment(EventType::PROVISION_REQUIRED); |
| 131 | metrics.mEventCounter.Increment(EventType::KEY_NEEDED); |
| 132 | metrics.mEventCounter.Increment(EventType::KEY_EXPIRED); |
| 133 | metrics.mEventCounter.Increment(EventType::VENDOR_DEFINED); |
| 134 | metrics.mEventCounter.Increment(EventType::SESSION_RECLAIMED); |
| 135 | |
| 136 | MediaAnalyticsItem item; |
| 137 | |
| 138 | metrics.Export(&item); |
| 139 | EXPECT_EQ(26, item.count()); |
| 140 | |
| 141 | // Verify the list of pairs of int64 metrics. |
| 142 | std::vector<std::pair<std::string, int64_t>> expected_values = { |
| 143 | { "/drm/mediadrm/open_session/ok/count", 1 }, |
| 144 | { "/drm/mediadrm/close_session/ok/count", 1 }, |
| 145 | { "/drm/mediadrm/get_key_request/ok/count", 1 }, |
| 146 | { "/drm/mediadrm/provide_key_response/ok/count", 1 }, |
| 147 | { "/drm/mediadrm/get_provision_request/ok/count", 1 }, |
| 148 | { "/drm/mediadrm/provide_provision_response/ok/count", 1 }, |
| 149 | { "/drm/mediadrm/get_device_unique_id/ok/count", 1 }, |
| 150 | { "/drm/mediadrm/open_session/error/count", 1 }, |
| 151 | { "/drm/mediadrm/close_session/error/count", 1 }, |
| 152 | { "/drm/mediadrm/get_key_request/error/count", 1 }, |
| 153 | { "/drm/mediadrm/provide_key_response/error/count", 1 }, |
| 154 | { "/drm/mediadrm/get_provision_request/error/count", 1 }, |
| 155 | { "/drm/mediadrm/provide_provision_response/error/count", 1 }, |
| 156 | { "/drm/mediadrm/get_device_unique_id/error/count", 1 }, |
| 157 | { "/drm/mediadrm/key_status_change/USABLE/count", 1 }, |
| 158 | { "/drm/mediadrm/key_status_change/EXPIRED/count", 1 }, |
| 159 | { "/drm/mediadrm/key_status_change/OUTPUT_NOT_ALLOWED/count", 1 }, |
| 160 | { "/drm/mediadrm/key_status_change/STATUS_PENDING/count", 1 }, |
| 161 | { "/drm/mediadrm/key_status_change/INTERNAL_ERROR/count", 1 }, |
| 162 | { "/drm/mediadrm/event/PROVISION_REQUIRED/count", 1 }, |
| 163 | { "/drm/mediadrm/event/KEY_NEEDED/count", 1 }, |
| 164 | { "/drm/mediadrm/event/KEY_EXPIRED/count", 1 }, |
| 165 | { "/drm/mediadrm/event/VENDOR_DEFINED/count", 1 }, |
| 166 | { "/drm/mediadrm/event/SESSION_RECLAIMED/count", 1 }}; |
| 167 | for (const auto& expected_pair : expected_values) { |
| 168 | int64_t value = -1; |
| 169 | EXPECT_TRUE(item.getInt64(expected_pair.first.c_str(), &value)) |
| 170 | << "Failed to get " << expected_pair.first; |
| 171 | EXPECT_EQ(expected_pair.second, value) |
| 172 | << "Unexpected value for " << expected_pair.first; |
| 173 | } |
| 174 | |
| 175 | // Validate timing values exist. |
| 176 | int64_t value = -1; |
| 177 | std::string name = metrics.mGetKeyRequestTiming.metric_name() |
| 178 | + "/ok/average_time_micros"; |
| 179 | EXPECT_TRUE(item.getInt64(name.c_str(), &value)); |
| 180 | EXPECT_GE(value, 0); |
| 181 | |
| 182 | value = -1; |
| 183 | name = metrics.mProvideKeyResponseTiming.metric_name() |
| 184 | + "/ok/average_time_micros"; |
| 185 | EXPECT_TRUE(item.getInt64(name.c_str(), &value)); |
| 186 | EXPECT_GE(value, 0); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | } // namespace android |