1 // Copyright (c) 2012 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 COMPONENTS_SYNC_JS_JS_TEST_UTIL_H_
6 #define COMPONENTS_SYNC_JS_JS_TEST_UTIL_H_
7 
8 #include <ostream>
9 #include <string>
10 
11 #include "base/memory/weak_ptr.h"
12 #include "components/sync/base/weak_handle.h"
13 #include "components/sync/js/js_backend.h"
14 #include "components/sync/js/js_controller.h"
15 #include "components/sync/js/js_event_handler.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 
18 namespace base {
19 class DictionaryValue;
20 }
21 
22 namespace syncer {
23 
24 class JsEventDetails;
25 
26 // Defined for googletest.  Equivalent to "*os << args.ToString()".
27 void PrintTo(const JsEventDetails& details, ::std::ostream* os);
28 
29 // A gmock matcher for JsEventDetails.  Use like:
30 //
31 //   EXPECT_CALL(mock, HandleJsEvent("foo", HasArgs(expected_details)));
32 ::testing::Matcher<const JsEventDetails&> HasDetails(
33     const JsEventDetails& expected_details);
34 
35 // Like HasDetails() but takes a DictionaryValue instead.
36 ::testing::Matcher<const JsEventDetails&> HasDetailsAsDictionary(
37     const base::DictionaryValue& expected_details);
38 
39 // Mocks.
40 
41 class MockJsBackend : public JsBackend,
42                       public base::SupportsWeakPtr<MockJsBackend> {
43  public:
44   MockJsBackend();
45   ~MockJsBackend() override;
46 
47   WeakHandle<JsBackend> AsWeakHandle();
48   MOCK_METHOD(void,
49               SetJsEventHandler,
50               (const WeakHandle<JsEventHandler>&),
51               (override));
52 };
53 
54 class MockJsController : public JsController,
55                          public base::SupportsWeakPtr<MockJsController> {
56  public:
57   MockJsController();
58   ~MockJsController() override;
59   MOCK_METHOD(void, AddJsEventHandler, (JsEventHandler*), (override));
60   MOCK_METHOD(void, RemoveJsEventHandler, (JsEventHandler*), (override));
61 };
62 
63 class MockJsEventHandler : public JsEventHandler,
64                            public base::SupportsWeakPtr<MockJsEventHandler> {
65  public:
66   MockJsEventHandler();
67   ~MockJsEventHandler() override;
68 
69   WeakHandle<JsEventHandler> AsWeakHandle();
70   MOCK_METHOD(void,
71               HandleJsEvent,
72               (const ::std::string&, const JsEventDetails&),
73               (override));
74 };
75 
76 }  // namespace syncer
77 
78 #endif  // COMPONENTS_SYNC_JS_JS_TEST_UTIL_H_
79