summaryrefslogtreecommitdiffstats
path: root/chrome/test/sync/engine/test_id_factory.h
blob: 9ae0469bffb0a195189fb87712331b1a49fc2512 (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
// Copyright (c) 2009 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.
//
// A tool making it easier to create IDs for unit testing.

#ifndef CHROME_TEST_SYNC_ENGINE_TEST_ID_FACTORY_H_
#define CHROME_TEST_SYNC_ENGINE_TEST_ID_FACTORY_H_
#pragma once

#include <string>

#include "base/string_number_conversions.h"
#include "chrome/browser/sync/syncable/syncable_id.h"

namespace browser_sync {

class TestIdFactory {
 public:
  TestIdFactory() : next_value_(1337000) {}
  ~TestIdFactory() {}

  // Get the root ID.
  static syncable::Id root() {
    return syncable::Id();
  }

  // Make an ID from a number.  If the number is zero, return the root ID.
  // If the number is positive, create a server ID based on the value.  If
  // the number is negative, create a local ID based on the value.  This
  // is deterministic, and [FromNumber(X) == FromNumber(Y)] iff [X == Y].
  static syncable::Id FromNumber(int64 value) {
    if (value == 0)
      return root();
    else if (value < 0)
      return syncable::Id::CreateFromClientString(base::Int64ToString(value));
    else
      return syncable::Id::CreateFromServerId(base::Int64ToString(value));
  }

  // Create a local ID from a name.
  static syncable::Id MakeLocal(std::string name) {
    return syncable::Id::CreateFromClientString(std::string("lient ") + name);
  }

  // Create a server ID from a string name.
  static syncable::Id MakeServer(std::string name) {
    return syncable::Id::CreateFromServerId(std::string("erver ") + name);
  }

  // Autogenerate a fresh local ID.
  syncable::Id NewLocalId() {
    return syncable::Id::CreateFromClientString(
        std::string("_auto ") + base::IntToString(-next_value()));
  }

  // Autogenerate a fresh server ID.
  syncable::Id NewServerId() {
    return syncable::Id::CreateFromServerId(
        std::string("_auto ") + base::IntToString(next_value()));
  }

 private:
  int next_value() {
    return next_value_++;
  }
  int next_value_;
};

}  // namespace browser_sync

#endif  // CHROME_TEST_SYNC_ENGINE_TEST_ID_FACTORY_H_