diff options
Diffstat (limited to 'sync/engine/clear_data_command_unittest.cc')
-rw-r--r-- | sync/engine/clear_data_command_unittest.cc | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/sync/engine/clear_data_command_unittest.cc b/sync/engine/clear_data_command_unittest.cc new file mode 100644 index 0000000..b94d08a --- /dev/null +++ b/sync/engine/clear_data_command_unittest.cc @@ -0,0 +1,117 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/engine/clear_data_command.h" +#include "sync/protocol/autofill_specifics.pb.h" +#include "sync/protocol/bookmark_specifics.pb.h" +#include "sync/protocol/preference_specifics.pb.h" +#include "sync/protocol/sync.pb.h" +#include "sync/test/engine/syncer_command_test.h" +#include "sync/test/sessions/test_scoped_session_event_listener.h" + +namespace browser_sync { + +using sessions::TestScopedSessionEventListener; +using syncable::FIRST_REAL_MODEL_TYPE; +using syncable::MODEL_TYPE_COUNT; + +// A test fixture for tests exercising ClearDataCommandTest. +class ClearDataCommandTest : public SyncerCommandTest { + protected: + ClearDataCommandTest() {} + ClearDataCommand command_; + + virtual void OnShouldStopSyncingPermanently() { + on_should_stop_syncing_permanently_called_ = true; + } + + protected: + bool on_should_stop_syncing_permanently_called_; + + private: + DISALLOW_COPY_AND_ASSIGN(ClearDataCommandTest); +}; + +class ClearEventHandler : public SyncEngineEventListener { + public: + ClearEventHandler() { + ResetReceivedEvents(); + } + bool ReceievedClearSuccessEvent() { return received_clear_success_event_; } + bool ReceievedClearFailedEvent() { return received_clear_failed_event_; } + void ResetReceivedEvents() { + received_clear_success_event_ = false; + received_clear_failed_event_ = false; + } + + virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { + if (event.what_happened == SyncEngineEvent::CLEAR_SERVER_DATA_FAILED) { + received_clear_failed_event_ = true; + } else if (event.what_happened == + SyncEngineEvent::CLEAR_SERVER_DATA_SUCCEEDED) { + received_clear_success_event_ = true; + } + } + + private: + bool received_clear_success_event_; + bool received_clear_failed_event_; +}; + +TEST_F(ClearDataCommandTest, ClearDataCommandExpectFailed) { + ConfigureMockServerConnection(); + scoped_ptr<ClearEventHandler> handler(new ClearEventHandler()); + TestScopedSessionEventListener reg(context(), handler.get()); + + directory()->set_store_birthday(mock_server()->store_birthday()); + mock_server()->SetServerNotReachable(); + on_should_stop_syncing_permanently_called_ = false; + + command_.Execute(session()); + + // Expect that the client sent a clear request, received failure, + // fired a failure event, but did not disable sync. + // + // A failure event will be bubbled back to the user's UI, and the + // user can press "clear" again. + // + // We do not want to disable sync in the client because the user may + // incorrectly get the impression that their private data has been cleared + // from the server (from the fact that their data is gone on the client). + // + // Any subsequent GetUpdates/Commit requests or attempts to enable sync + // will cause the server to attempt to resume the clearing process (within + // a bounded window of time) + const sync_pb::ClientToServerMessage& r = mock_server()->last_request(); + EXPECT_TRUE(r.has_clear_user_data()); + + EXPECT_TRUE(handler.get()->ReceievedClearFailedEvent()); + + EXPECT_FALSE(handler.get()->ReceievedClearSuccessEvent()); + EXPECT_FALSE(on_should_stop_syncing_permanently_called_); +} + +TEST_F(ClearDataCommandTest, ClearDataCommandExpectSuccess) { + ConfigureMockServerConnection(); + scoped_ptr<ClearEventHandler> handler(new ClearEventHandler()); + TestScopedSessionEventListener reg(context(), handler.get()); + + directory()->set_store_birthday(mock_server()->store_birthday()); + mock_server()->SetClearUserDataResponseStatus(sync_pb::SyncEnums::SUCCESS); + on_should_stop_syncing_permanently_called_ = false; + + command_.Execute(session()); + + // Expect that the client sent a clear request, fired off the success event + // in response, and disabled sync + const sync_pb::ClientToServerMessage& r = mock_server()->last_request(); + EXPECT_TRUE(r.has_clear_user_data()); + + EXPECT_TRUE(handler->ReceievedClearSuccessEvent()); + EXPECT_TRUE(on_should_stop_syncing_permanently_called_); + + EXPECT_FALSE(handler->ReceievedClearFailedEvent()); +} + +} // namespace browser_sync |