1+ /*
2+ *
3+ * Nextcloud Android client application
4+ *
5+ * @author Tobias Kaminsky
6+ * Copyright (C) 2020 Tobias Kaminsky
7+ * Copyright (C) 2020 Nextcloud GmbH
8+ *
9+ * This program is free software: you can redistribute it and/or modify
10+ * it under the terms of the GNU Affero General Public License as published by
11+ * the Free Software Foundation, either version 3 of the License, or
12+ * (at your option) any later version.
13+ *
14+ * This program is distributed in the hope that it will be useful,
15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+ * GNU Affero General Public License for more details.
18+ *
19+ * You should have received a copy of the GNU Affero General Public License
20+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
21+ */
22+
23+ package com .nmc .android .ui .conflict ;
24+
25+ import android .content .Intent ;
26+
27+ import com .nextcloud .client .account .UserAccountManagerImpl ;
28+ import com .owncloud .android .AbstractIT ;
29+ import com .owncloud .android .R ;
30+ import com .owncloud .android .datamodel .FileDataStorageManager ;
31+ import com .owncloud .android .datamodel .OCFile ;
32+ import com .owncloud .android .db .OCUpload ;
33+ import com .owncloud .android .ui .activity .ConflictsResolveActivity ;
34+ import com .owncloud .android .ui .dialog .ConflictsResolveDialog ;
35+ import com .owncloud .android .utils .FileStorageUtils ;
36+
37+ import org .junit .After ;
38+ import org .junit .Rule ;
39+ import org .junit .Test ;
40+
41+ import androidx .test .espresso .intent .rule .IntentsTestRule ;
42+
43+ import static androidx .test .espresso .Espresso .onView ;
44+ import static androidx .test .espresso .action .ViewActions .click ;
45+ import static androidx .test .espresso .matcher .ViewMatchers .withId ;
46+ import static androidx .test .platform .app .InstrumentationRegistry .getInstrumentation ;
47+ import static junit .framework .TestCase .assertTrue ;
48+ import static org .junit .Assert .assertEquals ;
49+
50+ public class ConflictsResolveConsentDialogIT extends AbstractIT {
51+ @ Rule public IntentsTestRule <ConflictsResolveActivity > activityRule =
52+ new IntentsTestRule <>(ConflictsResolveActivity .class , true , false );
53+ private boolean returnCode ;
54+
55+ @ Test
56+ public void replaceWithNewFile () {
57+ returnCode = false ;
58+
59+ OCUpload newUpload = new OCUpload (FileStorageUtils .getSavePath (user .getAccountName ()) + "/nonEmpty.txt" ,
60+ "/newFile.txt" ,
61+ user .getAccountName ());
62+
63+ OCFile existingFile = new OCFile ("/newFile.txt" );
64+ existingFile .setFileLength (1024000 );
65+ existingFile .setModificationTimestamp (1582019340 );
66+ existingFile .setRemoteId ("00000123abc" );
67+
68+ OCFile newFile = new OCFile ("/newFile.txt" );
69+ newFile .setFileLength (56000 );
70+ newFile .setModificationTimestamp (1522019340 );
71+ newFile .setStoragePath (FileStorageUtils .getSavePath (user .getAccountName ()) + "/nonEmpty.txt" );
72+
73+ FileDataStorageManager storageManager = new FileDataStorageManager (user , targetContext .getContentResolver ());
74+ storageManager .saveNewFile (existingFile );
75+
76+ Intent intent = new Intent (targetContext , ConflictsResolveActivity .class );
77+ intent .putExtra (ConflictsResolveActivity .EXTRA_FILE , newFile );
78+ intent .putExtra (ConflictsResolveActivity .EXTRA_EXISTING_FILE , existingFile );
79+ intent .putExtra (ConflictsResolveActivity .EXTRA_CONFLICT_UPLOAD_ID , newUpload .getUploadId ());
80+ intent .putExtra (ConflictsResolveActivity .EXTRA_LAUNCHED_FROM_TEST , true );
81+
82+ ConflictsResolveActivity sut = activityRule .launchActivity (intent );
83+
84+ ConflictsResolveConsentDialog dialog = ConflictsResolveConsentDialog .newInstance (existingFile ,
85+ newFile ,
86+ UserAccountManagerImpl
87+ .fromContext (targetContext )
88+ .getUser ()
89+ );
90+ dialog .showDialog (sut );
91+
92+ sut .listener = decision -> {
93+ assertEquals (decision , ConflictsResolveDialog .Decision .KEEP_LOCAL );
94+ returnCode = true ;
95+ };
96+
97+ getInstrumentation ().waitForIdleSync ();
98+
99+ onView (withId (R .id .replace_btn )).perform (click ());
100+
101+ assertTrue (returnCode );
102+ }
103+
104+ @ Test
105+ public void keepBothFiles () {
106+ returnCode = false ;
107+
108+ OCUpload newUpload = new OCUpload (FileStorageUtils .getSavePath (user .getAccountName ()) + "/nonEmpty.txt" ,
109+ "/newFile.txt" ,
110+ user .getAccountName ());
111+
112+ OCFile existingFile = new OCFile ("/newFile.txt" );
113+ existingFile .setFileLength (1024000 );
114+ existingFile .setModificationTimestamp (1582019340 );
115+
116+ OCFile newFile = new OCFile ("/newFile.txt" );
117+ newFile .setFileLength (56000 );
118+ newFile .setModificationTimestamp (1522019340 );
119+ newFile .setStoragePath (FileStorageUtils .getSavePath (user .getAccountName ()) + "/nonEmpty.txt" );
120+
121+ FileDataStorageManager storageManager = new FileDataStorageManager (user , targetContext .getContentResolver ());
122+ storageManager .saveNewFile (existingFile );
123+
124+ Intent intent = new Intent (targetContext , ConflictsResolveActivity .class );
125+ intent .putExtra (ConflictsResolveActivity .EXTRA_FILE , newFile );
126+ intent .putExtra (ConflictsResolveActivity .EXTRA_EXISTING_FILE , existingFile );
127+ intent .putExtra (ConflictsResolveActivity .EXTRA_CONFLICT_UPLOAD_ID , newUpload .getUploadId ());
128+ intent .putExtra (ConflictsResolveActivity .EXTRA_LAUNCHED_FROM_TEST , true );
129+
130+ ConflictsResolveActivity sut = activityRule .launchActivity (intent );
131+
132+ ConflictsResolveConsentDialog dialog = ConflictsResolveConsentDialog .newInstance (existingFile ,
133+ newFile ,
134+ UserAccountManagerImpl
135+ .fromContext (targetContext )
136+ .getUser ()
137+ );
138+ dialog .showDialog (sut );
139+
140+ sut .listener = decision -> {
141+ assertEquals (decision , ConflictsResolveDialog .Decision .KEEP_BOTH );
142+ returnCode = true ;
143+ };
144+
145+ getInstrumentation ().waitForIdleSync ();
146+
147+ onView (withId (R .id .keep_both_btn )).perform (click ());
148+
149+ assertTrue (returnCode );
150+ }
151+
152+ @ Test
153+ public void keepExistingFile () {
154+ returnCode = false ;
155+
156+ OCUpload newUpload = new OCUpload (FileStorageUtils .getSavePath (user .getAccountName ()) + "/nonEmpty.txt" ,
157+ "/newFile.txt" ,
158+ user .getAccountName ());
159+
160+ OCFile existingFile = new OCFile ("/newFile.txt" );
161+ existingFile .setFileLength (1024000 );
162+ existingFile .setModificationTimestamp (1582019340 );
163+
164+ OCFile newFile = new OCFile ("/newFile.txt" );
165+ newFile .setFileLength (56000 );
166+ newFile .setModificationTimestamp (1522019340 );
167+ newFile .setStoragePath (FileStorageUtils .getSavePath (user .getAccountName ()) + "/nonEmpty.txt" );
168+
169+ FileDataStorageManager storageManager = new FileDataStorageManager (user , targetContext .getContentResolver ());
170+ storageManager .saveNewFile (existingFile );
171+
172+ Intent intent = new Intent (targetContext , ConflictsResolveActivity .class );
173+ intent .putExtra (ConflictsResolveActivity .EXTRA_FILE , newFile );
174+ intent .putExtra (ConflictsResolveActivity .EXTRA_EXISTING_FILE , existingFile );
175+ intent .putExtra (ConflictsResolveActivity .EXTRA_CONFLICT_UPLOAD_ID , newUpload .getUploadId ());
176+ intent .putExtra (ConflictsResolveActivity .EXTRA_LAUNCHED_FROM_TEST , true );
177+
178+ ConflictsResolveActivity sut = activityRule .launchActivity (intent );
179+
180+ ConflictsResolveConsentDialog dialog = ConflictsResolveConsentDialog .newInstance (existingFile ,
181+ newFile ,
182+ UserAccountManagerImpl
183+ .fromContext (targetContext )
184+ .getUser ()
185+ );
186+ dialog .showDialog (sut );
187+
188+ sut .listener = decision -> {
189+ assertEquals (decision , ConflictsResolveDialog .Decision .KEEP_SERVER );
190+ returnCode = true ;
191+ };
192+
193+ getInstrumentation ().waitForIdleSync ();
194+
195+ onView (withId (R .id .cancel_keep_existing_btn )).perform (click ());
196+
197+ assertTrue (returnCode );
198+ }
199+
200+ @ After
201+ public void after () {
202+ getStorageManager ().deleteAllFiles ();
203+ }
204+ }
0 commit comments