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