OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
erikwright (departed)
2015/01/13 19:21:59
2015
mukesh agrawal
2015/01/20 21:13:15
Done.
| |
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/wifi_sync/wifi_credential_syncable_service.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/tracked_objects.h" | |
10 #include "components/wifi_sync/fake_wifi_config_delegate.h" | |
11 #include "components/wifi_sync/wifi_credential.h" | |
12 #include "components/wifi_sync/wifi_security_class.h" | |
13 #include "sync/api/fake_sync_change_processor.h" | |
14 #include "sync/api/sync_change.h" | |
15 #include "sync/api/sync_error.h" | |
16 #include "sync/api/sync_error_factory_mock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace wifi_sync { | |
20 | |
21 using syncer::FakeSyncChangeProcessor; | |
22 using syncer::SyncErrorFactoryMock; | |
23 | |
24 // Unit tests for WifiCredentialSyncableService. | |
25 class WifiCredentialSyncableServiceTest : public testing::Test { | |
26 protected: | |
27 WifiCredentialSyncableServiceTest() | |
28 : change_processor_(nullptr) { | |
29 auto config_delegate = make_scoped_ptr(new FakeWifiConfigDelegate()); | |
erikwright (departed)
2015/01/13 19:21:59
not a case for auto.
mukesh agrawal
2015/01/20 21:13:15
Done.
| |
30 config_delegate_ = config_delegate.get(); | |
erikwright (departed)
2015/01/13 19:21:59
I'm not sure if there's a hard convention or rule,
stevenjb
2015/01/14 00:12:49
That is correct, config_delegate_ should be initia
erikwright (departed)
2015/01/14 14:59:27
This pattern is common and reasonable in unit test
mukesh agrawal
2015/01/20 21:13:15
I've moved the allocation of FakeWifiConfigDelegat
| |
31 syncable_service_.reset( | |
32 new WifiCredentialSyncableService(config_delegate.Pass())); | |
33 } | |
34 | |
35 // Wrappers for methods in WifiCredentialSyncableService. Sorted by | |
36 // the declaration order of the WifiCredentialSyncableService | |
37 // methods they call. | |
stevenjb
2015/01/14 00:12:49
I don't think noting the sorting order of these me
mukesh agrawal
2015/01/20 21:13:15
Done.
| |
38 syncer::SyncError ProcessSyncChanges( | |
39 const tracked_objects::Location& caller_location, | |
40 const syncer::SyncChangeList& change_list) { | |
41 return syncable_service_->ProcessSyncChanges(caller_location, change_list); | |
42 } | |
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
I believe this case is correct. AddToSyncedNetwork
| |
43 bool AddToSyncedNetworks( | |
44 const std::string& item_id, const WifiCredential& credential) { | |
45 return syncable_service_->AddToSyncedNetworks(item_id, credential); | |
46 } | |
47 | |
48 // Returns the number of change requests received by | |
49 // |change_processor_|. | |
50 int GetChangeProcessorChangeCount() { | |
51 CHECK(change_processor_); | |
52 return change_processor_->changes().size(); | |
53 } | |
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
Done.
| |
54 // Returns the number of times AddToLocalNetworks has been called on | |
55 // |config_delegate_|. | |
56 int GetWifiDelegateAddCount() { | |
57 return config_delegate_->GetAddCount(); | |
58 } | |
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
Done.
| |
59 // Returns a new WifiCredential constructed from the given parameters. | |
60 WifiCredential MakeCredential(const std::string& ssid, | |
61 const WifiSecurityClass security_class, | |
62 const std::string& passphrase) { | |
63 scoped_ptr<WifiCredential> credential = | |
64 WifiCredential::Create( | |
65 WifiCredential::MakeSsidBytesForTest(ssid), | |
66 security_class, | |
67 passphrase); | |
68 CHECK(credential); | |
69 return *credential; | |
70 } | |
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
Done.
| |
71 // Starts |syncable_service_|, providing the service an empty | |
72 // initial data list. | |
73 void StartSyncing() { | |
74 auto change_processor = make_scoped_ptr(new FakeSyncChangeProcessor()); | |
75 change_processor_ = change_processor.get(); | |
76 syncable_service_->MergeDataAndStartSyncing( | |
77 syncer::WIFI_CREDENTIALS, | |
78 syncer::SyncDataList(), | |
79 change_processor.Pass(), | |
80 make_scoped_ptr(new SyncErrorFactoryMock())); | |
81 } | |
82 | |
83 private: | |
84 scoped_ptr<WifiCredentialSyncableService> syncable_service_; | |
85 FakeWifiConfigDelegate* config_delegate_; // Owned by |syncable_service_| | |
86 FakeSyncChangeProcessor* change_processor_; // Owned by |syncable_service_| | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest); | |
89 }; | |
90 | |
91 TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) { | |
92 auto sync_error(ProcessSyncChanges(FROM_HERE, syncer::SyncChangeList())); | |
erikwright (departed)
2015/01/13 19:21:59
not a case for auto, here and below
mukesh agrawal
2015/01/20 21:13:15
Done.
| |
93 ASSERT_TRUE(sync_error.IsSet()); | |
94 EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type()); | |
95 EXPECT_EQ(0, GetWifiDelegateAddCount()); | |
96 } | |
97 | |
98 TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesInvalidChange) { | |
99 StartSyncing(); | |
100 auto sync_error( | |
101 ProcessSyncChanges(FROM_HERE, | |
102 syncer::SyncChangeList(1, syncer::SyncChange()))); | |
103 ASSERT_TRUE(sync_error.IsSet()); | |
104 EXPECT_EQ(syncer::SyncError::DATATYPE_ERROR, sync_error.error_type()); | |
105 EXPECT_EQ(0, GetWifiDelegateAddCount()); | |
106 } | |
107 | |
108 // The success cases for ProcessSyncChanges will be tested as part of | |
erikwright (departed)
2015/01/13 19:21:59
Is this consistent with other syncable services?
mukesh agrawal
2015/01/20 21:13:15
Thanks for asking. Taking a closer look at some of
| |
109 // the sync integration tests. (We don't test the success cases here, | |
110 // due to the difficulty of creating proper SyncChanges.) | |
111 // | |
112 // TODO(quiche): add integration tests for success cases, and update | |
113 // above comment with references to them. | |
114 | |
115 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) { | |
116 EXPECT_FALSE( | |
117 AddToSyncedNetworks( | |
118 "fake-item-id", | |
119 MakeCredential("fake-ssid", SECURITY_CLASS_NONE, ""))); | |
120 } | |
121 | |
122 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) { | |
123 StartSyncing(); | |
124 EXPECT_TRUE( | |
125 AddToSyncedNetworks( | |
126 "fake-item-id", | |
127 MakeCredential("fake-ssid", SECURITY_CLASS_NONE, ""))); | |
128 EXPECT_EQ(1, GetChangeProcessorChangeCount()); | |
129 } | |
130 | |
131 } // namespace wifi_sync | |
OLD | NEW |