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" |
Chris Morin | 6fe6086 | 2018-11-13 02:49:28 | [diff] [blame] | 12 | #include "base/threading/thread_task_runner_handle.h" |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 13 | #include "chromeos/cryptohome/cryptohome_parameters.h" |
| 14 | #include "chromeos/dbus/dbus_thread_manager.h" |
Chris Morin | 6fe6086 | 2018-11-13 02:49:28 | [diff] [blame] | 15 | #include "chromeos/dbus/fake_upstart_client.h" |
Alexander Alekseev | 3f09a8f | 2018-05-03 02:52:10 | [diff] [blame] | 16 | #include "components/account_id/account_id.h" |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 17 | #include "components/arc/arc_prefs.h" |
| 18 | #include "components/prefs/testing_pref_service.h" |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
| 21 | namespace arc { |
| 22 | namespace { |
| 23 | |
Chris Morin | 6fe6086 | 2018-11-13 02:49:28 | [diff] [blame] | 24 | class TestUpstartClient : public chromeos::FakeUpstartClient { |
| 25 | public: |
| 26 | TestUpstartClient() = default; |
| 27 | ~TestUpstartClient() override = default; |
| 28 | |
| 29 | void StartJob(const std::string& job, |
| 30 | const std::vector<std::string>& upstart_env, |
| 31 | chromeos::VoidDBusMethodCallback callback) override { |
| 32 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 33 | FROM_HERE, base::BindOnce(std::move(callback), arc_available_)); |
| 34 | } |
| 35 | |
| 36 | void set_arc_available(bool arc_available) { arc_available_ = arc_available; } |
| 37 | |
| 38 | private: |
| 39 | bool arc_available_ = false; |
| 40 | |
| 41 | DISALLOW_COPY_AND_ASSIGN(TestUpstartClient); |
| 42 | }; |
| 43 | |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 44 | class ArcDataRemoverTest : public testing::Test { |
| 45 | public: |
| 46 | ArcDataRemoverTest() = default; |
| 47 | |
| 48 | void SetUp() override { |
Chris Morin | 6fe6086 | 2018-11-13 02:49:28 | [diff] [blame] | 49 | chromeos::DBusThreadManager::GetSetterForTesting()->SetUpstartClient( |
| 50 | std::make_unique<TestUpstartClient>()); |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 51 | chromeos::DBusThreadManager::Initialize(); |
| 52 | |
| 53 | prefs::RegisterProfilePrefs(prefs_.registry()); |
| 54 | } |
| 55 | |
| 56 | void TearDown() override { chromeos::DBusThreadManager::Shutdown(); } |
| 57 | |
| 58 | PrefService* prefs() { return &prefs_; } |
| 59 | |
| 60 | const cryptohome::Identification& cryptohome_id() const { |
| 61 | return cryptohome_id_; |
| 62 | } |
| 63 | |
Chris Morin | 6fe6086 | 2018-11-13 02:49:28 | [diff] [blame] | 64 | TestUpstartClient* upstart_client() { |
| 65 | return static_cast<TestUpstartClient*>( |
| 66 | chromeos::DBusThreadManager::Get()->GetUpstartClient()); |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
| 70 | TestingPrefServiceSimple prefs_; |
| 71 | const cryptohome::Identification cryptohome_id_{EmptyAccountId()}; |
| 72 | |
| 73 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(ArcDataRemoverTest); |
| 76 | }; |
| 77 | |
| 78 | TEST_F(ArcDataRemoverTest, NotScheduled) { |
| 79 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 80 | |
| 81 | base::RunLoop loop; |
| 82 | data_remover.Run(base::BindOnce( |
| 83 | [](base::RunLoop* loop, base::Optional<bool> result) { |
| 84 | EXPECT_EQ(result, base::nullopt); |
| 85 | loop->Quit(); |
| 86 | }, |
| 87 | &loop)); |
| 88 | loop.Run(); |
| 89 | } |
| 90 | |
| 91 | TEST_F(ArcDataRemoverTest, Success) { |
Chris Morin | 6fe6086 | 2018-11-13 02:49:28 | [diff] [blame] | 92 | upstart_client()->set_arc_available(true); |
Hidehiko Abe | c17522b | 2017-09-22 17:46:07 | [diff] [blame] | 93 | |
| 94 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 95 | data_remover.Schedule(); |
| 96 | |
| 97 | base::RunLoop loop; |
| 98 | data_remover.Run(base::BindOnce( |
| 99 | [](base::RunLoop* loop, base::Optional<bool> result) { |
| 100 | EXPECT_EQ(result, base::make_optional(true)); |
| 101 | loop->Quit(); |
| 102 | }, |
| 103 | &loop)); |
| 104 | loop.Run(); |
| 105 | } |
| 106 | |
| 107 | TEST_F(ArcDataRemoverTest, Fail) { |
| 108 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 109 | data_remover.Schedule(); |
| 110 | |
| 111 | base::RunLoop loop; |
| 112 | data_remover.Run(base::BindOnce( |
| 113 | [](base::RunLoop* loop, base::Optional<bool> result) { |
| 114 | EXPECT_EQ(result, base::make_optional(false)); |
| 115 | loop->Quit(); |
| 116 | }, |
| 117 | &loop)); |
| 118 | loop.Run(); |
| 119 | } |
| 120 | |
| 121 | TEST_F(ArcDataRemoverTest, PrefPersistsAcrossInstances) { |
| 122 | { |
| 123 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 124 | data_remover.Schedule(); |
| 125 | EXPECT_TRUE(data_remover.IsScheduledForTesting()); |
| 126 | } |
| 127 | |
| 128 | { |
| 129 | ArcDataRemover data_remover(prefs(), cryptohome_id()); |
| 130 | EXPECT_TRUE(data_remover.IsScheduledForTesting()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace |
| 135 | } // namespace arc |