1 /*
2  * Copyright (C) 2020, 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 <stats_pull_atom_callback.h>
20 
21 namespace {
22 
23 static const int64_t DEFAULT_COOL_DOWN_MILLIS = 1000LL;  // 1 second.
24 static const int64_t DEFAULT_TIMEOUT_MILLIS = 1500LL;    // 1.5 seconds.
25 
26 }  // anonymous namespace
27 
TEST(AStatsManager_PullAtomMetadataTest,TestEmpty)28 TEST(AStatsManager_PullAtomMetadataTest, TestEmpty) {
29     AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain();
30     EXPECT_EQ(AStatsManager_PullAtomMetadata_getCoolDownMillis(metadata), DEFAULT_COOL_DOWN_MILLIS);
31     EXPECT_EQ(AStatsManager_PullAtomMetadata_getTimeoutMillis(metadata), DEFAULT_TIMEOUT_MILLIS);
32     EXPECT_EQ(AStatsManager_PullAtomMetadata_getNumAdditiveFields(metadata), 0);
33     AStatsManager_PullAtomMetadata_release(metadata);
34 }
35 
TEST(AStatsManager_PullAtomMetadataTest,TestSetTimeoutMillis)36 TEST(AStatsManager_PullAtomMetadataTest, TestSetTimeoutMillis) {
37     int64_t timeoutMillis = 500;
38     AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain();
39     AStatsManager_PullAtomMetadata_setTimeoutMillis(metadata, timeoutMillis);
40     EXPECT_EQ(AStatsManager_PullAtomMetadata_getCoolDownMillis(metadata), DEFAULT_COOL_DOWN_MILLIS);
41     EXPECT_EQ(AStatsManager_PullAtomMetadata_getTimeoutMillis(metadata), timeoutMillis);
42     EXPECT_EQ(AStatsManager_PullAtomMetadata_getNumAdditiveFields(metadata), 0);
43     AStatsManager_PullAtomMetadata_release(metadata);
44 }
45 
TEST(AStatsManager_PullAtomMetadataTest,TestSetCoolDownMillis)46 TEST(AStatsManager_PullAtomMetadataTest, TestSetCoolDownMillis) {
47     int64_t coolDownMillis = 10000;
48     AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain();
49     AStatsManager_PullAtomMetadata_setCoolDownMillis(metadata, coolDownMillis);
50     EXPECT_EQ(AStatsManager_PullAtomMetadata_getCoolDownMillis(metadata), coolDownMillis);
51     EXPECT_EQ(AStatsManager_PullAtomMetadata_getTimeoutMillis(metadata), DEFAULT_TIMEOUT_MILLIS);
52     EXPECT_EQ(AStatsManager_PullAtomMetadata_getNumAdditiveFields(metadata), 0);
53     AStatsManager_PullAtomMetadata_release(metadata);
54 }
55 
TEST(AStatsManager_PullAtomMetadataTest,TestSetAdditiveFields)56 TEST(AStatsManager_PullAtomMetadataTest, TestSetAdditiveFields) {
57     const int numFields = 3;
58     int inputFields[numFields] = {2, 4, 6};
59     AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain();
60     AStatsManager_PullAtomMetadata_setAdditiveFields(metadata, inputFields, numFields);
61     EXPECT_EQ(AStatsManager_PullAtomMetadata_getCoolDownMillis(metadata), DEFAULT_COOL_DOWN_MILLIS);
62     EXPECT_EQ(AStatsManager_PullAtomMetadata_getTimeoutMillis(metadata), DEFAULT_TIMEOUT_MILLIS);
63     EXPECT_EQ(AStatsManager_PullAtomMetadata_getNumAdditiveFields(metadata), numFields);
64     int outputFields[numFields];
65     AStatsManager_PullAtomMetadata_getAdditiveFields(metadata, outputFields);
66     for (int i = 0; i < numFields; i++) {
67         EXPECT_EQ(inputFields[i], outputFields[i]);
68     }
69     AStatsManager_PullAtomMetadata_release(metadata);
70 }
71 
TEST(AStatsManager_PullAtomMetadataTest,TestSetAllElements)72 TEST(AStatsManager_PullAtomMetadataTest, TestSetAllElements) {
73     int64_t timeoutMillis = 500;
74     int64_t coolDownMillis = 10000;
75     const int numFields = 3;
76     int inputFields[numFields] = {2, 4, 6};
77 
78     AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain();
79     AStatsManager_PullAtomMetadata_setTimeoutMillis(metadata, timeoutMillis);
80     AStatsManager_PullAtomMetadata_setCoolDownMillis(metadata, coolDownMillis);
81     AStatsManager_PullAtomMetadata_setAdditiveFields(metadata, inputFields, numFields);
82 
83     EXPECT_EQ(AStatsManager_PullAtomMetadata_getCoolDownMillis(metadata), coolDownMillis);
84     EXPECT_EQ(AStatsManager_PullAtomMetadata_getTimeoutMillis(metadata), timeoutMillis);
85     EXPECT_EQ(AStatsManager_PullAtomMetadata_getNumAdditiveFields(metadata), numFields);
86     int outputFields[numFields];
87     AStatsManager_PullAtomMetadata_getAdditiveFields(metadata, outputFields);
88     for (int i = 0; i < numFields; i++) {
89         EXPECT_EQ(inputFields[i], outputFields[i]);
90     }
91     AStatsManager_PullAtomMetadata_release(metadata);
92 }
93