Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 5 | #include "components/arc/arc_data_remover.h" |
| 6 | |
Hidehiko Abe | 0cb8c74 | 2017-10-05 16:47:49 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 9 | #include "base/bind.h" |
Gabriel Charette | b164afef | 2017-11-21 20:59:31 | [diff] [blame] | 10 | #include "base/run_loop.h" |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 11 | #include "base/test/scoped_task_environment.h" |
| 12 | #include "chromeos/cryptohome/cryptohome_parameters.h" |
| 13 | #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 | #include "chromeos/dbus/fake_session_manager_client.h" |
Alexander Alekseev | 3f09a8f | 2018-05-03 02:52:10 | [diff] [blame^] | 15 | #include "components/account_id/account_id.h" |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 16 | #include "components/arc/arc_prefs.h" |
| 17 | #include "components/prefs/testing_pref_service.h" |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace arc { |
| 21 | namespace { |
| 22 | |
| 23 | class ArcDataRemoverTest : public testing::Test { |
| 24 | public: |
| 25 | ArcDataRemoverTest() = default; |
| 26 | |
| 27 | void SetUp() override { |
| 28 | chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| 29 | std::make_unique<chromeos::FakeSessionManagerClient>()); |
| 30 | chromeos::DBusThreadManager::Initialize(); |
| 31 | |
| 32 | prefs::RegisterProfilePrefs(prefs_.registry()); |
| 33 | } |
| 34 | |
| 35 | void TearDown() override { chromeos::DBusThreadManager::Shutdown(); } |
| 36 | |
| 37 | PrefService* prefs() { return &prefs_; } |
| 38 | |
| 39 | const cryptohome::Identification& cryptohome_id() const { |
| 40 | return cryptohome_id_; |
| 41 | } |
| 42 | |
| 43 | chromeos::FakeSessionManagerClient* session_manager_client() { |
| 44 | return static_cast<chromeos::FakeSessionManagerClient*>( |
| 45 | chromeos::DBusThreadManager::Get()->GetSessionManagerClient()); |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | TestingPrefServiceSimple prefs_; |
| 50 | const cryptohome::Identification cryptohome_id_{EmptyAccountId()}; |
| 51 | |
| 52 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(ArcDataRemoverTest); |
| 55 | }; |
| 56 | |
| 57 | TEST_F(ArcDataRemoverTest, NotScheduled) { |
| 58 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 59 | |
| 60 | base::RunLoop loop; |
| 61 | data_remover.Run(base::BindOnce( |
| 62 | [](base::RunLoop* loop, base::Optional<bool> result) { |
| 63 | EXPECT_EQ(result, base::nullopt); |
| 64 | loop->Quit(); |
| 65 | }, |
| 66 | &loop)); |
| 67 | loop.Run(); |
| 68 | } |
| 69 | |
| 70 | TEST_F(ArcDataRemoverTest, Success) { |
| 71 | session_manager_client()->set_arc_available(true); |
| 72 | |
| 73 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 74 | data_remover.Schedule(); |
| 75 | |
| 76 | base::RunLoop loop; |
| 77 | data_remover.Run(base::BindOnce( |
| 78 | [](base::RunLoop* loop, base::Optional<bool> result) { |
| 79 | EXPECT_EQ(result, base::make_optional(true)); |
| 80 | loop->Quit(); |
| 81 | }, |
| 82 | &loop)); |
| 83 | loop.Run(); |
| 84 | } |
| 85 | |
| 86 | TEST_F(ArcDataRemoverTest, Fail) { |
| 87 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 88 | data_remover.Schedule(); |
| 89 | |
| 90 | base::RunLoop loop; |
| 91 | data_remover.Run(base::BindOnce( |
| 92 | [](base::RunLoop* loop, base::Optional<bool> result) { |
| 93 | EXPECT_EQ(result, base::make_optional(false)); |
| 94 | loop->Quit(); |
| 95 | }, |
| 96 | &loop)); |
| 97 | loop.Run(); |
| 98 | } |
| 99 | |
| 100 | TEST_F(ArcDataRemoverTest, PrefPersistsAcrossInstances) { |
| 101 | { |
| 102 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 103 | data_remover.Schedule(); |
| 104 | EXPECT_TRUE(data_remover.IsScheduledForTesting()); |
| 105 | } |
| 106 | |
| 107 | { |
| 108 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 109 | EXPECT_TRUE(data_remover.IsScheduledForTesting()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace |
| 114 | } // namespace arc |