1 // Copyright 2013 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 #ifndef MOJO_EDK_SYSTEM_CORE_TEST_BASE_H_
6 #define MOJO_EDK_SYSTEM_CORE_TEST_BASE_H_
7 
8 #include <stddef.h>
9 
10 #include "base/macros.h"
11 #include "base/synchronization/lock.h"
12 #include "mojo/edk/embedder/embedder_internal.h"
13 #include "mojo/edk/system/test_utils.h"
14 #include "mojo/public/c/system/types.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 namespace mojo {
18 namespace edk {
19 
20 class Core;
21 class Awakable;
22 
23 namespace test {
24 
25 class CoreTestBase_MockHandleInfo;
26 
27 class CoreTestBase : public testing::Test {
28  public:
29   using MockHandleInfo = CoreTestBase_MockHandleInfo;
30 
31   CoreTestBase();
32   ~CoreTestBase() override;
33 
34  protected:
35   // |info| must remain alive until the returned handle is closed.
36   MojoHandle CreateMockHandle(MockHandleInfo* info);
37 
38   Core* core();
39 
40  private:
41   DISALLOW_COPY_AND_ASSIGN(CoreTestBase);
42 };
43 
44 class CoreTestBase_MockHandleInfo {
45  public:
46   CoreTestBase_MockHandleInfo();
47   ~CoreTestBase_MockHandleInfo();
48 
49   unsigned GetCtorCallCount() const;
50   unsigned GetDtorCallCount() const;
51   unsigned GetCloseCallCount() const;
52   unsigned GetWriteMessageCallCount() const;
53   unsigned GetReadMessageCallCount() const;
54   unsigned GetWriteDataCallCount() const;
55   unsigned GetBeginWriteDataCallCount() const;
56   unsigned GetEndWriteDataCallCount() const;
57   unsigned GetReadDataCallCount() const;
58   unsigned GetBeginReadDataCallCount() const;
59   unsigned GetEndReadDataCallCount() const;
60   unsigned GetAddAwakableCallCount() const;
61   unsigned GetRemoveAwakableCallCount() const;
62 
63   size_t GetAddedAwakableSize() const;
64   Awakable* GetAddedAwakableAt(unsigned i) const;
65 
66   // For use by |MockDispatcher|:
67   void IncrementCtorCallCount();
68   void IncrementDtorCallCount();
69   void IncrementCloseCallCount();
70   void IncrementWriteMessageCallCount();
71   void IncrementReadMessageCallCount();
72   void IncrementWriteDataCallCount();
73   void IncrementBeginWriteDataCallCount();
74   void IncrementEndWriteDataCallCount();
75   void IncrementReadDataCallCount();
76   void IncrementBeginReadDataCallCount();
77   void IncrementEndReadDataCallCount();
78   void IncrementAddAwakableCallCount();
79   void IncrementRemoveAwakableCallCount();
80 
81   void AllowAddAwakable(bool alllow);
82   bool IsAddAwakableAllowed() const;
83   void AwakableWasAdded(Awakable*);
84 
85  private:
86   mutable base::Lock lock_;  // Protects the following members.
87   unsigned ctor_call_count_;
88   unsigned dtor_call_count_;
89   unsigned close_call_count_;
90   unsigned write_message_call_count_;
91   unsigned read_message_call_count_;
92   unsigned write_data_call_count_;
93   unsigned begin_write_data_call_count_;
94   unsigned end_write_data_call_count_;
95   unsigned read_data_call_count_;
96   unsigned begin_read_data_call_count_;
97   unsigned end_read_data_call_count_;
98   unsigned add_awakable_call_count_;
99   unsigned remove_awakable_call_count_;
100 
101   bool add_awakable_allowed_;
102   std::vector<Awakable*> added_awakables_;
103 
104   DISALLOW_COPY_AND_ASSIGN(CoreTestBase_MockHandleInfo);
105 };
106 
107 }  // namespace test
108 }  // namespace edk
109 }  // namespace mojo
110 
111 #endif  // MOJO_EDK_SYSTEM_CORE_TEST_BASE_H_
112