Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37de9b71 authored by Andy Hung's avatar Andy Hung
Browse files

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
parent 692870bd
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -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
 *
+17 −0
Original line number Diff line number Diff line
@@ -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;