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/download_updates_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
14namespace browser_sync {
15
16using syncable::FIRST_REAL_MODEL_TYPE;
17using syncable::MODEL_TYPE_COUNT;
18
19// A test fixture for tests exercising DownloadUpdatesCommandTest.
20class DownloadUpdatesCommandTest : public SyncerCommandTest {
21 protected:
22  DownloadUpdatesCommandTest() {}
23
24  virtual void SetUp() {
25    workers()->clear();
26    mutable_routing_info()->clear();
27    // GROUP_PASSIVE worker.
28    workers()->push_back(make_scoped_refptr(new ModelSafeWorker()));
29    SyncerCommandTest::SetUp();
30  }
31
32  virtual void SetRoutingInfo(const syncable::ModelTypeSet& types) {
33    for (syncable::ModelTypeSet::const_iterator iter = types.begin();
34         iter != types.end(); ++iter) {
35      (*mutable_routing_info())[*iter] = GROUP_PASSIVE;
36    }
37  }
38
39  DownloadUpdatesCommand command_;
40 private:
41  DISALLOW_COPY_AND_ASSIGN(DownloadUpdatesCommandTest);
42};
43
44TEST_F(DownloadUpdatesCommandTest, SetRequestedTypes) {
45  {
46    SCOPED_TRACE("Several enabled datatypes, spread out across groups.");
47    syncable::ModelTypeBitSet enabled_types;
48    enabled_types[syncable::BOOKMARKS] = true;
49    enabled_types[syncable::AUTOFILL] = true;
50    enabled_types[syncable::PREFERENCES] = true;
51    sync_pb::EntitySpecifics get_updates_filter;
52    command_.SetRequestedTypes(enabled_types, &get_updates_filter);
53    ProtoExtensionValidator<sync_pb::EntitySpecifics> v(get_updates_filter);
54    v.ExpectHasExtension(sync_pb::autofill);
55    v.ExpectHasExtension(sync_pb::preference);
56    v.ExpectHasExtension(sync_pb::bookmark);
57    v.ExpectNoOtherFieldsOrExtensions();
58  }
59
60  {
61    SCOPED_TRACE("Top level folders.");
62    syncable::ModelTypeBitSet enabled_types;
63    enabled_types[syncable::TOP_LEVEL_FOLDER] = true;
64    enabled_types[syncable::BOOKMARKS] = true;
65    sync_pb::EntitySpecifics get_updates_filter;
66    command_.SetRequestedTypes(enabled_types, &get_updates_filter);
67    ProtoExtensionValidator<sync_pb::EntitySpecifics> v(get_updates_filter);
68    v.ExpectHasExtension(sync_pb::bookmark);
69    v.ExpectNoOtherFieldsOrExtensions();
70  }
71
72  {
73    SCOPED_TRACE("Bookmarks only.");
74    syncable::ModelTypeBitSet enabled_types;
75    enabled_types[syncable::BOOKMARKS] = true;
76    sync_pb::EntitySpecifics get_updates_filter;
77    command_.SetRequestedTypes(enabled_types, &get_updates_filter);
78    ProtoExtensionValidator<sync_pb::EntitySpecifics> v(get_updates_filter);
79    v.ExpectHasExtension(sync_pb::bookmark);
80    v.ExpectNoOtherFieldsOrExtensions();
81  }
82
83  {
84    SCOPED_TRACE("Autofill only.");
85    syncable::ModelTypeBitSet enabled_types;
86    enabled_types[syncable::AUTOFILL] = true;
87    sync_pb::EntitySpecifics get_updates_filter;
88    command_.SetRequestedTypes(enabled_types, &get_updates_filter);
89    ProtoExtensionValidator<sync_pb::EntitySpecifics> v(get_updates_filter);
90    v.ExpectHasExtension(sync_pb::autofill);
91    v.ExpectNoOtherFieldsOrExtensions();
92  }
93
94  {
95    SCOPED_TRACE("Preferences only.");
96    syncable::ModelTypeBitSet enabled_types;
97    enabled_types[syncable::PREFERENCES] = true;
98    sync_pb::EntitySpecifics get_updates_filter;
99    command_.SetRequestedTypes(enabled_types, &get_updates_filter);
100    ProtoExtensionValidator<sync_pb::EntitySpecifics> v(get_updates_filter);
101    v.ExpectHasExtension(sync_pb::preference);
102    v.ExpectNoOtherFieldsOrExtensions();
103  }
104}
105
106TEST_F(DownloadUpdatesCommandTest, ExecuteNoPayloads) {
107  ConfigureMockServerConnection();
108
109  syncable::ModelTypeSet types;
110  types.insert(syncable::AUTOFILL);
111  types.insert(syncable::BOOKMARKS);
112  types.insert(syncable::PREFERENCES);
113  SetRoutingInfo(types);
114  mock_server()->ExpectGetUpdatesRequestTypes(ModelTypeBitSetFromSet(types));
115  command_.ExecuteImpl(session());
116}
117
118TEST_F(DownloadUpdatesCommandTest, ExecuteWithPayloads) {
119  ConfigureMockServerConnection();
120
121  syncable::ModelTypeSet types;
122  types.insert(syncable::AUTOFILL);
123  types.insert(syncable::BOOKMARKS);
124  types.insert(syncable::PREFERENCES);
125  SetRoutingInfo(types);
126  sessions::SyncSourceInfo source;
127  source.types[syncable::AUTOFILL] = "autofill_payload";
128  source.types[syncable::BOOKMARKS] = "bookmark_payload";
129  source.types[syncable::PREFERENCES] = "preferences_payload";
130  mock_server()->ExpectGetUpdatesRequestTypes(ModelTypeBitSetFromSet(types));
131  mock_server()->ExpectGetUpdatesRequestPayloads(source.types);
132  command_.ExecuteImpl(session(source));
133}
134
135}  // namespace browser_sync
136