SimplePredicates have a configurable initial value that can be set to unknown or false. Previously, CombinationPredicates and MetricProducers were automatically initialized with an unknown condition value instead of being synced with the conditions of the SimplePredicates that they rely on. An initial condition cache is used to store initial conditions of ConditionTrackers after they are initialized. CombinationConditionTrackers evaluate their initial condition based on the initial conditions of their child SimpleConditionTrackers. MetricProducers also use the initial condition cache to set its condition during initialization. Added unit tests in SimpleConditionTracker_test to check that SimpleConditionTrackers have the correct initial conditions based on what the InitialValue is set to and the condition is updated properly after the first few condition change events. Added unit test in MetricsManager_test to check that all ConditionTrackers and MetricProducers have the correct initial conditions. Test: m statsd_test && adb sync data && adb shell data/nativetest64/statsd_test/statsd_test64 Bug: b/156762672 Change-Id: I9f6088f8c92fb18eb2ca8632aaa338fb0ed8e679
168 lines
5.5 KiB
C++
168 lines
5.5 KiB
C++
// Copyright (C) 2017 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include "condition/condition_util.h"
|
|
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <stdio.h>
|
|
#include <vector>
|
|
|
|
using namespace android::os::statsd;
|
|
using std::vector;
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
TEST(ConditionTrackerTest, TestUnknownCondition) {
|
|
LogicalOperation operation = LogicalOperation::AND;
|
|
|
|
vector<int> children;
|
|
children.push_back(0);
|
|
children.push_back(1);
|
|
children.push_back(2);
|
|
|
|
vector<ConditionState> conditionResults;
|
|
conditionResults.push_back(ConditionState::kUnknown);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
|
|
EXPECT_EQ(evaluateCombinationCondition(children, operation, conditionResults),
|
|
ConditionState::kUnknown);
|
|
}
|
|
|
|
TEST(ConditionTrackerTest, TestAndCondition) {
|
|
// Set up the matcher
|
|
LogicalOperation operation = LogicalOperation::AND;
|
|
|
|
vector<int> children;
|
|
children.push_back(0);
|
|
children.push_back(1);
|
|
children.push_back(2);
|
|
|
|
vector<ConditionState> conditionResults;
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
|
|
EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
conditionResults.clear();
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
|
|
EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
}
|
|
|
|
TEST(ConditionTrackerTest, TestOrCondition) {
|
|
// Set up the matcher
|
|
LogicalOperation operation = LogicalOperation::OR;
|
|
|
|
vector<int> children;
|
|
children.push_back(0);
|
|
children.push_back(1);
|
|
children.push_back(2);
|
|
|
|
vector<ConditionState> conditionResults;
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
|
|
EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
conditionResults.clear();
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
|
|
EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
}
|
|
|
|
TEST(ConditionTrackerTest, TestNotCondition) {
|
|
// Set up the matcher
|
|
LogicalOperation operation = LogicalOperation::NOT;
|
|
|
|
vector<int> children;
|
|
children.push_back(0);
|
|
|
|
vector<ConditionState> conditionResults;
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
|
|
EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
conditionResults.clear();
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
children.clear();
|
|
conditionResults.clear();
|
|
EXPECT_EQ(evaluateCombinationCondition(children, operation, conditionResults),
|
|
ConditionState::kUnknown);
|
|
}
|
|
|
|
TEST(ConditionTrackerTest, TestNandCondition) {
|
|
// Set up the matcher
|
|
LogicalOperation operation = LogicalOperation::NAND;
|
|
|
|
vector<int> children;
|
|
children.push_back(0);
|
|
children.push_back(1);
|
|
|
|
vector<ConditionState> conditionResults;
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
|
|
EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
conditionResults.clear();
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
conditionResults.clear();
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
}
|
|
|
|
TEST(ConditionTrackerTest, TestNorCondition) {
|
|
// Set up the matcher
|
|
LogicalOperation operation = LogicalOperation::NOR;
|
|
|
|
vector<int> children;
|
|
children.push_back(0);
|
|
children.push_back(1);
|
|
|
|
vector<ConditionState> conditionResults;
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
|
|
EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
conditionResults.clear();
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
conditionResults.push_back(ConditionState::kFalse);
|
|
EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
|
|
conditionResults.clear();
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
conditionResults.push_back(ConditionState::kTrue);
|
|
EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
|
|
}
|
|
|
|
#else
|
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
|
#endif
|