- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / test / integration / sync_errors_test.cc
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 "base/prefs/pref_member.h"
6 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/sync/profile_sync_service.h"
8 #include "chrome/browser/sync/profile_sync_service_harness.h"
9 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
10 #include "chrome/browser/sync/test/integration/passwords_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_test.h"
12 #include "chrome/common/pref_names.h"
13 #include "google_apis/gaia/google_service_auth_error.h"
14 #include "sync/protocol/sync_protocol_error.h"
15
16 using bookmarks_helper::AddFolder;
17 using bookmarks_helper::SetTitle;
18
19 class SyncErrorTest : public SyncTest {
20  public:
21   SyncErrorTest() : SyncTest(SINGLE_CLIENT) {}
22   virtual ~SyncErrorTest() {}
23
24  private:
25   DISALLOW_COPY_AND_ASSIGN(SyncErrorTest);
26 };
27
28 IN_PROC_BROWSER_TEST_F(SyncErrorTest, BirthdayErrorTest) {
29   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
30
31   const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
32   SetTitle(0, node1, L"new_title1");
33   ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Offline state change."));
34   TriggerBirthdayError();
35
36   // Now make one more change so we will do another sync.
37   const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
38   SetTitle(0, node2, L"new_title2");
39   ASSERT_TRUE(GetClient(0)->AwaitSyncDisabled("Birthday error."));
40 }
41
42 IN_PROC_BROWSER_TEST_F(SyncErrorTest, TransientErrorTest) {
43   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
44
45   const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
46   SetTitle(0, node1, L"new_title1");
47   ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Offline state change."));
48   TriggerTransientError();
49
50   // Now make one more change so we will do another sync.
51   const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
52   SetTitle(0, node2, L"new_title2");
53   ASSERT_TRUE(
54       GetClient(0)->AwaitExponentialBackoffVerification());
55 }
56
57 IN_PROC_BROWSER_TEST_F(SyncErrorTest, ActionableErrorTest) {
58   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
59
60   const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
61   SetTitle(0, node1, L"new_title1");
62   ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Sync."));
63
64   syncer::SyncProtocolError protocol_error;
65   protocol_error.error_type = syncer::TRANSIENT_ERROR;
66   protocol_error.action = syncer::UPGRADE_CLIENT;
67   protocol_error.error_description = "Not My Fault";
68   protocol_error.url = "www.google.com";
69   TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_ALWAYS);
70
71   // Now make one more change so we will do another sync.
72   const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
73   SetTitle(0, node2, L"new_title2");
74   ASSERT_TRUE(
75       GetClient(0)->AwaitActionableError());
76   ProfileSyncService::Status status = GetClient(0)->GetStatus();
77   ASSERT_EQ(status.sync_protocol_error.error_type, protocol_error.error_type);
78   ASSERT_EQ(status.sync_protocol_error.action, protocol_error.action);
79   ASSERT_EQ(status.sync_protocol_error.url, protocol_error.url);
80   ASSERT_EQ(status.sync_protocol_error.error_description,
81       protocol_error.error_description);
82 }
83
84 IN_PROC_BROWSER_TEST_F(SyncErrorTest, ErrorWhileSettingUp) {
85   ASSERT_TRUE(SetupClients());
86
87   syncer::SyncProtocolError protocol_error;
88   protocol_error.error_type = syncer::TRANSIENT_ERROR;
89   protocol_error.error_description = "Not My Fault";
90   protocol_error.url = "www.google.com";
91
92   if (clients()[0]->AutoStartEnabled()) {
93     // In auto start enabled platforms like chrome os we should be
94     // able to set up even if the first sync while setting up fails.
95     // Trigger error on every 2 out of 3 requests.
96     TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_TWO_THIRDS);
97     // Now setup sync and it should succeed.
98     ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
99   } else {
100     // In Non auto start enabled environments if the setup sync fails then
101     // the setup would fail. So setup sync normally.
102     ASSERT_TRUE(SetupSync()) << "Setup sync failed";
103     ASSERT_TRUE(clients()[0]->DisableSyncForDatatype(syncer::AUTOFILL));
104
105     // Trigger error on every 2 out of 3 requests.
106     TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_TWO_THIRDS);
107
108     // Now enable a datatype, whose first 2 syncs would fail, but we should
109     // recover and setup succesfully on the third attempt.
110     ASSERT_TRUE(clients()[0]->EnableSyncForDatatype(syncer::AUTOFILL));
111   }
112 }
113
114
115 IN_PROC_BROWSER_TEST_F(SyncErrorTest,
116     BirthdayErrorUsingActionableErrorTest) {
117   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
118
119   const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
120   SetTitle(0, node1, L"new_title1");
121   ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Sync."));
122
123   syncer::SyncProtocolError protocol_error;
124   protocol_error.error_type = syncer::NOT_MY_BIRTHDAY;
125   protocol_error.action = syncer::DISABLE_SYNC_ON_CLIENT;
126   protocol_error.error_description = "Not My Fault";
127   protocol_error.url = "www.google.com";
128   TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_ALWAYS);
129
130   // Now make one more change so we will do another sync.
131   const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
132   SetTitle(0, node2, L"new_title2");
133   ASSERT_TRUE(
134       GetClient(0)->AwaitSyncDisabled("Birthday Error."));
135   ProfileSyncService::Status status = GetClient(0)->GetStatus();
136   ASSERT_EQ(status.sync_protocol_error.error_type, protocol_error.error_type);
137   ASSERT_EQ(status.sync_protocol_error.action, protocol_error.action);
138   ASSERT_EQ(status.sync_protocol_error.url, protocol_error.url);
139   ASSERT_EQ(status.sync_protocol_error.error_description,
140       protocol_error.error_description);
141 }
142
143 // TODO(pavely): Fix this test. Test needs to successfully setup sync. Then
144 // setup server to trigger auth error and setup FakeURLFetcher to return
145 // INVALID_CREDENTIALS failure for access token request. Then it should
146 // trigger sync and verify that error surfaced through
147 // ProfileSyncService::GetAuthError()
148 //
149 // Trigger an auth error and make sure the sync client displays a warning in the
150 // UI.
151 IN_PROC_BROWSER_TEST_F(SyncErrorTest, DISABLED_AuthErrorTest) {
152   ASSERT_TRUE(SetupClients());
153   TriggerAuthError();
154
155   ASSERT_FALSE(GetClient(0)->SetupSync());
156   ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
157             GetClient(0)->service()->GetAuthError().state());
158 }
159
160 // TODO(pavely): Fix this test. Test needs to successfully setup sync. Then
161 // setup server to trigger xmpp auth error and setup FakeURLFetcher to return
162 // INVALID_CREDENTIALS failure for access token request. Then it should
163 // trigger sync and verify that error surfaced through
164 // ProfileSyncService::GetAuthError()
165 //
166 // Trigger an XMPP auth error, and make sure sync treats it like any
167 // other auth error.
168 IN_PROC_BROWSER_TEST_F(SyncErrorTest, DISABLED_XmppAuthErrorTest) {
169   ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
170
171   TriggerXmppAuthError();
172
173   ASSERT_FALSE(GetClient(0)->SetupSync());
174
175   ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
176             GetClient(0)->service()->GetAuthError().state());
177 }
178
179 // TODO(lipalani): Fix the typed_url dtc so this test case can pass.
180 IN_PROC_BROWSER_TEST_F(SyncErrorTest, DISABLED_DisableDatatypeWhileRunning) {
181   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
182   syncer::ModelTypeSet synced_datatypes =
183       GetClient(0)->service()->GetPreferredDataTypes();
184   ASSERT_TRUE(synced_datatypes.Has(syncer::TYPED_URLS));
185   GetProfile(0)->GetPrefs()->SetBoolean(
186       prefs::kSavingBrowserHistoryDisabled, true);
187
188   synced_datatypes = GetClient(0)->service()->GetPreferredDataTypes();
189   ASSERT_FALSE(synced_datatypes.Has(syncer::TYPED_URLS));
190
191   const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
192   SetTitle(0, node1, L"new_title1");
193   ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Sync."));
194   // TODO(lipalani)" Verify initial sync ended for typed url is false.
195 }
196