OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/user_events/user_event_sync_bridge.h" |
| 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "components/sync/driver/fake_sync_service.h" |
| 15 #include "components/sync/model/data_batch.h" |
| 16 #include "components/sync/model/model_type_store_test_util.h" |
| 17 #include "components/sync/model/recording_model_type_change_processor.h" |
| 18 #include "components/sync/protocol/sync.pb.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 using sync_pb::UserEventSpecifics; |
| 22 |
| 23 namespace syncer { |
| 24 |
| 25 namespace { |
| 26 |
| 27 void VerifyEqual(const UserEventSpecifics& s1, const UserEventSpecifics& s2) { |
| 28 EXPECT_EQ(s1.event_time_usec(), s2.event_time_usec()); |
| 29 EXPECT_EQ(s1.navigation_id(), s2.navigation_id()); |
| 30 EXPECT_EQ(s1.session_id(), s2.session_id()); |
| 31 } |
| 32 |
| 33 void VerifyDataBatchCount(int expected_count, |
| 34 std::unique_ptr<DataBatch> batch) { |
| 35 int actual_count = 0; |
| 36 while (batch->HasNext()) { |
| 37 ++actual_count; |
| 38 batch->Next(); |
| 39 } |
| 40 EXPECT_EQ(expected_count, actual_count); |
| 41 } |
| 42 |
| 43 void VerifyDataBatch(std::map<std::string, UserEventSpecifics> expected, |
| 44 std::unique_ptr<DataBatch> batch) { |
| 45 while (batch->HasNext()) { |
| 46 const KeyAndData& pair = batch->Next(); |
| 47 auto iter = expected.find(pair.first); |
| 48 ASSERT_NE(iter, expected.end()); |
| 49 VerifyEqual(iter->second, pair.second->specifics.user_event()); |
| 50 // Removing allows us to verify we don't see the same item multiple times, |
| 51 // and that we saw everything we expected. |
| 52 expected.erase(iter); |
| 53 } |
| 54 EXPECT_TRUE(expected.empty()); |
| 55 } |
| 56 |
| 57 base::Callback<void(std::unique_ptr<DataBatch> batch)> VerifyCallback( |
| 58 std::map<std::string, UserEventSpecifics> expected) { |
| 59 return base::Bind(&VerifyDataBatch, expected); |
| 60 } |
| 61 |
| 62 UserEventSpecifics CreateSpecifics(int64_t event_time_usec, |
| 63 int64_t navigation_id, |
| 64 uint64_t session_id) { |
| 65 UserEventSpecifics specifics; |
| 66 specifics.set_event_time_usec(event_time_usec); |
| 67 specifics.set_navigation_id(navigation_id); |
| 68 specifics.set_session_id(session_id); |
| 69 return specifics; |
| 70 } |
| 71 |
| 72 std::unique_ptr<UserEventSpecifics> SpecificsUniquePtr(int64_t event_time_usec, |
| 73 int64_t navigation_id, |
| 74 uint64_t session_id) { |
| 75 return base::MakeUnique<UserEventSpecifics>( |
| 76 CreateSpecifics(event_time_usec, navigation_id, session_id)); |
| 77 } |
| 78 |
| 79 class UserEventSyncBridgeTest : public testing::Test { |
| 80 protected: |
| 81 UserEventSyncBridgeTest() { |
| 82 bridge_ = base::MakeUnique<UserEventSyncBridge>( |
| 83 ModelTypeStoreTestUtil::FactoryForInMemoryStoreForTest(), |
| 84 RecordingModelTypeChangeProcessor::FactoryForBridgeTest(&processor_)); |
| 85 } |
| 86 |
| 87 UserEventSyncBridge* bridge() { return bridge_.get(); } |
| 88 const RecordingModelTypeChangeProcessor& processor() { return *processor_; } |
| 89 |
| 90 private: |
| 91 std::unique_ptr<UserEventSyncBridge> bridge_; |
| 92 RecordingModelTypeChangeProcessor* processor_; |
| 93 base::MessageLoop message_loop_; |
| 94 }; |
| 95 |
| 96 TEST_F(UserEventSyncBridgeTest, MetadataIsInitialized) { |
| 97 base::RunLoop().RunUntilIdle(); |
| 98 EXPECT_TRUE(processor().metadata()->GetModelTypeState().initial_sync_done()); |
| 99 } |
| 100 |
| 101 TEST_F(UserEventSyncBridgeTest, SingleRecord) { |
| 102 const UserEventSpecifics specifics(CreateSpecifics(1u, 2u, 3u)); |
| 103 bridge()->RecordUserEvent(base::MakeUnique<UserEventSpecifics>(specifics)); |
| 104 EXPECT_EQ(1u, processor().put_multimap().size()); |
| 105 |
| 106 const std::string storage_key = processor().put_multimap().begin()->first; |
| 107 bridge()->GetData({storage_key}, VerifyCallback({{storage_key, specifics}})); |
| 108 bridge()->GetData({"bogus"}, base::Bind(&VerifyDataBatchCount, 0)); |
| 109 bridge()->GetAllData(VerifyCallback({{storage_key, specifics}})); |
| 110 |
| 111 bridge()->DisableSync(); |
| 112 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 0)); |
| 113 EXPECT_EQ(0u, processor().delete_set().size()); |
| 114 } |
| 115 |
| 116 TEST_F(UserEventSyncBridgeTest, MultipleRecords) { |
| 117 bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 1u, 1u)); |
| 118 bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 1u, 2u)); |
| 119 bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 2u, 2u)); |
| 120 bridge()->RecordUserEvent(SpecificsUniquePtr(2u, 2u, 2u)); |
| 121 |
| 122 EXPECT_EQ(4u, processor().put_multimap().size()); |
| 123 std::set<std::string> unique_storage_keys; |
| 124 for (const auto& kv : processor().put_multimap()) { |
| 125 unique_storage_keys.insert(kv.first); |
| 126 } |
| 127 EXPECT_EQ(2u, unique_storage_keys.size()); |
| 128 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 4)); |
| 129 } |
| 130 |
| 131 } // namespace |
| 132 |
| 133 } // namespace syncer |
OLD | NEW |