OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/js_mutation_event_observer.h" | 5 #include "components/sync/core_impl/js_mutation_event_observer.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "sync/internal_api/public/base/model_type.h" | 10 #include "components/sync/base/model_type.h" |
11 #include "sync/internal_api/public/util/weak_handle.h" | 11 #include "components/sync/base/weak_handle.h" |
12 #include "sync/js/js_event_details.h" | 12 #include "components/sync/js/js_event_details.h" |
13 #include "sync/js/js_test_util.h" | 13 #include "components/sync/js/js_test_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace syncer { | 16 namespace syncer { |
17 namespace { | 17 namespace { |
18 | 18 |
19 using ::testing::InSequence; | 19 using ::testing::InSequence; |
20 using ::testing::StrictMock; | 20 using ::testing::StrictMock; |
21 | 21 |
22 class JsMutationEventObserverTest : public testing::Test { | 22 class JsMutationEventObserverTest : public testing::Test { |
23 protected: | 23 protected: |
24 JsMutationEventObserverTest() { | 24 JsMutationEventObserverTest() { |
25 js_mutation_event_observer_.SetJsEventHandler( | 25 js_mutation_event_observer_.SetJsEventHandler( |
26 mock_js_event_handler_.AsWeakHandle()); | 26 mock_js_event_handler_.AsWeakHandle()); |
27 } | 27 } |
28 | 28 |
29 private: | 29 private: |
30 // This must be destroyed after the member variables below in order | 30 // This must be destroyed after the member variables below in order |
31 // for WeakHandles to be destroyed properly. | 31 // for WeakHandles to be destroyed properly. |
32 base::MessageLoop message_loop_; | 32 base::MessageLoop message_loop_; |
33 | 33 |
34 protected: | 34 protected: |
35 StrictMock<MockJsEventHandler> mock_js_event_handler_; | 35 StrictMock<MockJsEventHandler> mock_js_event_handler_; |
36 JsMutationEventObserver js_mutation_event_observer_; | 36 JsMutationEventObserver js_mutation_event_observer_; |
37 | 37 |
38 void PumpLoop() { | 38 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
39 base::RunLoop().RunUntilIdle(); | |
40 } | |
41 }; | 39 }; |
42 | 40 |
43 TEST_F(JsMutationEventObserverTest, OnChangesApplied) { | 41 TEST_F(JsMutationEventObserverTest, OnChangesApplied) { |
44 InSequence dummy; | 42 InSequence dummy; |
45 | 43 |
46 // We don't test with passwords as that requires additional setup. | 44 // We don't test with passwords as that requires additional setup. |
47 | 45 |
48 // Build a list of example ChangeRecords. | 46 // Build a list of example ChangeRecords. |
49 ChangeRecord changes[MODEL_TYPE_COUNT]; | 47 ChangeRecord changes[MODEL_TYPE_COUNT]; |
50 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { | 48 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { |
(...skipping 10 matching lines...) Expand all Loading... |
61 break; | 59 break; |
62 } | 60 } |
63 } | 61 } |
64 | 62 |
65 // For each i, we call OnChangesApplied() with the first arg equal | 63 // For each i, we call OnChangesApplied() with the first arg equal |
66 // to i cast to ModelType and the second argument with the changes | 64 // to i cast to ModelType and the second argument with the changes |
67 // starting from changes[i]. | 65 // starting from changes[i]. |
68 | 66 |
69 // Set expectations for each data type. | 67 // Set expectations for each data type. |
70 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { | 68 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { |
71 const std::string& model_type_str = | 69 const std::string& model_type_str = ModelTypeToString(ModelTypeFromInt(i)); |
72 ModelTypeToString(ModelTypeFromInt(i)); | |
73 base::DictionaryValue expected_details; | 70 base::DictionaryValue expected_details; |
74 expected_details.SetString("modelType", model_type_str); | 71 expected_details.SetString("modelType", model_type_str); |
75 expected_details.SetString("writeTransactionId", "0"); | 72 expected_details.SetString("writeTransactionId", "0"); |
76 base::ListValue* expected_changes = new base::ListValue(); | 73 base::ListValue* expected_changes = new base::ListValue(); |
77 expected_details.Set("changes", expected_changes); | 74 expected_details.Set("changes", expected_changes); |
78 for (int j = i; j < MODEL_TYPE_COUNT; ++j) { | 75 for (int j = i; j < MODEL_TYPE_COUNT; ++j) { |
79 expected_changes->Append(changes[j].ToValue()); | 76 expected_changes->Append(changes[j].ToValue()); |
80 } | 77 } |
81 EXPECT_CALL(mock_js_event_handler_, | 78 EXPECT_CALL(mock_js_event_handler_, |
82 HandleJsEvent("onChangesApplied", | 79 HandleJsEvent("onChangesApplied", |
83 HasDetailsAsDictionary(expected_details))); | 80 HasDetailsAsDictionary(expected_details))); |
84 } | 81 } |
85 | 82 |
86 // Fire OnChangesApplied() for each data type. | 83 // Fire OnChangesApplied() for each data type. |
87 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { | 84 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { |
88 ChangeRecordList local_changes(changes + i, changes + arraysize(changes)); | 85 ChangeRecordList local_changes(changes + i, changes + arraysize(changes)); |
89 js_mutation_event_observer_.OnChangesApplied( | 86 js_mutation_event_observer_.OnChangesApplied( |
90 ModelTypeFromInt(i), | 87 ModelTypeFromInt(i), 0, ImmutableChangeRecordList(&local_changes)); |
91 0, ImmutableChangeRecordList(&local_changes)); | |
92 } | 88 } |
93 | 89 |
94 PumpLoop(); | 90 PumpLoop(); |
95 } | 91 } |
96 | 92 |
97 TEST_F(JsMutationEventObserverTest, OnChangesComplete) { | 93 TEST_F(JsMutationEventObserverTest, OnChangesComplete) { |
98 InSequence dummy; | 94 InSequence dummy; |
99 | 95 |
100 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | 96 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
101 base::DictionaryValue expected_details; | 97 base::DictionaryValue expected_details; |
102 expected_details.SetString( | 98 expected_details.SetString("modelType", |
103 "modelType", | 99 ModelTypeToString(ModelTypeFromInt(i))); |
104 ModelTypeToString(ModelTypeFromInt(i))); | |
105 EXPECT_CALL(mock_js_event_handler_, | 100 EXPECT_CALL(mock_js_event_handler_, |
106 HandleJsEvent("onChangesComplete", | 101 HandleJsEvent("onChangesComplete", |
107 HasDetailsAsDictionary(expected_details))); | 102 HasDetailsAsDictionary(expected_details))); |
108 } | 103 } |
109 | 104 |
110 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | 105 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
111 js_mutation_event_observer_.OnChangesComplete( | 106 js_mutation_event_observer_.OnChangesComplete(ModelTypeFromInt(i)); |
112 ModelTypeFromInt(i)); | |
113 } | 107 } |
114 PumpLoop(); | 108 PumpLoop(); |
115 } | 109 } |
116 | 110 |
117 } // namespace | 111 } // namespace |
118 } // namespace syncer | 112 } // namespace syncer |
OLD | NEW |