[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | #include "chrome/browser/sync/sync_js_controller.h" |
| 6 | |
| 7 | #include "base/message_loop.h" |
| 8 | #include "base/values.h" |
[email protected] | 649840c | 2011-08-10 12:58:00 | [diff] [blame] | 9 | #include "chrome/browser/sync/js/js_arg_list.h" |
| 10 | #include "chrome/browser/sync/js/js_event_details.h" |
| 11 | #include "chrome/browser/sync/js/js_test_util.h" |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 12 | #include "testing/gmock/include/gmock/gmock.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace browser_sync { |
| 16 | namespace { |
| 17 | |
| 18 | using ::testing::_; |
| 19 | using ::testing::InSequence; |
| 20 | using ::testing::Mock; |
| 21 | using ::testing::StrictMock; |
| 22 | |
| 23 | class SyncJsControllerTest : public testing::Test { |
| 24 | protected: |
| 25 | void PumpLoop() { |
| 26 | message_loop_.RunAllPending(); |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | MessageLoop message_loop_; |
| 31 | }; |
| 32 | |
| 33 | TEST_F(SyncJsControllerTest, Messages) { |
| 34 | InSequence dummy; |
| 35 | // |mock_backend| needs to outlive |sync_js_controller|. |
| 36 | StrictMock<MockJsBackend> mock_backend; |
| 37 | SyncJsController sync_js_controller; |
| 38 | |
| 39 | ListValue arg_list1, arg_list2; |
| 40 | arg_list1.Append(Value::CreateBooleanValue(false)); |
| 41 | arg_list2.Append(Value::CreateIntegerValue(5)); |
| 42 | JsArgList args1(&arg_list1), args2(&arg_list2); |
| 43 | |
| 44 | // TODO(akalin): Write matchers for WeakHandle and use them here |
| 45 | // instead of _. |
| 46 | EXPECT_CALL(mock_backend, SetJsEventHandler(_)); |
| 47 | EXPECT_CALL(mock_backend, ProcessJsMessage("test1", HasArgs(args2), _)); |
| 48 | EXPECT_CALL(mock_backend, ProcessJsMessage("test2", HasArgs(args1), _)); |
| 49 | |
| 50 | sync_js_controller.AttachJsBackend(mock_backend.AsWeakHandle()); |
| 51 | sync_js_controller.ProcessJsMessage("test1", args2, |
| 52 | WeakHandle<JsReplyHandler>()); |
| 53 | sync_js_controller.ProcessJsMessage("test2", args1, |
| 54 | WeakHandle<JsReplyHandler>()); |
| 55 | PumpLoop(); |
| 56 | |
| 57 | // Let destructor of |sync_js_controller| call RemoveBackend(). |
| 58 | } |
| 59 | |
| 60 | TEST_F(SyncJsControllerTest, QueuedMessages) { |
| 61 | // |mock_backend| needs to outlive |sync_js_controller|. |
| 62 | StrictMock<MockJsBackend> mock_backend; |
| 63 | SyncJsController sync_js_controller; |
| 64 | |
| 65 | ListValue arg_list1, arg_list2; |
| 66 | arg_list1.Append(Value::CreateBooleanValue(false)); |
| 67 | arg_list2.Append(Value::CreateIntegerValue(5)); |
| 68 | JsArgList args1(&arg_list1), args2(&arg_list2); |
| 69 | |
| 70 | // Should queue messages. |
| 71 | sync_js_controller.ProcessJsMessage("test1", args2, |
| 72 | WeakHandle<JsReplyHandler>()); |
| 73 | sync_js_controller.ProcessJsMessage("test2", args1, |
| 74 | WeakHandle<JsReplyHandler>()); |
| 75 | |
| 76 | Mock::VerifyAndClearExpectations(&mock_backend); |
| 77 | |
| 78 | // TODO(akalin): Write matchers for WeakHandle and use them here |
| 79 | // instead of _. |
| 80 | EXPECT_CALL(mock_backend, SetJsEventHandler(_)); |
| 81 | EXPECT_CALL(mock_backend, ProcessJsMessage("test1", HasArgs(args2), _)); |
| 82 | EXPECT_CALL(mock_backend, ProcessJsMessage("test2", HasArgs(args1), _)); |
| 83 | |
| 84 | // Should call the queued messages. |
| 85 | sync_js_controller.AttachJsBackend(mock_backend.AsWeakHandle()); |
| 86 | PumpLoop(); |
| 87 | |
| 88 | // Should do nothing. |
| 89 | sync_js_controller.AttachJsBackend(WeakHandle<JsBackend>()); |
| 90 | PumpLoop(); |
| 91 | |
| 92 | // Should also do nothing. |
| 93 | sync_js_controller.AttachJsBackend(WeakHandle<JsBackend>()); |
| 94 | PumpLoop(); |
| 95 | } |
| 96 | |
| 97 | TEST_F(SyncJsControllerTest, Events) { |
| 98 | InSequence dummy; |
| 99 | SyncJsController sync_js_controller; |
| 100 | |
| 101 | DictionaryValue details_dict1, details_dict2; |
| 102 | details_dict1.SetString("foo", "bar"); |
| 103 | details_dict2.SetInteger("baz", 5); |
| 104 | JsEventDetails details1(&details_dict1), details2(&details_dict2); |
| 105 | |
| 106 | StrictMock<MockJsEventHandler> event_handler1, event_handler2; |
| 107 | EXPECT_CALL(event_handler1, HandleJsEvent("event", HasDetails(details1))); |
| 108 | EXPECT_CALL(event_handler2, HandleJsEvent("event", HasDetails(details1))); |
| 109 | EXPECT_CALL(event_handler1, |
| 110 | HandleJsEvent("anotherevent", HasDetails(details2))); |
| 111 | EXPECT_CALL(event_handler2, |
| 112 | HandleJsEvent("anotherevent", HasDetails(details2))); |
| 113 | |
| 114 | sync_js_controller.AddJsEventHandler(&event_handler1); |
| 115 | sync_js_controller.AddJsEventHandler(&event_handler2); |
| 116 | sync_js_controller.HandleJsEvent("event", details1); |
| 117 | sync_js_controller.HandleJsEvent("anotherevent", details2); |
| 118 | sync_js_controller.RemoveJsEventHandler(&event_handler1); |
| 119 | sync_js_controller.RemoveJsEventHandler(&event_handler2); |
| 120 | sync_js_controller.HandleJsEvent("droppedevent", details2); |
| 121 | |
| 122 | PumpLoop(); |
| 123 | } |
| 124 | |
| 125 | } // namespace |
| 126 | } // namespace browser_sync |