Andy Hung | c89c8dc | 2019-10-16 17:48:21 -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 | |
| 17 | #define LOG_TAG "mediametrics_tests" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include "MediaAnalyticsService.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <media/MediaAnalyticsItem.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | TEST(mediametrics_tests, instantiate) { |
| 30 | sp mediaMetrics = new MediaAnalyticsService(); |
| 31 | status_t status; |
| 32 | |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 33 | // random keys ignored when empty |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 34 | std::unique_ptr<MediaAnalyticsItem> random_key(MediaAnalyticsItem::create("random_key")); |
| 35 | status = mediaMetrics->submit(random_key.get()); |
| 36 | ASSERT_EQ(PERMISSION_DENIED, status); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 37 | |
| 38 | // random keys ignored with data |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 39 | random_key->setInt32("foo", 10); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 40 | status = mediaMetrics->submit(random_key.get()); |
| 41 | ASSERT_EQ(PERMISSION_DENIED, status); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 42 | |
| 43 | // known keys ignored if empty |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 44 | std::unique_ptr<MediaAnalyticsItem> audiotrack_key(MediaAnalyticsItem::create("audiotrack")); |
| 45 | status = mediaMetrics->submit(audiotrack_key.get()); |
| 46 | ASSERT_EQ(BAD_VALUE, status); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 47 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 48 | // known keys not ignored if not empty |
| 49 | audiotrack_key->addInt32("foo", 10); |
| 50 | status = mediaMetrics->submit(audiotrack_key.get()); |
| 51 | ASSERT_EQ(NO_ERROR, status); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 52 | |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | // fluent style that goes directly to mediametrics |
| 56 | ASSERT_EQ(true, MediaAnalyticsItem("audiorecord") |
| 57 | .setInt32("value", 2) |
| 58 | .addInt32("bar", 1) |
| 59 | .addInt32("value", 3) |
| 60 | .selfrecord()); |
| 61 | */ |
| 62 | |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 63 | mediaMetrics->dump(fileno(stdout), {} /* args */); |
| 64 | } |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 65 | |
| 66 | TEST(mediametrics_tests, item_manipulation) { |
| 67 | MediaAnalyticsItem item("audiorecord"); |
| 68 | |
| 69 | item.setInt32("value", 2).addInt32("bar", 3).addInt32("value", 4); |
| 70 | |
| 71 | int32_t i32; |
| 72 | ASSERT_TRUE(item.getInt32("value", &i32)); |
| 73 | ASSERT_EQ(6, i32); |
| 74 | |
| 75 | ASSERT_TRUE(item.getInt32("bar", &i32)); |
| 76 | ASSERT_EQ(3, i32); |
| 77 | |
| 78 | item.setInt64("big", INT64_MAX).setInt64("smaller", INT64_MAX - 1).addInt64("smaller", -2); |
| 79 | |
| 80 | int64_t i64; |
| 81 | ASSERT_TRUE(item.getInt64("big", &i64)); |
| 82 | ASSERT_EQ(INT64_MAX, i64); |
| 83 | |
| 84 | ASSERT_TRUE(item.getInt64("smaller", &i64)); |
| 85 | ASSERT_EQ(INT64_MAX - 3, i64); |
| 86 | |
| 87 | item.setDouble("precise", 10.5).setDouble("small", 0.125).addDouble("precise", 0.25); |
| 88 | |
| 89 | double d; |
| 90 | ASSERT_TRUE(item.getDouble("precise", &d)); |
| 91 | ASSERT_EQ(10.75, d); |
| 92 | |
| 93 | ASSERT_TRUE(item.getDouble("small", &d)); |
| 94 | ASSERT_EQ(0.125, d); |
| 95 | |
| 96 | char *s; |
| 97 | item.setCString("name", "Frank").setCString("mother", "June").setCString("mother", "July"); |
| 98 | ASSERT_TRUE(item.getCString("name", &s)); |
| 99 | ASSERT_EQ(0, strcmp(s, "Frank")); |
| 100 | free(s); |
| 101 | |
| 102 | ASSERT_TRUE(item.getCString("mother", &s)); |
| 103 | ASSERT_EQ(0, strcmp(s, "July")); // "July" overwrites "June" |
| 104 | free(s); |
| 105 | |
| 106 | item.setRate("burgersPerHour", 5, 2); |
| 107 | int64_t b, h; |
| 108 | ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d)); |
| 109 | ASSERT_EQ(5, b); |
| 110 | ASSERT_EQ(2, h); |
| 111 | ASSERT_EQ(2.5, d); |
| 112 | |
| 113 | item.addRate("burgersPerHour", 4, 2); |
| 114 | ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d)); |
| 115 | ASSERT_EQ(9, b); |
| 116 | ASSERT_EQ(4, h); |
| 117 | ASSERT_EQ(2.25, d); |
| 118 | |
| 119 | printf("item: %s\n", item.toString().c_str()); |
| 120 | fflush(stdout); |
| 121 | |
| 122 | sp mediaMetrics = new MediaAnalyticsService(); |
| 123 | status_t status = mediaMetrics->submit(&item); |
| 124 | ASSERT_EQ(NO_ERROR, status); |
| 125 | mediaMetrics->dump(fileno(stdout), {} /* args */); |
| 126 | } |
| 127 | |
| 128 | TEST(mediametrics_tests, superbig_item) { |
| 129 | MediaAnalyticsItem item("TheBigOne"); |
| 130 | constexpr size_t count = 10000; |
| 131 | |
| 132 | for (size_t i = 0; i < count; ++i) { |
| 133 | item.setInt32(std::to_string(i).c_str(), i); |
| 134 | } |
| 135 | for (size_t i = 0; i < count; ++i) { |
| 136 | int32_t i32; |
| 137 | ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32)); |
| 138 | ASSERT_EQ((int32_t)i, i32); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | TEST(mediametrics_tests, superbig_item_removal) { |
| 143 | MediaAnalyticsItem item("TheOddBigOne"); |
| 144 | constexpr size_t count = 10000; |
| 145 | |
| 146 | for (size_t i = 0; i < count; ++i) { |
| 147 | item.setInt32(std::to_string(i).c_str(), i); |
| 148 | } |
| 149 | for (size_t i = 0; i < count; i += 2) { |
| 150 | item.filter(std::to_string(i).c_str()); // filter out all the evens. |
| 151 | } |
| 152 | for (size_t i = 0; i < count; ++i) { |
| 153 | int32_t i32; |
| 154 | if (i & 1) { // check to see that only the odds are left. |
| 155 | ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32)); |
| 156 | ASSERT_EQ((int32_t)i, i32); |
| 157 | } else { |
| 158 | ASSERT_FALSE(item.getInt32(std::to_string(i).c_str(), &i32)); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | TEST(mediametrics_tests, item_transmutation) { |
| 164 | MediaAnalyticsItem item("Alchemist's Stone"); |
| 165 | |
| 166 | item.setInt64("convert", 123); |
| 167 | int64_t i64; |
| 168 | ASSERT_TRUE(item.getInt64("convert", &i64)); |
| 169 | ASSERT_EQ(123, i64); |
| 170 | |
| 171 | item.addInt32("convert", 2); // changes type of 'convert' from i64 to i32 (and re-init). |
| 172 | ASSERT_FALSE(item.getInt64("convert", &i64)); // should be false, no value in i64. |
| 173 | |
| 174 | int32_t i32; |
| 175 | ASSERT_TRUE(item.getInt32("convert", &i32)); // check it is i32 and 2 (123 is discarded). |
| 176 | ASSERT_EQ(2, i32); |
| 177 | } |