/code/tests/cms/content_manager_unittest.cpp

https://github.com/wrimle/booxsdk · C++ · 410 lines · 302 code · 84 blank · 24 comment · 15 complexity · 7eb7ff76ca879287a670fa3e63dffa39 MD5 · raw file

  1. // Copyright 2007 Onyx International Inc.
  2. // All Rights Reserved.
  3. // Author: John
  4. #include "gtest/gtest.h"
  5. #include "onyx/base/base.h"
  6. #include "onyx/cms/content_manager.h"
  7. #include "onyx/cms/user_db.h"
  8. #include "onyx/cms/download_db.h"
  9. namespace
  10. {
  11. using namespace cms;
  12. static void CreateTempFile(const QString & path_name,
  13. const unsigned int size)
  14. {
  15. QFile file(path_name);
  16. file.open(QIODevice::ReadWrite);
  17. char * data = new char[size];
  18. file.write(data, size);
  19. file.close();
  20. delete [] data;
  21. }
  22. /// Open database that does not exist.
  23. /// Results: It can create a new database.
  24. TEST(ContentManagerTest, OpenNotExist)
  25. {
  26. QDir current = QDir::current();
  27. QString db = current.filePath("temp.db");
  28. ContentManager mgr;
  29. EXPECT_TRUE(mgr.open(db));
  30. mgr.close();
  31. current.remove(db);
  32. }
  33. /// Open database on readonly file system like CDROM.
  34. /// Results: It throws exception. Not sure where to catch it
  35. /// : in the Open function or in caller.
  36. TEST(ContentManagerTest, CreateOnReadOnlyFileSystem)
  37. {
  38. ContentManager mgr;
  39. EXPECT_FALSE(mgr.open("/xyz/abc/read_only_file_system.db"));
  40. }
  41. /// Put a new content into the database and query it.
  42. TEST(ContentManagerTest, GetContentNodeByName)
  43. {
  44. QDir current = QDir::current();
  45. QString db = current.filePath("temp.db");
  46. current.remove(db);
  47. ContentManager mgr;
  48. EXPECT_TRUE(mgr.open(db));
  49. static const int SIZE = 1024;
  50. static const QString TEMP_FILE_NAME = "temp.file";
  51. QString temp_file = current.filePath(TEMP_FILE_NAME);
  52. CreateTempFile(temp_file, SIZE);
  53. ContentNode node;
  54. QFileInfo info(current, TEMP_FILE_NAME);
  55. node.updateLastAccess();
  56. node.mutable_name() = info.fileName();
  57. node.mutable_location() = info.path();
  58. node.mutable_size() = info.size();
  59. node.mutable_authors() = "zzzzzzzzzzzz";
  60. node.mutable_description() = "test sample";
  61. node.mutable_title() = "abc";
  62. mgr.createContentNode(node);
  63. ContentNode result;
  64. EXPECT_TRUE(mgr.getContentNode(result, info.absoluteFilePath()));
  65. EXPECT_TRUE(node == result);
  66. // Should remove it otherwise the test will fail.
  67. current.remove(db);
  68. }
  69. /// Put a new content into the database and query it by url.
  70. TEST(ContentManagerTest, GetContentNodeByUrl)
  71. {
  72. QDir current = QDir::current();
  73. QString db = current.filePath("temp.db");
  74. current.remove(db);
  75. ContentManager mgr;
  76. EXPECT_TRUE(mgr.open(db));
  77. static const int SIZE = 1024;
  78. static const QString TEMP_FILE_NAME = "temp.file";
  79. QString temp_file = current.filePath(TEMP_FILE_NAME);
  80. CreateTempFile(temp_file, SIZE);
  81. ContentNode node;
  82. QFileInfo info(current, TEMP_FILE_NAME);
  83. node.updateLastAccess();
  84. node.mutable_name() = info.fileName();
  85. node.mutable_location() = info.path();
  86. node.mutable_size() = info.size();
  87. node.mutable_authors() = "zzzzzzzzzzzz";
  88. node.mutable_description() = "test sample";
  89. node.mutable_title() = "abc";
  90. const QString my_url = "http://www.test.com/";
  91. const QString wrong_url = "http://www.test.com./";
  92. EXPECT_TRUE(mgr.updateContentNodeByUrl(node, my_url));
  93. node.mutable_title() = "cba";
  94. EXPECT_TRUE(mgr.updateContentNodeByUrl(node, my_url));
  95. ContentNode result;
  96. EXPECT_TRUE(mgr.getContentNodeByUrl(result, my_url));
  97. EXPECT_TRUE(node.name() == result.name());
  98. EXPECT_TRUE(node.authors() == result.authors());
  99. EXPECT_TRUE(node.title() == result.title());
  100. EXPECT_FALSE(mgr.getContentNodeByUrl(result, wrong_url));
  101. // Should remove it otherwise the test will fail.
  102. current.remove(db);
  103. }
  104. /// Test the attribute blob object.
  105. TEST(ContentManagerTest, GetAttribute)
  106. {
  107. QDir current = QDir::current();
  108. QString db = current.filePath("temp.db");
  109. current.remove(db);
  110. ContentManager mgr;
  111. EXPECT_TRUE(mgr.open(db));
  112. static const int SIZE = 1024;
  113. static const QString TEMP_FILE_NAME = "temp.file";
  114. QString temp_file = current.filePath(TEMP_FILE_NAME);
  115. CreateTempFile(temp_file, SIZE);
  116. ContentNode node;
  117. QFileInfo info(current, TEMP_FILE_NAME);
  118. node.updateLastAccess();
  119. node.mutable_name() = info.fileName();
  120. node.mutable_location() = info.path();
  121. node.mutable_size() = info.size();
  122. node.mutable_authors() = "zzzzzzzzzzzz";
  123. node.mutable_description() = "test sample";
  124. node.mutable_title() = "abc";
  125. QVariantMap input, output;
  126. input.insert("isbn1", "aaaaaaaa");
  127. input.insert("isbn2", "bbbbbbbb");
  128. input.insert("isbn3", "cccccccc");
  129. node.setAttributes(input);
  130. mgr.createContentNode(node);
  131. ContentNode result;
  132. mgr.getContentNode(node.id(), result);
  133. result.attributes(output);
  134. EXPECT_TRUE(input == output);
  135. QString data = output.value("isbn1").toString();
  136. data = output.value("isbn2").toString();
  137. data = output.value("isbn3").toString();
  138. // test update node
  139. QVariantMap new_input, new_output;
  140. new_input.insert("isbn2", "bbbbbbbbxxxxxxx");
  141. new_input.insert("isbn3", "ccccccccxxxxxxx");
  142. result.setAttributes(new_input);
  143. mgr.updateContentNode(result);
  144. ContentNode xx;
  145. mgr.getContentNode(node.id(), xx);
  146. xx.attributes(new_output);
  147. data = new_output.value("isbn2").toString();
  148. data = new_output.value("isbn3").toString();
  149. // Should remove it otherwise the test will fail.
  150. current.remove(db);
  151. }
  152. /// Test the options blob object.
  153. TEST(ContentManagerTest, GetOption)
  154. {
  155. QDir current = QDir::current();
  156. QString db = current.filePath("temp.db");
  157. current.remove(db);
  158. ContentManager mgr;
  159. EXPECT_TRUE(mgr.open(db));
  160. static const int SIZE = 1024;
  161. static const QString TEMP_FILE_NAME = "temp.file";
  162. QString temp_file = current.filePath(TEMP_FILE_NAME);
  163. CreateTempFile(temp_file, SIZE);
  164. ContentNode node;
  165. QFileInfo info(current, TEMP_FILE_NAME);
  166. node.updateLastAccess();
  167. node.mutable_name() = info.fileName();
  168. node.mutable_location() = info.path();
  169. node.mutable_size() = info.size();
  170. node.mutable_authors() = "zzzzzzzzzzzz";
  171. node.mutable_description() = "test sample";
  172. node.mutable_title() = "abc";
  173. mgr.createContentNode(node);
  174. cms_blob options;
  175. options.resize(SIZE);
  176. for(int i = 0; i < SIZE; ++i)
  177. {
  178. options[i] = qrand();
  179. }
  180. mgr.updateOptions(node.id(), options);
  181. cms_blob result;
  182. mgr.getContentOptions(node.id(), result);
  183. EXPECT_TRUE(options == result);
  184. // Should remove it otherwise the test will fail.
  185. current.remove(db);
  186. }
  187. /// Test the bookmarks blob object.
  188. TEST(ContentManagerTest, GetBookmark)
  189. {
  190. QDir current = QDir::current();
  191. QString db = current.filePath("temp.db");
  192. current.remove(db);
  193. ContentManager mgr;
  194. EXPECT_TRUE(mgr.open(db));
  195. static const int SIZE = 1024;
  196. static const QString TEMP_FILE_NAME = "temp.file";
  197. QString temp_file = current.filePath(TEMP_FILE_NAME);
  198. CreateTempFile(temp_file, SIZE);
  199. ContentNode node;
  200. QFileInfo info(current, TEMP_FILE_NAME);
  201. node.updateLastAccess();
  202. node.mutable_name() = info.fileName();
  203. node.mutable_location() = info.path();
  204. node.mutable_size() = info.size();
  205. node.mutable_authors() = "zzzzzzzzzzzz";
  206. node.mutable_description() = "test sample";
  207. node.mutable_title() = "abc";
  208. mgr.createContentNode(node);
  209. cms_blob bookmarks;
  210. bookmarks.resize(SIZE);
  211. for(int i = 0; i < SIZE; ++i)
  212. {
  213. bookmarks[i] = qrand();
  214. }
  215. mgr.updateBookmarks(node.id(), bookmarks);
  216. cms_blob result;
  217. mgr.getBookmarks(node.id(), result);
  218. EXPECT_TRUE(bookmarks == result);
  219. // Should remove it otherwise the test will fail.
  220. current.remove(db);
  221. }
  222. TEST(ContentManagerTest, RecentDocuments)
  223. {
  224. QDir current = QDir::current();
  225. QString db = current.filePath("temp.db");
  226. current.remove(db);
  227. ContentManager mgr;
  228. EXPECT_TRUE(mgr.open(db));
  229. static const int SIZE = 1024;
  230. static const int COUNT = 10;
  231. cms_long ids[COUNT] = {CMS_INVALID_ID};
  232. for(int i = 0; i < COUNT; ++i)
  233. {
  234. QString name = "temp_%1.file";
  235. name = name.arg(i);
  236. QString temp_file = current.filePath(name);
  237. CreateTempFile(temp_file, SIZE);
  238. ContentNode node;
  239. mgr.getContentNode(node, temp_file);
  240. node.updateLastAccess();
  241. mgr.updateContentNode(node);
  242. ids[i] = node.id();
  243. EXPECT_TRUE(mgr.addToRecentDocuments(temp_file));
  244. // Should add a sleep here.
  245. }
  246. cms::cms_ids result;
  247. EXPECT_TRUE(mgr.getRecentDocuments(result));
  248. for(int i = 0; i < COUNT; ++i)
  249. {
  250. EXPECT_TRUE(ids[i] == result[i]);
  251. }
  252. QString path = mgr.latestReading();
  253. EXPECT_FALSE(path.isEmpty());
  254. // Should remove it otherwise the test will fail.
  255. current.remove(db);
  256. }
  257. TEST(ContentManagerTest, sketchDB)
  258. {
  259. QString db = getSketchDB("a.pdf");
  260. }
  261. TEST(ContentManagerTest, Shortcuts)
  262. {
  263. QDir current = QDir::current();
  264. QString db = current.filePath("temp.db");
  265. current.remove(db);
  266. ContentManager mgr;
  267. EXPECT_TRUE(mgr.open(db));
  268. static const int COUNT = 10;
  269. for(int i = 0; i < COUNT; ++i)
  270. {
  271. QString name = "temp.file";
  272. EXPECT_TRUE(mgr.link(name, name));
  273. EXPECT_TRUE(mgr.links(name) == 1);
  274. EXPECT_TRUE(mgr.unlinkBySource(name));
  275. EXPECT_TRUE(mgr.links(name) == 0);
  276. }
  277. // Should remove it otherwise the test will fail.
  278. current.remove(db);
  279. }
  280. TEST(ContentManagerTest, Notes)
  281. {
  282. QDir current = QDir::current();
  283. QString db = current.filePath("temp.db");
  284. current.remove(db);
  285. ContentManager mgr;
  286. EXPECT_TRUE(mgr.open(db));
  287. Notes notes;
  288. mgr.allNotes(notes);
  289. NoteInfo note;
  290. note.mutable_name() = mgr.suggestedNoteName();
  291. EXPECT_TRUE(mgr.addNoteIndex(note));
  292. // THIS IS FAILING!
  293. // EXPECT_TRUE(mgr.removeNote(note.name()));
  294. QString path = internalSketchDBPath(mgr.suggestedNoteName());
  295. EXPECT_FALSE(path.isEmpty());
  296. current.remove(db);
  297. }
  298. TEST(ContentManagerTest, UserDB)
  299. {
  300. UserDB db;
  301. EXPECT_TRUE(db.open());
  302. db.store("adobe_id", "aaaa");
  303. db.store("user", "my_user_name");
  304. db.store("password", "my_password");
  305. QString result;
  306. QVariant value;
  307. db.load("adobe_id", value);
  308. result = value.toString();
  309. db.load("user", value);
  310. result = value.toString();
  311. db.load("password", value);
  312. result = value.toString();
  313. }
  314. TEST(ContentManagerTest, DownloadDB)
  315. {
  316. DownloadDB db;
  317. EXPECT_TRUE(db.open());
  318. const QString url = "http://test.com/test.html";
  319. DownloadInfoList list = db.pendingList();
  320. EXPECT_TRUE(db.updateState(url, cms::DOWNLOADING));
  321. list = db.pendingList();
  322. DownloadItemInfo item;
  323. item.setUrl(url);
  324. EXPECT_TRUE(list.contains(item));
  325. }
  326. } // end of namespace