MediaMetrics: Add Defer class

This is a helper which assists logging on exit of a method.

Test: atest mediametrics_tests
Bug: 138583596
Change-Id: I73c0e7e469dc9fd1b29934ef308f9d2f15146bec
gugelfrei
Andy Hung 5 years ago
parent 692870bd73
commit 37de9b7193

@ -131,6 +131,25 @@ static inline bool startsWith(const std::string &s, const char (&comp)[N]) {
return !strncmp(s.c_str(), comp, N - 1);
}
static inline bool startsWith(const std::string& s, const std::string& comp) {
return !strncmp(s.c_str(), comp.c_str(), comp.size() - 1);
}
/**
* Defers a function to run in the destructor.
*
* This helper class is used to log results on exit of a method.
*/
class Defer {
public:
template <typename U>
Defer(U &&f) : mThunk(std::forward<U>(f)) {}
~Defer() { mThunk(); }
private:
const std::function<void()> mThunk;
};
/**
* Media Metrics BaseItem
*

@ -35,6 +35,23 @@ static size_t countNewlines(const char *s) {
return count;
}
TEST(mediametrics_tests, startsWith) {
std::string s("test");
ASSERT_EQ(true, android::mediametrics::startsWith(s, "te"));
ASSERT_EQ(true, android::mediametrics::startsWith(s, std::string("tes")));
ASSERT_EQ(false, android::mediametrics::startsWith(s, "ts"));
ASSERT_EQ(false, android::mediametrics::startsWith(s, std::string("est")));
}
TEST(mediametrics_tests, defer) {
bool check = false;
{
android::mediametrics::Defer defer([&] { check = true; });
ASSERT_EQ(false, check);
}
ASSERT_EQ(true, check);
}
TEST(mediametrics_tests, instantiate) {
sp mediaMetrics = new MediaMetricsService();
status_t status;

Loading…
Cancel
Save