summaryrefslogtreecommitdiffstats
path: root/chromium/sync/js/sync_js_controller_unittest.cc
blob: eca617c2d4528b5534250ea67c931c3c66e59091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/js/sync_js_controller.h"

#include "base/message_loop/message_loop.h"
#include "base/values.h"
#include "sync/js/js_arg_list.h"
#include "sync/js/js_event_details.h"
#include "sync/js/js_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {
namespace {

using ::testing::_;
using ::testing::InSequence;
using ::testing::Mock;
using ::testing::StrictMock;

class SyncJsControllerTest : public testing::Test {
 protected:
  void PumpLoop() {
    message_loop_.RunUntilIdle();
  }

 private:
  base::MessageLoop message_loop_;
};

ACTION_P(ReplyToMessage, reply_name) {
  arg2.Call(FROM_HERE, &JsReplyHandler::HandleJsReply, reply_name, JsArgList());
}

TEST_F(SyncJsControllerTest, Messages) {
  InSequence dummy;
  // |mock_backend| needs to outlive |sync_js_controller|.
  StrictMock<MockJsBackend> mock_backend;
  StrictMock<MockJsReplyHandler> mock_reply_handler;
  SyncJsController sync_js_controller;

  base::ListValue arg_list1, arg_list2;
  arg_list1.Append(new base::FundamentalValue(false));
  arg_list2.Append(new base::FundamentalValue(5));
  JsArgList args1(&arg_list1), args2(&arg_list2);

  EXPECT_CALL(mock_backend, SetJsEventHandler(_));
  EXPECT_CALL(mock_backend, ProcessJsMessage("test1", HasArgs(args2), _))
      .WillOnce(ReplyToMessage("test1_reply"));
  EXPECT_CALL(mock_backend, ProcessJsMessage("test2", HasArgs(args1), _))
      .WillOnce(ReplyToMessage("test2_reply"));

  sync_js_controller.AttachJsBackend(mock_backend.AsWeakHandle());
  sync_js_controller.ProcessJsMessage("test1",
                                      args2,
                                      mock_reply_handler.AsWeakHandle());
  sync_js_controller.ProcessJsMessage("test2",
                                      args1,
                                      mock_reply_handler.AsWeakHandle());

  // The replies should be waiting on our message loop.
  EXPECT_CALL(mock_reply_handler, HandleJsReply("test1_reply", _));
  EXPECT_CALL(mock_reply_handler, HandleJsReply("test2_reply", _));
  PumpLoop();

  // Let destructor of |sync_js_controller| call RemoveBackend().
}

TEST_F(SyncJsControllerTest, QueuedMessages) {
  // |mock_backend| needs to outlive |sync_js_controller|.
  StrictMock<MockJsBackend> mock_backend;
  StrictMock<MockJsReplyHandler> mock_reply_handler;
  SyncJsController sync_js_controller;

  base::ListValue arg_list1, arg_list2;
  arg_list1.Append(new base::FundamentalValue(false));
  arg_list2.Append(new base::FundamentalValue(5));
  JsArgList args1(&arg_list1), args2(&arg_list2);

  // Should queue messages.
  sync_js_controller.ProcessJsMessage(
      "test1",
      args2,
      mock_reply_handler.AsWeakHandle());
  sync_js_controller.ProcessJsMessage(
      "test2",
      args1,
      mock_reply_handler.AsWeakHandle());

  // Should do nothing.
  PumpLoop();
  Mock::VerifyAndClearExpectations(&mock_backend);


  // Should call the queued messages.
  EXPECT_CALL(mock_backend, SetJsEventHandler(_));
  EXPECT_CALL(mock_backend, ProcessJsMessage("test1", HasArgs(args2), _))
      .WillOnce(ReplyToMessage("test1_reply"));
  EXPECT_CALL(mock_backend, ProcessJsMessage("test2", HasArgs(args1), _))
      .WillOnce(ReplyToMessage("test2_reply"));
  EXPECT_CALL(mock_reply_handler, HandleJsReply("test1_reply", _));
  EXPECT_CALL(mock_reply_handler, HandleJsReply("test2_reply", _));

  sync_js_controller.AttachJsBackend(mock_backend.AsWeakHandle());
  PumpLoop();

  // Should do nothing.
  sync_js_controller.AttachJsBackend(WeakHandle<JsBackend>());
  PumpLoop();

  // Should also do nothing.
  sync_js_controller.AttachJsBackend(WeakHandle<JsBackend>());
  PumpLoop();
}

TEST_F(SyncJsControllerTest, Events) {
  InSequence dummy;
  SyncJsController sync_js_controller;

  base::DictionaryValue details_dict1, details_dict2;
  details_dict1.SetString("foo", "bar");
  details_dict2.SetInteger("baz", 5);
  JsEventDetails details1(&details_dict1), details2(&details_dict2);

  StrictMock<MockJsEventHandler> event_handler1, event_handler2;
  EXPECT_CALL(event_handler1, HandleJsEvent("event", HasDetails(details1)));
  EXPECT_CALL(event_handler2, HandleJsEvent("event", HasDetails(details1)));
  EXPECT_CALL(event_handler1,
              HandleJsEvent("anotherevent", HasDetails(details2)));
  EXPECT_CALL(event_handler2,
              HandleJsEvent("anotherevent", HasDetails(details2)));

  sync_js_controller.AddJsEventHandler(&event_handler1);
  sync_js_controller.AddJsEventHandler(&event_handler2);
  sync_js_controller.HandleJsEvent("event", details1);
  sync_js_controller.HandleJsEvent("anotherevent", details2);
  sync_js_controller.RemoveJsEventHandler(&event_handler1);
  sync_js_controller.RemoveJsEventHandler(&event_handler2);
  sync_js_controller.HandleJsEvent("droppedevent", details2);

  PumpLoop();
}

}  // namespace
}  // namespace syncer