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