language_pack_manager_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Copyright 2021 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 "chromeos/language/language_packs/language_pack_manager.h"
  5. #include "base/bind.h"
  6. #include "base/logging.h"
  7. #include "base/run_loop.h"
  8. #include "base/test/task_environment.h"
  9. #include "chromeos/dbus/dlcservice/dlcservice_client.h"
  10. #include "chromeos/dbus/dlcservice/fake_dlcservice_client.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. using ::chromeos::language_packs::LanguagePackManager;
  14. using ::dlcservice::DlcState;
  15. using ::testing::_;
  16. using ::testing::Invoke;
  17. using ::testing::Return;
  18. using ::testing::WithArg;
  19. namespace chromeos {
  20. namespace language_packs {
  21. namespace {
  22. constexpr char kFakeDlcId[] = "FakeDlc";
  23. constexpr char kSupportedLocale[] = "es";
  24. // We need a mock callback so that we can check that it gets called.
  25. class CallbackForTesting {
  26. public:
  27. OnInstallCompleteCallback GetInstallCallback() {
  28. return base::BindOnce(&CallbackForTesting::Callback,
  29. base::Unretained(this));
  30. }
  31. GetPackStateCallback GetPackStateCallback() {
  32. return base::BindOnce(&CallbackForTesting::Callback,
  33. base::Unretained(this));
  34. }
  35. OnUninstallCompleteCallback GetRemoveCallback() {
  36. return base::BindOnce(&CallbackForTesting::Callback,
  37. base::Unretained(this));
  38. }
  39. MOCK_METHOD(void, Callback, (const PackResult&), ());
  40. };
  41. class MockObserver : public LanguagePackManager::Observer {
  42. public:
  43. MOCK_METHOD(void, OnPackStateChanged, (const PackResult& pack_result));
  44. };
  45. // Utility function that creates a DlcState with no error, populated with id
  46. // and path.
  47. DlcState CreateInstalledState() {
  48. DlcState output;
  49. output.set_state(dlcservice::DlcState_State_INSTALLED);
  50. output.set_id(kHandwritingFeatureId);
  51. output.set_root_path("/path");
  52. return output;
  53. }
  54. } // namespace
  55. class LanguagePackManagerTest : public testing::Test {
  56. public:
  57. void SetUp() override {
  58. // The Fake DLC Service needs to be initialized before we instantiate
  59. // LanguagePackManager.
  60. DlcserviceClient::InitializeFake();
  61. dlcservice_client_ =
  62. static_cast<FakeDlcserviceClient*>(DlcserviceClient::Get());
  63. manager_ = LanguagePackManager::GetInstance();
  64. manager_->Initialize();
  65. ResetPackResult();
  66. base::RunLoop().RunUntilIdle();
  67. }
  68. void TearDown() override {
  69. manager_->ResetForTesting();
  70. DlcserviceClient::Shutdown();
  71. }
  72. void InstallTestCallback(const PackResult& pack_result) {
  73. pack_result_ = pack_result;
  74. }
  75. void GetPackStateTestCallback(const PackResult& pack_result) {
  76. pack_result_ = pack_result;
  77. }
  78. void RemoveTestCallback(const PackResult& pack_result) {
  79. pack_result_ = pack_result;
  80. }
  81. protected:
  82. LanguagePackManager* manager_;
  83. PackResult pack_result_;
  84. FakeDlcserviceClient* dlcservice_client_;
  85. private:
  86. base::test::SingleThreadTaskEnvironment task_environment_;
  87. void ResetPackResult() {
  88. PackResult temp = PackResult();
  89. pack_result_ = temp;
  90. }
  91. };
  92. TEST_F(LanguagePackManagerTest, InstallSuccessTest) {
  93. dlcservice_client_->set_install_error(dlcservice::kErrorNone);
  94. dlcservice_client_->set_install_root_path("/path");
  95. // We need to use an existing Pack ID, so that we do get a result back.
  96. manager_->InstallPack(
  97. kHandwritingFeatureId, kSupportedLocale,
  98. base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
  99. base::Unretained(this)));
  100. base::RunLoop().RunUntilIdle();
  101. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
  102. EXPECT_EQ(pack_result_.pack_state, PackResult::INSTALLED);
  103. EXPECT_EQ(pack_result_.path, "/path");
  104. }
  105. TEST_F(LanguagePackManagerTest, InstallFailureTest) {
  106. dlcservice_client_->set_install_error(dlcservice::kErrorInternal);
  107. // We need to use an existing Pack ID, so that we do get a result back.
  108. manager_->InstallPack(
  109. kHandwritingFeatureId, kSupportedLocale,
  110. base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
  111. base::Unretained(this)));
  112. base::RunLoop().RunUntilIdle();
  113. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
  114. EXPECT_NE(pack_result_.pack_state, PackResult::INSTALLED);
  115. }
  116. TEST_F(LanguagePackManagerTest, InstallWrongIdTest) {
  117. manager_->InstallPack(
  118. kFakeDlcId, kSupportedLocale,
  119. base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
  120. base::Unretained(this)));
  121. base::RunLoop().RunUntilIdle();
  122. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInvalidDlc);
  123. EXPECT_EQ(pack_result_.pack_state, PackResult::WRONG_ID);
  124. }
  125. // Check that the callback is actually called.
  126. TEST_F(LanguagePackManagerTest, InstallCallbackTest) {
  127. dlcservice_client_->set_install_error(dlcservice::kErrorNone);
  128. dlcservice_client_->set_install_root_path("/path");
  129. testing::StrictMock<CallbackForTesting> callback;
  130. EXPECT_CALL(callback, Callback(_));
  131. manager_->InstallPack(kFakeDlcId, kSupportedLocale,
  132. callback.GetInstallCallback());
  133. base::RunLoop().RunUntilIdle();
  134. }
  135. TEST_F(LanguagePackManagerTest, GetPackStateSuccessTest) {
  136. dlcservice_client_->set_get_dlc_state_error(dlcservice::kErrorNone);
  137. dlcservice::DlcState dlc_state;
  138. dlc_state.set_state(dlcservice::DlcState_State_INSTALLED);
  139. dlc_state.set_is_verified(true);
  140. dlc_state.set_root_path("/path");
  141. dlcservice_client_->set_dlc_state(dlc_state);
  142. // We need to use an existing Pack ID, so that we do get a result back.
  143. manager_->GetPackState(
  144. kHandwritingFeatureId, kSupportedLocale,
  145. base::BindOnce(&LanguagePackManagerTest::GetPackStateTestCallback,
  146. base::Unretained(this)));
  147. base::RunLoop().RunUntilIdle();
  148. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
  149. EXPECT_EQ(pack_result_.pack_state, PackResult::INSTALLED);
  150. EXPECT_EQ(pack_result_.path, "/path");
  151. }
  152. TEST_F(LanguagePackManagerTest, GetPackStateFailureTest) {
  153. dlcservice_client_->set_get_dlc_state_error(dlcservice::kErrorInternal);
  154. // We need to use an existing Pack ID, so that we do get a result back.
  155. manager_->GetPackState(
  156. kHandwritingFeatureId, kSupportedLocale,
  157. base::BindOnce(&LanguagePackManagerTest::GetPackStateTestCallback,
  158. base::Unretained(this)));
  159. base::RunLoop().RunUntilIdle();
  160. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
  161. EXPECT_NE(pack_result_.pack_state, PackResult::INSTALLED);
  162. }
  163. TEST_F(LanguagePackManagerTest, GetPackStateWrongIdTest) {
  164. manager_->GetPackState(
  165. kFakeDlcId, kSupportedLocale,
  166. base::BindOnce(&LanguagePackManagerTest::GetPackStateTestCallback,
  167. base::Unretained(this)));
  168. base::RunLoop().RunUntilIdle();
  169. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInvalidDlc);
  170. EXPECT_EQ(pack_result_.pack_state, PackResult::WRONG_ID);
  171. }
  172. // Check that the callback is actually called.
  173. TEST_F(LanguagePackManagerTest, GetPackStateCallbackTest) {
  174. dlcservice_client_->set_get_dlc_state_error(dlcservice::kErrorNone);
  175. testing::StrictMock<CallbackForTesting> callback;
  176. EXPECT_CALL(callback, Callback(_));
  177. manager_->GetPackState(kFakeDlcId, kSupportedLocale,
  178. callback.GetPackStateCallback());
  179. base::RunLoop().RunUntilIdle();
  180. }
  181. TEST_F(LanguagePackManagerTest, RemovePackSuccessTest) {
  182. dlcservice_client_->set_uninstall_error(dlcservice::kErrorNone);
  183. // We need to use an existing Pack ID, so that we do get a result back.
  184. manager_->RemovePack(
  185. kHandwritingFeatureId, kSupportedLocale,
  186. base::BindOnce(&LanguagePackManagerTest::RemoveTestCallback,
  187. base::Unretained(this)));
  188. base::RunLoop().RunUntilIdle();
  189. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
  190. EXPECT_EQ(pack_result_.pack_state, PackResult::NOT_INSTALLED);
  191. }
  192. TEST_F(LanguagePackManagerTest, RemovePackFailureTest) {
  193. dlcservice_client_->set_uninstall_error(dlcservice::kErrorInternal);
  194. // We need to use an existing Pack ID, so that we do get a result back.
  195. manager_->RemovePack(
  196. kHandwritingFeatureId, kSupportedLocale,
  197. base::BindOnce(&LanguagePackManagerTest::RemoveTestCallback,
  198. base::Unretained(this)));
  199. base::RunLoop().RunUntilIdle();
  200. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
  201. }
  202. TEST_F(LanguagePackManagerTest, RemovePackWrongIdTest) {
  203. manager_->RemovePack(
  204. kFakeDlcId, kSupportedLocale,
  205. base::BindOnce(&LanguagePackManagerTest::RemoveTestCallback,
  206. base::Unretained(this)));
  207. base::RunLoop().RunUntilIdle();
  208. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInvalidDlc);
  209. EXPECT_EQ(pack_result_.pack_state, PackResult::WRONG_ID);
  210. }
  211. // Check that the callback is actually called.
  212. TEST_F(LanguagePackManagerTest, RemovePackCallbackTest) {
  213. dlcservice_client_->set_uninstall_error(dlcservice::kErrorNone);
  214. testing::StrictMock<CallbackForTesting> callback;
  215. EXPECT_CALL(callback, Callback(_));
  216. manager_->RemovePack(kFakeDlcId, kSupportedLocale,
  217. callback.GetRemoveCallback());
  218. base::RunLoop().RunUntilIdle();
  219. }
  220. TEST_F(LanguagePackManagerTest, InstallObserverTest) {
  221. dlcservice_client_->set_install_error(dlcservice::kErrorNone);
  222. dlcservice_client_->set_install_root_path("/path");
  223. const DlcState dlc_state = CreateInstalledState();
  224. MockObserver observer;
  225. EXPECT_CALL(observer, OnPackStateChanged(_)).Times(0);
  226. dlcservice_client_->NotifyObserversForTest(dlc_state);
  227. // Add an Observer and expect it to be notified.
  228. manager_->AddObserver(&observer);
  229. EXPECT_CALL(observer, OnPackStateChanged(_)).Times(1);
  230. dlcservice_client_->NotifyObserversForTest(dlc_state);
  231. base::RunLoop().RunUntilIdle();
  232. }
  233. TEST_F(LanguagePackManagerTest, RemoveObserverTest) {
  234. dlcservice_client_->set_install_error(dlcservice::kErrorNone);
  235. dlcservice_client_->set_install_root_path("/path");
  236. const DlcState dlc_state = CreateInstalledState();
  237. MockObserver observer;
  238. // Add an Observer and expect it to be notified.
  239. manager_->AddObserver(&observer);
  240. EXPECT_CALL(observer, OnPackStateChanged(_)).Times(1);
  241. dlcservice_client_->NotifyObserversForTest(dlc_state);
  242. // Remove the Observer and there should be no more notifications.
  243. manager_->RemoveObserver(&observer);
  244. EXPECT_CALL(observer, OnPackStateChanged(_)).Times(0);
  245. dlcservice_client_->NotifyObserversForTest(dlc_state);
  246. base::RunLoop().RunUntilIdle();
  247. }
  248. // Check that all supported locales are available.
  249. TEST_F(LanguagePackManagerTest, CheckAllLocalesAvailable) {
  250. // Handwriting Recognition.
  251. const std::vector<std::string> handwriting({"es", "ja"});
  252. for (const auto& locale : handwriting) {
  253. EXPECT_TRUE(manager_->IsPackAvailable(kHandwritingFeatureId, locale));
  254. }
  255. }
  256. TEST_F(LanguagePackManagerTest, IsPackAvailableFalseTest) {
  257. // Correct ID, wrong language (Polish).
  258. bool available = manager_->IsPackAvailable(kHandwritingFeatureId, "pl");
  259. EXPECT_FALSE(available);
  260. // ID doesn't exists.
  261. available = manager_->IsPackAvailable("foo", "fr");
  262. EXPECT_FALSE(available);
  263. }
  264. TEST_F(LanguagePackManagerTest, InstallBasePackSuccess) {
  265. dlcservice_client_->set_install_error(dlcservice::kErrorNone);
  266. dlcservice_client_->set_install_root_path("/path");
  267. // We need to use an existing Pack ID, so that we do get a result back.
  268. manager_->InstallBasePack(
  269. kHandwritingFeatureId,
  270. base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
  271. base::Unretained(this)));
  272. base::RunLoop().RunUntilIdle();
  273. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
  274. EXPECT_EQ(pack_result_.pack_state, PackResult::INSTALLED);
  275. EXPECT_EQ(pack_result_.path, "/path");
  276. }
  277. TEST_F(LanguagePackManagerTest, InstallBasePackFailureTestFailure) {
  278. dlcservice_client_->set_install_error(dlcservice::kErrorInternal);
  279. // We need to use an existing Pack ID, so that we do get a result back.
  280. manager_->InstallBasePack(
  281. kHandwritingFeatureId,
  282. base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
  283. base::Unretained(this)));
  284. base::RunLoop().RunUntilIdle();
  285. EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
  286. EXPECT_NE(pack_result_.pack_state, PackResult::INSTALLED);
  287. }
  288. } // namespace language_packs
  289. } // namespace chromeos