1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- // 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 "ash/components/fwupd/firmware_update_manager.h"
- #include <deque>
- #include <map>
- #include <memory>
- #include <string>
- #include "ash/components/fwupd/fake_fwupd_download_client.h"
- #include "ash/components/fwupd/histogram_util.h"
- #include "ash/constants/ash_features.h"
- #include "ash/shell.h"
- #include "ash/system/firmware_update/firmware_update_notification_controller.h"
- #include "ash/test/ash_test_base.h"
- #include "ash/webui/firmware_update_ui/mojom/firmware_update.mojom-test-utils.h"
- #include "ash/webui/firmware_update_ui/mojom/firmware_update.mojom.h"
- #include "base/files/file.h"
- #include "base/files/file_path.h"
- #include "base/files/file_util.h"
- #include "base/path_service.h"
- #include "base/strings/utf_string_conversions.h"
- #include "base/test/metrics/histogram_tester.h"
- #include "base/test/scoped_feature_list.h"
- #include "base/test/task_environment.h"
- #include "chromeos/ash/components/dbus/fwupd/fwupd_client.h"
- #include "components/user_manager/user_type.h"
- #include "dbus/message.h"
- #include "dbus/mock_bus.h"
- #include "dbus/mock_object_proxy.h"
- #include "mojo/public/cpp/bindings/pending_remote.h"
- #include "mojo/public/cpp/bindings/receiver.h"
- #include "services/network/public/cpp/shared_url_loader_factory.h"
- #include "services/network/test/test_url_loader_factory.h"
- #include "testing/gmock/include/gmock/gmock.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "ui/message_center/fake_message_center.h"
- #include "ui/message_center/message_center.h"
- using message_center::MessageCenter;
- using message_center::Notification;
- using ::testing::_;
- using ::testing::Invoke;
- using ::testing::Return;
- namespace {
- const char kFakeDeviceIdForTesting[] = "Fake_Device_ID";
- const char kFakeInternalDeviceIdForTesting[] = "Fake_Internal_Device_ID";
- const char kFakeDeviceNameForTesting[] = "Fake Device Name";
- const char kFakeInternalDeviceNameForTesting[] = "Fake Internal Device Name";
- const char kFakeUpdateDescriptionForTesting[] =
- "This is a fake update for testing.";
- const uint32_t kFakeUpdatePriorityForTesting = 1;
- const uint32_t kFakeCriticalUpdatePriorityForTesting = 3;
- const char kFakeUpdateVersionForTesting[] = "1.0.0";
- const char kFakeUpdateUriForTesting[] =
- "file:///usr/share/fwupd/remotes.d/vendor/firmware/testFirmwarePath-V1.cab";
- const char kFakeUpdateFileNameForTesting[] = "testFirmwarePath-V1.cab";
- const char kEmptyFileSha256ForTesting[] =
- "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
- const char kFilePathIdentifier[] = "file://";
- const char kFwupdServiceName[] = "org.freedesktop.fwupd";
- const char kFwupdServicePath[] = "/";
- const char kDescriptionKey[] = "Description";
- const char kIdKey[] = "DeviceId";
- const char kNameKey[] = "Name";
- const char kPriorityKey[] = "Urgency";
- const char kUriKey[] = "Uri";
- const char kVersionKey[] = "Version";
- const char kChecksumKey[] = "Checksum";
- const char kDownloadDir[] = "firmware-updates";
- const char kCacheDir[] = "cache";
- const char kCabExtension[] = ".cab";
- const char kFirmwareUpdateNotificationId[] =
- "cros_firmware_update_notification_id";
- const char kFlagsKey[] = "Flags";
- const uint64_t kFakeFlagForTesting = 1;
- void RunResponseCallback(dbus::ObjectProxy::ResponseOrErrorCallback callback,
- std::unique_ptr<dbus::Response> response) {
- std::move(callback).Run(response.get(), nullptr);
- }
- class FakeUpdateObserver : public ash::firmware_update::mojom::UpdateObserver {
- public:
- void OnUpdateListChanged(
- std::vector<ash::firmware_update::mojom::FirmwareUpdatePtr>
- firmware_updates) override {
- updates_ = std::move(firmware_updates);
- ++num_times_notified_;
- }
- mojo::PendingRemote<ash::firmware_update::mojom::UpdateObserver>
- pending_remote() {
- return receiver_.BindNewPipeAndPassRemote();
- }
- const std::vector<ash::firmware_update::mojom::FirmwareUpdatePtr>& updates()
- const {
- return updates_;
- }
- int num_times_notified() { return num_times_notified_; }
- private:
- std::vector<ash::firmware_update::mojom::FirmwareUpdatePtr> updates_;
- mojo::Receiver<ash::firmware_update::mojom::UpdateObserver> receiver_{this};
- int num_times_notified_ = 0;
- };
- class FakeUpdateProgressObserver
- : public ash::firmware_update::mojom::UpdateProgressObserver {
- public:
- void OnStatusChanged(
- ash::firmware_update::mojom::InstallationProgressPtr update) override {
- update_ = std::move(update);
- }
- mojo::PendingRemote<ash::firmware_update::mojom::UpdateProgressObserver>
- pending_remote() {
- return receiver_.BindNewPipeAndPassRemote();
- }
- const ash::firmware_update::mojom::InstallationProgressPtr& GetLatestUpdate()
- const {
- return update_;
- }
- private:
- ash::firmware_update::mojom::InstallationProgressPtr update_;
- mojo::Receiver<ash::firmware_update::mojom::UpdateProgressObserver> receiver_{
- this};
- };
- } // namespace
- namespace ash {
- class FirmwareUpdateManagerTest : public AshTestBase {
- public:
- FirmwareUpdateManagerTest() {
- scoped_feature_list_.InitAndEnableFeature(
- ::ash::features::kFirmwareUpdaterApp);
- dbus::Bus::Options options;
- options.bus_type = dbus::Bus::SYSTEM;
- bus_ = base::MakeRefCounted<dbus::MockBus>(options);
- dbus::ObjectPath fwupd_service_path(kFwupdServicePath);
- proxy_ = base::MakeRefCounted<dbus::MockObjectProxy>(
- bus_.get(), kFwupdServiceName, fwupd_service_path);
- EXPECT_CALL(*bus_.get(),
- GetObjectProxy(kFwupdServiceName, fwupd_service_path))
- .WillRepeatedly(testing::Return(proxy_.get()));
- EXPECT_CALL(*proxy_, DoConnectToSignal(_, _, _, _))
- .WillRepeatedly(Return());
- FwupdClient::Initialize(bus_.get());
- dbus_client_ = FwupdClient::Get();
- fake_fwupd_download_client_ = std::make_unique<FakeFwupdDownloadClient>();
- firmware_update_manager_ = std::make_unique<FirmwareUpdateManager>();
- firmware_update_manager_->BindInterface(
- update_provider_remote_.BindNewPipeAndPassReceiver());
- }
- FirmwareUpdateManagerTest(const FirmwareUpdateManagerTest&) = delete;
- FirmwareUpdateManagerTest& operator=(const FirmwareUpdateManagerTest&) =
- delete;
- ~FirmwareUpdateManagerTest() override {
- // Destructor depends on FirmwareUpdateManager.
- firmware_update_notification_controller_.reset();
- // Destructor depends on FwupdClient.
- firmware_update_manager_.reset();
- FwupdClient::Shutdown();
- }
- void OnMethodCalled(dbus::MethodCall* method_call,
- int timeout_ms,
- dbus::ObjectProxy::ResponseOrErrorCallback* callback) {
- ASSERT_FALSE(dbus_responses_.empty());
- auto response = std::move(dbus_responses_.front());
- task_environment()->GetMainThreadTaskRunner()->PostTask(
- FROM_HERE, base::BindOnce(&RunResponseCallback, std::move(*callback),
- std::move(response)));
- dbus_responses_.pop_front();
- }
- protected:
- void InitializeNotificationController() {
- firmware_update_notification_controller_ =
- std::make_unique<FirmwareUpdateNotificationController>(
- message_center());
- firmware_update_notification_controller_
- ->OnFirmwareUpdateManagerInitialized();
- }
- void StartInstall(const std::string& device_id,
- const base::FilePath& filepath) {
- base::RunLoop loop;
- firmware_update_manager_->StartInstall(
- device_id, filepath,
- base::BindOnce([](base::OnceClosure done) { std::move(done).Run(); },
- loop.QuitClosure()));
- loop.Run();
- }
- void RequestDevices() {
- firmware_update_manager_->RequestDevices();
- base::RunLoop().RunUntilIdle();
- }
- void SetFakeUrlForTesting(const std::string& fake_url) {
- firmware_update_manager_->SetFakeUrlForTesting(fake_url);
- }
- message_center::MessageCenter* message_center() const {
- return message_center::MessageCenter::Get();
- }
- std::unique_ptr<dbus::Response> CreateEmptyDeviceResponse() {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateOneDeviceResponse() {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- dbus::MessageWriter dict_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kNameKey);
- dict_writer.AppendVariantOfString(kFakeDeviceNameForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kIdKey);
- dict_writer.AppendVariantOfString(kFakeDeviceIdForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateInternalDeviceResponse() {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- dbus::MessageWriter dict_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kNameKey);
- dict_writer.AppendVariantOfString(kFakeInternalDeviceNameForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kIdKey);
- dict_writer.AppendVariantOfString(kFakeInternalDeviceIdForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kFlagsKey);
- dict_writer.AppendVariantOfUint64(kFakeFlagForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateTwoDeviceResponse() {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- dbus::MessageWriter dict_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kNameKey);
- dict_writer.AppendVariantOfString(std::string(kFakeDeviceNameForTesting) +
- "1");
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kIdKey);
- dict_writer.AppendVariantOfString(std::string(kFakeDeviceIdForTesting) +
- "1");
- device_array_writer.CloseContainer(&dict_writer);
- // Prepare the next device entry.
- response_array_writer.CloseContainer(&device_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kNameKey);
- dict_writer.AppendVariantOfString(std::string(kFakeDeviceNameForTesting) +
- "2");
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kIdKey);
- dict_writer.AppendVariantOfString(std::string(kFakeDeviceIdForTesting) +
- "2");
- device_array_writer.CloseContainer(&dict_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateOneUpdateResponse() {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- dbus::MessageWriter dict_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kDescriptionKey);
- dict_writer.AppendVariantOfString(kFakeUpdateDescriptionForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kVersionKey);
- dict_writer.AppendVariantOfString(kFakeUpdateVersionForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kPriorityKey);
- dict_writer.AppendVariantOfUint32(kFakeUpdatePriorityForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kUriKey);
- dict_writer.AppendVariantOfString(kFakeUpdateUriForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kChecksumKey);
- dict_writer.AppendVariantOfString(kEmptyFileSha256ForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateOneUpdateResponseWithChecksum(
- const std::string& checksum) {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- dbus::MessageWriter dict_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kDescriptionKey);
- dict_writer.AppendVariantOfString(kFakeUpdateDescriptionForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kVersionKey);
- dict_writer.AppendVariantOfString(kFakeUpdateVersionForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kPriorityKey);
- dict_writer.AppendVariantOfUint32(kFakeUpdatePriorityForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kUriKey);
- dict_writer.AppendVariantOfString(kFakeUpdateUriForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kChecksumKey);
- dict_writer.AppendVariantOfString(checksum);
- device_array_writer.CloseContainer(&dict_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateOneCriticalUpdateResponse() {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- dbus::MessageWriter dict_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kDescriptionKey);
- dict_writer.AppendVariantOfString(kFakeUpdateDescriptionForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kVersionKey);
- dict_writer.AppendVariantOfString(kFakeUpdateVersionForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kPriorityKey);
- dict_writer.AppendVariantOfUint32(kFakeCriticalUpdatePriorityForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kUriKey);
- dict_writer.AppendVariantOfString(kFakeUpdateUriForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- device_array_writer.OpenDictEntry(&dict_writer);
- dict_writer.AppendString(kChecksumKey);
- dict_writer.AppendVariantOfString(kEmptyFileSha256ForTesting);
- device_array_writer.CloseContainer(&dict_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateNoUpdateResponse() {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- dbus::MessageWriter response_array_writer(nullptr);
- dbus::MessageWriter device_array_writer(nullptr);
- // The response is an array of arrays of dictionaries. Each dictionary is
- // one device description.
- response_writer.OpenArray("a{sv}", &response_array_writer);
- response_array_writer.OpenArray("{sv}", &device_array_writer);
- response_array_writer.CloseContainer(&device_array_writer);
- response_writer.CloseContainer(&response_array_writer);
- return response;
- }
- std::unique_ptr<dbus::Response> CreateBoolResponse(bool success) {
- auto response = dbus::Response::CreateEmpty();
- dbus::MessageWriter response_writer(response.get());
- response_writer.AppendBool(success);
- return response;
- }
- void SetupObserver(FakeUpdateObserver* observer) {
- firmware_update_manager_->ObservePeripheralUpdates(
- observer->pending_remote());
- base::RunLoop().RunUntilIdle();
- }
- network::TestURLLoaderFactory& GetTestUrlLoaderFactory() {
- return fake_fwupd_download_client_->test_url_loader_factory();
- }
- void SetupProgressObserver(FakeUpdateProgressObserver* observer) {
- install_controller_remote_->AddObserver(observer->pending_remote());
- base::RunLoop().RunUntilIdle();
- }
- bool PrepareForUpdate(const std::string& device_id) {
- mojo::PendingRemote<ash::firmware_update::mojom::InstallController>
- pending_remote;
- ash::firmware_update::mojom::UpdateProviderAsyncWaiter(
- update_provider_remote_.get())
- .PrepareForUpdate(device_id, &pending_remote);
- if (!pending_remote.is_valid())
- return false;
- install_controller_remote_.Bind(std::move(pending_remote));
- base::RunLoop().RunUntilIdle();
- return true;
- }
- void SetProperties(uint32_t percentage, uint32_t status) {
- dbus_client_->SetPropertiesForTesting(percentage, status);
- base::RunLoop().RunUntilIdle();
- }
- void BeginUpdate(const std::string& device_id,
- const base::FilePath& filepath) {
- firmware_update_manager_->BeginUpdate(device_id, filepath);
- }
- void RequestAllUpdates() { firmware_update_manager_->RequestAllUpdates(); }
- // `FwupdClient` must be be before `FirmwareUpdateManager`.
- FwupdClient* dbus_client_ = nullptr;
- std::unique_ptr<FakeFwupdDownloadClient> fake_fwupd_download_client_;
- std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
- // `FirmwareUpdateNotificationController` must be be after
- // `FirmwareUpdateManager` to ensure that
- // `FirmwareUpdateNotificationController` is removed as an observer before
- // `FirmwareUpdateManager` is destroyed.
- std::unique_ptr<FirmwareUpdateNotificationController>
- firmware_update_notification_controller_;
- mojo::Remote<ash::firmware_update::mojom::UpdateProvider>
- update_provider_remote_;
- mojo::Remote<ash::firmware_update::mojom::InstallController>
- install_controller_remote_;
- // Mock bus for simulating calls.
- scoped_refptr<dbus::MockBus> bus_;
- scoped_refptr<dbus::MockObjectProxy> proxy_;
- // Fake responses.
- std::deque<std::unique_ptr<dbus::Response>> dbus_responses_;
- base::test::ScopedFeatureList scoped_feature_list_;
- };
- TEST_F(FirmwareUpdateManagerTest, CorrectMockInstance) {
- EXPECT_EQ(dbus_client_, FwupdClient::Get());
- }
- TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesNoDevices) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateEmptyDeviceResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
- update_observer.updates();
- base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(updates.empty());
- ASSERT_EQ(0U, firmware_update_manager_->GetUpdateCount());
- }
- TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesOneDeviceNoUpdates) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateNoUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
- update_observer.updates();
- base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(updates.empty());
- ASSERT_EQ(0U, firmware_update_manager_->GetUpdateCount());
- }
- TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesOneDeviceOneUpdate) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
- update_observer.updates();
- base::RunLoop().RunUntilIdle();
- ASSERT_EQ(1U, updates.size());
- ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
- EXPECT_EQ(kFakeDeviceIdForTesting, updates[0]->device_id);
- EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeDeviceNameForTesting)),
- updates[0]->device_name);
- EXPECT_EQ(kFakeUpdateVersionForTesting, updates[0]->device_version);
- EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeUpdateDescriptionForTesting)),
- updates[0]->device_description);
- EXPECT_EQ(ash::firmware_update::mojom::UpdatePriority(
- kFakeUpdatePriorityForTesting),
- updates[0]->priority);
- EXPECT_EQ(kFakeUpdateUriForTesting, updates[0]->filepath.value());
- }
- TEST_F(FirmwareUpdateManagerTest, RequestUpdatesClearsCache) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
- update_observer.updates();
- base::RunLoop().RunUntilIdle();
- ASSERT_EQ(1U, updates.size());
- ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- RequestDevices();
- // Expect cache to clear and only 1 updates now instead of 2.
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& new_updates =
- update_observer.updates();
- ASSERT_EQ(1U, new_updates.size());
- ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
- }
- TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesTwoDeviceOneWithUpdate) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateTwoDeviceResponse());
- dbus_responses_.push_back(CreateNoUpdateResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- base::RunLoop().RunUntilIdle();
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
- update_observer.updates();
- base::RunLoop().RunUntilIdle();
- ASSERT_EQ(1U, updates.size());
- ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
- // The second device was the one with the update.
- EXPECT_EQ(std::string(kFakeDeviceIdForTesting) + "2", updates[0]->device_id);
- EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeDeviceNameForTesting) + "2"),
- updates[0]->device_name);
- EXPECT_EQ(kFakeUpdateVersionForTesting, updates[0]->device_version);
- EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeUpdateDescriptionForTesting)),
- updates[0]->device_description);
- EXPECT_EQ(ash::firmware_update::mojom::UpdatePriority(
- kFakeUpdatePriorityForTesting),
- updates[0]->priority);
- }
- TEST_F(FirmwareUpdateManagerTest, RequestUpdatesMutipleTimes) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateTwoDeviceResponse());
- dbus_responses_.push_back(CreateNoUpdateResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- base::RunLoop().RunUntilIdle();
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
- update_observer.updates();
- base::RunLoop().RunUntilIdle();
- ASSERT_EQ(1, update_observer.num_times_notified());
- ASSERT_EQ(1U, updates.size());
- ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
- // Request all updates multiple times, this time while a request is already
- // being made.
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- RequestAllUpdates();
- RequestAllUpdates();
- base::RunLoop().RunUntilIdle();
- // Expect only one additional RequestAllUpdates() to go through.
- ASSERT_EQ(1U, updates.size());
- ASSERT_EQ(2, update_observer.num_times_notified());
- // Now request all updates again, this time after the previous request has
- // been completed.
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- RequestAllUpdates();
- base::RunLoop().RunUntilIdle();
- // Expect another additional RequestAllUpdates() to go through.
- ASSERT_EQ(1U, updates.size());
- ASSERT_EQ(3, update_observer.num_times_notified());
- }
- TEST_F(FirmwareUpdateManagerTest, RequestInstall) {
- base::HistogramTester histogram_tester;
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- dbus_responses_.push_back(dbus::Response::CreateEmpty());
- // Add dbus response for RequestAllUpdates() call made after an install
- // is completed.
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- ASSERT_EQ(1, update_observer.num_times_notified());
- const std::string fake_url =
- std::string("https://faketesturl/") + kFakeUpdateFileNameForTesting;
- std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
- SetFakeUrlForTesting(fake_url);
- GetTestUrlLoaderFactory().AddResponse(fake_url, "");
- EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
- FakeUpdateProgressObserver update_progress_observer;
- SetupProgressObserver(&update_progress_observer);
- StartInstall(std::string(kFakeDeviceIdForTesting), base::FilePath(fake_url));
- base::RunLoop().RunUntilIdle();
- base::FilePath root_dir;
- CHECK(base::PathService::Get(base::DIR_TEMP, &root_dir));
- const base::FilePath root_path =
- root_dir.Append(FILE_PATH_LITERAL(kDownloadDir))
- .Append(FILE_PATH_LITERAL(kCacheDir));
- const std::string test_filename =
- std::string(kFakeDeviceIdForTesting) + std::string(kCabExtension);
- base::FilePath full_path = root_path.Append(test_filename);
- // TODO(jimmyxgong): Check that the file was created. Tests are failing
- // because file isn't created initially.
- EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kSuccess,
- update_progress_observer.GetLatestUpdate()->state);
- // Expect RequestAllUpdates() to have been called after an install to refresh
- // the update list.
- ASSERT_EQ(2, update_observer.num_times_notified());
- histogram_tester.ExpectUniqueSample(
- "ChromeOS.FirmwareUpdateUi.InstallResult",
- firmware_update::metrics::FirmwareUpdateInstallResult::kSuccess, 1);
- }
- TEST_F(FirmwareUpdateManagerTest, RequestInstallLocalPatch) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- dbus_responses_.push_back(dbus::Response::CreateEmpty());
- // Add dbus response for RequestAllUpdates() call made after an install
- // is completed.
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- base::RunLoop().RunUntilIdle();
- base::FilePath root_dir;
- CHECK(base::PathService::Get(base::DIR_TEMP, &root_dir));
- const base::FilePath root_path =
- root_dir.Append(FILE_PATH_LITERAL(kDownloadDir))
- .Append(FILE_PATH_LITERAL(kCacheDir));
- const std::string test_filename =
- std::string(kFakeDeviceIdForTesting) + std::string(kCabExtension);
- base::FilePath full_path = root_path.Append(test_filename);
- // Create a temporary file to simulate a .cab available for install.
- base::WriteFile(full_path, "", 0);
- EXPECT_TRUE(base::PathExists(full_path));
- const std::string uri = kFilePathIdentifier + full_path.value();
- EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
- FakeUpdateProgressObserver update_progress_observer;
- SetupProgressObserver(&update_progress_observer);
- StartInstall(std::string(kFakeDeviceIdForTesting), base::FilePath(uri));
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kSuccess,
- update_progress_observer.GetLatestUpdate()->state);
- }
- TEST_F(FirmwareUpdateManagerTest, OnPropertiesChangedResponse) {
- EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
- FakeUpdateProgressObserver update_progress_observer;
- SetupProgressObserver(&update_progress_observer);
- // Initial state.
- SetProperties(/**percentage=*/0u, /**status=*/0u);
- EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kUnknown,
- update_progress_observer.GetLatestUpdate()->state);
- EXPECT_EQ(0u, update_progress_observer.GetLatestUpdate()->percentage);
- // Install in progress.
- SetProperties(/**percentage=*/1u, /**status=*/5u);
- EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kUpdating,
- update_progress_observer.GetLatestUpdate()->state);
- EXPECT_EQ(1u, update_progress_observer.GetLatestUpdate()->percentage);
- // Device restarting
- SetProperties(/**percentage=*/100u, /**status=*/4u);
- EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kRestarting,
- update_progress_observer.GetLatestUpdate()->state);
- EXPECT_EQ(100u, update_progress_observer.GetLatestUpdate()->percentage);
- // Emitted once install is completed and device has been restarted.
- SetProperties(/**percentage=*/100u, /**status=*/0u);
- EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kUnknown,
- update_progress_observer.GetLatestUpdate()->state);
- EXPECT_EQ(100u, update_progress_observer.GetLatestUpdate()->percentage);
- }
- TEST_F(FirmwareUpdateManagerTest, InvalidFile) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- base::RunLoop().RunUntilIdle();
- dbus_responses_.push_back(dbus::Response::CreateEmpty());
- std::string fake_url = "https://faketesturl/";
- std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
- SetFakeUrlForTesting(fake_url);
- GetTestUrlLoaderFactory().AddResponse(fake_url, "");
- EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
- FakeUpdateProgressObserver update_progress_observer;
- SetupProgressObserver(&update_progress_observer);
- BeginUpdate(std::string(kFakeDeviceIdForTesting),
- base::FilePath("BadTestFilename@#.cab"));
- base::RunLoop().RunUntilIdle();
- // An invalid filepath will never trigger the install. Expect no updates
- // progress to be available.
- EXPECT_TRUE(!update_progress_observer.GetLatestUpdate());
- }
- TEST_F(FirmwareUpdateManagerTest, InvalidChecksum) {
- base::HistogramTester histogram_tester;
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponseWithChecksum(
- "badbbadbad1ef97238fb24c5e40a979bc544bb2b0967b863e43e7d58e0d9a923f"));
- dbus_responses_.push_back(dbus::Response::CreateEmpty());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- ASSERT_EQ(1, update_observer.num_times_notified());
- // No updates available since checksum is empty.
- const auto& updates = update_observer.updates();
- ASSERT_TRUE(updates.empty());
- }
- TEST_F(FirmwareUpdateManagerTest, EmptyChecksum) {
- base::HistogramTester histogram_tester;
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponseWithChecksum(""));
- dbus_responses_.push_back(dbus::Response::CreateEmpty());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- ASSERT_EQ(1, update_observer.num_times_notified());
- // No updates available since checksum is empty.
- const auto& updates = update_observer.updates();
- ASSERT_TRUE(updates.empty());
- }
- TEST_F(FirmwareUpdateManagerTest, WrongChecksumVariant) {
- base::HistogramTester histogram_tester;
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponseWithChecksum(
- "badbbadbad1ef97238fb24c5e40a979bc544bb2b"));
- dbus_responses_.push_back(dbus::Response::CreateEmpty());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- ASSERT_EQ(1, update_observer.num_times_notified());
- // No updates available since checksum is empty.
- const auto& updates = update_observer.updates();
- ASSERT_TRUE(updates.empty());
- }
- TEST_F(FirmwareUpdateManagerTest, NotificationShownForCriticalUpdate) {
- InitializeNotificationController();
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneCriticalUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- EXPECT_TRUE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- message_center()->RemoveNotification(kFirmwareUpdateNotificationId,
- /*by_user=*/true);
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneCriticalUpdateResponse());
- RequestDevices();
- base::RunLoop().RunUntilIdle();
- // Request updates again and verify that the notification is not being
- // shown multiple times for the same update.
- EXPECT_FALSE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- }
- TEST_F(FirmwareUpdateManagerTest, NotificationNotShownIfNoCriticalUpdates) {
- InitializeNotificationController();
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- EXPECT_FALSE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- EXPECT_FALSE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- }
- TEST_F(FirmwareUpdateManagerTest, DeviceCountMetric) {
- base::HistogramTester histogram_tester;
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- histogram_tester.ExpectUniqueSample(
- "ChromeOS.FirmwareUpdateUi.OnStartup.DeviceCount", 1, 1);
- RequestDevices();
- histogram_tester.ExpectUniqueSample(
- "ChromeOS.FirmwareUpdateUi.OnRefresh.DeviceCount", 1, 1);
- }
- TEST_F(FirmwareUpdateManagerTest, UpdateCountMetric) {
- base::HistogramTester histogram_tester;
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- histogram_tester.ExpectBucketCount(
- "ChromeOS.FirmwareUpdateUi.OnStartup.CriticalUpdateCount", 0, 1);
- histogram_tester.ExpectBucketCount(
- "ChromeOS.FirmwareUpdateUi.OnStartup.UpdateCount", 1, 1);
- RequestDevices();
- histogram_tester.ExpectBucketCount(
- "ChromeOS.FirmwareUpdateUi.OnRefresh.CriticalUpdateCount", 0, 1);
- histogram_tester.ExpectBucketCount(
- "ChromeOS.FirmwareUpdateUi.OnRefresh.UpdateCount", 1, 1);
- }
- class FirmwareUpdateStartupNotificationTest : public NoSessionAshTestBase {
- public:
- FirmwareUpdateStartupNotificationTest() = default;
- ~FirmwareUpdateStartupNotificationTest() override = default;
- void SetUp() override {
- FwupdClient::InitializeFake();
- dbus_client_ = FwupdClient::Get();
- firmware_update_manager_ = std::make_unique<FirmwareUpdateManager>();
- EXPECT_TRUE(FirmwareUpdateManager::IsInitialized());
- SetShouldShowNotificationForTest(true);
- NoSessionAshTestBase::SetUp();
- }
- void TearDown() override {
- firmware_update_notification_controller_.reset();
- firmware_update_manager_.reset();
- FwupdClient::Shutdown();
- NoSessionAshTestBase::TearDown();
- }
- protected:
- void InitializeNotificationController() {
- firmware_update_notification_controller_ =
- std::make_unique<FirmwareUpdateNotificationController>(
- message_center());
- firmware_update_notification_controller_
- ->OnFirmwareUpdateManagerInitialized();
- }
- message_center::MessageCenter* message_center() const {
- return message_center::MessageCenter::Get();
- }
- message_center::Notification* FindShortcutsChangedNotification() {
- return message_center::MessageCenter::Get()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId);
- }
- void SetShouldShowNotificationForTest(bool show_notification) {
- FirmwareUpdateManager::Get()->set_should_show_notification_for_test(
- show_notification);
- }
- void SimulateFetchingUpdates() {
- FirmwareUpdateManager::Get()->RequestAllUpdates();
- }
- FwupdClient* dbus_client_ = nullptr;
- std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
- std::unique_ptr<FirmwareUpdateNotificationController>
- firmware_update_notification_controller_;
- };
- TEST_F(FirmwareUpdateStartupNotificationTest,
- StartupNotificationShownRegularUser) {
- // Notification should be shown at login.
- SimulateUserLogin("user1@email.com");
- InitializeNotificationController();
- SimulateFetchingUpdates();
- EXPECT_TRUE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- }
- TEST_F(FirmwareUpdateStartupNotificationTest,
- StartupNotificationShownGuestUser) {
- // Notification should not be shown at login if the user is a guest.
- SimulateUserLogin("user1@email.com", user_manager::USER_TYPE_GUEST);
- InitializeNotificationController();
- SimulateFetchingUpdates();
- EXPECT_FALSE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- }
- TEST_F(FirmwareUpdateStartupNotificationTest, StartupNotificationShownKiosk) {
- // Notification should not be shown at login if the user is in kiosk mode.
- SimulateUserLogin("user1@email.com", user_manager::USER_TYPE_KIOSK_APP);
- InitializeNotificationController();
- SimulateFetchingUpdates();
- EXPECT_FALSE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- }
- TEST_F(FirmwareUpdateStartupNotificationTest,
- StartupNotificationShownKioskPWA) {
- // Notification should not be shown at login if the user is in kiosk mode.
- SimulateUserLogin("user1@email.com", user_manager::USER_TYPE_WEB_KIOSK_APP);
- InitializeNotificationController();
- SimulateFetchingUpdates();
- EXPECT_FALSE(message_center()->FindVisibleNotificationById(
- kFirmwareUpdateNotificationId));
- }
- TEST_F(FirmwareUpdateManagerTest, InternalDeviceFiltered) {
- EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
- .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
- dbus_responses_.push_back(CreateOneDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- dbus_responses_.push_back(CreateInternalDeviceResponse());
- dbus_responses_.push_back(CreateOneUpdateResponse());
- FakeUpdateObserver update_observer;
- SetupObserver(&update_observer);
- const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
- update_observer.updates();
- base::RunLoop().RunUntilIdle();
- ASSERT_EQ(1U, updates.size());
- EXPECT_EQ(kFakeDeviceIdForTesting, updates[0]->device_id);
- }
- } // namespace ash
|