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 SYNC_JS_JS_TEST_UTIL_H_ 6 #define SYNC_JS_JS_TEST_UTIL_H_ 7 8 #include <ostream> 9 #include <string> 10 11 #include "base/memory/weak_ptr.h" 12 #include "sync/internal_api/public/util/weak_handle.h" 13 #include "sync/js/js_backend.h" 14 #include "sync/js/js_controller.h" 15 #include "sync/js/js_event_handler.h" 16 #include "sync/js/js_reply_handler.h" 17 #include "testing/gmock/include/gmock/gmock.h" 18 19 namespace base { 20 class DictionaryValue; 21 class ListValue; 22 } 23 24 namespace syncer { 25 26 class JsArgList; 27 class JsEventDetails; 28 29 // Defined for googletest. Equivalent to "*os << args.ToString()". 30 void PrintTo(const JsArgList& args, ::std::ostream* os); 31 void PrintTo(const JsEventDetails& details, ::std::ostream* os); 32 33 // A gmock matcher for JsArgList. Use like: 34 // 35 // EXPECT_CALL(mock, HandleJsReply("foo", HasArgs(expected_args))); 36 ::testing::Matcher<const JsArgList&> HasArgs(const JsArgList& expected_args); 37 38 // Like HasArgs() but takes a ListValue instead. 39 ::testing::Matcher<const JsArgList&> HasArgsAsList( 40 const base::ListValue& expected_args); 41 42 // A gmock matcher for JsEventDetails. Use like: 43 // 44 // EXPECT_CALL(mock, HandleJsEvent("foo", HasArgs(expected_details))); 45 ::testing::Matcher<const JsEventDetails&> HasDetails( 46 const JsEventDetails& expected_details); 47 48 // Like HasDetails() but takes a DictionaryValue instead. 49 ::testing::Matcher<const JsEventDetails&> HasDetailsAsDictionary( 50 const base::DictionaryValue& expected_details); 51 52 // Mocks. 53 54 class MockJsBackend : public JsBackend, 55 public base::SupportsWeakPtr<MockJsBackend> { 56 public: 57 MockJsBackend(); 58 virtual ~MockJsBackend(); 59 60 WeakHandle<JsBackend> AsWeakHandle(); 61 62 MOCK_METHOD1(SetJsEventHandler, void(const WeakHandle<JsEventHandler>&)); 63 MOCK_METHOD3(ProcessJsMessage, void(const ::std::string&, const JsArgList&, 64 const WeakHandle<JsReplyHandler>&)); 65 }; 66 67 class MockJsController : public JsController, 68 public base::SupportsWeakPtr<MockJsController> { 69 public: 70 MockJsController(); 71 virtual ~MockJsController(); 72 73 MOCK_METHOD1(AddJsEventHandler, void(JsEventHandler*)); 74 MOCK_METHOD1(RemoveJsEventHandler, void(JsEventHandler*)); 75 MOCK_METHOD3(ProcessJsMessage, 76 void(const ::std::string&, const JsArgList&, 77 const WeakHandle<JsReplyHandler>&)); 78 }; 79 80 class MockJsEventHandler 81 : public JsEventHandler, 82 public base::SupportsWeakPtr<MockJsEventHandler> { 83 public: 84 MockJsEventHandler(); 85 virtual ~MockJsEventHandler(); 86 87 WeakHandle<JsEventHandler> AsWeakHandle(); 88 89 MOCK_METHOD2(HandleJsEvent, 90 void(const ::std::string&, const JsEventDetails&)); 91 }; 92 93 class MockJsReplyHandler 94 : public JsReplyHandler, 95 public base::SupportsWeakPtr<MockJsReplyHandler> { 96 public: 97 MockJsReplyHandler(); 98 virtual ~MockJsReplyHandler(); 99 100 WeakHandle<JsReplyHandler> AsWeakHandle(); 101 102 MOCK_METHOD2(HandleJsReply, 103 void(const ::std::string&, const JsArgList&)); 104 }; 105 106 } // namespace syncer 107 108 #endif // SYNC_JS_JS_TEST_UTIL_H_ 109