| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 
|  | 2 // Use of this source code is governed by a BSD-style license that can be | 
|  | 3 // found in the LICENSE file. | 
|  | 4 | 
|  | 5 // This file provides some utility classes that help with testing APIs which use | 
|  | 6 // callbacks. | 
|  | 7 // | 
|  | 8 // -- InvokeRunnable -- | 
|  | 9 // The InvokeRunnable is an action that can be used a gMock mock object to | 
|  | 10 // invoke the Run() method on mock argument.  Example: | 
|  | 11 // | 
|  | 12 //   class MockFoo : public Foo { | 
|  | 13 //    public: | 
|  | 14 //     MOCK_METHOD0(DoSomething, void(Task* done_cb)); | 
|  | 15 //   }; | 
|  | 16 // | 
|  | 17 //   EXPECT_CALL(foo, DoSomething(_)).WillOnce(WithArg<0>(InvokeRunnable())); | 
|  | 18 // | 
|  | 19 // Then you pass "foo" to something that will eventually call DoSomething(). | 
|  | 20 // The mock action will ensure that passed in done_cb is invoked. | 
|  | 21 // | 
|  | 22 // | 
|  | 23 // -- TaskMocker -- | 
|  | 24 // The TaskMocker class lets you create mock callbacks.  Callbacks are | 
|  | 25 // difficult to mock because ownership of the callback object is often passed | 
|  | 26 // to the funciton being invoked.  TaskMocker solves this by providing a | 
|  | 27 // GetTask() function that creates a new, single-use task that delegates to | 
|  | 28 // the originating TaskMocker object.  Expectations are placed on the | 
|  | 29 // originating TaskMocker object.  Each callback retrieved by GetTask() is | 
|  | 30 // tracked to ensure that it is properly deleted.  The TaskMocker expects to | 
|  | 31 // outlive all the callbacks retrieved by GetTask(). | 
|  | 32 // | 
|  | 33 // Example: | 
|  | 34 // | 
|  | 35 //   TaskMocker done_cb; | 
|  | 36 //   EXPECT_CALL(done_cb, Run()).Times(3); | 
|  | 37 // | 
|  | 38 //   func1(done_cb.GetTask()); | 
|  | 39 //   func2(done_cb.GetTask()); | 
|  | 40 //   func3(done_cb.GetTask()); | 
|  | 41 // | 
|  | 42 //   // All 3 callbacks from GetTask() should be deleted before done_cb goes out | 
|  | 43 //   // of scope. | 
|  | 44 // | 
|  | 45 // This class is not threadsafe. | 
|  | 46 // | 
|  | 47 // TODO(ajwong): Is it even worth bothering with gmock here? | 
|  | 48 // TODO(ajwong): Move MockFilterCallback here and merge the implementation | 
|  | 49 // differences. | 
|  | 50 | 
|  | 51 #ifndef MEDIA_BASE_MOCK_TASK_H_ | 
|  | 52 #define MEDIA_BASE_MOCK_TASK_H_ | 
|  | 53 | 
|  | 54 #include "base/task.h" | 
|  | 55 #include "testing/gmock/include/gmock/gmock.h" | 
|  | 56 | 
|  | 57 namespace media { | 
|  | 58 | 
|  | 59 ACTION(InvokeRunnable) { | 
|  | 60   arg0->Run(); | 
|  | 61   delete arg0; | 
|  | 62 } | 
|  | 63 | 
|  | 64 class TaskMocker { | 
|  | 65  public: | 
|  | 66   TaskMocker() | 
|  | 67       : outstanding_tasks_(0) { | 
|  | 68   } | 
|  | 69   ~TaskMocker() { | 
|  | 70     CHECK(outstanding_tasks_ == 0) | 
|  | 71         << "If outstanding_tasks_ is not zero, tasks have been leaked."; | 
|  | 72   } | 
|  | 73 | 
|  | 74   Task* CreateTask() { | 
|  | 75     return new CountingTask(this); | 
|  | 76   } | 
|  | 77 | 
|  | 78   MOCK_METHOD0(Run, void()); | 
|  | 79 | 
|  | 80  private: | 
|  | 81   friend class CountingTask; | 
|  | 82   class CountingTask : public Task { | 
|  | 83    public: | 
|  | 84     CountingTask(TaskMocker* origin) | 
|  | 85         : origin_(origin) { | 
|  | 86       origin_->outstanding_tasks_++; | 
|  | 87     } | 
|  | 88 | 
|  | 89     virtual void Run() { | 
|  | 90       origin_->Run(); | 
|  | 91     } | 
|  | 92 | 
|  | 93     virtual ~CountingTask() { | 
|  | 94       origin_->outstanding_tasks_--; | 
|  | 95     } | 
|  | 96 | 
|  | 97    private: | 
|  | 98     TaskMocker* origin_; | 
|  | 99 | 
|  | 100     DISALLOW_COPY_AND_ASSIGN(CountingTask); | 
|  | 101   }; | 
|  | 102 | 
|  | 103   int outstanding_tasks_; | 
|  | 104 | 
|  | 105   DISALLOW_COPY_AND_ASSIGN(TaskMocker); | 
|  | 106 }; | 
|  | 107 | 
|  | 108 }  // namespace media | 
|  | 109 | 
|  | 110 #endif  //MEDIA_BASE_MOCK_TASK_H_ | 
| OLD | NEW | 
|---|