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

Commit 72c0b57f authored by Fan Xu's avatar Fan Xu
Browse files

Add a helper function to get service runtime

The bufferhub binder service would be used a lot after migrated to
binder, so have a helper function and including all the checks inside it
should help a lot.

Also rewrite the test to use and check this function as well.

Test: "atest buffer_hub_binder_service-test" passed.
Bug: 112338294
Change-Id: Ifa66f25cc284b2c03586b8dc75e05801bf393fa6
parent fada6b89
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -58,5 +58,24 @@ status_t BufferHubBinderService::dump(int fd, const Vector<String16>& args) {
  return NO_ERROR;
}

sp<IBufferHub> BufferHubBinderService::getServiceProxy() {
  sp<IServiceManager> sm = defaultServiceManager();
  sp<IBinder> service = sm->checkService(String16(getServiceName()));

  if (service == nullptr) {
    ALOGE("getServiceProxy(): %s binder service not found!", getServiceName());
    return nullptr;
  }

  sp<IBufferHub> ret = interface_cast<IBufferHub>(service);
  if (ret == nullptr) {
    ALOGE("getServiceProxy(): %s binder service type casting error!",
          getServiceName());
    return nullptr;
  }

  return ret;
}

}  // namespace dvr
}  // namespace android
+3 −0
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@ class BufferHubBinderService : public BinderService<BufferHubBinderService>,
  // usage: adb shell dumpsys bufferhubd
  virtual status_t dump(int fd, const Vector<String16>& args) override;

  // Helper function to get the BpReference to this service
  static sp<IBufferHub> getServiceProxy();

 private:
  std::shared_ptr<BufferHubService> pdx_service_;
};
+4 −5
Original line number Diff line number Diff line
#include <binder/IServiceManager.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <private/dvr/buffer_hub_binder.h>
@@ -8,6 +7,8 @@ namespace dvr {

namespace {

using testing::Ne;

class BufferHubBinderServiceTest : public ::testing::Test {
  // Add setup and teardown if necessary
};
@@ -15,10 +16,8 @@ class BufferHubBinderServiceTest : public ::testing::Test {
TEST_F(BufferHubBinderServiceTest, TestInitialize) {
  // Create a new service will kill the current one.
  // So just check if Binder service is running
  sp<IServiceManager> sm = defaultServiceManager();
  sp<IBinder> service =
      sm->checkService(String16(BufferHubBinderService::getServiceName()));
  EXPECT_THAT(service, ::testing::Ne(nullptr));
  sp<IBufferHub> service = BufferHubBinderService::getServiceProxy();
  EXPECT_THAT(service, Ne(nullptr));
}

}  // namespace