Chromium Code Reviews| Index: components/wifi_sync/wifi_credential_syncable_service_unittest.cc |
| diff --git a/components/wifi_sync/wifi_credential_syncable_service_unittest.cc b/components/wifi_sync/wifi_credential_syncable_service_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8ab3eab30b70ca95a93f33fe40d1b3cd7f54ef1d |
| --- /dev/null |
| +++ b/components/wifi_sync/wifi_credential_syncable_service_unittest.cc |
| @@ -0,0 +1,131 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/wifi_sync/wifi_credential_syncable_service.h" |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/tracked_objects.h" |
| +#include "components/wifi_sync/fake_wifi_config_delegate.h" |
| +#include "components/wifi_sync/wifi_credential.h" |
| +#include "components/wifi_sync/wifi_security_class.h" |
| +#include "sync/api/fake_sync_change_processor.h" |
| +#include "sync/api/sync_change.h" |
| +#include "sync/api/sync_error.h" |
| +#include "sync/api/sync_error_factory_mock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace wifi_sync { |
| + |
| +using syncer::FakeSyncChangeProcessor; |
| +using syncer::SyncErrorFactoryMock; |
| + |
| +// Unit tests for WifiCredentialSyncableService. |
| +class WifiCredentialSyncableServiceTest : public testing::Test { |
| + protected: |
| + WifiCredentialSyncableServiceTest() |
| + : change_processor_(nullptr) { |
| + 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.
|
| + 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
|
| + syncable_service_.reset( |
| + new WifiCredentialSyncableService(config_delegate.Pass())); |
| + } |
| + |
| + // Wrappers for methods in WifiCredentialSyncableService. Sorted by |
| + // the declaration order of the WifiCredentialSyncableService |
| + // 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.
|
| + syncer::SyncError ProcessSyncChanges( |
| + const tracked_objects::Location& caller_location, |
| + const syncer::SyncChangeList& change_list) { |
| + return syncable_service_->ProcessSyncChanges(caller_location, change_list); |
| + } |
|
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
I believe this case is correct. AddToSyncedNetwork
|
| + bool AddToSyncedNetworks( |
| + const std::string& item_id, const WifiCredential& credential) { |
| + return syncable_service_->AddToSyncedNetworks(item_id, credential); |
| + } |
| + |
| + // Returns the number of change requests received by |
| + // |change_processor_|. |
| + int GetChangeProcessorChangeCount() { |
| + CHECK(change_processor_); |
| + return change_processor_->changes().size(); |
| + } |
|
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
Done.
|
| + // Returns the number of times AddToLocalNetworks has been called on |
| + // |config_delegate_|. |
| + int GetWifiDelegateAddCount() { |
| + return config_delegate_->GetAddCount(); |
| + } |
|
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
Done.
|
| + // Returns a new WifiCredential constructed from the given parameters. |
| + WifiCredential MakeCredential(const std::string& ssid, |
| + const WifiSecurityClass security_class, |
| + const std::string& passphrase) { |
| + scoped_ptr<WifiCredential> credential = |
| + WifiCredential::Create( |
| + WifiCredential::MakeSsidBytesForTest(ssid), |
| + security_class, |
| + passphrase); |
| + CHECK(credential); |
| + return *credential; |
| + } |
|
stevenjb
2015/01/14 00:12:49
WS
mukesh agrawal
2015/01/20 21:13:15
Done.
|
| + // Starts |syncable_service_|, providing the service an empty |
| + // initial data list. |
| + void StartSyncing() { |
| + auto change_processor = make_scoped_ptr(new FakeSyncChangeProcessor()); |
| + change_processor_ = change_processor.get(); |
| + syncable_service_->MergeDataAndStartSyncing( |
| + syncer::WIFI_CREDENTIALS, |
| + syncer::SyncDataList(), |
| + change_processor.Pass(), |
| + make_scoped_ptr(new SyncErrorFactoryMock())); |
| + } |
| + |
| + private: |
| + scoped_ptr<WifiCredentialSyncableService> syncable_service_; |
| + FakeWifiConfigDelegate* config_delegate_; // Owned by |syncable_service_| |
| + FakeSyncChangeProcessor* change_processor_; // Owned by |syncable_service_| |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest); |
| +}; |
| + |
| +TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) { |
| + 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.
|
| + ASSERT_TRUE(sync_error.IsSet()); |
| + EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type()); |
| + EXPECT_EQ(0, GetWifiDelegateAddCount()); |
| +} |
| + |
| +TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesInvalidChange) { |
| + StartSyncing(); |
| + auto sync_error( |
| + ProcessSyncChanges(FROM_HERE, |
| + syncer::SyncChangeList(1, syncer::SyncChange()))); |
| + ASSERT_TRUE(sync_error.IsSet()); |
| + EXPECT_EQ(syncer::SyncError::DATATYPE_ERROR, sync_error.error_type()); |
| + EXPECT_EQ(0, GetWifiDelegateAddCount()); |
| +} |
| + |
| +// 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
|
| +// the sync integration tests. (We don't test the success cases here, |
| +// due to the difficulty of creating proper SyncChanges.) |
| +// |
| +// TODO(quiche): add integration tests for success cases, and update |
| +// above comment with references to them. |
| + |
| +TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) { |
| + EXPECT_FALSE( |
| + AddToSyncedNetworks( |
| + "fake-item-id", |
| + MakeCredential("fake-ssid", SECURITY_CLASS_NONE, ""))); |
| +} |
| + |
| +TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) { |
| + StartSyncing(); |
| + EXPECT_TRUE( |
| + AddToSyncedNetworks( |
| + "fake-item-id", |
| + MakeCredential("fake-ssid", SECURITY_CLASS_NONE, ""))); |
| + EXPECT_EQ(1, GetChangeProcessorChangeCount()); |
| +} |
| + |
| +} // namespace wifi_sync |