chromiumembedded / cef

Chromium Embedded Framework (CEF). A simple framework for embedding Chromium-based browsers in other applications.
https://bitbucket.org/chromiumembedded/cef/
Other
3.36k stars 467 forks source link

Provide gMock headers for CEF API classes #3145

Open magreenblatt opened 3 years ago

magreenblatt commented 3 years ago

Original report by me.


Applications using CEF may wish to write unit tests to verify expected CEF API behaviors without necessarily initializing CEF. GoogleTest supports this concept via gMock. We could rather easily generate mocks for CEF API classes using the existing translator tooling. Alternatively, we could use the Mock class generator provided by Google.

Example mock headers:

#include <include/cef_browser.h>
#include <include/cef_frame.h>
#include "gmock/gmock.h"

class MockCefBrowser : public CefBrowser {
 public:
  MockCefBrowser() {};

  ...
  MOCK_METHOD(CefRefPtr<CefFrame>, GetMainFrame, ());
  ...

 private:
  IMPLEMENT_REFCOUNTING(MockCefBrowser);
};

class MockCefFrame : public CefFrame {
 public:
  MockCefFrame() {};

  ...
  MOCK_METHOD(CefString, GetURL, ());
  ...

 private:
  IMPLEMENT_REFCOUNTING(MockCefFrame);
};

Example test code:

class FrameTest : public ::testing::Test {
 protected:
  void SetUp() override {
    browser_ = new MockCefBrowser();
    frame_ = new MockCefFrame();

    EXPECT_CALL(*browser_, GetMainFrame()).WillRepeatedly(Return(frame_));
    EXPECT_CALL(*frame_, GetURL()).WillRepeatedly(Return(CefString(URL)));
  }

  static constexpr char URL[] = "http://example.com";

  CefRefPtr<MockCefBrowser> browser_;
  CefRefPtr<MockCefFrame> frame_;
};

TEST_F(FrameTest, TestGetURL) {
  EXPECT_STREQ(browser_->GetMainFrame()->GetURL().ToString().c_str(), URL);
  ...
}

magreenblatt commented 3 years ago