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

Commit 370e9407 authored by Andy Hung's avatar Andy Hung Committed by Android (Google) Code Review
Browse files

Merge changes I73c0e7e4,I9161f98f

* changes:
  MediaMetrics: Add Defer class
  MediaMetrics: Add Elem getter to Item
parents cab8bfe6 37de9b71
Loading
Loading
Loading
Loading
+33 −9
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
 *
@@ -798,14 +817,14 @@ public:
    Item& operator=(Item&& other) = default;

    bool operator==(const Item& other) const {
        if (mPid != other.mPid
            || mUid != other.mUid
            || mPkgName != other.mPkgName
            || mPkgVersionCode != other.mPkgVersionCode
            || mKey != other.mKey
            || mTimestamp != other.mTimestamp
            || mProps != other.mProps) return false;
         return true;
        return mPid == other.mPid
            && mUid == other.mUid
            && mPkgName == other.mPkgName
            && mPkgVersionCode == other.mPkgVersionCode
            && mKey == other.mKey
            && mTimestamp == other.mTimestamp
            && mProps == other.mProps
            ;
    }
    bool operator!=(const Item& other) const {
        return !(*this == other);
@@ -935,6 +954,11 @@ public:
        return get(key, value);
    }

    const Prop::Elem* get(const char *key) const {
        const Prop *prop = findProp(key);
        return prop == nullptr ? nullptr : &prop->get();
    }

        // Deliver the item to MediaMetrics
        bool selfrecord();

+22 −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;
@@ -262,27 +279,32 @@ TEST(mediametrics_tests, item_iteration) {
          int32_t i32;
          ASSERT_TRUE(prop.get(&i32));
          ASSERT_EQ(1, i32);
          ASSERT_EQ(1, std::get<int32_t>(prop.get()));
          mask |= 1;
      } else if (!strcmp(name, "i64")) {
          int64_t i64;
          ASSERT_TRUE(prop.get(&i64));
          ASSERT_EQ(2, i64);
          ASSERT_EQ(2, std::get<int64_t>(prop.get()));
          mask |= 2;
      } else if (!strcmp(name, "double")) {
          double d;
          ASSERT_TRUE(prop.get(&d));
          ASSERT_EQ(3.125, d);
          ASSERT_EQ(3.125, std::get<double>(prop.get()));
          mask |= 4;
      } else if (!strcmp(name, "string")) {
          std::string s;
          ASSERT_TRUE(prop.get(&s));
          ASSERT_EQ("abc", s);
          ASSERT_EQ(s, std::get<std::string>(prop.get()));
          mask |= 8;
      } else if (!strcmp(name, "rate")) {
          std::pair<int64_t, int64_t> r;
          ASSERT_TRUE(prop.get(&r));
          ASSERT_EQ(11, r.first);
          ASSERT_EQ(12, r.second);
          ASSERT_EQ(r, std::get<decltype(r)>(prop.get()));
          mask |= 16;
      } else {
          FAIL();