| 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[syncable::MODEL_TYPE_COUNT]; | 49 syncer::ChangeRecord changes[syncer::MODEL_TYPE_COUNT]; |
| 50 for (int i = syncable::AUTOFILL_PROFILE; | 50 for (int i = syncer::AUTOFILL_PROFILE; i < syncer::MODEL_TYPE_COUNT; ++i) { |
| 51 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 52 changes[i].id = i; | 51 changes[i].id = i; |
| 53 switch (i % 3) { | 52 switch (i % 3) { |
| 54 case 0: | 53 case 0: |
| 55 changes[i].action = | 54 changes[i].action = |
| 56 syncer::ChangeRecord::ACTION_ADD; | 55 syncer::ChangeRecord::ACTION_ADD; |
| 57 break; | 56 break; |
| 58 case 1: | 57 case 1: |
| 59 changes[i].action = | 58 changes[i].action = |
| 60 syncer::ChangeRecord::ACTION_UPDATE; | 59 syncer::ChangeRecord::ACTION_UPDATE; |
| 61 break; | 60 break; |
| 62 default: | 61 default: |
| 63 changes[i].action = | 62 changes[i].action = |
| 64 syncer::ChangeRecord::ACTION_DELETE; | 63 syncer::ChangeRecord::ACTION_DELETE; |
| 65 break; | 64 break; |
| 66 } | 65 } |
| 67 } | 66 } |
| 68 | 67 |
| 69 // For each i, we call OnChangesApplied() with the first arg equal | 68 // For each i, we call OnChangesApplied() with the first arg equal |
| 70 // to i cast to ModelType and the second argument with the changes | 69 // to i cast to ModelType and the second argument with the changes |
| 71 // starting from changes[i]. | 70 // starting from changes[i]. |
| 72 | 71 |
| 73 // Set expectations for each data type. | 72 // Set expectations for each data type. |
| 74 for (int i = syncable::AUTOFILL_PROFILE; | 73 for (int i = syncer::AUTOFILL_PROFILE; i < syncer::MODEL_TYPE_COUNT; ++i) { |
| 75 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 76 const std::string& model_type_str = | 74 const std::string& model_type_str = |
| 77 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)); | 75 syncer::ModelTypeToString(syncer::ModelTypeFromInt(i)); |
| 78 DictionaryValue expected_details; | 76 DictionaryValue expected_details; |
| 79 expected_details.SetString("modelType", model_type_str); | 77 expected_details.SetString("modelType", model_type_str); |
| 80 expected_details.SetString("writeTransactionId", "0"); | 78 expected_details.SetString("writeTransactionId", "0"); |
| 81 ListValue* expected_changes = new ListValue(); | 79 ListValue* expected_changes = new ListValue(); |
| 82 expected_details.Set("changes", expected_changes); | 80 expected_details.Set("changes", expected_changes); |
| 83 for (int j = i; j < syncable::MODEL_TYPE_COUNT; ++j) { | 81 for (int j = i; j < syncer::MODEL_TYPE_COUNT; ++j) { |
| 84 expected_changes->Append(changes[j].ToValue()); | 82 expected_changes->Append(changes[j].ToValue()); |
| 85 } | 83 } |
| 86 EXPECT_CALL(mock_js_event_handler_, | 84 EXPECT_CALL(mock_js_event_handler_, |
| 87 HandleJsEvent("onChangesApplied", | 85 HandleJsEvent("onChangesApplied", |
| 88 HasDetailsAsDictionary(expected_details))); | 86 HasDetailsAsDictionary(expected_details))); |
| 89 } | 87 } |
| 90 | 88 |
| 91 // Fire OnChangesApplied() for each data type. | 89 // Fire OnChangesApplied() for each data type. |
| 92 for (int i = syncable::AUTOFILL_PROFILE; | 90 for (int i = syncer::AUTOFILL_PROFILE; i < syncer::MODEL_TYPE_COUNT; ++i) { |
| 93 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 94 syncer::ChangeRecordList | 91 syncer::ChangeRecordList |
| 95 local_changes(changes + i, changes + arraysize(changes)); | 92 local_changes(changes + i, changes + arraysize(changes)); |
| 96 js_mutation_event_observer_.OnChangesApplied( | 93 js_mutation_event_observer_.OnChangesApplied( |
| 97 syncable::ModelTypeFromInt(i), | 94 syncer::ModelTypeFromInt(i), |
| 98 0, syncer::ImmutableChangeRecordList(&local_changes)); | 95 0, syncer::ImmutableChangeRecordList(&local_changes)); |
| 99 } | 96 } |
| 100 | 97 |
| 101 PumpLoop(); | 98 PumpLoop(); |
| 102 } | 99 } |
| 103 | 100 |
| 104 TEST_F(JsMutationEventObserverTest, OnChangesComplete) { | 101 TEST_F(JsMutationEventObserverTest, OnChangesComplete) { |
| 105 InSequence dummy; | 102 InSequence dummy; |
| 106 | 103 |
| 107 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | 104 for (int i = syncer::FIRST_REAL_MODEL_TYPE; |
| 108 i < syncable::MODEL_TYPE_COUNT; ++i) { | 105 i < syncer::MODEL_TYPE_COUNT; ++i) { |
| 109 DictionaryValue expected_details; | 106 DictionaryValue expected_details; |
| 110 expected_details.SetString( | 107 expected_details.SetString( |
| 111 "modelType", | 108 "modelType", |
| 112 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i))); | 109 syncer::ModelTypeToString(syncer::ModelTypeFromInt(i))); |
| 113 EXPECT_CALL(mock_js_event_handler_, | 110 EXPECT_CALL(mock_js_event_handler_, |
| 114 HandleJsEvent("onChangesComplete", | 111 HandleJsEvent("onChangesComplete", |
| 115 HasDetailsAsDictionary(expected_details))); | 112 HasDetailsAsDictionary(expected_details))); |
| 116 } | 113 } |
| 117 | 114 |
| 118 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | 115 for (int i = syncer::FIRST_REAL_MODEL_TYPE; |
| 119 i < syncable::MODEL_TYPE_COUNT; ++i) { | 116 i < syncer::MODEL_TYPE_COUNT; ++i) { |
| 120 js_mutation_event_observer_.OnChangesComplete( | 117 js_mutation_event_observer_.OnChangesComplete( |
| 121 syncable::ModelTypeFromInt(i)); | 118 syncer::ModelTypeFromInt(i)); |
| 122 } | 119 } |
| 123 PumpLoop(); | 120 PumpLoop(); |
| 124 } | 121 } |
| 125 | 122 |
| 126 } // namespace | 123 } // namespace |
| 127 } // namespace syncer | 124 } // namespace syncer |
| OLD | NEW |