OLD | NEW |
| (Empty) |
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 #include "chrome/browser/sync/internal_api/js_mutation_event_observer.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/values.h" | |
10 #include "sync/js/js_event_details.h" | |
11 #include "sync/js/js_test_util.h" | |
12 #include "sync/syncable/model_type.h" | |
13 #include "sync/util/weak_handle.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace browser_sync { | |
17 namespace { | |
18 | |
19 using ::testing::InSequence; | |
20 using ::testing::StrictMock; | |
21 | |
22 class JsMutationEventObserverTest : public testing::Test { | |
23 protected: | |
24 JsMutationEventObserverTest() { | |
25 js_mutation_event_observer_.SetJsEventHandler( | |
26 mock_js_event_handler_.AsWeakHandle()); | |
27 } | |
28 | |
29 private: | |
30 // This must be destroyed after the member variables below in order | |
31 // for WeakHandles to be destroyed properly. | |
32 MessageLoop message_loop_; | |
33 | |
34 protected: | |
35 StrictMock<MockJsEventHandler> mock_js_event_handler_; | |
36 JsMutationEventObserver js_mutation_event_observer_; | |
37 | |
38 void PumpLoop() { | |
39 message_loop_.RunAllPending(); | |
40 } | |
41 }; | |
42 | |
43 TEST_F(JsMutationEventObserverTest, OnChangesApplied) { | |
44 InSequence dummy; | |
45 | |
46 // We don't test with passwords as that requires additional setup. | |
47 | |
48 // Build a list of example ChangeRecords. | |
49 sync_api::ChangeRecord changes[syncable::MODEL_TYPE_COUNT]; | |
50 for (int i = syncable::AUTOFILL_PROFILE; | |
51 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
52 changes[i].id = i; | |
53 switch (i % 3) { | |
54 case 0: | |
55 changes[i].action = | |
56 sync_api::ChangeRecord::ACTION_ADD; | |
57 break; | |
58 case 1: | |
59 changes[i].action = | |
60 sync_api::ChangeRecord::ACTION_UPDATE; | |
61 break; | |
62 default: | |
63 changes[i].action = | |
64 sync_api::ChangeRecord::ACTION_DELETE; | |
65 break; | |
66 } | |
67 } | |
68 | |
69 // For each i, we call OnChangesApplied() with the first arg equal | |
70 // to i cast to ModelType and the second argument with the changes | |
71 // starting from changes[i]. | |
72 | |
73 // Set expectations for each data type. | |
74 for (int i = syncable::AUTOFILL_PROFILE; | |
75 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
76 const std::string& model_type_str = | |
77 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)); | |
78 DictionaryValue expected_details; | |
79 expected_details.SetString("modelType", model_type_str); | |
80 expected_details.SetString("writeTransactionId", "0"); | |
81 ListValue* expected_changes = new ListValue(); | |
82 expected_details.Set("changes", expected_changes); | |
83 for (int j = i; j < syncable::MODEL_TYPE_COUNT; ++j) { | |
84 expected_changes->Append(changes[j].ToValue()); | |
85 } | |
86 EXPECT_CALL(mock_js_event_handler_, | |
87 HandleJsEvent("onChangesApplied", | |
88 HasDetailsAsDictionary(expected_details))); | |
89 } | |
90 | |
91 // Fire OnChangesApplied() for each data type. | |
92 for (int i = syncable::AUTOFILL_PROFILE; | |
93 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
94 sync_api::ChangeRecordList | |
95 local_changes(changes + i, changes + arraysize(changes)); | |
96 js_mutation_event_observer_.OnChangesApplied( | |
97 syncable::ModelTypeFromInt(i), | |
98 0, sync_api::ImmutableChangeRecordList(&local_changes)); | |
99 } | |
100 | |
101 PumpLoop(); | |
102 } | |
103 | |
104 TEST_F(JsMutationEventObserverTest, OnChangesComplete) { | |
105 InSequence dummy; | |
106 | |
107 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
108 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
109 DictionaryValue expected_details; | |
110 expected_details.SetString( | |
111 "modelType", | |
112 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i))); | |
113 EXPECT_CALL(mock_js_event_handler_, | |
114 HandleJsEvent("onChangesComplete", | |
115 HasDetailsAsDictionary(expected_details))); | |
116 } | |
117 | |
118 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
119 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
120 js_mutation_event_observer_.OnChangesComplete( | |
121 syncable::ModelTypeFromInt(i)); | |
122 } | |
123 PumpLoop(); | |
124 } | |
125 | |
126 } // namespace | |
127 } // namespace browser_sync | |
OLD | NEW |