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.
5 #include "sync/internal_api/js_mutation_event_observer.h"
7 #include "base/basictypes.h"
8 #include "base/message_loop.h"
9 #include "base/values.h"
10 #include "sync/internal_api/public/base/model_type.h"
11 #include "sync/internal_api/public/util/weak_handle.h"
12 #include "sync/js/js_event_details.h"
13 #include "sync/js/js_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 using ::testing::InSequence
;
20 using ::testing::StrictMock
;
22 class JsMutationEventObserverTest
: public testing::Test
{
24 JsMutationEventObserverTest() {
25 js_mutation_event_observer_
.SetJsEventHandler(
26 mock_js_event_handler_
.AsWeakHandle());
30 // This must be destroyed after the member variables below in order
31 // for WeakHandles to be destroyed properly.
32 MessageLoop message_loop_
;
35 StrictMock
<MockJsEventHandler
> mock_js_event_handler_
;
36 JsMutationEventObserver js_mutation_event_observer_
;
39 message_loop_
.RunAllPending();
43 TEST_F(JsMutationEventObserverTest
, OnChangesApplied
) {
46 // We don't test with passwords as that requires additional setup.
48 // Build a list of example ChangeRecords.
49 syncer::ChangeRecord changes
[syncer::MODEL_TYPE_COUNT
];
50 for (int i
= syncer::AUTOFILL_PROFILE
; i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
55 syncer::ChangeRecord::ACTION_ADD
;
59 syncer::ChangeRecord::ACTION_UPDATE
;
63 syncer::ChangeRecord::ACTION_DELETE
;
68 // For each i, we call OnChangesApplied() with the first arg equal
69 // to i cast to ModelType and the second argument with the changes
70 // starting from changes[i].
72 // Set expectations for each data type.
73 for (int i
= syncer::AUTOFILL_PROFILE
; i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
74 const std::string
& model_type_str
=
75 syncer::ModelTypeToString(syncer::ModelTypeFromInt(i
));
76 DictionaryValue expected_details
;
77 expected_details
.SetString("modelType", model_type_str
);
78 expected_details
.SetString("writeTransactionId", "0");
79 ListValue
* expected_changes
= new ListValue();
80 expected_details
.Set("changes", expected_changes
);
81 for (int j
= i
; j
< syncer::MODEL_TYPE_COUNT
; ++j
) {
82 expected_changes
->Append(changes
[j
].ToValue());
84 EXPECT_CALL(mock_js_event_handler_
,
85 HandleJsEvent("onChangesApplied",
86 HasDetailsAsDictionary(expected_details
)));
89 // Fire OnChangesApplied() for each data type.
90 for (int i
= syncer::AUTOFILL_PROFILE
; i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
91 syncer::ChangeRecordList
92 local_changes(changes
+ i
, changes
+ arraysize(changes
));
93 js_mutation_event_observer_
.OnChangesApplied(
94 syncer::ModelTypeFromInt(i
),
95 0, syncer::ImmutableChangeRecordList(&local_changes
));
101 TEST_F(JsMutationEventObserverTest
, OnChangesComplete
) {
104 for (int i
= syncer::FIRST_REAL_MODEL_TYPE
;
105 i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
106 DictionaryValue expected_details
;
107 expected_details
.SetString(
109 syncer::ModelTypeToString(syncer::ModelTypeFromInt(i
)));
110 EXPECT_CALL(mock_js_event_handler_
,
111 HandleJsEvent("onChangesComplete",
112 HasDetailsAsDictionary(expected_details
)));
115 for (int i
= syncer::FIRST_REAL_MODEL_TYPE
;
116 i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
117 js_mutation_event_observer_
.OnChangesComplete(
118 syncer::ModelTypeFromInt(i
));
124 } // namespace syncer