| 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 "sync/internal_api/js_mutation_event_observer.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "sync/internal_api/public/base/model_type.h" | 10 #include "sync/internal_api/public/base/model_type.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 message_loop_.RunAllPending(); | 39 message_loop_.RunAllPending(); |
| 40 } | 40 } |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 TEST_F(JsMutationEventObserverTest, OnChangesApplied) { | 43 TEST_F(JsMutationEventObserverTest, OnChangesApplied) { |
| 44 InSequence dummy; | 44 InSequence dummy; |
| 45 | 45 |
| 46 // We don't test with passwords as that requires additional setup. | 46 // We don't test with passwords as that requires additional setup. |
| 47 | 47 |
| 48 // Build a list of example ChangeRecords. | 48 // Build a list of example ChangeRecords. |
| 49 syncer::ChangeRecord changes[syncer::MODEL_TYPE_COUNT]; | 49 ChangeRecord changes[MODEL_TYPE_COUNT]; |
| 50 for (int i = syncer::AUTOFILL_PROFILE; i < syncer::MODEL_TYPE_COUNT; ++i) { | 50 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { |
| 51 changes[i].id = i; | 51 changes[i].id = i; |
| 52 switch (i % 3) { | 52 switch (i % 3) { |
| 53 case 0: | 53 case 0: |
| 54 changes[i].action = | 54 changes[i].action = ChangeRecord::ACTION_ADD; |
| 55 syncer::ChangeRecord::ACTION_ADD; | |
| 56 break; | 55 break; |
| 57 case 1: | 56 case 1: |
| 58 changes[i].action = | 57 changes[i].action = ChangeRecord::ACTION_UPDATE; |
| 59 syncer::ChangeRecord::ACTION_UPDATE; | |
| 60 break; | 58 break; |
| 61 default: | 59 default: |
| 62 changes[i].action = | 60 changes[i].action = ChangeRecord::ACTION_DELETE; |
| 63 syncer::ChangeRecord::ACTION_DELETE; | |
| 64 break; | 61 break; |
| 65 } | 62 } |
| 66 } | 63 } |
| 67 | 64 |
| 68 // For each i, we call OnChangesApplied() with the first arg equal | 65 // For each i, we call OnChangesApplied() with the first arg equal |
| 69 // to i cast to ModelType and the second argument with the changes | 66 // to i cast to ModelType and the second argument with the changes |
| 70 // starting from changes[i]. | 67 // starting from changes[i]. |
| 71 | 68 |
| 72 // Set expectations for each data type. | 69 // Set expectations for each data type. |
| 73 for (int i = syncer::AUTOFILL_PROFILE; i < syncer::MODEL_TYPE_COUNT; ++i) { | 70 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { |
| 74 const std::string& model_type_str = | 71 const std::string& model_type_str = |
| 75 syncer::ModelTypeToString(syncer::ModelTypeFromInt(i)); | 72 ModelTypeToString(ModelTypeFromInt(i)); |
| 76 DictionaryValue expected_details; | 73 DictionaryValue expected_details; |
| 77 expected_details.SetString("modelType", model_type_str); | 74 expected_details.SetString("modelType", model_type_str); |
| 78 expected_details.SetString("writeTransactionId", "0"); | 75 expected_details.SetString("writeTransactionId", "0"); |
| 79 ListValue* expected_changes = new ListValue(); | 76 ListValue* expected_changes = new ListValue(); |
| 80 expected_details.Set("changes", expected_changes); | 77 expected_details.Set("changes", expected_changes); |
| 81 for (int j = i; j < syncer::MODEL_TYPE_COUNT; ++j) { | 78 for (int j = i; j < MODEL_TYPE_COUNT; ++j) { |
| 82 expected_changes->Append(changes[j].ToValue()); | 79 expected_changes->Append(changes[j].ToValue()); |
| 83 } | 80 } |
| 84 EXPECT_CALL(mock_js_event_handler_, | 81 EXPECT_CALL(mock_js_event_handler_, |
| 85 HandleJsEvent("onChangesApplied", | 82 HandleJsEvent("onChangesApplied", |
| 86 HasDetailsAsDictionary(expected_details))); | 83 HasDetailsAsDictionary(expected_details))); |
| 87 } | 84 } |
| 88 | 85 |
| 89 // Fire OnChangesApplied() for each data type. | 86 // Fire OnChangesApplied() for each data type. |
| 90 for (int i = syncer::AUTOFILL_PROFILE; i < syncer::MODEL_TYPE_COUNT; ++i) { | 87 for (int i = AUTOFILL_PROFILE; i < MODEL_TYPE_COUNT; ++i) { |
| 91 syncer::ChangeRecordList | 88 ChangeRecordList local_changes(changes + i, changes + arraysize(changes)); |
| 92 local_changes(changes + i, changes + arraysize(changes)); | |
| 93 js_mutation_event_observer_.OnChangesApplied( | 89 js_mutation_event_observer_.OnChangesApplied( |
| 94 syncer::ModelTypeFromInt(i), | 90 ModelTypeFromInt(i), |
| 95 0, syncer::ImmutableChangeRecordList(&local_changes)); | 91 0, ImmutableChangeRecordList(&local_changes)); |
| 96 } | 92 } |
| 97 | 93 |
| 98 PumpLoop(); | 94 PumpLoop(); |
| 99 } | 95 } |
| 100 | 96 |
| 101 TEST_F(JsMutationEventObserverTest, OnChangesComplete) { | 97 TEST_F(JsMutationEventObserverTest, OnChangesComplete) { |
| 102 InSequence dummy; | 98 InSequence dummy; |
| 103 | 99 |
| 104 for (int i = syncer::FIRST_REAL_MODEL_TYPE; | 100 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
| 105 i < syncer::MODEL_TYPE_COUNT; ++i) { | |
| 106 DictionaryValue expected_details; | 101 DictionaryValue expected_details; |
| 107 expected_details.SetString( | 102 expected_details.SetString( |
| 108 "modelType", | 103 "modelType", |
| 109 syncer::ModelTypeToString(syncer::ModelTypeFromInt(i))); | 104 ModelTypeToString(ModelTypeFromInt(i))); |
| 110 EXPECT_CALL(mock_js_event_handler_, | 105 EXPECT_CALL(mock_js_event_handler_, |
| 111 HandleJsEvent("onChangesComplete", | 106 HandleJsEvent("onChangesComplete", |
| 112 HasDetailsAsDictionary(expected_details))); | 107 HasDetailsAsDictionary(expected_details))); |
| 113 } | 108 } |
| 114 | 109 |
| 115 for (int i = syncer::FIRST_REAL_MODEL_TYPE; | 110 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
| 116 i < syncer::MODEL_TYPE_COUNT; ++i) { | |
| 117 js_mutation_event_observer_.OnChangesComplete( | 111 js_mutation_event_observer_.OnChangesComplete( |
| 118 syncer::ModelTypeFromInt(i)); | 112 ModelTypeFromInt(i)); |
| 119 } | 113 } |
| 120 PumpLoop(); | 114 PumpLoop(); |
| 121 } | 115 } |
| 122 | 116 |
| 123 } // namespace | 117 } // namespace |
| 124 } // namespace syncer | 118 } // namespace syncer |
| OLD | NEW |