fake_drive_service_unittest.cc 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  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/drive/service/fake_drive_service.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/bind.h"
  10. #include "base/files/file_util.h"
  11. #include "base/files/scoped_temp_dir.h"
  12. #include "base/hash/md5.h"
  13. #include "base/ranges/algorithm.h"
  14. #include "base/run_loop.h"
  15. #include "base/strings/string_number_conversions.h"
  16. #include "base/strings/stringprintf.h"
  17. #include "base/strings/utf_string_conversions.h"
  18. #include "base/test/task_environment.h"
  19. #include "base/time/time.h"
  20. #include "components/drive/file_system_core_util.h"
  21. #include "components/drive/service/test_util.h"
  22. #include "google_apis/common/test_util.h"
  23. #include "google_apis/drive/drive_api_parser.h"
  24. #include "testing/gtest/include/gtest/gtest.h"
  25. using google_apis::AboutResource;
  26. using google_apis::ApiErrorCode;
  27. using google_apis::ChangeList;
  28. using google_apis::ChangeResource;
  29. using google_apis::FileList;
  30. using google_apis::FileResource;
  31. using google_apis::GetContentCallback;
  32. using google_apis::HTTP_CREATED;
  33. using google_apis::HTTP_FORBIDDEN;
  34. using google_apis::HTTP_NO_CONTENT;
  35. using google_apis::HTTP_NOT_FOUND;
  36. using google_apis::HTTP_PRECONDITION;
  37. using google_apis::HTTP_RESUME_INCOMPLETE;
  38. using google_apis::HTTP_SUCCESS;
  39. using google_apis::NO_CONNECTION;
  40. using google_apis::OTHER_ERROR;
  41. using google_apis::ProgressCallback;
  42. using google_apis::StartPageToken;
  43. using google_apis::TeamDriveList;
  44. using google_apis::UploadRangeResponse;
  45. namespace drive {
  46. namespace test_util {
  47. using google_apis::test_util::AppendProgressCallbackResult;
  48. using google_apis::test_util::CreateCopyResultCallback;
  49. using google_apis::test_util::ProgressInfo;
  50. using google_apis::test_util::TestGetContentCallback;
  51. using google_apis::test_util::WriteStringToFile;
  52. } // namespace test_util
  53. namespace {
  54. constexpr char TEAM_DRIVE_ID_1[] = "the1stTeamDriveId";
  55. constexpr char TEAM_DRIVE_NAME_1[] = "The First Team Drive";
  56. constexpr char TEAM_DRIVE_ID_2[] = "the2ndTeamDriveId";
  57. constexpr char TEAM_DRIVE_NAME_2[] = "The Seconcd Team Drive";
  58. constexpr char TEAM_DRIVE_ID_3[] = "the3rdTeamDriveId";
  59. constexpr char TEAM_DRIVE_NAME_3[] = "The Third Team Drive";
  60. // Creates a new FileResourceCapabilities object with mixed (true/false)
  61. // capability settings.
  62. google_apis::FileResourceCapabilities CreateMixedFileCapabilities() {
  63. google_apis::FileResourceCapabilities capabilities;
  64. capabilities.set_can_add_children(true);
  65. capabilities.set_can_change_restricted_download(false);
  66. capabilities.set_can_comment(true);
  67. capabilities.set_can_copy(false);
  68. capabilities.set_can_delete(true);
  69. capabilities.set_can_download(false);
  70. capabilities.set_can_edit(true);
  71. capabilities.set_can_list_children(false);
  72. capabilities.set_can_move_item_into_team_drive(true);
  73. capabilities.set_can_move_team_drive_item(false);
  74. capabilities.set_can_read_revisions(true);
  75. capabilities.set_can_read_team_drive(false);
  76. capabilities.set_can_remove_children(true);
  77. capabilities.set_can_rename(false);
  78. capabilities.set_can_share(true);
  79. capabilities.set_can_trash(false);
  80. capabilities.set_can_untrash(true);
  81. return capabilities;
  82. }
  83. // Creates a new TeamDriveCapabilities object with mixed (true/false)
  84. // capability settings.
  85. google_apis::TeamDriveCapabilities CreateMixedTeamDriveCapabilities() {
  86. google_apis::TeamDriveCapabilities capabilities;
  87. capabilities.set_can_add_children(true);
  88. capabilities.set_can_comment(false);
  89. capabilities.set_can_copy(true);
  90. capabilities.set_can_delete_team_drive(false);
  91. capabilities.set_can_download(true);
  92. capabilities.set_can_edit(false);
  93. capabilities.set_can_list_children(true);
  94. capabilities.set_can_manage_members(false);
  95. capabilities.set_can_read_revisions(true);
  96. capabilities.set_can_remove_children(false);
  97. capabilities.set_can_rename(true);
  98. capabilities.set_can_rename_team_drive(false);
  99. capabilities.set_can_share(true);
  100. return capabilities;
  101. }
  102. // Compares two FileResourceCapabilities objects with EXPECT_EQ.
  103. void ExpectFileCapabilitiesEqual(
  104. const google_apis::FileResourceCapabilities& expectedCapabilities,
  105. const google_apis::FileResourceCapabilities& actualCapabilities) {
  106. EXPECT_EQ(expectedCapabilities.can_add_children(),
  107. actualCapabilities.can_add_children());
  108. EXPECT_EQ(expectedCapabilities.can_change_restricted_download(),
  109. actualCapabilities.can_change_restricted_download());
  110. EXPECT_EQ(expectedCapabilities.can_comment(),
  111. actualCapabilities.can_comment());
  112. EXPECT_EQ(expectedCapabilities.can_copy(), actualCapabilities.can_copy());
  113. EXPECT_EQ(expectedCapabilities.can_delete(), actualCapabilities.can_delete());
  114. EXPECT_EQ(expectedCapabilities.can_download(),
  115. actualCapabilities.can_download());
  116. EXPECT_EQ(expectedCapabilities.can_edit(), actualCapabilities.can_edit());
  117. EXPECT_EQ(expectedCapabilities.can_list_children(),
  118. actualCapabilities.can_list_children());
  119. EXPECT_EQ(expectedCapabilities.can_move_item_into_team_drive(),
  120. actualCapabilities.can_move_item_into_team_drive());
  121. EXPECT_EQ(expectedCapabilities.can_move_team_drive_item(),
  122. actualCapabilities.can_move_team_drive_item());
  123. EXPECT_EQ(expectedCapabilities.can_read_revisions(),
  124. actualCapabilities.can_read_revisions());
  125. EXPECT_EQ(expectedCapabilities.can_read_team_drive(),
  126. actualCapabilities.can_read_team_drive());
  127. EXPECT_EQ(expectedCapabilities.can_remove_children(),
  128. actualCapabilities.can_remove_children());
  129. EXPECT_EQ(expectedCapabilities.can_rename(), actualCapabilities.can_rename());
  130. EXPECT_EQ(expectedCapabilities.can_share(), actualCapabilities.can_share());
  131. EXPECT_EQ(expectedCapabilities.can_trash(), actualCapabilities.can_trash());
  132. EXPECT_EQ(expectedCapabilities.can_untrash(),
  133. actualCapabilities.can_untrash());
  134. }
  135. // Compares two FileResourceCapabilities objects with EXPECT_EQ.
  136. void ExpectTeamDriveCapabilitiesEqual(
  137. const google_apis::TeamDriveCapabilities& expectedCapabilities,
  138. const google_apis::TeamDriveCapabilities& actualCapabilities) {
  139. EXPECT_EQ(expectedCapabilities.can_add_children(),
  140. actualCapabilities.can_add_children());
  141. EXPECT_EQ(expectedCapabilities.can_comment(),
  142. actualCapabilities.can_comment());
  143. EXPECT_EQ(expectedCapabilities.can_copy(), actualCapabilities.can_copy());
  144. EXPECT_EQ(expectedCapabilities.can_delete_team_drive(),
  145. actualCapabilities.can_delete_team_drive());
  146. EXPECT_EQ(expectedCapabilities.can_download(),
  147. actualCapabilities.can_download());
  148. EXPECT_EQ(expectedCapabilities.can_edit(), actualCapabilities.can_edit());
  149. EXPECT_EQ(expectedCapabilities.can_list_children(),
  150. actualCapabilities.can_list_children());
  151. EXPECT_EQ(expectedCapabilities.can_manage_members(),
  152. actualCapabilities.can_manage_members());
  153. EXPECT_EQ(expectedCapabilities.can_read_revisions(),
  154. actualCapabilities.can_read_revisions());
  155. EXPECT_EQ(expectedCapabilities.can_remove_children(),
  156. actualCapabilities.can_remove_children());
  157. EXPECT_EQ(expectedCapabilities.can_rename(), actualCapabilities.can_rename());
  158. EXPECT_EQ(expectedCapabilities.can_rename_team_drive(),
  159. actualCapabilities.can_rename_team_drive());
  160. EXPECT_EQ(expectedCapabilities.can_share(), actualCapabilities.can_share());
  161. }
  162. class FakeDriveServiceTest : public testing::Test {
  163. protected:
  164. // Returns the resource entry that matches |resource_id|.
  165. std::unique_ptr<FileResource> FindEntry(const std::string& resource_id) {
  166. ApiErrorCode error = OTHER_ERROR;
  167. std::unique_ptr<FileResource> entry;
  168. fake_service_.GetFileResource(
  169. resource_id, test_util::CreateCopyResultCallback(&error, &entry));
  170. base::RunLoop().RunUntilIdle();
  171. return entry;
  172. }
  173. // Returns true if the resource identified by |resource_id| exists.
  174. bool Exists(const std::string& resource_id) {
  175. std::unique_ptr<FileResource> entry = FindEntry(resource_id);
  176. return entry && !entry->labels().is_trashed();
  177. }
  178. // Adds a new directory at |parent_resource_id| with the given name.
  179. // Returns true on success.
  180. bool AddNewDirectory(const std::string& parent_resource_id,
  181. const std::string& directory_title) {
  182. ApiErrorCode error = OTHER_ERROR;
  183. std::unique_ptr<FileResource> entry;
  184. fake_service_.AddNewDirectory(
  185. parent_resource_id, directory_title, AddNewDirectoryOptions(),
  186. test_util::CreateCopyResultCallback(&error, &entry));
  187. base::RunLoop().RunUntilIdle();
  188. return error == HTTP_CREATED;
  189. }
  190. // Returns true if the resource identified by |resource_id| has a parent
  191. // identified by |parent_id|.
  192. bool HasParent(const std::string& resource_id, const std::string& parent_id) {
  193. std::unique_ptr<FileResource> entry = FindEntry(resource_id);
  194. if (entry) {
  195. for (size_t i = 0; i < entry->parents().size(); ++i) {
  196. if (entry->parents()[i].file_id() == parent_id)
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. int64_t GetLargestChangeByAboutResource() {
  203. ApiErrorCode error;
  204. std::unique_ptr<AboutResource> about_resource;
  205. fake_service_.GetAboutResource(
  206. test_util::CreateCopyResultCallback(&error, &about_resource));
  207. base::RunLoop().RunUntilIdle();
  208. return about_resource->largest_change_id();
  209. }
  210. base::test::SingleThreadTaskEnvironment task_environment_;
  211. FakeDriveService fake_service_;
  212. };
  213. TEST_F(FakeDriveServiceTest, GetAllFileList) {
  214. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  215. ApiErrorCode error = OTHER_ERROR;
  216. std::unique_ptr<FileList> file_list;
  217. fake_service_.GetAllFileList(
  218. util::kTeamDriveIdDefaultCorpus,
  219. test_util::CreateCopyResultCallback(&error, &file_list));
  220. base::RunLoop().RunUntilIdle();
  221. EXPECT_EQ(HTTP_SUCCESS, error);
  222. ASSERT_TRUE(file_list);
  223. // Do some sanity check.
  224. EXPECT_EQ(15U, file_list->items().size());
  225. EXPECT_EQ(1, fake_service_.file_list_load_count());
  226. }
  227. TEST_F(FakeDriveServiceTest, GetAllFileList_TeamDrives) {
  228. ASSERT_TRUE(test_util::SetUpTeamDriveTestEntries(
  229. &fake_service_, TEAM_DRIVE_ID_1, TEAM_DRIVE_NAME_1));
  230. ApiErrorCode error = OTHER_ERROR;
  231. std::unique_ptr<FileList> file_list;
  232. fake_service_.GetAllFileList(
  233. TEAM_DRIVE_ID_1, test_util::CreateCopyResultCallback(&error, &file_list));
  234. base::RunLoop().RunUntilIdle();
  235. EXPECT_EQ(HTTP_SUCCESS, error);
  236. ASSERT_TRUE(file_list);
  237. // Do some sanity check.
  238. EXPECT_EQ(13U, file_list->items().size());
  239. EXPECT_EQ(1, fake_service_.file_list_load_count());
  240. }
  241. TEST_F(FakeDriveServiceTest, GetAllFileList_Offline) {
  242. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  243. fake_service_.set_offline(true);
  244. ApiErrorCode error = OTHER_ERROR;
  245. std::unique_ptr<FileList> file_list;
  246. fake_service_.GetAllFileList(
  247. util::kTeamDriveIdDefaultCorpus,
  248. test_util::CreateCopyResultCallback(&error, &file_list));
  249. base::RunLoop().RunUntilIdle();
  250. EXPECT_EQ(NO_CONNECTION, error);
  251. EXPECT_FALSE(file_list);
  252. }
  253. TEST_F(FakeDriveServiceTest, GetFileListInDirectory_InRootDirectory) {
  254. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  255. ApiErrorCode error = OTHER_ERROR;
  256. std::unique_ptr<FileList> file_list;
  257. fake_service_.GetFileListInDirectory(
  258. fake_service_.GetRootResourceId(),
  259. test_util::CreateCopyResultCallback(&error, &file_list));
  260. base::RunLoop().RunUntilIdle();
  261. EXPECT_EQ(HTTP_SUCCESS, error);
  262. ASSERT_TRUE(file_list);
  263. // Do some sanity check. There are 8 entries in the root directory.
  264. EXPECT_EQ(8U, file_list->items().size());
  265. EXPECT_EQ(1, fake_service_.directory_load_count());
  266. }
  267. TEST_F(FakeDriveServiceTest, GetFileListInDirectory_InNonRootDirectory) {
  268. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  269. ApiErrorCode error = OTHER_ERROR;
  270. std::unique_ptr<FileList> file_list;
  271. fake_service_.GetFileListInDirectory(
  272. "1_folder_resource_id",
  273. test_util::CreateCopyResultCallback(&error, &file_list));
  274. base::RunLoop().RunUntilIdle();
  275. EXPECT_EQ(HTTP_SUCCESS, error);
  276. ASSERT_TRUE(file_list);
  277. // Do some sanity check. There is three entries in 1_folder_resource_id
  278. // directory.
  279. EXPECT_EQ(3U, file_list->items().size());
  280. EXPECT_EQ(1, fake_service_.directory_load_count());
  281. }
  282. TEST_F(FakeDriveServiceTest, GetFileListInDirectory_Offline) {
  283. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  284. fake_service_.set_offline(true);
  285. ApiErrorCode error = OTHER_ERROR;
  286. std::unique_ptr<FileList> file_list;
  287. fake_service_.GetFileListInDirectory(
  288. fake_service_.GetRootResourceId(),
  289. test_util::CreateCopyResultCallback(&error, &file_list));
  290. base::RunLoop().RunUntilIdle();
  291. EXPECT_EQ(NO_CONNECTION, error);
  292. EXPECT_FALSE(file_list);
  293. }
  294. TEST_F(FakeDriveServiceTest, Search) {
  295. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  296. ApiErrorCode error = OTHER_ERROR;
  297. std::unique_ptr<FileList> file_list;
  298. fake_service_.Search("File", // search_query
  299. test_util::CreateCopyResultCallback(&error, &file_list));
  300. base::RunLoop().RunUntilIdle();
  301. EXPECT_EQ(HTTP_SUCCESS, error);
  302. ASSERT_TRUE(file_list);
  303. // Do some sanity check. There are 4 entries that contain "File" in their
  304. // titles.
  305. EXPECT_EQ(4U, file_list->items().size());
  306. }
  307. TEST_F(FakeDriveServiceTest, Search_WithAttribute) {
  308. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  309. ApiErrorCode error = OTHER_ERROR;
  310. std::unique_ptr<FileList> file_list;
  311. fake_service_.Search("title:1.txt", // search_query
  312. test_util::CreateCopyResultCallback(&error, &file_list));
  313. base::RunLoop().RunUntilIdle();
  314. EXPECT_EQ(HTTP_SUCCESS, error);
  315. ASSERT_TRUE(file_list);
  316. // Do some sanity check. There are 4 entries that contain "1.txt" in their
  317. // titles.
  318. EXPECT_EQ(4U, file_list->items().size());
  319. }
  320. TEST_F(FakeDriveServiceTest, Search_MultipleQueries) {
  321. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  322. ApiErrorCode error = OTHER_ERROR;
  323. std::unique_ptr<FileList> file_list;
  324. fake_service_.Search("Directory 1", // search_query
  325. test_util::CreateCopyResultCallback(&error, &file_list));
  326. base::RunLoop().RunUntilIdle();
  327. EXPECT_EQ(HTTP_SUCCESS, error);
  328. ASSERT_TRUE(file_list);
  329. // There are 2 entries that contain both "Directory" and "1" in their titles.
  330. EXPECT_EQ(2U, file_list->items().size());
  331. fake_service_.Search("\"Directory 1\"", // search_query
  332. test_util::CreateCopyResultCallback(&error, &file_list));
  333. base::RunLoop().RunUntilIdle();
  334. EXPECT_EQ(HTTP_SUCCESS, error);
  335. ASSERT_TRUE(file_list);
  336. // There is 1 entry that contain "Directory 1" in its title.
  337. EXPECT_EQ(1U, file_list->items().size());
  338. }
  339. TEST_F(FakeDriveServiceTest, Search_Offline) {
  340. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  341. fake_service_.set_offline(true);
  342. ApiErrorCode error = OTHER_ERROR;
  343. std::unique_ptr<FileList> file_list;
  344. fake_service_.Search("Directory 1", // search_query
  345. test_util::CreateCopyResultCallback(&error, &file_list));
  346. base::RunLoop().RunUntilIdle();
  347. EXPECT_EQ(NO_CONNECTION, error);
  348. EXPECT_FALSE(file_list);
  349. }
  350. TEST_F(FakeDriveServiceTest, Search_Deleted) {
  351. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  352. ApiErrorCode error = OTHER_ERROR;
  353. fake_service_.DeleteResource("2_file_resource_id",
  354. std::string(), // etag
  355. test_util::CreateCopyResultCallback(&error));
  356. base::RunLoop().RunUntilIdle();
  357. EXPECT_EQ(HTTP_NO_CONTENT, error);
  358. error = OTHER_ERROR;
  359. std::unique_ptr<FileList> file_list;
  360. fake_service_.Search("File", // search_query
  361. test_util::CreateCopyResultCallback(&error, &file_list));
  362. base::RunLoop().RunUntilIdle();
  363. EXPECT_EQ(HTTP_SUCCESS, error);
  364. ASSERT_TRUE(file_list);
  365. // Do some sanity check. There are 4 entries that contain "File" in their
  366. // titles and one of them is deleted.
  367. EXPECT_EQ(3U, file_list->items().size());
  368. }
  369. TEST_F(FakeDriveServiceTest, Search_Trashed) {
  370. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  371. ApiErrorCode error = OTHER_ERROR;
  372. fake_service_.TrashResource("2_file_resource_id",
  373. test_util::CreateCopyResultCallback(&error));
  374. base::RunLoop().RunUntilIdle();
  375. EXPECT_EQ(HTTP_SUCCESS, error);
  376. error = OTHER_ERROR;
  377. std::unique_ptr<FileList> file_list;
  378. fake_service_.Search("File", // search_query
  379. test_util::CreateCopyResultCallback(&error, &file_list));
  380. base::RunLoop().RunUntilIdle();
  381. EXPECT_EQ(HTTP_SUCCESS, error);
  382. ASSERT_TRUE(file_list);
  383. // Do some sanity check. There are 4 entries that contain "File" in their
  384. // titles and one of them is deleted.
  385. EXPECT_EQ(3U, file_list->items().size());
  386. }
  387. TEST_F(FakeDriveServiceTest, SearchByTitle) {
  388. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  389. ApiErrorCode error = OTHER_ERROR;
  390. std::unique_ptr<FileList> file_list;
  391. fake_service_.SearchByTitle(
  392. "1.txt", // title
  393. fake_service_.GetRootResourceId(), // directory_resource_id
  394. test_util::CreateCopyResultCallback(&error, &file_list));
  395. base::RunLoop().RunUntilIdle();
  396. EXPECT_EQ(HTTP_SUCCESS, error);
  397. ASSERT_TRUE(file_list);
  398. // Do some sanity check. There are 2 entries that contain "1.txt" in their
  399. // titles directly under the root directory.
  400. EXPECT_EQ(2U, file_list->items().size());
  401. }
  402. TEST_F(FakeDriveServiceTest, SearchByTitle_EmptyDirectoryResourceId) {
  403. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  404. ApiErrorCode error = OTHER_ERROR;
  405. std::unique_ptr<FileList> file_list;
  406. fake_service_.SearchByTitle(
  407. "1.txt", // title
  408. "", // directory resource id
  409. test_util::CreateCopyResultCallback(&error, &file_list));
  410. base::RunLoop().RunUntilIdle();
  411. EXPECT_EQ(HTTP_SUCCESS, error);
  412. ASSERT_TRUE(file_list);
  413. // Do some sanity check. There are 4 entries that contain "1.txt" in their
  414. // titles.
  415. EXPECT_EQ(4U, file_list->items().size());
  416. }
  417. TEST_F(FakeDriveServiceTest, SearchByTitle_Offline) {
  418. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  419. fake_service_.set_offline(true);
  420. ApiErrorCode error = OTHER_ERROR;
  421. std::unique_ptr<FileList> file_list;
  422. fake_service_.SearchByTitle(
  423. "Directory 1", // title
  424. fake_service_.GetRootResourceId(), // directory_resource_id
  425. test_util::CreateCopyResultCallback(&error, &file_list));
  426. base::RunLoop().RunUntilIdle();
  427. EXPECT_EQ(NO_CONNECTION, error);
  428. EXPECT_FALSE(file_list);
  429. }
  430. TEST_F(FakeDriveServiceTest, GetChangeList_NoNewEntries) {
  431. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  432. ApiErrorCode error = OTHER_ERROR;
  433. std::unique_ptr<ChangeList> change_list;
  434. fake_service_.GetChangeList(
  435. fake_service_.about_resource().largest_change_id() + 1,
  436. test_util::CreateCopyResultCallback(&error, &change_list));
  437. base::RunLoop().RunUntilIdle();
  438. EXPECT_EQ(HTTP_SUCCESS, error);
  439. ASSERT_TRUE(change_list);
  440. EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
  441. change_list->largest_change_id());
  442. // This should be empty as the latest changestamp was passed to
  443. // GetChangeList(), hence there should be no new entries.
  444. EXPECT_EQ(0U, change_list->items().size());
  445. // It's considered loaded even if the result is empty.
  446. EXPECT_EQ(1, fake_service_.change_list_load_count());
  447. }
  448. TEST_F(FakeDriveServiceTest, GetChangeList_WithNewEntry) {
  449. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  450. const int64_t old_largest_change_id =
  451. fake_service_.about_resource().largest_change_id();
  452. // Add a new directory in the root directory.
  453. ASSERT_TRUE(
  454. AddNewDirectory(fake_service_.GetRootResourceId(), "new directory"));
  455. // Get the resource list newer than old_largest_change_id.
  456. ApiErrorCode error = OTHER_ERROR;
  457. std::unique_ptr<ChangeList> change_list;
  458. fake_service_.GetChangeList(
  459. old_largest_change_id + 1,
  460. test_util::CreateCopyResultCallback(&error, &change_list));
  461. base::RunLoop().RunUntilIdle();
  462. EXPECT_EQ(HTTP_SUCCESS, error);
  463. ASSERT_TRUE(change_list);
  464. EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
  465. change_list->largest_change_id());
  466. // The result should only contain the newly created directory.
  467. ASSERT_EQ(1U, change_list->items().size());
  468. ASSERT_TRUE(change_list->items()[0]->file());
  469. EXPECT_EQ("new directory", change_list->items()[0]->file()->title());
  470. EXPECT_EQ(1, fake_service_.change_list_load_count());
  471. }
  472. TEST_F(FakeDriveServiceTest, GetChangeList_WithNewTeamDrive) {
  473. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  474. const int64_t old_largest_change_id =
  475. fake_service_.about_resource().largest_change_id();
  476. // Add a new team drive.
  477. fake_service_.AddTeamDrive(TEAM_DRIVE_ID_1, TEAM_DRIVE_NAME_1, "");
  478. // Get the resource list newer than old_largest_change_id.
  479. ApiErrorCode error = OTHER_ERROR;
  480. std::unique_ptr<ChangeList> change_list;
  481. fake_service_.GetChangeList(
  482. old_largest_change_id + 1,
  483. test_util::CreateCopyResultCallback(&error, &change_list));
  484. base::RunLoop().RunUntilIdle();
  485. EXPECT_EQ(HTTP_SUCCESS, error);
  486. ASSERT_TRUE(change_list);
  487. EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
  488. change_list->largest_change_id());
  489. // The result should only contain the newly created tam drive.
  490. ASSERT_EQ(1U, change_list->items().size());
  491. ASSERT_TRUE(change_list->items()[0]->team_drive());
  492. EXPECT_EQ(TEAM_DRIVE_ID_1, change_list->items()[0]->team_drive()->id());
  493. EXPECT_EQ(TEAM_DRIVE_NAME_1, change_list->items()[0]->team_drive()->name());
  494. EXPECT_EQ(1, fake_service_.change_list_load_count());
  495. }
  496. TEST_F(FakeDriveServiceTest, GetChangeList_Offline) {
  497. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  498. fake_service_.set_offline(true);
  499. ApiErrorCode error = OTHER_ERROR;
  500. std::unique_ptr<ChangeList> change_list;
  501. fake_service_.GetChangeList(
  502. 654321, // start_changestamp
  503. test_util::CreateCopyResultCallback(&error, &change_list));
  504. base::RunLoop().RunUntilIdle();
  505. EXPECT_EQ(NO_CONNECTION, error);
  506. EXPECT_FALSE(change_list);
  507. }
  508. TEST_F(FakeDriveServiceTest, GetChangeList_DeletedEntry) {
  509. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  510. ASSERT_TRUE(Exists("2_file_resource_id"));
  511. const int64_t old_largest_change_id =
  512. fake_service_.about_resource().largest_change_id();
  513. ApiErrorCode error = OTHER_ERROR;
  514. fake_service_.DeleteResource("2_file_resource_id",
  515. std::string(), // etag
  516. test_util::CreateCopyResultCallback(&error));
  517. base::RunLoop().RunUntilIdle();
  518. ASSERT_EQ(HTTP_NO_CONTENT, error);
  519. ASSERT_FALSE(Exists("2_file_resource_id"));
  520. // Get the resource list newer than old_largest_change_id.
  521. error = OTHER_ERROR;
  522. std::unique_ptr<ChangeList> change_list;
  523. fake_service_.GetChangeList(
  524. old_largest_change_id + 1,
  525. test_util::CreateCopyResultCallback(&error, &change_list));
  526. base::RunLoop().RunUntilIdle();
  527. EXPECT_EQ(HTTP_SUCCESS, error);
  528. ASSERT_TRUE(change_list);
  529. EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
  530. change_list->largest_change_id());
  531. // The result should only contain the deleted file.
  532. ASSERT_EQ(1U, change_list->items().size());
  533. const ChangeResource& item = *change_list->items()[0];
  534. EXPECT_EQ("2_file_resource_id", item.file_id());
  535. EXPECT_FALSE(item.file());
  536. EXPECT_TRUE(item.is_deleted());
  537. EXPECT_EQ(1, fake_service_.change_list_load_count());
  538. }
  539. TEST_F(FakeDriveServiceTest, GetChangeList_TrashedEntry) {
  540. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  541. ASSERT_TRUE(Exists("2_file_resource_id"));
  542. const int64_t old_largest_change_id =
  543. fake_service_.about_resource().largest_change_id();
  544. ApiErrorCode error = OTHER_ERROR;
  545. fake_service_.TrashResource("2_file_resource_id",
  546. test_util::CreateCopyResultCallback(&error));
  547. base::RunLoop().RunUntilIdle();
  548. ASSERT_EQ(HTTP_SUCCESS, error);
  549. ASSERT_FALSE(Exists("2_file_resource_id"));
  550. // Get the resource list newer than old_largest_change_id.
  551. error = OTHER_ERROR;
  552. std::unique_ptr<ChangeList> change_list;
  553. fake_service_.GetChangeList(
  554. old_largest_change_id + 1,
  555. test_util::CreateCopyResultCallback(&error, &change_list));
  556. base::RunLoop().RunUntilIdle();
  557. EXPECT_EQ(HTTP_SUCCESS, error);
  558. ASSERT_TRUE(change_list);
  559. EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
  560. change_list->largest_change_id());
  561. // The result should only contain the trashed file.
  562. ASSERT_EQ(1U, change_list->items().size());
  563. const ChangeResource& item = *change_list->items()[0];
  564. EXPECT_EQ("2_file_resource_id", item.file_id());
  565. ASSERT_TRUE(item.file());
  566. EXPECT_TRUE(item.file()->labels().is_trashed());
  567. EXPECT_EQ(1, fake_service_.change_list_load_count());
  568. }
  569. TEST_F(FakeDriveServiceTest, GetAllTeamDriveList) {
  570. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  571. fake_service_.set_default_max_results(2);
  572. fake_service_.AddTeamDrive(TEAM_DRIVE_ID_1, TEAM_DRIVE_NAME_1);
  573. fake_service_.AddTeamDrive(TEAM_DRIVE_ID_2, TEAM_DRIVE_NAME_2);
  574. fake_service_.AddTeamDrive(TEAM_DRIVE_ID_3, TEAM_DRIVE_NAME_3);
  575. ApiErrorCode error = OTHER_ERROR;
  576. std::unique_ptr<TeamDriveList> team_drive_list;
  577. fake_service_.GetAllTeamDriveList(
  578. test_util::CreateCopyResultCallback(&error, &team_drive_list));
  579. base::RunLoop().RunUntilIdle();
  580. EXPECT_EQ(HTTP_SUCCESS, error);
  581. ASSERT_TRUE(team_drive_list);
  582. EXPECT_EQ(2U, team_drive_list->items().size());
  583. EXPECT_EQ(1, fake_service_.team_drive_list_load_count());
  584. ASSERT_FALSE(team_drive_list->next_page_token().empty());
  585. // Second page loading.
  586. // Keep the next page token before releasing the |team_drive_list|.
  587. std::string next_page_token(team_drive_list->next_page_token());
  588. error = OTHER_ERROR;
  589. team_drive_list.reset();
  590. fake_service_.GetRemainingTeamDriveList(
  591. next_page_token,
  592. test_util::CreateCopyResultCallback(&error, &team_drive_list));
  593. base::RunLoop().RunUntilIdle();
  594. EXPECT_EQ(HTTP_SUCCESS, error);
  595. ASSERT_TRUE(team_drive_list);
  596. EXPECT_EQ(1U, team_drive_list->items().size());
  597. EXPECT_EQ(1, fake_service_.team_drive_list_load_count());
  598. ASSERT_TRUE(team_drive_list->next_page_token().empty());
  599. }
  600. TEST_F(FakeDriveServiceTest, GetRemainingFileList_GetAllFileList) {
  601. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  602. fake_service_.set_default_max_results(6);
  603. ApiErrorCode error = OTHER_ERROR;
  604. std::unique_ptr<FileList> file_list;
  605. fake_service_.GetAllFileList(
  606. util::kTeamDriveIdDefaultCorpus,
  607. test_util::CreateCopyResultCallback(&error, &file_list));
  608. base::RunLoop().RunUntilIdle();
  609. EXPECT_EQ(HTTP_SUCCESS, error);
  610. ASSERT_TRUE(file_list);
  611. // Do some sanity check.
  612. // The number of results is 14 entries. Thus, it should split into three
  613. // chunks: 6, 6, and then 2.
  614. EXPECT_EQ(6U, file_list->items().size());
  615. EXPECT_EQ(1, fake_service_.file_list_load_count());
  616. // Second page loading.
  617. // Keep the next url before releasing the |file_list|.
  618. GURL next_url(file_list->next_link());
  619. error = OTHER_ERROR;
  620. file_list.reset();
  621. fake_service_.GetRemainingFileList(
  622. next_url, test_util::CreateCopyResultCallback(&error, &file_list));
  623. base::RunLoop().RunUntilIdle();
  624. EXPECT_EQ(HTTP_SUCCESS, error);
  625. ASSERT_TRUE(file_list);
  626. EXPECT_EQ(6U, file_list->items().size());
  627. EXPECT_EQ(1, fake_service_.file_list_load_count());
  628. // Third page loading.
  629. next_url = file_list->next_link();
  630. error = OTHER_ERROR;
  631. file_list.reset();
  632. fake_service_.GetRemainingFileList(
  633. next_url, test_util::CreateCopyResultCallback(&error, &file_list));
  634. base::RunLoop().RunUntilIdle();
  635. EXPECT_EQ(HTTP_SUCCESS, error);
  636. ASSERT_TRUE(file_list);
  637. EXPECT_EQ(3U, file_list->items().size());
  638. EXPECT_EQ(1, fake_service_.file_list_load_count());
  639. }
  640. TEST_F(FakeDriveServiceTest, GetRemainingFileList_GetFileListInDirectory) {
  641. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  642. fake_service_.set_default_max_results(3);
  643. ApiErrorCode error = OTHER_ERROR;
  644. std::unique_ptr<FileList> file_list;
  645. fake_service_.GetFileListInDirectory(
  646. fake_service_.GetRootResourceId(),
  647. test_util::CreateCopyResultCallback(&error, &file_list));
  648. base::RunLoop().RunUntilIdle();
  649. EXPECT_EQ(HTTP_SUCCESS, error);
  650. ASSERT_TRUE(file_list);
  651. // Do some sanity check.
  652. // The number of results is 8 entries. Thus, it should split into three
  653. // chunks: 3, 3, and then 2.
  654. EXPECT_EQ(3U, file_list->items().size());
  655. EXPECT_EQ(1, fake_service_.directory_load_count());
  656. // Second page loading.
  657. // Keep the next url before releasing the |file_list|.
  658. GURL next_url = file_list->next_link();
  659. error = OTHER_ERROR;
  660. file_list.reset();
  661. fake_service_.GetRemainingFileList(
  662. next_url, test_util::CreateCopyResultCallback(&error, &file_list));
  663. base::RunLoop().RunUntilIdle();
  664. EXPECT_EQ(HTTP_SUCCESS, error);
  665. ASSERT_TRUE(file_list);
  666. EXPECT_EQ(3U, file_list->items().size());
  667. EXPECT_EQ(1, fake_service_.directory_load_count());
  668. // Third page loading.
  669. next_url = file_list->next_link();
  670. error = OTHER_ERROR;
  671. file_list.reset();
  672. fake_service_.GetRemainingFileList(
  673. next_url, test_util::CreateCopyResultCallback(&error, &file_list));
  674. base::RunLoop().RunUntilIdle();
  675. EXPECT_EQ(HTTP_SUCCESS, error);
  676. ASSERT_TRUE(file_list);
  677. EXPECT_EQ(2U, file_list->items().size());
  678. EXPECT_EQ(1, fake_service_.directory_load_count());
  679. }
  680. TEST_F(FakeDriveServiceTest, GetRemainingFileList_Search) {
  681. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  682. fake_service_.set_default_max_results(2);
  683. ApiErrorCode error = OTHER_ERROR;
  684. std::unique_ptr<FileList> file_list;
  685. fake_service_.Search("File", // search_query
  686. test_util::CreateCopyResultCallback(&error, &file_list));
  687. base::RunLoop().RunUntilIdle();
  688. EXPECT_EQ(HTTP_SUCCESS, error);
  689. ASSERT_TRUE(file_list);
  690. // Do some sanity check.
  691. // The number of results is 4 entries. Thus, it should split into two
  692. // chunks: 2, and then 2
  693. EXPECT_EQ(2U, file_list->items().size());
  694. // Second page loading.
  695. // Keep the next url before releasing the |file_list|.
  696. GURL next_url = file_list->next_link();
  697. error = OTHER_ERROR;
  698. file_list.reset();
  699. fake_service_.GetRemainingFileList(
  700. next_url, test_util::CreateCopyResultCallback(&error, &file_list));
  701. base::RunLoop().RunUntilIdle();
  702. EXPECT_EQ(HTTP_SUCCESS, error);
  703. ASSERT_TRUE(file_list);
  704. EXPECT_EQ(2U, file_list->items().size());
  705. }
  706. TEST_F(FakeDriveServiceTest, GetRemainingChangeList_GetChangeList) {
  707. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  708. fake_service_.set_default_max_results(2);
  709. const int64_t old_largest_change_id =
  710. fake_service_.about_resource().largest_change_id();
  711. // Add 5 new directory in the root directory.
  712. for (int i = 0; i < 5; ++i) {
  713. ASSERT_TRUE(AddNewDirectory(fake_service_.GetRootResourceId(),
  714. base::StringPrintf("new directory %d", i)));
  715. }
  716. ApiErrorCode error = OTHER_ERROR;
  717. std::unique_ptr<ChangeList> change_list;
  718. fake_service_.GetChangeList(
  719. old_largest_change_id + 1, // start_changestamp
  720. test_util::CreateCopyResultCallback(&error, &change_list));
  721. base::RunLoop().RunUntilIdle();
  722. EXPECT_EQ(HTTP_SUCCESS, error);
  723. ASSERT_TRUE(change_list);
  724. // Do some sanity check.
  725. // The number of results is 5 entries. Thus, it should split into three
  726. // chunks: 2, 2 and then 1.
  727. EXPECT_EQ(2U, change_list->items().size());
  728. EXPECT_EQ(1, fake_service_.change_list_load_count());
  729. // Second page loading.
  730. // Keep the next url before releasing the |change_list|.
  731. GURL next_url = change_list->next_link();
  732. error = OTHER_ERROR;
  733. change_list.reset();
  734. fake_service_.GetRemainingChangeList(
  735. next_url, test_util::CreateCopyResultCallback(&error, &change_list));
  736. base::RunLoop().RunUntilIdle();
  737. EXPECT_EQ(HTTP_SUCCESS, error);
  738. ASSERT_TRUE(change_list);
  739. EXPECT_EQ(2U, change_list->items().size());
  740. EXPECT_EQ(1, fake_service_.change_list_load_count());
  741. // Third page loading.
  742. next_url = change_list->next_link();
  743. error = OTHER_ERROR;
  744. change_list.reset();
  745. fake_service_.GetRemainingChangeList(
  746. next_url, test_util::CreateCopyResultCallback(&error, &change_list));
  747. base::RunLoop().RunUntilIdle();
  748. EXPECT_EQ(HTTP_SUCCESS, error);
  749. ASSERT_TRUE(change_list);
  750. EXPECT_EQ(1U, change_list->items().size());
  751. EXPECT_EQ(1, fake_service_.change_list_load_count());
  752. }
  753. TEST_F(FakeDriveServiceTest, GetAboutResource) {
  754. ApiErrorCode error = OTHER_ERROR;
  755. std::unique_ptr<AboutResource> about_resource;
  756. fake_service_.GetAboutResource(
  757. test_util::CreateCopyResultCallback(&error, &about_resource));
  758. base::RunLoop().RunUntilIdle();
  759. EXPECT_EQ(HTTP_SUCCESS, error);
  760. ASSERT_TRUE(about_resource);
  761. // Do some sanity check.
  762. EXPECT_EQ(fake_service_.GetRootResourceId(),
  763. about_resource->root_folder_id());
  764. EXPECT_EQ(1, fake_service_.about_resource_load_count());
  765. }
  766. TEST_F(FakeDriveServiceTest, GetStartPageToken) {
  767. ApiErrorCode error = OTHER_ERROR;
  768. std::unique_ptr<StartPageToken> start_page_token;
  769. fake_service_.GetStartPageToken(
  770. util::kTeamDriveIdDefaultCorpus,
  771. test_util::CreateCopyResultCallback(&error, &start_page_token));
  772. base::RunLoop().RunUntilIdle();
  773. EXPECT_EQ(HTTP_SUCCESS, error);
  774. ASSERT_TRUE(start_page_token);
  775. // Do some sanity check.
  776. EXPECT_EQ(drive::util::ConvertChangestampToStartPageToken(
  777. GetLargestChangeByAboutResource()),
  778. start_page_token->start_page_token());
  779. EXPECT_EQ(1, fake_service_.start_page_token_load_count());
  780. }
  781. TEST_F(FakeDriveServiceTest, GetStartPageToken_TeamDrive) {
  782. ASSERT_TRUE(test_util::SetUpTeamDriveTestEntries(
  783. &fake_service_, TEAM_DRIVE_ID_1, TEAM_DRIVE_NAME_1));
  784. ApiErrorCode error = OTHER_ERROR;
  785. std::unique_ptr<StartPageToken> start_page_token;
  786. fake_service_.GetStartPageToken(
  787. TEAM_DRIVE_ID_1,
  788. test_util::CreateCopyResultCallback(&error, &start_page_token));
  789. base::RunLoop().RunUntilIdle();
  790. EXPECT_EQ(HTTP_SUCCESS, error);
  791. ASSERT_TRUE(start_page_token);
  792. // Do some sanity check.
  793. EXPECT_NE("", start_page_token->start_page_token());
  794. EXPECT_EQ(1, fake_service_.start_page_token_load_count());
  795. }
  796. TEST_F(FakeDriveServiceTest, GetAboutResource_Offline) {
  797. fake_service_.set_offline(true);
  798. ApiErrorCode error = OTHER_ERROR;
  799. std::unique_ptr<AboutResource> about_resource;
  800. fake_service_.GetAboutResource(
  801. test_util::CreateCopyResultCallback(&error, &about_resource));
  802. base::RunLoop().RunUntilIdle();
  803. EXPECT_EQ(NO_CONNECTION, error);
  804. EXPECT_FALSE(about_resource);
  805. }
  806. TEST_F(FakeDriveServiceTest, GetFileResource_ExistingFile) {
  807. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  808. const std::string kResourceId = "2_file_resource_id";
  809. ApiErrorCode error = OTHER_ERROR;
  810. std::unique_ptr<FileResource> entry;
  811. fake_service_.GetFileResource(
  812. kResourceId, test_util::CreateCopyResultCallback(&error, &entry));
  813. base::RunLoop().RunUntilIdle();
  814. EXPECT_EQ(HTTP_SUCCESS, error);
  815. ASSERT_TRUE(entry);
  816. // Do some sanity check.
  817. EXPECT_EQ(kResourceId, entry->file_id());
  818. }
  819. TEST_F(FakeDriveServiceTest, GetFileResource_NonexistingFile) {
  820. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  821. const std::string kResourceId = "nonexisting_resource_id";
  822. ApiErrorCode error = OTHER_ERROR;
  823. std::unique_ptr<FileResource> entry;
  824. fake_service_.GetFileResource(
  825. kResourceId, test_util::CreateCopyResultCallback(&error, &entry));
  826. base::RunLoop().RunUntilIdle();
  827. EXPECT_EQ(HTTP_NOT_FOUND, error);
  828. ASSERT_FALSE(entry);
  829. }
  830. TEST_F(FakeDriveServiceTest, GetFileResource_Offline) {
  831. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  832. fake_service_.set_offline(true);
  833. const std::string kResourceId = "2_file_resource_id";
  834. ApiErrorCode error = OTHER_ERROR;
  835. std::unique_ptr<FileResource> entry;
  836. fake_service_.GetFileResource(
  837. kResourceId, test_util::CreateCopyResultCallback(&error, &entry));
  838. base::RunLoop().RunUntilIdle();
  839. EXPECT_EQ(NO_CONNECTION, error);
  840. EXPECT_FALSE(entry);
  841. }
  842. TEST_F(FakeDriveServiceTest, DeleteResource_ExistingFile) {
  843. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  844. // Resource "2_file_resource_id" should now exist.
  845. ASSERT_TRUE(Exists("2_file_resource_id"));
  846. ApiErrorCode error = OTHER_ERROR;
  847. fake_service_.DeleteResource("2_file_resource_id",
  848. std::string(), // etag
  849. test_util::CreateCopyResultCallback(&error));
  850. base::RunLoop().RunUntilIdle();
  851. EXPECT_EQ(HTTP_NO_CONTENT, error);
  852. // Resource "2_file_resource_id" should be gone now.
  853. EXPECT_FALSE(Exists("2_file_resource_id"));
  854. error = OTHER_ERROR;
  855. fake_service_.DeleteResource("2_file_resource_id",
  856. std::string(), // etag
  857. test_util::CreateCopyResultCallback(&error));
  858. base::RunLoop().RunUntilIdle();
  859. EXPECT_EQ(HTTP_NOT_FOUND, error);
  860. EXPECT_FALSE(Exists("2_file_resource_id"));
  861. }
  862. TEST_F(FakeDriveServiceTest, DeleteResource_NonexistingFile) {
  863. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  864. ApiErrorCode error = OTHER_ERROR;
  865. fake_service_.DeleteResource("nonexisting_resource_id",
  866. std::string(), // etag
  867. test_util::CreateCopyResultCallback(&error));
  868. base::RunLoop().RunUntilIdle();
  869. EXPECT_EQ(HTTP_NOT_FOUND, error);
  870. }
  871. TEST_F(FakeDriveServiceTest, DeleteResource_ETagMatch) {
  872. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  873. // Resource "2_file_resource_id" should now exist.
  874. std::unique_ptr<FileResource> entry = FindEntry("2_file_resource_id");
  875. ASSERT_TRUE(entry);
  876. ASSERT_FALSE(entry->labels().is_trashed());
  877. ASSERT_FALSE(entry->etag().empty());
  878. ApiErrorCode error = OTHER_ERROR;
  879. fake_service_.DeleteResource("2_file_resource_id",
  880. entry->etag() + "_mismatch",
  881. test_util::CreateCopyResultCallback(&error));
  882. base::RunLoop().RunUntilIdle();
  883. EXPECT_EQ(HTTP_PRECONDITION, error);
  884. // Resource "2_file_resource_id" should still exist.
  885. EXPECT_TRUE(Exists("2_file_resource_id"));
  886. error = OTHER_ERROR;
  887. fake_service_.DeleteResource("2_file_resource_id", entry->etag(),
  888. test_util::CreateCopyResultCallback(&error));
  889. base::RunLoop().RunUntilIdle();
  890. EXPECT_EQ(HTTP_NO_CONTENT, error);
  891. // Resource "2_file_resource_id" should be gone now.
  892. EXPECT_FALSE(Exists("2_file_resource_id"));
  893. }
  894. TEST_F(FakeDriveServiceTest, DeleteResource_Offline) {
  895. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  896. fake_service_.set_offline(true);
  897. ApiErrorCode error = OTHER_ERROR;
  898. fake_service_.DeleteResource("2_file_resource_id",
  899. std::string(), // etag
  900. test_util::CreateCopyResultCallback(&error));
  901. base::RunLoop().RunUntilIdle();
  902. EXPECT_EQ(NO_CONNECTION, error);
  903. }
  904. TEST_F(FakeDriveServiceTest, DeleteResource_Forbidden) {
  905. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  906. EXPECT_EQ(HTTP_SUCCESS, fake_service_.SetUserPermission(
  907. "2_file_resource_id",
  908. google_apis::drive::PERMISSION_ROLE_READER));
  909. ApiErrorCode error = OTHER_ERROR;
  910. fake_service_.DeleteResource("2_file_resource_id",
  911. std::string(), // etag
  912. test_util::CreateCopyResultCallback(&error));
  913. base::RunLoop().RunUntilIdle();
  914. EXPECT_EQ(HTTP_FORBIDDEN, error);
  915. }
  916. TEST_F(FakeDriveServiceTest, TrashResource_ExistingFile) {
  917. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  918. // Resource "2_file_resource_id" should now exist.
  919. ASSERT_TRUE(Exists("2_file_resource_id"));
  920. ApiErrorCode error = OTHER_ERROR;
  921. fake_service_.TrashResource("2_file_resource_id",
  922. test_util::CreateCopyResultCallback(&error));
  923. base::RunLoop().RunUntilIdle();
  924. EXPECT_EQ(HTTP_SUCCESS, error);
  925. // Resource "2_file_resource_id" should be gone now.
  926. EXPECT_FALSE(Exists("2_file_resource_id"));
  927. error = OTHER_ERROR;
  928. fake_service_.TrashResource("2_file_resource_id",
  929. test_util::CreateCopyResultCallback(&error));
  930. base::RunLoop().RunUntilIdle();
  931. EXPECT_EQ(HTTP_NOT_FOUND, error);
  932. EXPECT_FALSE(Exists("2_file_resource_id"));
  933. }
  934. TEST_F(FakeDriveServiceTest, TrashResource_NonexistingFile) {
  935. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  936. ApiErrorCode error = OTHER_ERROR;
  937. fake_service_.TrashResource("nonexisting_resource_id",
  938. test_util::CreateCopyResultCallback(&error));
  939. base::RunLoop().RunUntilIdle();
  940. EXPECT_EQ(HTTP_NOT_FOUND, error);
  941. }
  942. TEST_F(FakeDriveServiceTest, TrashResource_Offline) {
  943. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  944. fake_service_.set_offline(true);
  945. ApiErrorCode error = OTHER_ERROR;
  946. fake_service_.TrashResource("2_file_resource_id",
  947. test_util::CreateCopyResultCallback(&error));
  948. base::RunLoop().RunUntilIdle();
  949. EXPECT_EQ(NO_CONNECTION, error);
  950. }
  951. TEST_F(FakeDriveServiceTest, TrashResource_Forbidden) {
  952. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  953. EXPECT_EQ(HTTP_SUCCESS, fake_service_.SetUserPermission(
  954. "2_file_resource_id",
  955. google_apis::drive::PERMISSION_ROLE_READER));
  956. ApiErrorCode error = OTHER_ERROR;
  957. fake_service_.TrashResource("2_file_resource_id",
  958. test_util::CreateCopyResultCallback(&error));
  959. base::RunLoop().RunUntilIdle();
  960. EXPECT_EQ(HTTP_FORBIDDEN, error);
  961. }
  962. TEST_F(FakeDriveServiceTest, DownloadFile_ExistingFile) {
  963. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  964. base::ScopedTempDir temp_dir;
  965. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  966. std::vector<test_util::ProgressInfo> download_progress_values;
  967. const base::FilePath kOutputFilePath =
  968. temp_dir.GetPath().AppendASCII("whatever.txt");
  969. ApiErrorCode error = OTHER_ERROR;
  970. base::FilePath output_file_path;
  971. test_util::TestGetContentCallback get_content_callback;
  972. fake_service_.DownloadFile(
  973. kOutputFilePath, "2_file_resource_id",
  974. test_util::CreateCopyResultCallback(&error, &output_file_path),
  975. get_content_callback.callback(),
  976. base::BindRepeating(&test_util::AppendProgressCallbackResult,
  977. &download_progress_values));
  978. base::RunLoop().RunUntilIdle();
  979. EXPECT_EQ(HTTP_SUCCESS, error);
  980. EXPECT_EQ(output_file_path, kOutputFilePath);
  981. std::string content;
  982. ASSERT_TRUE(base::ReadFileToString(output_file_path, &content));
  983. EXPECT_EQ("This is some test content.", content);
  984. ASSERT_TRUE(!download_progress_values.empty());
  985. EXPECT_TRUE(base::ranges::is_sorted(download_progress_values));
  986. EXPECT_LE(0, download_progress_values.front().first);
  987. EXPECT_GE(26, download_progress_values.back().first);
  988. EXPECT_EQ(content, get_content_callback.GetConcatenatedData());
  989. }
  990. TEST_F(FakeDriveServiceTest, DownloadFile_NonexistingFile) {
  991. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  992. base::ScopedTempDir temp_dir;
  993. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  994. const base::FilePath kOutputFilePath =
  995. temp_dir.GetPath().AppendASCII("whatever.txt");
  996. ApiErrorCode error = OTHER_ERROR;
  997. base::FilePath output_file_path;
  998. fake_service_.DownloadFile(
  999. kOutputFilePath, "non_existent_file_resource_id",
  1000. test_util::CreateCopyResultCallback(&error, &output_file_path),
  1001. GetContentCallback(), ProgressCallback());
  1002. base::RunLoop().RunUntilIdle();
  1003. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1004. }
  1005. TEST_F(FakeDriveServiceTest, DownloadFile_Offline) {
  1006. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1007. fake_service_.set_offline(true);
  1008. base::ScopedTempDir temp_dir;
  1009. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  1010. const base::FilePath kOutputFilePath =
  1011. temp_dir.GetPath().AppendASCII("whatever.txt");
  1012. ApiErrorCode error = OTHER_ERROR;
  1013. base::FilePath output_file_path;
  1014. fake_service_.DownloadFile(
  1015. kOutputFilePath, "2_file_resource_id",
  1016. test_util::CreateCopyResultCallback(&error, &output_file_path),
  1017. GetContentCallback(), ProgressCallback());
  1018. base::RunLoop().RunUntilIdle();
  1019. EXPECT_EQ(NO_CONNECTION, error);
  1020. }
  1021. TEST_F(FakeDriveServiceTest, CopyResource) {
  1022. const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
  1023. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1024. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1025. const std::string kResourceId = "2_file_resource_id";
  1026. const std::string kParentResourceId = "2_folder_resource_id";
  1027. ApiErrorCode error = OTHER_ERROR;
  1028. std::unique_ptr<FileResource> entry;
  1029. base::Time modified_date_utc;
  1030. EXPECT_TRUE(base::Time::FromUTCExploded(kModifiedDate, &modified_date_utc));
  1031. fake_service_.CopyResource(
  1032. kResourceId, kParentResourceId, "new title", modified_date_utc,
  1033. test_util::CreateCopyResultCallback(&error, &entry));
  1034. base::RunLoop().RunUntilIdle();
  1035. EXPECT_EQ(HTTP_SUCCESS, error);
  1036. ASSERT_TRUE(entry);
  1037. // The copied entry should have the new resource ID and the title.
  1038. EXPECT_NE(kResourceId, entry->file_id());
  1039. EXPECT_EQ("new title", entry->title());
  1040. EXPECT_EQ(modified_date_utc, entry->modified_date());
  1041. EXPECT_EQ(modified_date_utc, entry->modified_by_me_date());
  1042. EXPECT_TRUE(HasParent(entry->file_id(), kParentResourceId));
  1043. // Should be incremented as a new hosted document was created.
  1044. EXPECT_EQ(old_largest_change_id + 1,
  1045. fake_service_.about_resource().largest_change_id());
  1046. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1047. }
  1048. TEST_F(FakeDriveServiceTest, CopyResource_NonExisting) {
  1049. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1050. const std::string kResourceId = "nonexisting_resource_id";
  1051. ApiErrorCode error = OTHER_ERROR;
  1052. std::unique_ptr<FileResource> entry;
  1053. fake_service_.CopyResource(
  1054. kResourceId, "1_folder_resource_id", "new title", base::Time(),
  1055. test_util::CreateCopyResultCallback(&error, &entry));
  1056. base::RunLoop().RunUntilIdle();
  1057. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1058. }
  1059. TEST_F(FakeDriveServiceTest, CopyResource_EmptyParentResourceId) {
  1060. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1061. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1062. const std::string kResourceId = "2_file_resource_id";
  1063. ApiErrorCode error = OTHER_ERROR;
  1064. std::unique_ptr<FileResource> entry;
  1065. fake_service_.CopyResource(
  1066. kResourceId, std::string(), "new title", base::Time(),
  1067. test_util::CreateCopyResultCallback(&error, &entry));
  1068. base::RunLoop().RunUntilIdle();
  1069. EXPECT_EQ(HTTP_SUCCESS, error);
  1070. ASSERT_TRUE(entry);
  1071. // The copied entry should have the new resource ID and the title.
  1072. EXPECT_NE(kResourceId, entry->file_id());
  1073. EXPECT_EQ("new title", entry->title());
  1074. EXPECT_TRUE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
  1075. // Should be incremented as a new hosted document was created.
  1076. EXPECT_EQ(old_largest_change_id + 1,
  1077. fake_service_.about_resource().largest_change_id());
  1078. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1079. }
  1080. TEST_F(FakeDriveServiceTest, CopyResource_Offline) {
  1081. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1082. fake_service_.set_offline(true);
  1083. const std::string kResourceId = "2_file_resource_id";
  1084. ApiErrorCode error = OTHER_ERROR;
  1085. std::unique_ptr<FileResource> entry;
  1086. fake_service_.CopyResource(
  1087. kResourceId, "1_folder_resource_id", "new title", base::Time(),
  1088. test_util::CreateCopyResultCallback(&error, &entry));
  1089. base::RunLoop().RunUntilIdle();
  1090. EXPECT_EQ(NO_CONNECTION, error);
  1091. EXPECT_FALSE(entry);
  1092. }
  1093. TEST_F(FakeDriveServiceTest, UpdateResource) {
  1094. const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
  1095. const base::Time::Exploded kViewedDate = {2013, 8, 1, 20, 16, 00, 14, 234};
  1096. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1097. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1098. const std::string kResourceId = "2_file_resource_id";
  1099. const std::string kParentResourceId = "2_folder_resource_id";
  1100. ApiErrorCode error = OTHER_ERROR;
  1101. std::unique_ptr<FileResource> entry;
  1102. base::Time modified_date_utc;
  1103. base::Time viewed_date_utc;
  1104. EXPECT_TRUE(base::Time::FromUTCExploded(kModifiedDate, &modified_date_utc));
  1105. EXPECT_TRUE(base::Time::FromUTCExploded(kViewedDate, &viewed_date_utc));
  1106. fake_service_.UpdateResource(
  1107. kResourceId, kParentResourceId, "new title", modified_date_utc,
  1108. viewed_date_utc, google_apis::drive::Properties(),
  1109. test_util::CreateCopyResultCallback(&error, &entry));
  1110. base::RunLoop().RunUntilIdle();
  1111. EXPECT_EQ(HTTP_SUCCESS, error);
  1112. ASSERT_TRUE(entry);
  1113. // The updated entry should have the new title.
  1114. EXPECT_EQ(kResourceId, entry->file_id());
  1115. EXPECT_EQ("new title", entry->title());
  1116. EXPECT_EQ(modified_date_utc, entry->modified_date());
  1117. EXPECT_EQ(modified_date_utc, entry->modified_by_me_date());
  1118. EXPECT_EQ(viewed_date_utc, entry->last_viewed_by_me_date());
  1119. EXPECT_TRUE(HasParent(kResourceId, kParentResourceId));
  1120. // Should be incremented as a new hosted document was created.
  1121. EXPECT_EQ(old_largest_change_id + 1,
  1122. fake_service_.about_resource().largest_change_id());
  1123. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1124. }
  1125. TEST_F(FakeDriveServiceTest, UpdateResource_NonExisting) {
  1126. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1127. const std::string kResourceId = "nonexisting_resource_id";
  1128. ApiErrorCode error = OTHER_ERROR;
  1129. std::unique_ptr<FileResource> entry;
  1130. fake_service_.UpdateResource(
  1131. kResourceId, "1_folder_resource_id", "new title", base::Time(),
  1132. base::Time(), google_apis::drive::Properties(),
  1133. test_util::CreateCopyResultCallback(&error, &entry));
  1134. base::RunLoop().RunUntilIdle();
  1135. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1136. }
  1137. TEST_F(FakeDriveServiceTest, UpdateResource_EmptyParentResourceId) {
  1138. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1139. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1140. const std::string kResourceId = "2_file_resource_id";
  1141. // Just make sure that the resource is under root.
  1142. ASSERT_TRUE(HasParent(kResourceId, "fake_root"));
  1143. ApiErrorCode error = OTHER_ERROR;
  1144. std::unique_ptr<FileResource> entry;
  1145. fake_service_.UpdateResource(
  1146. kResourceId, std::string(), "new title", base::Time(), base::Time(),
  1147. google_apis::drive::Properties(),
  1148. test_util::CreateCopyResultCallback(&error, &entry));
  1149. base::RunLoop().RunUntilIdle();
  1150. EXPECT_EQ(HTTP_SUCCESS, error);
  1151. ASSERT_TRUE(entry);
  1152. // The updated entry should have the new title.
  1153. EXPECT_EQ(kResourceId, entry->file_id());
  1154. EXPECT_EQ("new title", entry->title());
  1155. EXPECT_TRUE(HasParent(kResourceId, "fake_root"));
  1156. // Should be incremented as a new hosted document was created.
  1157. EXPECT_EQ(old_largest_change_id + 1,
  1158. fake_service_.about_resource().largest_change_id());
  1159. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1160. }
  1161. TEST_F(FakeDriveServiceTest, UpdateResource_Offline) {
  1162. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1163. fake_service_.set_offline(true);
  1164. const std::string kResourceId = "2_file_resource_id";
  1165. ApiErrorCode error = OTHER_ERROR;
  1166. std::unique_ptr<FileResource> entry;
  1167. fake_service_.UpdateResource(
  1168. kResourceId, std::string(), "new title", base::Time(), base::Time(),
  1169. google_apis::drive::Properties(),
  1170. test_util::CreateCopyResultCallback(&error, &entry));
  1171. base::RunLoop().RunUntilIdle();
  1172. EXPECT_EQ(NO_CONNECTION, error);
  1173. EXPECT_FALSE(entry);
  1174. }
  1175. TEST_F(FakeDriveServiceTest, UpdateResource_Forbidden) {
  1176. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1177. const std::string kResourceId = "2_file_resource_id";
  1178. EXPECT_EQ(HTTP_SUCCESS,
  1179. fake_service_.SetUserPermission(
  1180. kResourceId, google_apis::drive::PERMISSION_ROLE_READER));
  1181. ApiErrorCode error = OTHER_ERROR;
  1182. std::unique_ptr<FileResource> entry;
  1183. fake_service_.UpdateResource(
  1184. kResourceId, std::string(), "new title", base::Time(), base::Time(),
  1185. google_apis::drive::Properties(),
  1186. test_util::CreateCopyResultCallback(&error, &entry));
  1187. base::RunLoop().RunUntilIdle();
  1188. EXPECT_EQ(HTTP_FORBIDDEN, error);
  1189. EXPECT_FALSE(entry);
  1190. }
  1191. TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInRootDirectory) {
  1192. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1193. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1194. const std::string kResourceId = "2_file_resource_id";
  1195. const std::string kOldParentResourceId = fake_service_.GetRootResourceId();
  1196. const std::string kNewParentResourceId = "1_folder_resource_id";
  1197. // Here's the original parent link.
  1198. EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
  1199. EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
  1200. ApiErrorCode error = OTHER_ERROR;
  1201. fake_service_.AddResourceToDirectory(
  1202. kNewParentResourceId, kResourceId,
  1203. test_util::CreateCopyResultCallback(&error));
  1204. base::RunLoop().RunUntilIdle();
  1205. EXPECT_EQ(HTTP_SUCCESS, error);
  1206. // The parent link should now be changed.
  1207. EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
  1208. EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
  1209. // Should be incremented as a file was moved.
  1210. EXPECT_EQ(old_largest_change_id + 1,
  1211. fake_service_.about_resource().largest_change_id());
  1212. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1213. }
  1214. TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInNonRootDirectory) {
  1215. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1216. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1217. const std::string kResourceId = "subdirectory_file_1_id";
  1218. const std::string kOldParentResourceId = "1_folder_resource_id";
  1219. const std::string kNewParentResourceId = "2_folder_resource_id";
  1220. // Here's the original parent link.
  1221. EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
  1222. EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
  1223. ApiErrorCode error = OTHER_ERROR;
  1224. fake_service_.AddResourceToDirectory(
  1225. kNewParentResourceId, kResourceId,
  1226. test_util::CreateCopyResultCallback(&error));
  1227. base::RunLoop().RunUntilIdle();
  1228. EXPECT_EQ(HTTP_SUCCESS, error);
  1229. // The parent link should now be changed.
  1230. EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
  1231. EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
  1232. // Should be incremented as a file was moved.
  1233. EXPECT_EQ(old_largest_change_id + 1,
  1234. fake_service_.about_resource().largest_change_id());
  1235. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1236. }
  1237. TEST_F(FakeDriveServiceTest, AddResourceToDirectory_NonexistingFile) {
  1238. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1239. const std::string kResourceId = "nonexisting_file";
  1240. const std::string kNewParentResourceId = "1_folder_resource_id";
  1241. ApiErrorCode error = OTHER_ERROR;
  1242. fake_service_.AddResourceToDirectory(
  1243. kNewParentResourceId, kResourceId,
  1244. test_util::CreateCopyResultCallback(&error));
  1245. base::RunLoop().RunUntilIdle();
  1246. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1247. }
  1248. TEST_F(FakeDriveServiceTest, AddResourceToDirectory_OrphanFile) {
  1249. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1250. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1251. const std::string kResourceId = "1_orphanfile_resource_id";
  1252. const std::string kNewParentResourceId = "1_folder_resource_id";
  1253. // The file does not belong to any directory, even to the root.
  1254. EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
  1255. EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
  1256. ApiErrorCode error = OTHER_ERROR;
  1257. fake_service_.AddResourceToDirectory(
  1258. kNewParentResourceId, kResourceId,
  1259. test_util::CreateCopyResultCallback(&error));
  1260. base::RunLoop().RunUntilIdle();
  1261. EXPECT_EQ(HTTP_SUCCESS, error);
  1262. // The parent link should now be changed.
  1263. EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
  1264. EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
  1265. // Should be incremented as a file was moved.
  1266. EXPECT_EQ(old_largest_change_id + 1,
  1267. fake_service_.about_resource().largest_change_id());
  1268. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1269. }
  1270. TEST_F(FakeDriveServiceTest, AddResourceToDirectory_Offline) {
  1271. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1272. fake_service_.set_offline(true);
  1273. const std::string kResourceId = "2_file_resource_id";
  1274. const std::string kNewParentResourceId = "1_folder_resource_id";
  1275. ApiErrorCode error = OTHER_ERROR;
  1276. fake_service_.AddResourceToDirectory(
  1277. kNewParentResourceId, kResourceId,
  1278. test_util::CreateCopyResultCallback(&error));
  1279. base::RunLoop().RunUntilIdle();
  1280. EXPECT_EQ(NO_CONNECTION, error);
  1281. }
  1282. TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_ExistingFile) {
  1283. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1284. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1285. const std::string kResourceId = "subdirectory_file_1_id";
  1286. const std::string kParentResourceId = "1_folder_resource_id";
  1287. std::unique_ptr<FileResource> entry = FindEntry(kResourceId);
  1288. ASSERT_TRUE(entry);
  1289. // The entry should have a parent now.
  1290. ASSERT_FALSE(entry->parents().empty());
  1291. ApiErrorCode error = OTHER_ERROR;
  1292. fake_service_.RemoveResourceFromDirectory(
  1293. kParentResourceId, kResourceId,
  1294. test_util::CreateCopyResultCallback(&error));
  1295. base::RunLoop().RunUntilIdle();
  1296. EXPECT_EQ(HTTP_NO_CONTENT, error);
  1297. entry = FindEntry(kResourceId);
  1298. ASSERT_TRUE(entry);
  1299. // The entry should have no parent now.
  1300. ASSERT_TRUE(entry->parents().empty());
  1301. // Should be incremented as a file was moved to the root directory.
  1302. EXPECT_EQ(old_largest_change_id + 1,
  1303. fake_service_.about_resource().largest_change_id());
  1304. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1305. }
  1306. TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_NonexistingFile) {
  1307. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1308. const std::string kResourceId = "nonexisting_file";
  1309. const std::string kParentResourceId = "1_folder_resource_id";
  1310. ApiErrorCode error = OTHER_ERROR;
  1311. fake_service_.RemoveResourceFromDirectory(
  1312. kParentResourceId, kResourceId,
  1313. test_util::CreateCopyResultCallback(&error));
  1314. base::RunLoop().RunUntilIdle();
  1315. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1316. }
  1317. TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_OrphanFile) {
  1318. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1319. const std::string kResourceId = "1_orphanfile_resource_id";
  1320. const std::string kParentResourceId = fake_service_.GetRootResourceId();
  1321. ApiErrorCode error = OTHER_ERROR;
  1322. fake_service_.RemoveResourceFromDirectory(
  1323. kParentResourceId, kResourceId,
  1324. test_util::CreateCopyResultCallback(&error));
  1325. base::RunLoop().RunUntilIdle();
  1326. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1327. }
  1328. TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_Offline) {
  1329. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1330. fake_service_.set_offline(true);
  1331. const std::string kResourceId = "subdirectory_file_1_id";
  1332. const std::string kParentResourceId = "1_folder_resource_id";
  1333. ApiErrorCode error = OTHER_ERROR;
  1334. fake_service_.RemoveResourceFromDirectory(
  1335. kParentResourceId, kResourceId,
  1336. test_util::CreateCopyResultCallback(&error));
  1337. base::RunLoop().RunUntilIdle();
  1338. EXPECT_EQ(NO_CONNECTION, error);
  1339. }
  1340. TEST_F(FakeDriveServiceTest, AddNewDirectory_EmptyParent) {
  1341. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1342. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1343. ApiErrorCode error = OTHER_ERROR;
  1344. std::unique_ptr<FileResource> entry;
  1345. fake_service_.AddNewDirectory(
  1346. std::string(), "new directory", AddNewDirectoryOptions(),
  1347. test_util::CreateCopyResultCallback(&error, &entry));
  1348. base::RunLoop().RunUntilIdle();
  1349. EXPECT_EQ(HTTP_CREATED, error);
  1350. ASSERT_TRUE(entry);
  1351. EXPECT_TRUE(entry->IsDirectory());
  1352. EXPECT_EQ("resource_id_1", entry->file_id());
  1353. EXPECT_EQ("new directory", entry->title());
  1354. EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
  1355. // Should be incremented as a new directory was created.
  1356. EXPECT_EQ(old_largest_change_id + 1,
  1357. fake_service_.about_resource().largest_change_id());
  1358. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1359. }
  1360. TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectory) {
  1361. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1362. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1363. ApiErrorCode error = OTHER_ERROR;
  1364. std::unique_ptr<FileResource> entry;
  1365. fake_service_.AddNewDirectory(
  1366. fake_service_.GetRootResourceId(), "new directory",
  1367. AddNewDirectoryOptions(),
  1368. test_util::CreateCopyResultCallback(&error, &entry));
  1369. base::RunLoop().RunUntilIdle();
  1370. EXPECT_EQ(HTTP_CREATED, error);
  1371. ASSERT_TRUE(entry);
  1372. EXPECT_TRUE(entry->IsDirectory());
  1373. EXPECT_EQ("resource_id_1", entry->file_id());
  1374. EXPECT_EQ("new directory", entry->title());
  1375. EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
  1376. // Should be incremented as a new directory was created.
  1377. EXPECT_EQ(old_largest_change_id + 1,
  1378. fake_service_.about_resource().largest_change_id());
  1379. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1380. }
  1381. TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectoryOnEmptyFileSystem) {
  1382. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1383. ApiErrorCode error = OTHER_ERROR;
  1384. std::unique_ptr<FileResource> entry;
  1385. fake_service_.AddNewDirectory(
  1386. fake_service_.GetRootResourceId(), "new directory",
  1387. AddNewDirectoryOptions(),
  1388. test_util::CreateCopyResultCallback(&error, &entry));
  1389. base::RunLoop().RunUntilIdle();
  1390. EXPECT_EQ(HTTP_CREATED, error);
  1391. ASSERT_TRUE(entry);
  1392. EXPECT_TRUE(entry->IsDirectory());
  1393. EXPECT_EQ("resource_id_1", entry->file_id());
  1394. EXPECT_EQ("new directory", entry->title());
  1395. EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
  1396. // Should be incremented as a new directory was created.
  1397. EXPECT_EQ(old_largest_change_id + 1,
  1398. fake_service_.about_resource().largest_change_id());
  1399. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1400. }
  1401. TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonRootDirectory) {
  1402. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1403. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1404. const std::string kParentResourceId = "1_folder_resource_id";
  1405. ApiErrorCode error = OTHER_ERROR;
  1406. std::unique_ptr<FileResource> entry;
  1407. fake_service_.AddNewDirectory(
  1408. kParentResourceId, "new directory", AddNewDirectoryOptions(),
  1409. test_util::CreateCopyResultCallback(&error, &entry));
  1410. base::RunLoop().RunUntilIdle();
  1411. EXPECT_EQ(HTTP_CREATED, error);
  1412. ASSERT_TRUE(entry);
  1413. EXPECT_TRUE(entry->IsDirectory());
  1414. EXPECT_EQ("resource_id_1", entry->file_id());
  1415. EXPECT_EQ("new directory", entry->title());
  1416. EXPECT_TRUE(HasParent(entry->file_id(), kParentResourceId));
  1417. // Should be incremented as a new directory was created.
  1418. EXPECT_EQ(old_largest_change_id + 1,
  1419. fake_service_.about_resource().largest_change_id());
  1420. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1421. }
  1422. TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonexistingDirectory) {
  1423. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1424. const std::string kParentResourceId = "nonexisting_resource_id";
  1425. ApiErrorCode error = OTHER_ERROR;
  1426. std::unique_ptr<FileResource> entry;
  1427. fake_service_.AddNewDirectory(
  1428. kParentResourceId, "new directory", AddNewDirectoryOptions(),
  1429. test_util::CreateCopyResultCallback(&error, &entry));
  1430. base::RunLoop().RunUntilIdle();
  1431. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1432. EXPECT_FALSE(entry);
  1433. }
  1434. TEST_F(FakeDriveServiceTest, AddNewDirectory_Offline) {
  1435. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1436. fake_service_.set_offline(true);
  1437. ApiErrorCode error = OTHER_ERROR;
  1438. std::unique_ptr<FileResource> entry;
  1439. fake_service_.AddNewDirectory(
  1440. fake_service_.GetRootResourceId(), "new directory",
  1441. AddNewDirectoryOptions(),
  1442. test_util::CreateCopyResultCallback(&error, &entry));
  1443. base::RunLoop().RunUntilIdle();
  1444. EXPECT_EQ(NO_CONNECTION, error);
  1445. EXPECT_FALSE(entry);
  1446. }
  1447. TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_Offline) {
  1448. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1449. fake_service_.set_offline(true);
  1450. ApiErrorCode error = OTHER_ERROR;
  1451. GURL upload_location;
  1452. fake_service_.InitiateUploadNewFile(
  1453. "test/foo", 13, "1_folder_resource_id", "new file.foo",
  1454. UploadNewFileOptions(),
  1455. test_util::CreateCopyResultCallback(&error, &upload_location));
  1456. base::RunLoop().RunUntilIdle();
  1457. EXPECT_EQ(NO_CONNECTION, error);
  1458. EXPECT_TRUE(upload_location.is_empty());
  1459. }
  1460. TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_NotFound) {
  1461. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1462. ApiErrorCode error = OTHER_ERROR;
  1463. GURL upload_location;
  1464. fake_service_.InitiateUploadNewFile(
  1465. "test/foo", 13, "non_existent", "new file.foo", UploadNewFileOptions(),
  1466. test_util::CreateCopyResultCallback(&error, &upload_location));
  1467. base::RunLoop().RunUntilIdle();
  1468. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1469. EXPECT_TRUE(upload_location.is_empty());
  1470. }
  1471. TEST_F(FakeDriveServiceTest, InitiateUploadNewFile) {
  1472. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1473. ApiErrorCode error = OTHER_ERROR;
  1474. GURL upload_location;
  1475. fake_service_.InitiateUploadNewFile(
  1476. "test/foo", 13, "1_folder_resource_id", "new file.foo",
  1477. UploadNewFileOptions(),
  1478. test_util::CreateCopyResultCallback(&error, &upload_location));
  1479. base::RunLoop().RunUntilIdle();
  1480. EXPECT_EQ(HTTP_SUCCESS, error);
  1481. EXPECT_FALSE(upload_location.is_empty());
  1482. EXPECT_NE(GURL("https://1_folder_resumable_create_media_link?mode=newfile"),
  1483. upload_location);
  1484. }
  1485. TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_Offline) {
  1486. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1487. fake_service_.set_offline(true);
  1488. ApiErrorCode error = OTHER_ERROR;
  1489. GURL upload_location;
  1490. fake_service_.InitiateUploadExistingFile(
  1491. "test/foo", 13, "2_file_resource_id", UploadExistingFileOptions(),
  1492. test_util::CreateCopyResultCallback(&error, &upload_location));
  1493. base::RunLoop().RunUntilIdle();
  1494. EXPECT_EQ(NO_CONNECTION, error);
  1495. EXPECT_TRUE(upload_location.is_empty());
  1496. }
  1497. TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_Forbidden) {
  1498. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1499. EXPECT_EQ(HTTP_SUCCESS, fake_service_.SetUserPermission(
  1500. "2_file_resource_id",
  1501. google_apis::drive::PERMISSION_ROLE_READER));
  1502. ApiErrorCode error = OTHER_ERROR;
  1503. GURL upload_location;
  1504. fake_service_.InitiateUploadExistingFile(
  1505. "test/foo", 13, "2_file_resource_id", UploadExistingFileOptions(),
  1506. test_util::CreateCopyResultCallback(&error, &upload_location));
  1507. base::RunLoop().RunUntilIdle();
  1508. EXPECT_EQ(HTTP_FORBIDDEN, error);
  1509. EXPECT_TRUE(upload_location.is_empty());
  1510. }
  1511. TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_NotFound) {
  1512. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1513. ApiErrorCode error = OTHER_ERROR;
  1514. GURL upload_location;
  1515. fake_service_.InitiateUploadExistingFile(
  1516. "test/foo", 13, "non_existent", UploadExistingFileOptions(),
  1517. test_util::CreateCopyResultCallback(&error, &upload_location));
  1518. base::RunLoop().RunUntilIdle();
  1519. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1520. EXPECT_TRUE(upload_location.is_empty());
  1521. }
  1522. TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_WrongETag) {
  1523. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1524. UploadExistingFileOptions options;
  1525. options.etag = "invalid_etag";
  1526. ApiErrorCode error = OTHER_ERROR;
  1527. GURL upload_location;
  1528. fake_service_.InitiateUploadExistingFile(
  1529. "text/plain", 13, "2_file_resource_id", options,
  1530. test_util::CreateCopyResultCallback(&error, &upload_location));
  1531. base::RunLoop().RunUntilIdle();
  1532. EXPECT_EQ(HTTP_PRECONDITION, error);
  1533. EXPECT_TRUE(upload_location.is_empty());
  1534. }
  1535. TEST_F(FakeDriveServiceTest, InitiateUpload_ExistingFile) {
  1536. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1537. std::unique_ptr<FileResource> entry = FindEntry("2_file_resource_id");
  1538. ASSERT_TRUE(entry);
  1539. UploadExistingFileOptions options;
  1540. options.etag = entry->etag();
  1541. ApiErrorCode error = OTHER_ERROR;
  1542. GURL upload_location;
  1543. fake_service_.InitiateUploadExistingFile(
  1544. "text/plain", 13, "2_file_resource_id", options,
  1545. test_util::CreateCopyResultCallback(&error, &upload_location));
  1546. base::RunLoop().RunUntilIdle();
  1547. EXPECT_EQ(HTTP_SUCCESS, error);
  1548. EXPECT_TRUE(upload_location.is_valid());
  1549. }
  1550. TEST_F(FakeDriveServiceTest, ResumeUpload_Offline) {
  1551. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1552. ApiErrorCode error = OTHER_ERROR;
  1553. GURL upload_location;
  1554. fake_service_.InitiateUploadNewFile(
  1555. "test/foo", 15, "1_folder_resource_id", "new file.foo",
  1556. UploadNewFileOptions(),
  1557. test_util::CreateCopyResultCallback(&error, &upload_location));
  1558. base::RunLoop().RunUntilIdle();
  1559. EXPECT_EQ(HTTP_SUCCESS, error);
  1560. EXPECT_FALSE(upload_location.is_empty());
  1561. EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
  1562. upload_location);
  1563. fake_service_.set_offline(true);
  1564. UploadRangeResponse response;
  1565. std::unique_ptr<FileResource> entry;
  1566. fake_service_.ResumeUpload(
  1567. upload_location, 0, 13, 15, "test/foo", base::FilePath(),
  1568. test_util::CreateCopyResultCallback(&response, &entry),
  1569. ProgressCallback());
  1570. base::RunLoop().RunUntilIdle();
  1571. EXPECT_EQ(NO_CONNECTION, response.code);
  1572. EXPECT_FALSE(entry);
  1573. }
  1574. TEST_F(FakeDriveServiceTest, ResumeUpload_NotFound) {
  1575. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1576. ApiErrorCode error = OTHER_ERROR;
  1577. GURL upload_location;
  1578. fake_service_.InitiateUploadNewFile(
  1579. "test/foo", 15, "1_folder_resource_id", "new file.foo",
  1580. UploadNewFileOptions(),
  1581. test_util::CreateCopyResultCallback(&error, &upload_location));
  1582. base::RunLoop().RunUntilIdle();
  1583. ASSERT_EQ(HTTP_SUCCESS, error);
  1584. UploadRangeResponse response;
  1585. std::unique_ptr<FileResource> entry;
  1586. fake_service_.ResumeUpload(
  1587. GURL("https://foo.com/"), 0, 13, 15, "test/foo", base::FilePath(),
  1588. test_util::CreateCopyResultCallback(&response, &entry),
  1589. ProgressCallback());
  1590. base::RunLoop().RunUntilIdle();
  1591. EXPECT_EQ(HTTP_NOT_FOUND, response.code);
  1592. EXPECT_FALSE(entry);
  1593. }
  1594. TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) {
  1595. base::ScopedTempDir temp_dir;
  1596. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  1597. base::FilePath local_file_path =
  1598. temp_dir.GetPath().Append(FILE_PATH_LITERAL("File 1.txt"));
  1599. std::string contents("hogefugapiyo");
  1600. ASSERT_TRUE(test_util::WriteStringToFile(local_file_path, contents));
  1601. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1602. std::unique_ptr<FileResource> entry = FindEntry("2_file_resource_id");
  1603. ASSERT_TRUE(entry);
  1604. UploadExistingFileOptions options;
  1605. options.etag = entry->etag();
  1606. ApiErrorCode error = OTHER_ERROR;
  1607. GURL upload_location;
  1608. fake_service_.InitiateUploadExistingFile(
  1609. "text/plain", contents.size(), "2_file_resource_id", options,
  1610. test_util::CreateCopyResultCallback(&error, &upload_location));
  1611. base::RunLoop().RunUntilIdle();
  1612. ASSERT_EQ(HTTP_SUCCESS, error);
  1613. UploadRangeResponse response;
  1614. entry.reset();
  1615. std::vector<test_util::ProgressInfo> upload_progress_values;
  1616. fake_service_.ResumeUpload(
  1617. upload_location, 0, contents.size() / 2, contents.size(), "text/plain",
  1618. local_file_path, test_util::CreateCopyResultCallback(&response, &entry),
  1619. base::BindRepeating(&test_util::AppendProgressCallbackResult,
  1620. &upload_progress_values));
  1621. base::RunLoop().RunUntilIdle();
  1622. EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
  1623. EXPECT_FALSE(entry);
  1624. ASSERT_TRUE(!upload_progress_values.empty());
  1625. EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
  1626. EXPECT_LE(0, upload_progress_values.front().first);
  1627. EXPECT_GE(static_cast<int64_t>(contents.size() / 2),
  1628. upload_progress_values.back().first);
  1629. upload_progress_values.clear();
  1630. fake_service_.ResumeUpload(
  1631. upload_location, contents.size() / 2, contents.size(), contents.size(),
  1632. "text/plain", local_file_path,
  1633. test_util::CreateCopyResultCallback(&response, &entry),
  1634. base::BindRepeating(&test_util::AppendProgressCallbackResult,
  1635. &upload_progress_values));
  1636. base::RunLoop().RunUntilIdle();
  1637. EXPECT_EQ(HTTP_SUCCESS, response.code);
  1638. EXPECT_TRUE(entry.get());
  1639. EXPECT_EQ(static_cast<int64_t>(contents.size()), entry->file_size());
  1640. EXPECT_TRUE(Exists(entry->file_id()));
  1641. ASSERT_TRUE(!upload_progress_values.empty());
  1642. EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
  1643. EXPECT_LE(0, upload_progress_values.front().first);
  1644. EXPECT_GE(static_cast<int64_t>(contents.size() - contents.size() / 2),
  1645. upload_progress_values.back().first);
  1646. EXPECT_EQ(base::MD5String(contents), entry->md5_checksum());
  1647. }
  1648. TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) {
  1649. base::ScopedTempDir temp_dir;
  1650. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  1651. base::FilePath local_file_path =
  1652. temp_dir.GetPath().Append(FILE_PATH_LITERAL("new file.foo"));
  1653. std::string contents("hogefugapiyo");
  1654. ASSERT_TRUE(test_util::WriteStringToFile(local_file_path, contents));
  1655. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1656. ApiErrorCode error = OTHER_ERROR;
  1657. GURL upload_location;
  1658. fake_service_.InitiateUploadNewFile(
  1659. "test/foo", contents.size(), "1_folder_resource_id", "new file.foo",
  1660. UploadNewFileOptions(),
  1661. test_util::CreateCopyResultCallback(&error, &upload_location));
  1662. base::RunLoop().RunUntilIdle();
  1663. EXPECT_EQ(HTTP_SUCCESS, error);
  1664. EXPECT_FALSE(upload_location.is_empty());
  1665. EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
  1666. upload_location);
  1667. UploadRangeResponse response;
  1668. std::unique_ptr<FileResource> entry;
  1669. std::vector<test_util::ProgressInfo> upload_progress_values;
  1670. fake_service_.ResumeUpload(
  1671. upload_location, 0, contents.size() / 2, contents.size(), "test/foo",
  1672. local_file_path, test_util::CreateCopyResultCallback(&response, &entry),
  1673. base::BindRepeating(&test_util::AppendProgressCallbackResult,
  1674. &upload_progress_values));
  1675. base::RunLoop().RunUntilIdle();
  1676. EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
  1677. EXPECT_FALSE(entry);
  1678. ASSERT_TRUE(!upload_progress_values.empty());
  1679. EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
  1680. EXPECT_LE(0, upload_progress_values.front().first);
  1681. EXPECT_GE(static_cast<int64_t>(contents.size() / 2),
  1682. upload_progress_values.back().first);
  1683. upload_progress_values.clear();
  1684. fake_service_.ResumeUpload(
  1685. upload_location, contents.size() / 2, contents.size(), contents.size(),
  1686. "test/foo", local_file_path,
  1687. test_util::CreateCopyResultCallback(&response, &entry),
  1688. base::BindRepeating(&test_util::AppendProgressCallbackResult,
  1689. &upload_progress_values));
  1690. base::RunLoop().RunUntilIdle();
  1691. EXPECT_EQ(HTTP_CREATED, response.code);
  1692. EXPECT_TRUE(entry.get());
  1693. EXPECT_EQ(static_cast<int64_t>(contents.size()), entry->file_size());
  1694. EXPECT_TRUE(Exists(entry->file_id()));
  1695. ASSERT_TRUE(!upload_progress_values.empty());
  1696. EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
  1697. EXPECT_LE(0, upload_progress_values.front().first);
  1698. EXPECT_GE(static_cast<int64_t>(contents.size() - contents.size() / 2),
  1699. upload_progress_values.back().first);
  1700. EXPECT_EQ(base::MD5String(contents), entry->md5_checksum());
  1701. }
  1702. TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectory) {
  1703. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1704. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1705. const std::string kContentType = "text/plain";
  1706. const std::string kContentData = "This is some test content.";
  1707. const std::string kTitle = "new file";
  1708. ApiErrorCode error = OTHER_ERROR;
  1709. std::unique_ptr<FileResource> entry;
  1710. fake_service_.AddNewFile(kContentType, kContentData,
  1711. fake_service_.GetRootResourceId(), kTitle,
  1712. false, // shared_with_me
  1713. test_util::CreateCopyResultCallback(&error, &entry));
  1714. base::RunLoop().RunUntilIdle();
  1715. EXPECT_EQ(HTTP_CREATED, error);
  1716. ASSERT_TRUE(entry);
  1717. EXPECT_EQ(kContentType, entry->mime_type());
  1718. EXPECT_EQ(static_cast<int64_t>(kContentData.size()), entry->file_size());
  1719. EXPECT_EQ("resource_id_1", entry->file_id());
  1720. EXPECT_EQ(kTitle, entry->title());
  1721. EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
  1722. // Should be incremented as a new directory was created.
  1723. EXPECT_EQ(old_largest_change_id + 1,
  1724. fake_service_.about_resource().largest_change_id());
  1725. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1726. EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
  1727. }
  1728. TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectoryOnEmptyFileSystem) {
  1729. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1730. const std::string kContentType = "text/plain";
  1731. const std::string kContentData = "This is some test content.";
  1732. const std::string kTitle = "new file";
  1733. ApiErrorCode error = OTHER_ERROR;
  1734. std::unique_ptr<FileResource> entry;
  1735. fake_service_.AddNewFile(kContentType, kContentData,
  1736. fake_service_.GetRootResourceId(), kTitle,
  1737. false, // shared_with_me
  1738. test_util::CreateCopyResultCallback(&error, &entry));
  1739. base::RunLoop().RunUntilIdle();
  1740. EXPECT_EQ(HTTP_CREATED, error);
  1741. ASSERT_TRUE(entry);
  1742. EXPECT_EQ(kContentType, entry->mime_type());
  1743. EXPECT_EQ(static_cast<int64_t>(kContentData.size()), entry->file_size());
  1744. EXPECT_EQ("resource_id_1", entry->file_id());
  1745. EXPECT_EQ(kTitle, entry->title());
  1746. EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
  1747. // Should be incremented as a new directory was created.
  1748. EXPECT_EQ(old_largest_change_id + 1,
  1749. fake_service_.about_resource().largest_change_id());
  1750. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1751. EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
  1752. }
  1753. TEST_F(FakeDriveServiceTest, AddNewFile_ToNonRootDirectory) {
  1754. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1755. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1756. const std::string kContentType = "text/plain";
  1757. const std::string kContentData = "This is some test content.";
  1758. const std::string kTitle = "new file";
  1759. const std::string kParentResourceId = "1_folder_resource_id";
  1760. ApiErrorCode error = OTHER_ERROR;
  1761. std::unique_ptr<FileResource> entry;
  1762. fake_service_.AddNewFile(kContentType, kContentData, kParentResourceId,
  1763. kTitle,
  1764. false, // shared_with_me
  1765. test_util::CreateCopyResultCallback(&error, &entry));
  1766. base::RunLoop().RunUntilIdle();
  1767. EXPECT_EQ(HTTP_CREATED, error);
  1768. ASSERT_TRUE(entry);
  1769. EXPECT_EQ(kContentType, entry->mime_type());
  1770. EXPECT_EQ(static_cast<int64_t>(kContentData.size()), entry->file_size());
  1771. EXPECT_EQ("resource_id_1", entry->file_id());
  1772. EXPECT_EQ(kTitle, entry->title());
  1773. EXPECT_TRUE(HasParent(entry->file_id(), kParentResourceId));
  1774. // Should be incremented as a new directory was created.
  1775. EXPECT_EQ(old_largest_change_id + 1,
  1776. fake_service_.about_resource().largest_change_id());
  1777. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1778. EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
  1779. }
  1780. TEST_F(FakeDriveServiceTest, AddNewFile_ToNonexistingDirectory) {
  1781. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1782. const std::string kContentType = "text/plain";
  1783. const std::string kContentData = "This is some test content.";
  1784. const std::string kTitle = "new file";
  1785. const std::string kParentResourceId = "nonexisting_resource_id";
  1786. ApiErrorCode error = OTHER_ERROR;
  1787. std::unique_ptr<FileResource> entry;
  1788. fake_service_.AddNewFile(kContentType, kContentData, kParentResourceId,
  1789. kTitle,
  1790. false, // shared_with_me
  1791. test_util::CreateCopyResultCallback(&error, &entry));
  1792. base::RunLoop().RunUntilIdle();
  1793. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1794. EXPECT_FALSE(entry);
  1795. }
  1796. TEST_F(FakeDriveServiceTest, AddNewFile_Offline) {
  1797. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1798. fake_service_.set_offline(true);
  1799. const std::string kContentType = "text/plain";
  1800. const std::string kContentData = "This is some test content.";
  1801. const std::string kTitle = "new file";
  1802. ApiErrorCode error = OTHER_ERROR;
  1803. std::unique_ptr<FileResource> entry;
  1804. fake_service_.AddNewFile(kContentType, kContentData,
  1805. fake_service_.GetRootResourceId(), kTitle,
  1806. false, // shared_with_me
  1807. test_util::CreateCopyResultCallback(&error, &entry));
  1808. base::RunLoop().RunUntilIdle();
  1809. EXPECT_EQ(NO_CONNECTION, error);
  1810. EXPECT_FALSE(entry);
  1811. }
  1812. TEST_F(FakeDriveServiceTest, AddNewFile_SharedWithMeLabel) {
  1813. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1814. const std::string kContentType = "text/plain";
  1815. const std::string kContentData = "This is some test content.";
  1816. const std::string kTitle = "new file";
  1817. int64_t old_largest_change_id = GetLargestChangeByAboutResource();
  1818. ApiErrorCode error = OTHER_ERROR;
  1819. std::unique_ptr<FileResource> entry;
  1820. fake_service_.AddNewFile(kContentType, kContentData,
  1821. fake_service_.GetRootResourceId(), kTitle,
  1822. true, // shared_with_me
  1823. test_util::CreateCopyResultCallback(&error, &entry));
  1824. base::RunLoop().RunUntilIdle();
  1825. EXPECT_EQ(HTTP_CREATED, error);
  1826. ASSERT_TRUE(entry);
  1827. EXPECT_EQ(kContentType, entry->mime_type());
  1828. EXPECT_EQ(static_cast<int64_t>(kContentData.size()), entry->file_size());
  1829. EXPECT_EQ("resource_id_1", entry->file_id());
  1830. EXPECT_EQ(kTitle, entry->title());
  1831. EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
  1832. EXPECT_FALSE(entry->shared_with_me_date().is_null());
  1833. // Should be incremented as a new directory was created.
  1834. EXPECT_EQ(old_largest_change_id + 1,
  1835. fake_service_.about_resource().largest_change_id());
  1836. EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
  1837. EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
  1838. }
  1839. TEST_F(FakeDriveServiceTest, SetLastModifiedTime_ExistingFile) {
  1840. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1841. const std::string kResourceId = "2_file_resource_id";
  1842. base::Time time;
  1843. ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
  1844. ApiErrorCode error = OTHER_ERROR;
  1845. std::unique_ptr<FileResource> entry;
  1846. fake_service_.SetLastModifiedTime(
  1847. kResourceId, time, test_util::CreateCopyResultCallback(&error, &entry));
  1848. base::RunLoop().RunUntilIdle();
  1849. EXPECT_EQ(HTTP_SUCCESS, error);
  1850. ASSERT_TRUE(entry);
  1851. EXPECT_EQ(time, entry->modified_date());
  1852. EXPECT_EQ(time, entry->modified_by_me_date());
  1853. }
  1854. TEST_F(FakeDriveServiceTest, SetLastModifiedTime_NonexistingFile) {
  1855. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1856. const std::string kResourceId = "nonexisting_resource_id";
  1857. base::Time time;
  1858. ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
  1859. ApiErrorCode error = OTHER_ERROR;
  1860. std::unique_ptr<FileResource> entry;
  1861. fake_service_.SetLastModifiedTime(
  1862. kResourceId, time, test_util::CreateCopyResultCallback(&error, &entry));
  1863. base::RunLoop().RunUntilIdle();
  1864. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1865. EXPECT_FALSE(entry);
  1866. }
  1867. TEST_F(FakeDriveServiceTest, SetLastModifiedTime_Offline) {
  1868. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1869. fake_service_.set_offline(true);
  1870. const std::string kResourceId = "2_file_resource_id";
  1871. base::Time time;
  1872. ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
  1873. ApiErrorCode error = OTHER_ERROR;
  1874. std::unique_ptr<FileResource> entry;
  1875. fake_service_.SetLastModifiedTime(
  1876. kResourceId, time, test_util::CreateCopyResultCallback(&error, &entry));
  1877. base::RunLoop().RunUntilIdle();
  1878. EXPECT_EQ(NO_CONNECTION, error);
  1879. EXPECT_FALSE(entry);
  1880. }
  1881. TEST_F(FakeDriveServiceTest, SetFileCapabilities_ExistingFile) {
  1882. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1883. const std::string kResourceId = "2_file_resource_id";
  1884. const google_apis::FileResourceCapabilities& kCapabilities =
  1885. CreateMixedFileCapabilities();
  1886. ApiErrorCode error = OTHER_ERROR;
  1887. std::unique_ptr<FileResource> entry;
  1888. fake_service_.SetFileCapabilities(
  1889. kResourceId, kCapabilities,
  1890. test_util::CreateCopyResultCallback(&error, &entry));
  1891. base::RunLoop().RunUntilIdle();
  1892. EXPECT_EQ(HTTP_SUCCESS, error);
  1893. ASSERT_TRUE(entry);
  1894. ExpectFileCapabilitiesEqual(kCapabilities, entry->capabilities());
  1895. }
  1896. TEST_F(FakeDriveServiceTest, SetFileCapabilities_NonexistingFile) {
  1897. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1898. const std::string kResourceId = "nonexisting_resource_id";
  1899. const google_apis::FileResourceCapabilities& kCapabilities =
  1900. CreateMixedFileCapabilities();
  1901. ApiErrorCode error = OTHER_ERROR;
  1902. std::unique_ptr<FileResource> entry;
  1903. fake_service_.SetFileCapabilities(
  1904. kResourceId, kCapabilities,
  1905. test_util::CreateCopyResultCallback(&error, &entry));
  1906. base::RunLoop().RunUntilIdle();
  1907. EXPECT_EQ(HTTP_NOT_FOUND, error);
  1908. EXPECT_FALSE(entry);
  1909. }
  1910. TEST_F(FakeDriveServiceTest, SetFileCapabilities_Offline) {
  1911. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1912. fake_service_.set_offline(true);
  1913. const std::string kResourceId = "2_file_resource_id";
  1914. const google_apis::FileResourceCapabilities& kCapabilities =
  1915. CreateMixedFileCapabilities();
  1916. ApiErrorCode error = OTHER_ERROR;
  1917. std::unique_ptr<FileResource> entry;
  1918. fake_service_.SetFileCapabilities(
  1919. kResourceId, kCapabilities,
  1920. test_util::CreateCopyResultCallback(&error, &entry));
  1921. base::RunLoop().RunUntilIdle();
  1922. EXPECT_EQ(NO_CONNECTION, error);
  1923. EXPECT_FALSE(entry);
  1924. }
  1925. TEST_F(FakeDriveServiceTest, SetTeamDriveCapabilities_ExistingTeamDrive) {
  1926. ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
  1927. // Add a new team drive.
  1928. fake_service_.AddTeamDrive(TEAM_DRIVE_ID_1, TEAM_DRIVE_NAME_1, "");
  1929. const google_apis::TeamDriveCapabilities& kCapabilities =
  1930. CreateMixedTeamDriveCapabilities();
  1931. bool result =
  1932. fake_service_.SetTeamDriveCapabilities(TEAM_DRIVE_ID_1, kCapabilities);
  1933. EXPECT_TRUE(result);
  1934. base::RunLoop().RunUntilIdle();
  1935. ApiErrorCode error = OTHER_ERROR;
  1936. std::unique_ptr<TeamDriveList> team_drive_list;
  1937. fake_service_.GetAllTeamDriveList(
  1938. test_util::CreateCopyResultCallback(&error, &team_drive_list));
  1939. base::RunLoop().RunUntilIdle();
  1940. EXPECT_EQ(HTTP_SUCCESS, error);
  1941. ASSERT_TRUE(team_drive_list);
  1942. EXPECT_EQ(1U, team_drive_list->items().size());
  1943. ExpectTeamDriveCapabilitiesEqual(kCapabilities,
  1944. team_drive_list->items()[0]->capabilities());
  1945. }
  1946. } // namespace
  1947. } // namespace drive