model_type_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2012 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. #include <memory>
  5. #include <set>
  6. #include <string>
  7. #include "base/strings/string_util.h"
  8. #include "base/test/values_test_util.h"
  9. #include "base/values.h"
  10. #include "components/sync/base/model_type.h"
  11. #include "components/sync/protocol/entity_specifics.pb.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace syncer {
  14. namespace {
  15. class ModelTypeTest : public testing::Test {};
  16. TEST_F(ModelTypeTest, ModelTypeToValue) {
  17. for (int i = 0; i < GetNumModelTypes(); ++i) {
  18. ModelType model_type = ModelTypeFromInt(i);
  19. base::ExpectStringValue(ModelTypeToDebugString(model_type),
  20. *ModelTypeToValue(model_type));
  21. }
  22. }
  23. TEST_F(ModelTypeTest, ModelTypeSetToValue) {
  24. const ModelTypeSet model_types(BOOKMARKS, APPS);
  25. std::unique_ptr<base::ListValue> value(ModelTypeSetToValue(model_types));
  26. ASSERT_TRUE(value->is_list());
  27. base::Value::ConstListView value_list = value->GetListDeprecated();
  28. ASSERT_EQ(2u, value_list.size());
  29. ASSERT_TRUE(value_list[0].is_string());
  30. EXPECT_EQ("Bookmarks", value_list[0].GetString());
  31. ASSERT_TRUE(value_list[1].is_string());
  32. EXPECT_EQ("Apps", value_list[1].GetString());
  33. }
  34. TEST_F(ModelTypeTest, IsRealDataType) {
  35. EXPECT_FALSE(IsRealDataType(UNSPECIFIED));
  36. EXPECT_TRUE(IsRealDataType(FIRST_REAL_MODEL_TYPE));
  37. EXPECT_TRUE(IsRealDataType(LAST_REAL_MODEL_TYPE));
  38. EXPECT_TRUE(IsRealDataType(BOOKMARKS));
  39. EXPECT_TRUE(IsRealDataType(APPS));
  40. EXPECT_TRUE(IsRealDataType(ARC_PACKAGE));
  41. EXPECT_TRUE(IsRealDataType(PRINTERS));
  42. EXPECT_TRUE(IsRealDataType(PRINTERS_AUTHORIZATION_SERVERS));
  43. EXPECT_TRUE(IsRealDataType(READING_LIST));
  44. }
  45. // Make sure we can convert ModelTypes to and from specifics field
  46. // numbers.
  47. TEST_F(ModelTypeTest, ModelTypeToFromSpecificsFieldNumber) {
  48. ModelTypeSet protocol_types = ProtocolTypes();
  49. for (ModelType type : protocol_types) {
  50. int field_number = GetSpecificsFieldNumberFromModelType(type);
  51. EXPECT_EQ(type, GetModelTypeFromSpecificsFieldNumber(field_number));
  52. }
  53. }
  54. TEST_F(ModelTypeTest, ModelTypeOfInvalidSpecificsFieldNumber) {
  55. EXPECT_EQ(UNSPECIFIED, GetModelTypeFromSpecificsFieldNumber(0));
  56. }
  57. TEST_F(ModelTypeTest, ModelTypeHistogramMapping) {
  58. std::set<ModelTypeForHistograms> histogram_values;
  59. ModelTypeSet all_types = ModelTypeSet::All();
  60. for (ModelType type : all_types) {
  61. SCOPED_TRACE(ModelTypeToDebugString(type));
  62. ModelTypeForHistograms histogram_value = ModelTypeHistogramValue(type);
  63. EXPECT_TRUE(histogram_values.insert(histogram_value).second)
  64. << "Expected histogram values to be unique";
  65. EXPECT_LE(static_cast<int>(histogram_value),
  66. static_cast<int>(ModelTypeForHistograms::kMaxValue));
  67. }
  68. }
  69. TEST_F(ModelTypeTest, ModelTypeToStableIdentifier) {
  70. std::set<int> identifiers;
  71. ModelTypeSet all_types = ModelTypeSet::All();
  72. for (ModelType type : all_types) {
  73. SCOPED_TRACE(ModelTypeToDebugString(type));
  74. int stable_identifier = ModelTypeToStableIdentifier(type);
  75. EXPECT_GT(stable_identifier, 0);
  76. EXPECT_TRUE(identifiers.insert(stable_identifier).second)
  77. << "Expected identifier values to be unique";
  78. }
  79. // Hard code a few example model_types to make it harder to break that the
  80. // identifiers are stable.
  81. EXPECT_EQ(3, ModelTypeToStableIdentifier(BOOKMARKS));
  82. EXPECT_EQ(7, ModelTypeToStableIdentifier(AUTOFILL));
  83. EXPECT_EQ(9, ModelTypeToStableIdentifier(TYPED_URLS));
  84. }
  85. TEST_F(ModelTypeTest, DefaultFieldValues) {
  86. ModelTypeSet types = ProtocolTypes();
  87. for (ModelType type : types) {
  88. SCOPED_TRACE(ModelTypeToDebugString(type));
  89. sync_pb::EntitySpecifics specifics;
  90. AddDefaultFieldValue(type, &specifics);
  91. EXPECT_TRUE(specifics.IsInitialized());
  92. std::string tmp;
  93. EXPECT_TRUE(specifics.SerializeToString(&tmp));
  94. sync_pb::EntitySpecifics from_string;
  95. EXPECT_TRUE(from_string.ParseFromString(tmp));
  96. EXPECT_TRUE(from_string.IsInitialized());
  97. EXPECT_EQ(type, GetModelTypeFromSpecifics(from_string));
  98. }
  99. }
  100. TEST_F(ModelTypeTest, ModelTypeToRootTagValues) {
  101. for (ModelType model_type : ProtocolTypes()) {
  102. std::string root_tag = ModelTypeToRootTag(model_type);
  103. if (IsRealDataType(model_type)) {
  104. EXPECT_TRUE(base::StartsWith(root_tag, "google_chrome_",
  105. base::CompareCase::INSENSITIVE_ASCII));
  106. } else {
  107. EXPECT_EQ(root_tag, "INVALID");
  108. }
  109. }
  110. }
  111. TEST_F(ModelTypeTest, ModelTypeDebugStringIsNotEmpty) {
  112. for (ModelType model_type : ModelTypeSet::All()) {
  113. EXPECT_NE("", ModelTypeToDebugString(model_type));
  114. }
  115. }
  116. TEST_F(ModelTypeTest, ModelTypeNotificationTypeMapping) {
  117. ModelTypeSet all_types = ModelTypeSet::All();
  118. for (ModelType model_type : all_types) {
  119. std::string notification_type;
  120. bool ret = RealModelTypeToNotificationType(model_type, &notification_type);
  121. if (ret) {
  122. auto notified_model_type = ModelType::UNSPECIFIED;
  123. ASSERT_NE(model_type, notified_model_type);
  124. EXPECT_TRUE(NotificationTypeToRealModelType(notification_type,
  125. &notified_model_type));
  126. EXPECT_EQ(model_type, notified_model_type);
  127. } else {
  128. EXPECT_FALSE(ProtocolTypes().Has(model_type));
  129. EXPECT_TRUE(notification_type.empty());
  130. }
  131. }
  132. }
  133. } // namespace
  134. } // namespace syncer