• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NDEBUG 0
18 #define LOG_TAG "MediaMetricsTest"
19 
20 #include <gtest/gtest.h>
21 #include <utils/Log.h>
22 #include <media/MediaMetrics.h>
23 
24 //-----------------------------------------------------------------
25 class MediaMetricsTest : public ::testing::Test {
26 
27 protected:
MediaMetricsTest()28     MediaMetricsTest() { }
29 
~MediaMetricsTest()30     virtual ~MediaMetricsTest() { }
31 
32     /* Test setup*/
SetUp()33     virtual void SetUp() {
34         handle_ = mediametrics_create("foo");
35     }
36 
TearDown()37     virtual void TearDown() {
38         mediametrics_delete(handle_);
39     }
40 
41     mediametrics_handle_t handle_;
42 };
43 
44 //-------------------------------------------------------------------------------------------------
TEST_F(MediaMetricsTest,testCreateDelete)45 TEST_F(MediaMetricsTest, testCreateDelete) {
46     // Will be done with SetUp and TearDown.
47 }
48 
TEST_F(MediaMetricsTest,testInt32)49 TEST_F(MediaMetricsTest, testInt32) {
50     mediametrics_setInt32(handle_, "attr1", 100);
51     int32_t value;
52     EXPECT_TRUE(mediametrics_getInt32(handle_, "attr1",  &value));
53     EXPECT_EQ(100, value);
54 
55     mediametrics_addInt32(handle_, "attr1", 50);
56     EXPECT_TRUE(mediametrics_getInt32(handle_, "attr1",  &value));
57     EXPECT_EQ(150, value);
58 }
59 
TEST_F(MediaMetricsTest,testInt64)60 TEST_F(MediaMetricsTest, testInt64) {
61     mediametrics_setInt64(handle_, "attr2", 1e10);
62     int64_t value;
63     EXPECT_TRUE(mediametrics_getInt64(handle_, "attr2",  &value));
64     EXPECT_EQ(1e10, value);
65 
66     mediametrics_addInt64(handle_, "attr2", 50);
67     EXPECT_TRUE(mediametrics_getInt64(handle_, "attr2",  &value));
68     EXPECT_EQ(1e10 + 50, value);
69 }
70 
TEST_F(MediaMetricsTest,testDouble)71 TEST_F(MediaMetricsTest, testDouble) {
72     mediametrics_setDouble(handle_, "attr3", 100.0);
73     double value;
74     EXPECT_TRUE(mediametrics_getDouble(handle_, "attr3",  &value));
75     EXPECT_DOUBLE_EQ(100.0, value);
76 
77     mediametrics_addDouble(handle_, "attr3", 50.0);
78     EXPECT_TRUE(mediametrics_getDouble(handle_, "attr3",  &value));
79     EXPECT_DOUBLE_EQ(150.0, value);
80 }
81 
TEST_F(MediaMetricsTest,testRate)82 TEST_F(MediaMetricsTest, testRate) {
83     mediametrics_setRate(handle_, "attr4", 30, 1000);
84     int64_t count;
85     int64_t duration;
86     double rate;
87     EXPECT_TRUE(mediametrics_getRate(handle_, "attr4",  &count, &duration, &rate));
88     EXPECT_EQ(30, count);
89     EXPECT_EQ(1000, duration);
90     EXPECT_DOUBLE_EQ(30/1000.0, rate);
91 
92     mediametrics_addRate(handle_, "attr4", 29, 1000);
93     EXPECT_TRUE(mediametrics_getRate(handle_, "attr4",  &count, &duration, &rate));
94     EXPECT_EQ(59, count);
95     EXPECT_EQ(2000, duration);
96     EXPECT_DOUBLE_EQ(59/2000.0, rate);
97 }
98 
TEST_F(MediaMetricsTest,testCString)99 TEST_F(MediaMetricsTest, testCString) {
100     mediametrics_setCString(handle_, "attr5", "test_string");
101     char *value = nullptr;
102     EXPECT_TRUE(mediametrics_getCString(handle_, "attr5",  &value));
103     EXPECT_STREQ("test_string", value);
104     mediametrics_freeCString(value);
105 }
106 
TEST_F(MediaMetricsTest,testCount)107 TEST_F(MediaMetricsTest, testCount) {
108     mediametrics_setInt32(handle_, "attr1", 100);
109     EXPECT_EQ(1, mediametrics_count(handle_));
110     mediametrics_setInt32(handle_, "attr2", 200);
111     mediametrics_setInt32(handle_, "attr3", 300);
112     EXPECT_EQ(3, mediametrics_count(handle_));
113 }
114 
TEST_F(MediaMetricsTest,testReadable)115 TEST_F(MediaMetricsTest, testReadable) {
116     mediametrics_setInt32(handle_, "attr1", 1);
117     mediametrics_setInt64(handle_, "attr2", 2);
118     mediametrics_setDouble(handle_, "attr3", 3.0);
119     mediametrics_setRate(handle_, "attr4", 4, 5);
120     mediametrics_setCString(handle_, "attr5", "test_string");
121 
122     EXPECT_TRUE(strlen(mediametrics_readable(handle_)) > 0);
123 }
124 
TEST_F(MediaMetricsTest,testSelfRecord)125 TEST_F(MediaMetricsTest, testSelfRecord) {
126     mediametrics_setInt32(handle_, "attr1", 100);
127     mediametrics_setInt64(handle_, "attr2", 1e10);
128     mediametrics_setDouble(handle_, "attr3", 100.0);
129     mediametrics_setRate(handle_, "attr4", 30, 1000);
130     mediametrics_setCString(handle_, "attr5", "test_string");
131     mediametrics_setUid(handle_, 10000);
132 
133     EXPECT_TRUE(mediametrics_selfRecord(handle_));
134 }
135 
TEST_F(MediaMetricsTest,testIsEnabled)136 TEST_F(MediaMetricsTest, testIsEnabled) {
137     EXPECT_TRUE(mediametrics_isEnabled());
138 }
139 
main(int argc,char ** argv)140 int main(int argc, char **argv) {
141     testing::InitGoogleTest(&argc, argv);
142 
143     return RUN_ALL_TESTS();
144 }
145 
146