123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- // Copyright 2021 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "chromeos/language/language_packs/language_pack_manager.h"
- #include "base/bind.h"
- #include "base/logging.h"
- #include "base/run_loop.h"
- #include "base/test/task_environment.h"
- #include "chromeos/dbus/dlcservice/dlcservice_client.h"
- #include "chromeos/dbus/dlcservice/fake_dlcservice_client.h"
- #include "testing/gmock/include/gmock/gmock.h"
- #include "testing/gtest/include/gtest/gtest.h"
- using ::chromeos::language_packs::LanguagePackManager;
- using ::dlcservice::DlcState;
- using ::testing::_;
- using ::testing::Invoke;
- using ::testing::Return;
- using ::testing::WithArg;
- namespace chromeos {
- namespace language_packs {
- namespace {
- constexpr char kFakeDlcId[] = "FakeDlc";
- constexpr char kSupportedLocale[] = "es";
- // We need a mock callback so that we can check that it gets called.
- class CallbackForTesting {
- public:
- OnInstallCompleteCallback GetInstallCallback() {
- return base::BindOnce(&CallbackForTesting::Callback,
- base::Unretained(this));
- }
- GetPackStateCallback GetPackStateCallback() {
- return base::BindOnce(&CallbackForTesting::Callback,
- base::Unretained(this));
- }
- OnUninstallCompleteCallback GetRemoveCallback() {
- return base::BindOnce(&CallbackForTesting::Callback,
- base::Unretained(this));
- }
- MOCK_METHOD(void, Callback, (const PackResult&), ());
- };
- class MockObserver : public LanguagePackManager::Observer {
- public:
- MOCK_METHOD(void, OnPackStateChanged, (const PackResult& pack_result));
- };
- // Utility function that creates a DlcState with no error, populated with id
- // and path.
- DlcState CreateInstalledState() {
- DlcState output;
- output.set_state(dlcservice::DlcState_State_INSTALLED);
- output.set_id(kHandwritingFeatureId);
- output.set_root_path("/path");
- return output;
- }
- } // namespace
- class LanguagePackManagerTest : public testing::Test {
- public:
- void SetUp() override {
- // The Fake DLC Service needs to be initialized before we instantiate
- // LanguagePackManager.
- DlcserviceClient::InitializeFake();
- dlcservice_client_ =
- static_cast<FakeDlcserviceClient*>(DlcserviceClient::Get());
- manager_ = LanguagePackManager::GetInstance();
- manager_->Initialize();
- ResetPackResult();
- base::RunLoop().RunUntilIdle();
- }
- void TearDown() override {
- manager_->ResetForTesting();
- DlcserviceClient::Shutdown();
- }
- void InstallTestCallback(const PackResult& pack_result) {
- pack_result_ = pack_result;
- }
- void GetPackStateTestCallback(const PackResult& pack_result) {
- pack_result_ = pack_result;
- }
- void RemoveTestCallback(const PackResult& pack_result) {
- pack_result_ = pack_result;
- }
- protected:
- LanguagePackManager* manager_;
- PackResult pack_result_;
- FakeDlcserviceClient* dlcservice_client_;
- private:
- base::test::SingleThreadTaskEnvironment task_environment_;
- void ResetPackResult() {
- PackResult temp = PackResult();
- pack_result_ = temp;
- }
- };
- TEST_F(LanguagePackManagerTest, InstallSuccessTest) {
- dlcservice_client_->set_install_error(dlcservice::kErrorNone);
- dlcservice_client_->set_install_root_path("/path");
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->InstallPack(
- kHandwritingFeatureId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
- EXPECT_EQ(pack_result_.pack_state, PackResult::INSTALLED);
- EXPECT_EQ(pack_result_.path, "/path");
- }
- TEST_F(LanguagePackManagerTest, InstallFailureTest) {
- dlcservice_client_->set_install_error(dlcservice::kErrorInternal);
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->InstallPack(
- kHandwritingFeatureId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
- EXPECT_NE(pack_result_.pack_state, PackResult::INSTALLED);
- }
- TEST_F(LanguagePackManagerTest, InstallWrongIdTest) {
- manager_->InstallPack(
- kFakeDlcId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInvalidDlc);
- EXPECT_EQ(pack_result_.pack_state, PackResult::WRONG_ID);
- }
- // Check that the callback is actually called.
- TEST_F(LanguagePackManagerTest, InstallCallbackTest) {
- dlcservice_client_->set_install_error(dlcservice::kErrorNone);
- dlcservice_client_->set_install_root_path("/path");
- testing::StrictMock<CallbackForTesting> callback;
- EXPECT_CALL(callback, Callback(_));
- manager_->InstallPack(kFakeDlcId, kSupportedLocale,
- callback.GetInstallCallback());
- base::RunLoop().RunUntilIdle();
- }
- TEST_F(LanguagePackManagerTest, GetPackStateSuccessTest) {
- dlcservice_client_->set_get_dlc_state_error(dlcservice::kErrorNone);
- dlcservice::DlcState dlc_state;
- dlc_state.set_state(dlcservice::DlcState_State_INSTALLED);
- dlc_state.set_is_verified(true);
- dlc_state.set_root_path("/path");
- dlcservice_client_->set_dlc_state(dlc_state);
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->GetPackState(
- kHandwritingFeatureId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::GetPackStateTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
- EXPECT_EQ(pack_result_.pack_state, PackResult::INSTALLED);
- EXPECT_EQ(pack_result_.path, "/path");
- }
- TEST_F(LanguagePackManagerTest, GetPackStateFailureTest) {
- dlcservice_client_->set_get_dlc_state_error(dlcservice::kErrorInternal);
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->GetPackState(
- kHandwritingFeatureId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::GetPackStateTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
- EXPECT_NE(pack_result_.pack_state, PackResult::INSTALLED);
- }
- TEST_F(LanguagePackManagerTest, GetPackStateWrongIdTest) {
- manager_->GetPackState(
- kFakeDlcId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::GetPackStateTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInvalidDlc);
- EXPECT_EQ(pack_result_.pack_state, PackResult::WRONG_ID);
- }
- // Check that the callback is actually called.
- TEST_F(LanguagePackManagerTest, GetPackStateCallbackTest) {
- dlcservice_client_->set_get_dlc_state_error(dlcservice::kErrorNone);
- testing::StrictMock<CallbackForTesting> callback;
- EXPECT_CALL(callback, Callback(_));
- manager_->GetPackState(kFakeDlcId, kSupportedLocale,
- callback.GetPackStateCallback());
- base::RunLoop().RunUntilIdle();
- }
- TEST_F(LanguagePackManagerTest, RemovePackSuccessTest) {
- dlcservice_client_->set_uninstall_error(dlcservice::kErrorNone);
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->RemovePack(
- kHandwritingFeatureId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::RemoveTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
- EXPECT_EQ(pack_result_.pack_state, PackResult::NOT_INSTALLED);
- }
- TEST_F(LanguagePackManagerTest, RemovePackFailureTest) {
- dlcservice_client_->set_uninstall_error(dlcservice::kErrorInternal);
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->RemovePack(
- kHandwritingFeatureId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::RemoveTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
- }
- TEST_F(LanguagePackManagerTest, RemovePackWrongIdTest) {
- manager_->RemovePack(
- kFakeDlcId, kSupportedLocale,
- base::BindOnce(&LanguagePackManagerTest::RemoveTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInvalidDlc);
- EXPECT_EQ(pack_result_.pack_state, PackResult::WRONG_ID);
- }
- // Check that the callback is actually called.
- TEST_F(LanguagePackManagerTest, RemovePackCallbackTest) {
- dlcservice_client_->set_uninstall_error(dlcservice::kErrorNone);
- testing::StrictMock<CallbackForTesting> callback;
- EXPECT_CALL(callback, Callback(_));
- manager_->RemovePack(kFakeDlcId, kSupportedLocale,
- callback.GetRemoveCallback());
- base::RunLoop().RunUntilIdle();
- }
- TEST_F(LanguagePackManagerTest, InstallObserverTest) {
- dlcservice_client_->set_install_error(dlcservice::kErrorNone);
- dlcservice_client_->set_install_root_path("/path");
- const DlcState dlc_state = CreateInstalledState();
- MockObserver observer;
- EXPECT_CALL(observer, OnPackStateChanged(_)).Times(0);
- dlcservice_client_->NotifyObserversForTest(dlc_state);
- // Add an Observer and expect it to be notified.
- manager_->AddObserver(&observer);
- EXPECT_CALL(observer, OnPackStateChanged(_)).Times(1);
- dlcservice_client_->NotifyObserversForTest(dlc_state);
- base::RunLoop().RunUntilIdle();
- }
- TEST_F(LanguagePackManagerTest, RemoveObserverTest) {
- dlcservice_client_->set_install_error(dlcservice::kErrorNone);
- dlcservice_client_->set_install_root_path("/path");
- const DlcState dlc_state = CreateInstalledState();
- MockObserver observer;
- // Add an Observer and expect it to be notified.
- manager_->AddObserver(&observer);
- EXPECT_CALL(observer, OnPackStateChanged(_)).Times(1);
- dlcservice_client_->NotifyObserversForTest(dlc_state);
- // Remove the Observer and there should be no more notifications.
- manager_->RemoveObserver(&observer);
- EXPECT_CALL(observer, OnPackStateChanged(_)).Times(0);
- dlcservice_client_->NotifyObserversForTest(dlc_state);
- base::RunLoop().RunUntilIdle();
- }
- // Check that all supported locales are available.
- TEST_F(LanguagePackManagerTest, CheckAllLocalesAvailable) {
- // Handwriting Recognition.
- const std::vector<std::string> handwriting({"es", "ja"});
- for (const auto& locale : handwriting) {
- EXPECT_TRUE(manager_->IsPackAvailable(kHandwritingFeatureId, locale));
- }
- }
- TEST_F(LanguagePackManagerTest, IsPackAvailableFalseTest) {
- // Correct ID, wrong language (Polish).
- bool available = manager_->IsPackAvailable(kHandwritingFeatureId, "pl");
- EXPECT_FALSE(available);
- // ID doesn't exists.
- available = manager_->IsPackAvailable("foo", "fr");
- EXPECT_FALSE(available);
- }
- TEST_F(LanguagePackManagerTest, InstallBasePackSuccess) {
- dlcservice_client_->set_install_error(dlcservice::kErrorNone);
- dlcservice_client_->set_install_root_path("/path");
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->InstallBasePack(
- kHandwritingFeatureId,
- base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorNone);
- EXPECT_EQ(pack_result_.pack_state, PackResult::INSTALLED);
- EXPECT_EQ(pack_result_.path, "/path");
- }
- TEST_F(LanguagePackManagerTest, InstallBasePackFailureTestFailure) {
- dlcservice_client_->set_install_error(dlcservice::kErrorInternal);
- // We need to use an existing Pack ID, so that we do get a result back.
- manager_->InstallBasePack(
- kHandwritingFeatureId,
- base::BindOnce(&LanguagePackManagerTest::InstallTestCallback,
- base::Unretained(this)));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(pack_result_.operation_error, dlcservice::kErrorInternal);
- EXPECT_NE(pack_result_.pack_state, PackResult::INSTALLED);
- }
- } // namespace language_packs
- } // namespace chromeos
|