1// Copyright (c) 2010 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 "chrome/browser/sync/engine/clear_data_command.h"
6#include "chrome/browser/sync/protocol/autofill_specifics.pb.h"
7#include "chrome/browser/sync/protocol/bookmark_specifics.pb.h"
8#include "chrome/browser/sync/protocol/preference_specifics.pb.h"
9#include "chrome/browser/sync/protocol/sync.pb.h"
10#include "chrome/browser/sync/syncable/directory_manager.h"
11#include "chrome/test/sync/engine/proto_extension_validator.h"
12#include "chrome/test/sync/engine/syncer_command_test.h"
13#include "chrome/test/sync/sessions/test_scoped_session_event_listener.h"
14
15namespace browser_sync {
16
17using sessions::TestScopedSessionEventListener;
18using syncable::FIRST_REAL_MODEL_TYPE;
19using syncable::MODEL_TYPE_COUNT;
20
21// A test fixture for tests exercising ClearDataCommandTest.
22class ClearDataCommandTest : public SyncerCommandTest {
23 protected:
24  ClearDataCommandTest() {}
25  ClearDataCommand command_;
26
27  virtual void OnShouldStopSyncingPermanently() {
28    on_should_stop_syncing_permanently_called_ = true;
29  }
30
31 protected:
32  bool on_should_stop_syncing_permanently_called_;
33
34 private:
35  DISALLOW_COPY_AND_ASSIGN(ClearDataCommandTest);
36};
37
38class ClearEventHandler : public SyncEngineEventListener {
39 public:
40  ClearEventHandler() {
41    ResetReceivedEvents();
42  }
43  bool ReceievedClearSuccessEvent() { return received_clear_success_event_; }
44  bool ReceievedClearFailedEvent() { return received_clear_failed_event_; }
45  void ResetReceivedEvents() {
46    received_clear_success_event_ = false;
47    received_clear_failed_event_ = false;
48  }
49
50  virtual void OnSyncEngineEvent(const SyncEngineEvent& event) {
51    if (event.what_happened == SyncEngineEvent::CLEAR_SERVER_DATA_FAILED) {
52      received_clear_failed_event_ = true;
53    } else if (event.what_happened ==
54      SyncEngineEvent::CLEAR_SERVER_DATA_SUCCEEDED) {
55        received_clear_success_event_ = true;
56    }
57  }
58
59 private:
60  bool received_clear_success_event_;
61  bool received_clear_failed_event_;
62};
63
64TEST_F(ClearDataCommandTest, ClearDataCommandExpectFailed) {
65  syncable::ScopedDirLookup dir(syncdb()->manager(), syncdb()->name());
66  ASSERT_TRUE(dir.good());
67
68  ConfigureMockServerConnection();
69  scoped_ptr<ClearEventHandler> handler(new ClearEventHandler());
70  TestScopedSessionEventListener reg(context(), handler.get());
71
72  dir->set_store_birthday(mock_server()->store_birthday());
73  mock_server()->SetServerNotReachable();
74  on_should_stop_syncing_permanently_called_ = false;
75
76  command_.Execute(session());
77
78  // Expect that the client sent a clear request, received failure,
79  // fired a failure event, but did not disable sync.
80  //
81  // A failure event will be bubbled back to the user's UI, and the
82  // user can press "clear" again.
83  //
84  // We do not want to disable sync in the client because the user may
85  // incorrectly get the impression that their private data has been cleared
86  // from the server (from the fact that their data is gone on the client).
87  //
88  // Any subsequent GetUpdates/Commit requests or attempts to enable sync
89  // will cause the server to attempt to resume the clearing process (within
90  // a bounded window of time)
91  const sync_pb::ClientToServerMessage& r = mock_server()->last_request();
92  EXPECT_TRUE(r.has_clear_user_data());
93
94  EXPECT_TRUE(handler.get()->ReceievedClearFailedEvent());
95
96  EXPECT_FALSE(handler.get()->ReceievedClearSuccessEvent());
97  EXPECT_FALSE(on_should_stop_syncing_permanently_called_);
98}
99
100TEST_F(ClearDataCommandTest, ClearDataCommandExpectSuccess) {
101  syncable::ScopedDirLookup dir(syncdb()->manager(), syncdb()->name());
102  ASSERT_TRUE(dir.good());
103
104  ConfigureMockServerConnection();
105  scoped_ptr<ClearEventHandler> handler(new ClearEventHandler());
106  TestScopedSessionEventListener reg(context(), handler.get());
107
108  dir->set_store_birthday(mock_server()->store_birthday());
109  mock_server()->SetClearUserDataResponseStatus(
110      sync_pb::ClientToServerResponse::SUCCESS);
111  on_should_stop_syncing_permanently_called_ = false;
112
113  command_.Execute(session());
114
115  // Expect that the client sent a clear request, fired off the success event
116  // in response, and disabled sync
117  const sync_pb::ClientToServerMessage& r = mock_server()->last_request();
118  EXPECT_TRUE(r.has_clear_user_data());
119
120  EXPECT_TRUE(handler->ReceievedClearSuccessEvent());
121  EXPECT_TRUE(on_should_stop_syncing_permanently_called_);
122
123  EXPECT_FALSE(handler->ReceievedClearFailedEvent());
124}
125
126}  // namespace browser_sync
127
128