summaryrefslogtreecommitdiffstats
path: root/sync/engine/clear_data_command_unittest.cc
blob: b94d08a019e5fa065661d8b495bcff2eeae6d536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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