sync_error_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "components/sync/model/sync_error.h"
  5. #include "base/location.h"
  6. #include "base/logging.h"
  7. #include "testing/gmock/include/gmock/gmock.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace syncer {
  10. namespace {
  11. using std::string;
  12. using ::testing::HasSubstr;
  13. using SyncErrorTest = ::testing::Test;
  14. TEST_F(SyncErrorTest, Unset) {
  15. SyncError error;
  16. EXPECT_FALSE(error.IsSet());
  17. }
  18. TEST_F(SyncErrorTest, Default) {
  19. base::Location location = FROM_HERE;
  20. std::string msg = "test";
  21. ModelType type = PREFERENCES;
  22. SyncError error(location, SyncError::DATATYPE_ERROR, msg, type);
  23. ASSERT_TRUE(error.IsSet());
  24. EXPECT_EQ(location.line_number(), error.location().line_number());
  25. EXPECT_EQ("datatype error was encountered: ", error.GetMessagePrefix());
  26. EXPECT_EQ(msg, error.message());
  27. EXPECT_EQ(type, error.model_type());
  28. EXPECT_EQ(SyncError::SYNC_ERROR_SEVERITY_ERROR, error.GetSeverity());
  29. }
  30. TEST_F(SyncErrorTest, LowSeverity) {
  31. base::Location location = FROM_HERE;
  32. std::string msg = "test";
  33. ModelType type = PREFERENCES;
  34. SyncError error(location, SyncError::DATATYPE_POLICY_ERROR, msg, type);
  35. ASSERT_TRUE(error.IsSet());
  36. EXPECT_EQ(location.line_number(), error.location().line_number());
  37. EXPECT_EQ("disabled due to configuration constraints: ",
  38. error.GetMessagePrefix());
  39. EXPECT_EQ(msg, error.message());
  40. EXPECT_EQ(type, error.model_type());
  41. EXPECT_EQ(SyncError::SYNC_ERROR_SEVERITY_INFO, error.GetSeverity());
  42. }
  43. TEST_F(SyncErrorTest, Reset) {
  44. base::Location location = FROM_HERE;
  45. std::string msg = "test";
  46. ModelType type = PREFERENCES;
  47. SyncError error;
  48. EXPECT_FALSE(error.IsSet());
  49. error.Reset(location, msg, type);
  50. ASSERT_TRUE(error.IsSet());
  51. EXPECT_EQ(location.line_number(), error.location().line_number());
  52. EXPECT_EQ(msg, error.message());
  53. EXPECT_EQ(type, error.model_type());
  54. base::Location location2 = FROM_HERE;
  55. std::string msg2 = "test";
  56. ModelType type2 = PREFERENCES;
  57. error.Reset(location2, msg2, type2);
  58. ASSERT_TRUE(error.IsSet());
  59. EXPECT_EQ(location2.line_number(), error.location().line_number());
  60. EXPECT_EQ(msg2, error.message());
  61. EXPECT_EQ(type2, error.model_type());
  62. }
  63. TEST_F(SyncErrorTest, Copy) {
  64. base::Location location = FROM_HERE;
  65. std::string msg = "test";
  66. ModelType type = PREFERENCES;
  67. SyncError error1;
  68. EXPECT_FALSE(error1.IsSet());
  69. SyncError error2(error1);
  70. EXPECT_FALSE(error2.IsSet());
  71. error1.Reset(location, msg, type);
  72. ASSERT_TRUE(error1.IsSet());
  73. EXPECT_EQ(location.line_number(), error1.location().line_number());
  74. EXPECT_EQ(msg, error1.message());
  75. EXPECT_EQ(type, error1.model_type());
  76. SyncError error3(error1);
  77. ASSERT_TRUE(error3.IsSet());
  78. EXPECT_EQ(error1.location().line_number(), error3.location().line_number());
  79. EXPECT_EQ(error1.message(), error3.message());
  80. EXPECT_EQ(error1.model_type(), error3.model_type());
  81. SyncError error4;
  82. EXPECT_FALSE(error4.IsSet());
  83. SyncError error5(error4);
  84. EXPECT_FALSE(error5.IsSet());
  85. }
  86. TEST_F(SyncErrorTest, Assign) {
  87. base::Location location = FROM_HERE;
  88. std::string msg = "test";
  89. ModelType type = PREFERENCES;
  90. SyncError error1;
  91. EXPECT_FALSE(error1.IsSet());
  92. SyncError error2;
  93. error2 = error1;
  94. EXPECT_FALSE(error2.IsSet());
  95. error1.Reset(location, msg, type);
  96. ASSERT_TRUE(error1.IsSet());
  97. EXPECT_EQ(location.line_number(), error1.location().line_number());
  98. EXPECT_EQ(msg, error1.message());
  99. EXPECT_EQ(type, error1.model_type());
  100. error2 = error1;
  101. ASSERT_TRUE(error2.IsSet());
  102. EXPECT_EQ(error1.location().line_number(), error2.location().line_number());
  103. EXPECT_EQ(error1.message(), error2.message());
  104. EXPECT_EQ(error1.model_type(), error2.model_type());
  105. error2 = SyncError();
  106. EXPECT_FALSE(error2.IsSet());
  107. }
  108. TEST_F(SyncErrorTest, ToString) {
  109. base::Location location = FROM_HERE;
  110. std::string msg = "test";
  111. ModelType type = PREFERENCES;
  112. std::string expected = std::string(ModelTypeToDebugString(type)) +
  113. " datatype error was encountered: " + msg;
  114. LOG(INFO) << "Expect " << expected;
  115. SyncError error(location, SyncError::DATATYPE_ERROR, msg, type);
  116. EXPECT_TRUE(error.IsSet());
  117. EXPECT_THAT(error.ToString(), HasSubstr(expected));
  118. SyncError error2;
  119. EXPECT_FALSE(error2.IsSet());
  120. EXPECT_EQ(std::string(), error2.ToString());
  121. error2 = error;
  122. EXPECT_TRUE(error2.IsSet());
  123. EXPECT_THAT(error.ToString(), HasSubstr(expected));
  124. }
  125. } // namespace
  126. } // namespace syncer