Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: components/sync/user_events/user_event_service_unittest.cc

Issue 2909283003: [Sync] Split UserEventService into interface and impl, add fake impl, add unit tests. (Closed)
Patch Set: Added a NoOp service to handle OffTheRecord. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/sync/user_events/user_event_service_impl_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_service.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/test/scoped_feature_list.h"
10 #include "components/sync/base/model_type.h"
11 #include "components/sync/driver/fake_sync_service.h"
12 #include "components/sync/driver/sync_driver_switches.h"
13 #include "components/sync/model/model_type_store_test_util.h"
14 #include "components/sync/model/recording_model_type_change_processor.h"
15 #include "components/sync/protocol/sync.pb.h"
16 #include "components/sync/user_events/user_event_sync_bridge.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using sync_pb::UserEventSpecifics;
20
21 namespace syncer {
22
23 namespace {
24
25 class TestSyncService : public FakeSyncService {
26 public:
27 TestSyncService(bool is_engine_initialized, ModelTypeSet preferred_data_types)
28 : is_engine_initialized_(is_engine_initialized),
29 preferred_data_types_(preferred_data_types) {}
30
31 bool IsEngineInitialized() const override { return is_engine_initialized_; }
32
33 ModelTypeSet GetPreferredDataTypes() const override {
34 return preferred_data_types_;
35 }
36
37 private:
38 bool is_engine_initialized_;
39 ModelTypeSet preferred_data_types_;
40 };
41
42 class UserEventServiceTest : public testing::Test {
43 protected:
44 UserEventServiceTest() {
45 scoped_feature_list_ = base::MakeUnique<base::test::ScopedFeatureList>();
46 scoped_feature_list_->InitAndEnableFeature(switches::kSyncUserEvents);
47 }
48
49 std::unique_ptr<UserEventSyncBridge> MakeBridge() {
50 return base::MakeUnique<UserEventSyncBridge>(
51 ModelTypeStoreTestUtil::FactoryForInMemoryStoreForTest(),
52 RecordingModelTypeChangeProcessor::FactoryForBridgeTest(&processor_));
53 }
54
55 const RecordingModelTypeChangeProcessor& processor() { return *processor_; }
56
57 void DisableUserEvents() {
58 scoped_feature_list_ = base::MakeUnique<base::test::ScopedFeatureList>();
59 scoped_feature_list_->Init();
60 }
61
62 private:
63 std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
64 RecordingModelTypeChangeProcessor* processor_;
65 base::MessageLoop message_loop_;
66 };
67
68 TEST_F(UserEventServiceTest, ShouldNotRecordNoSync) {
69 UserEventService service(nullptr, MakeBridge());
70 service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
71 EXPECT_EQ(0u, processor().put_multimap().size());
72 }
73
74 TEST_F(UserEventServiceTest, ShouldNotRecordFeatureIsDisabled) {
75 DisableUserEvents();
76 TestSyncService sync_service(false, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
77 UserEventService service(&sync_service, MakeBridge());
78 service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
79 EXPECT_EQ(0u, processor().put_multimap().size());
80 }
81
82 TEST_F(UserEventServiceTest, ShouldNotRecordNoHistory) {
83 TestSyncService sync_service(true, ModelTypeSet());
84 UserEventService service(&sync_service, MakeBridge());
85 service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
86 EXPECT_EQ(0u, processor().put_multimap().size());
87 }
88
89 TEST_F(UserEventServiceTest, ShouldNotRecordEngineOff) {
90 TestSyncService sync_service(false, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
91 UserEventService service(&sync_service, MakeBridge());
92 service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
93 EXPECT_EQ(0u, processor().put_multimap().size());
94 }
95
96 TEST_F(UserEventServiceTest, ShouldRecord) {
97 TestSyncService sync_service(true, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
98 UserEventService service(&sync_service, MakeBridge());
99 service.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
100 EXPECT_EQ(1u, processor().put_multimap().size());
101 }
102
103 TEST_F(UserEventServiceTest, SessionIdIsDifferent) {
104 TestSyncService sync_service(true, ModelTypeSet(HISTORY_DELETE_DIRECTIVES));
105
106 UserEventService service1(&sync_service, MakeBridge());
107 service1.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
108 ASSERT_EQ(1u, processor().put_multimap().size());
109 auto put1 = processor().put_multimap().begin();
110 int64_t session_id1 = put1->second->specifics.user_event().session_id();
111
112 UserEventService service2(&sync_service, MakeBridge());
113 service2.RecordUserEvent(base::MakeUnique<UserEventSpecifics>());
114 // The object processor() points to has changed to be |service2|'s processor.
115 ASSERT_EQ(1u, processor().put_multimap().size());
116 auto put2 = processor().put_multimap().begin();
117 int64_t session_id2 = put2->second->specifics.user_event().session_id();
118
119 EXPECT_NE(session_id1, session_id2);
120 }
121
122 } // namespace
123
124 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/user_events/user_event_service_impl_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698