Mocking methods that return scoped_ptr

161 views
Skip to first unread message

Marshall Greenblatt

unread,
May 1, 2012, 4:01:07 PM5/1/12
to chromium-dev
Hi All,

I'm adding a new interface that returns values using scoped_ptr like this:

virtual scoped_ptr<gfx::Image> GetImageNamed(int resource_id) = 0;

I would like to mock out the new interface for testing purposes but I'm not sure how to handle these methods. For example, the following does not work because scoped_ptr has no copy constructor:

MOCK_METHOD1(GetImageNamed, scoped_ptr<gfx::Image>(int resource_id));

What is the correct way to mock these methods? Is there an existing mock implementation somewhere in the code that I could use as a model?

Thanks,
Marshall

Marshall Greenblatt

unread,
May 1, 2012, 4:41:57 PM5/1/12
to chromium-dev, ajw...@chromium.org

The problem using Gmock with scoped_ptr has been discussed previously:
http://groups.google.com/a/chromium.org/group/chromium-dev/msg/ddd7942f93b8ed4b

Was a good solution ever discovered?
 

Thanks,
Marshall

Sergey Ulanov

unread,
May 1, 2012, 5:06:10 PM5/1/12
to magree...@gmail.com, chromium-dev
You can use the following hack for such methods:

class MockObject {
 public:
  MOCK_METHOD0(ReturnsScopedPtrMock, Type*());
  virtual scoped_ptr<Type> ReturnsScopedPtr() OVERRIDE {
    return scoped_ptr<Type>(ReturnsScopedPtrMock());
  }
};

Then you set expectations for ReturnsScopedPtrMock() instead of ReturnsScopedPtr(). You can also apply similar hack to mock methods that accept scoped_ptr<> parameters (for example see MockVideoStub in remoting/protocol/protocol_mock_objects.h). I don't think there is a better solution right now.


--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Alexei Svitkine

unread,
May 1, 2012, 5:08:12 PM5/1/12
to ser...@chromium.org, magree...@gmail.com, chromium-dev
On Tue, May 1, 2012 at 5:06 PM, Sergey Ulanov <ser...@chromium.org> wrote:
>   MOCK_METHOD0(ReturnsScopedPtrMock, Type*());
>   virtual scoped_ptr<Type> ReturnsScopedPtr() OVERRIDE {
>     return scoped_ptr<Type>(ReturnsScopedPtrMock());
>   }

Sounds like it should be possible to make a new MOCK_SCOPED_PTR_* macro
that would do that...

-Alexei

Marshall Greenblatt

unread,
May 1, 2012, 5:13:20 PM5/1/12
to Sergey Ulanov, chromium-dev
On Tue, May 1, 2012 at 5:06 PM, Sergey Ulanov <ser...@chromium.org> wrote:
You can use the following hack for such methods:

class MockObject {
 public:
  MOCK_METHOD0(ReturnsScopedPtrMock, Type*());
  virtual scoped_ptr<Type> ReturnsScopedPtr() OVERRIDE {
    return scoped_ptr<Type>(ReturnsScopedPtrMock());
  }
};

Then you set expectations for ReturnsScopedPtrMock() instead of ReturnsScopedPtr(). You can also apply similar hack to mock methods that accept scoped_ptr<> parameters (for example see MockVideoStub in remoting/protocol/protocol_mock_objects.h). I don't think there is a better solution right now.

Thanks, that works :-).
 

Drew Wilson

unread,
May 1, 2012, 5:19:09 PM5/1/12
to magree...@gmail.com, Sergey Ulanov, chromium-dev
Reading through that other thread - did we ever start moving forward on migrating existing (or just new?) APIs to using scoped_ptr::Pass() instead of raw pointers? Or does Gmock provide enough friction that we're never really going to make widespread use of it?

-atw

Ami Fischman

unread,
May 1, 2012, 7:03:36 PM5/1/12
to atwi...@chromium.org, magree...@gmail.com, Sergey Ulanov, chromium-dev
Reading through that other thread - did we ever start moving forward on migrating existing (or just new?) APIs to using scoped_ptr::Pass() instead of raw pointers? Or does Gmock provide enough friction that we're never really going to make widespread use of it?

I haven't heard of the gmock-related friction stopping anyone.  codesearch says there are lots of uses of the new API.
Anecdote: I converted media/-related code and it was quite satisfying to replace comments like "takes ownership of |foo|" with the compiler-checked annotation of scoped_ptr/Pass().

Cheers,
-a

Wez

unread,
May 1, 2012, 7:30:15 PM5/1/12
to fisc...@chromium.org, atwi...@chromium.org, magree...@gmail.com, Sergey Ulanov, chromium-dev
+1... we've been migrating Chromoting to use Pass() and so far it's
been a relatively painless win for us. :)
Reply all
Reply to author
Forward
0 new messages