File: fake_rmad_client_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (688 lines) | stat: -rw-r--r-- 27,728 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/rmad/fake_rmad_client.h"

#include <memory>
#include <optional>
#include <string>

#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/object_path.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;

namespace ash {

namespace {
class FakeRmadClientTest : public testing::Test {
 public:
  FakeRmadClientTest() = default;
  FakeRmadClientTest(const FakeRmadClientTest&) = delete;
  FakeRmadClientTest& operator=(const FakeRmadClientTest&) = delete;

  void SetUp() override {
    // Create a fake client.
    // The constructor registers the singleton with RmadClient.
    // Rmad::InitializeFake is not called because it sets up the fake states for
    // testing ShimlessRMA app.
    new FakeRmadClient();
    client_ = RmadClient::Get();
  }

  void TearDown() override { RmadClient::Shutdown(); }

  FakeRmadClient* fake_client_() {
    return static_cast<FakeRmadClient*>(client_.get());
  }

  raw_ptr<RmadClient, DanglingUntriaged> client_ =
      nullptr;  // Unowned convenience pointer.
  // A message loop to emulate asynchronous behavior.
  base::test::SingleThreadTaskEnvironment task_environment_;
};

// Interface for observing changes from rmad.
class TestObserver : public RmadClient::Observer {
 public:
  explicit TestObserver(RmadClient* client) : client_(client) {
    client_->AddObserver(this);
  }
  TestObserver(const TestObserver&) = delete;
  TestObserver& operator=(const TestObserver&) = delete;

  ~TestObserver() override { client_->RemoveObserver(this); }

  int num_error() const { return num_error_; }
  rmad::RmadErrorCode last_error() const { return last_error_; }
  int num_calibration_progress() const { return num_calibration_progress_; }
  const rmad::CalibrationComponentStatus& last_calibration_component_status()
      const {
    return last_calibration_component_status_;
  }
  int num_calibration_overall_progress() const {
    return num_calibration_overall_progress_;
  }
  rmad::CalibrationOverallStatus last_calibration_overall_status() const {
    return last_calibration_overall_status_;
  }
  int num_provisioning_progress() const { return num_provisioning_progress_; }
  rmad::ProvisionStatus last_provisioning_status() const {
    return last_provisioning_status_;
  }
  int num_hardware_write_protection_state() {
    return num_hardware_write_protection_state_;
  }
  bool last_hardware_write_protection_state() {
    return last_hardware_write_protection_state_;
  }
  int num_power_cable_state() { return num_power_cable_state_; }
  bool last_power_cable_state() { return last_power_cable_state_; }
  int num_external_disk_state() const { return num_external_disk_state_; }
  bool last_external_disk_state() const { return last_external_disk_state_; }
  int num_hardware_verification_result() const {
    return num_hardware_verification_result_;
  }
  const rmad::HardwareVerificationResult& last_hardware_verification_result()
      const {
    return last_hardware_verification_result_;
  }
  int num_ro_firmware_update_progress() const {
    return num_ro_firmware_update_progress_;
  }
  rmad::UpdateRoFirmwareStatus last_ro_firmware_update_progress() const {
    return last_ro_firmware_update_progress_;
  }

  // Called when an error occurs outside of state transitions.
  // e.g. while calibrating devices.
  void Error(rmad::RmadErrorCode error) override {
    num_error_++;
    last_error_ = error;
  }

  // Called when calibration progress is updated.
  void CalibrationProgress(
      const rmad::CalibrationComponentStatus& componentStatus) override {
    num_calibration_progress_++;
    last_calibration_component_status_ = componentStatus;
  }

  // Called when calibration progress is updated.
  void CalibrationOverallProgress(
      rmad::CalibrationOverallStatus status) override {
    num_calibration_overall_progress_++;
    last_calibration_overall_status_ = status;
  }

  // Called when provisioning progress is updated.
  void ProvisioningProgress(const rmad::ProvisionStatus& status) override {
    num_provisioning_progress_++;
    last_provisioning_status_ = status;
  }

  // Called when hardware write protection state changes.
  void HardwareWriteProtectionState(bool enabled) override {
    num_hardware_write_protection_state_++;
    last_hardware_write_protection_state_ = enabled;
  }

  // Called when power cable is plugged in or removed.
  void PowerCableState(bool plugged_in) override {
    num_power_cable_state_++;
    last_power_cable_state_ = plugged_in;
  }

  // Called when external disk is plugged in or removed.
  void ExternalDiskState(bool detected) override {
    num_external_disk_state_++;
    last_external_disk_state_ = detected;
  }

  // Called when hardware verification completes.
  void HardwareVerificationResult(
      const rmad::HardwareVerificationResult& last_hardware_verification_result)
      override {
    num_hardware_verification_result_++;
    last_hardware_verification_result_ = last_hardware_verification_result;
  }

  // Called when overall calibration progress is updated.
  void RoFirmwareUpdateProgress(rmad::UpdateRoFirmwareStatus status) override {
    num_ro_firmware_update_progress_++;
    last_ro_firmware_update_progress_ = status;
  }

 private:
  raw_ptr<RmadClient> client_;  // Not owned.
  int num_error_ = 0;
  rmad::RmadErrorCode last_error_ = rmad::RmadErrorCode::RMAD_ERROR_NOT_SET;
  int num_calibration_progress_ = 0;
  rmad::CalibrationComponentStatus last_calibration_component_status_;
  int num_calibration_overall_progress_ = 0;
  rmad::CalibrationOverallStatus last_calibration_overall_status_ =
      rmad::CalibrationOverallStatus::RMAD_CALIBRATION_OVERALL_UNKNOWN;
  int num_provisioning_progress_ = 0;
  rmad::ProvisionStatus last_provisioning_status_;
  int num_hardware_write_protection_state_ = 0;
  bool last_hardware_write_protection_state_ = true;
  int num_power_cable_state_ = 0;
  bool last_power_cable_state_ = true;
  int num_external_disk_state_ = 0;
  bool last_external_disk_state_ = true;
  int num_hardware_verification_result_ = 0;
  rmad::HardwareVerificationResult last_hardware_verification_result_;
  int num_ro_firmware_update_progress_ = 0;
  rmad::UpdateRoFirmwareStatus last_ro_firmware_update_progress_;
};

rmad::RmadState CreateWelcomeState() {
  rmad::RmadState state;
  state.set_allocated_welcome(new rmad::WelcomeState());
  return state;
}

rmad::RmadState CreateDeviceDestinationState() {
  rmad::RmadState state;
  state.set_allocated_device_destination(new rmad::DeviceDestinationState());
  return state;
}

rmad::GetStateReply CreateWelcomeStateReply(rmad::RmadErrorCode error) {
  rmad::GetStateReply reply;
  reply.set_allocated_state(new rmad::RmadState());
  reply.mutable_state()->set_allocated_welcome(new rmad::WelcomeState());
  reply.set_error(error);
  return reply;
}

rmad::GetStateReply CreateDeviceDestinationStateReply(
    rmad::RmadErrorCode error) {
  rmad::GetStateReply reply;
  reply.set_allocated_state(new rmad::RmadState());
  reply.mutable_state()->set_allocated_device_destination(
      new rmad::DeviceDestinationState());
  reply.set_error(error);
  return reply;
}

TEST_F(FakeRmadClientTest, GetCurrentState_Default_RmaNotRequired) {
  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->GetCurrentState(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_RMA_NOT_REQUIRED);
  EXPECT_FALSE(response->has_state());
}

TEST_F(FakeRmadClientTest, GetCurrentState_Welcome_Ok) {
  std::vector<rmad::GetStateReply> fake_states;
  fake_states.push_back(CreateWelcomeStateReply(rmad::RMAD_ERROR_OK));
  fake_client_()->SetFakeStateReplies(std::move(fake_states));

  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->GetCurrentState(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
  EXPECT_TRUE(response->has_state());
  EXPECT_TRUE(response->state().has_welcome());
}

TEST_F(FakeRmadClientTest, GetCurrentState_Welcome_CorrectStateReturned) {
  std::vector<rmad::GetStateReply> fake_states;
  // Use any error to test.
  rmad::GetStateReply state =
      CreateWelcomeStateReply(rmad::RMAD_ERROR_MISSING_COMPONENT);
  state.mutable_state()->mutable_welcome()->set_choice(
      rmad::WelcomeState_FinalizeChoice_RMAD_CHOICE_FINALIZE_REPAIR);
  fake_states.push_back(std::move(state));
  fake_client_()->SetFakeStateReplies(std::move(fake_states));

  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->GetCurrentState(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_MISSING_COMPONENT);
  EXPECT_TRUE(response->has_state());
  EXPECT_TRUE(response->state().has_welcome());
  EXPECT_EQ(response->state().welcome().choice(),
            rmad::WelcomeState_FinalizeChoice_RMAD_CHOICE_FINALIZE_REPAIR);
}

TEST_F(FakeRmadClientTest, TransitionNextState_Default_RmaNotRequired) {
  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->TransitionNextState(std::move(CreateWelcomeState()),
                               future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_RMA_NOT_REQUIRED);
  EXPECT_FALSE(response->has_state());
}

TEST_F(FakeRmadClientTest, TransitionNextState_NoNextState_Fails) {
  std::vector<rmad::GetStateReply> fake_states;
  fake_states.push_back(
      rmad::GetStateReply(CreateWelcomeStateReply(rmad::RMAD_ERROR_OK)));
  fake_client_()->SetFakeStateReplies(std::move(fake_states));

  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->TransitionNextState(std::move(CreateWelcomeState()),
                               future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_TRANSITION_FAILED);
  EXPECT_TRUE(response->has_state());
  EXPECT_TRUE(response->state().has_welcome());
}

TEST_F(FakeRmadClientTest, TransitionNextState_HasNextState_Ok) {
  std::vector<rmad::GetStateReply> fake_states;
  fake_states.push_back(
      rmad::GetStateReply(CreateWelcomeStateReply(rmad::RMAD_ERROR_OK)));
  fake_states.push_back(rmad::GetStateReply(
      CreateDeviceDestinationStateReply(rmad::RMAD_ERROR_OK)));
  fake_client_()->SetFakeStateReplies(std::move(fake_states));

  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->TransitionNextState(std::move(CreateWelcomeState()),
                               future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
  EXPECT_TRUE(response->has_state());
  EXPECT_TRUE(response->state().has_device_destination());
}

TEST_F(FakeRmadClientTest, TransitionNextState_WrongCurrentState_Invalid) {
  std::vector<rmad::GetStateReply> fake_states;
  fake_states.push_back(
      rmad::GetStateReply(CreateWelcomeStateReply(rmad::RMAD_ERROR_OK)));
  fake_states.push_back(rmad::GetStateReply(
      CreateDeviceDestinationStateReply(rmad::RMAD_ERROR_OK)));
  fake_client_()->SetFakeStateReplies(std::move(fake_states));

  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->TransitionNextState(std::move(CreateDeviceDestinationState()),
                               future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_REQUEST_INVALID);
  EXPECT_TRUE(response->has_state());
  EXPECT_TRUE(response->state().has_welcome());
}

TEST_F(FakeRmadClientTest, TransitionPreviousState_Default_RmaNotRequired) {
  base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
  client_->TransitionPreviousState(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_RMA_NOT_REQUIRED);
  EXPECT_FALSE(response->has_state());
}

TEST_F(FakeRmadClientTest, TransitionPreviousState_HasPreviousState_Ok) {
  std::vector<rmad::GetStateReply> fake_states;
  fake_states.push_back(
      rmad::GetStateReply(CreateWelcomeStateReply(rmad::RMAD_ERROR_OK)));
  fake_states.push_back(rmad::GetStateReply(
      CreateDeviceDestinationStateReply(rmad::RMAD_ERROR_OK)));
  fake_client_()->SetFakeStateReplies(std::move(fake_states));

  {
    base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
    client_->TransitionNextState(std::move(CreateWelcomeState()),
                                 future.GetCallback());
    const auto& response = future.Get();
    EXPECT_TRUE(response.has_value());
    EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
    EXPECT_TRUE(response->has_state());
    EXPECT_TRUE(response->state().has_device_destination());
  }
  {
    base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
    client_->TransitionPreviousState(future.GetCallback());
    const auto& response = future.Get();
    LOG(ERROR) << "Prev started";
    EXPECT_TRUE(response.has_value());
    EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
    EXPECT_TRUE(response->has_state());
    EXPECT_TRUE(response->state().has_welcome());
  }
}

TEST_F(FakeRmadClientTest,
       TransitionPreviousState_HasPreviousState_StateUpdated) {
  std::vector<rmad::GetStateReply> fake_states;
  fake_states.push_back(CreateWelcomeStateReply(rmad::RMAD_ERROR_OK));
  fake_states.push_back(rmad::GetStateReply(
      CreateDeviceDestinationStateReply(rmad::RMAD_ERROR_OK)));
  fake_client_()->SetFakeStateReplies(std::move(fake_states));

  {
    base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
    client_->GetCurrentState(future.GetCallback());
    EXPECT_TRUE(future.Get().has_value());
    EXPECT_EQ(future.Get()->error(), rmad::RMAD_ERROR_OK);
    EXPECT_TRUE(future.Get()->has_state());
    EXPECT_TRUE(future.Get()->state().has_welcome());
    EXPECT_EQ(future.Get()->state().welcome().choice(),
              rmad::WelcomeState_FinalizeChoice_RMAD_CHOICE_UNKNOWN);
  }
  {
    rmad::RmadState current_state = CreateWelcomeState();
    current_state.mutable_welcome()->set_choice(
        rmad::WelcomeState_FinalizeChoice_RMAD_CHOICE_FINALIZE_REPAIR);

    base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
    client_->TransitionNextState(std::move(current_state),
                                 future.GetCallback());
    EXPECT_TRUE(future.Get().has_value());
    EXPECT_EQ(future.Get()->error(), rmad::RMAD_ERROR_OK);
    EXPECT_TRUE(future.Get()->has_state());
    EXPECT_TRUE(future.Get()->state().has_device_destination());
  }
  {
    base::test::TestFuture<std::optional<rmad::GetStateReply>> future;
    client_->TransitionPreviousState(future.GetCallback());
    const auto& response = future.Get();
    LOG(ERROR) << "Prev started";
    EXPECT_TRUE(response.has_value());
    EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
    EXPECT_TRUE(response->has_state());
    EXPECT_TRUE(response->state().has_welcome());
    EXPECT_EQ(response->state().welcome().choice(),
              rmad::WelcomeState_FinalizeChoice_RMAD_CHOICE_FINALIZE_REPAIR);
  }
}

TEST_F(FakeRmadClientTest, Abortable_Default_Rma_Not_Required) {
  base::test::TestFuture<std::optional<rmad::AbortRmaReply>> future;
  client_->AbortRma(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_RMA_NOT_REQUIRED);
}

TEST_F(FakeRmadClientTest, Abortable_SetFalse_CannotCancel) {
  fake_client_()->SetAbortable(false);
  base::test::TestFuture<std::optional<rmad::AbortRmaReply>> future;
  client_->AbortRma(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_CANNOT_CANCEL_RMA);
}

TEST_F(FakeRmadClientTest, Abortable_SetTrue_Rma_Not_Required) {
  fake_client_()->SetAbortable(true);
  base::test::TestFuture<std::optional<rmad::AbortRmaReply>> future;
  client_->AbortRma(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_RMA_NOT_REQUIRED);
}

TEST_F(FakeRmadClientTest, GetLog) {
  const std::string expected_log = "This is my test log for the RMA process";
  fake_client_()->SetGetLogReply(expected_log, rmad::RMAD_ERROR_OK);
  base::test::TestFuture<std::optional<rmad::GetLogReply>> future;
  client_->GetLog(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->log(), expected_log);
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
}

TEST_F(FakeRmadClientTest, SaveLog) {
  const std::string expected_save_path = "fake save path for testing";
  fake_client_()->SetSaveLogReply(expected_save_path, rmad::RMAD_ERROR_OK);
  base::test::TestFuture<std::optional<rmad::SaveLogReply>> future;
  client_->SaveLog("Diagnostics log text", future.GetCallback());
  const auto& response = future.Get();

  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->save_path(), expected_save_path);
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
}

TEST_F(FakeRmadClientTest, RecordBrowserActionMetric) {
  fake_client_()->SetRecordBrowserActionMetricReply(rmad::RMAD_ERROR_OK);
  base::test::TestFuture<std::optional<rmad::RecordBrowserActionMetricReply>>
      future;

  rmad::RecordBrowserActionMetricRequest request;
  request.set_diagnostics(true);
  request.set_os_update(false);

  client_->RecordBrowserActionMetric(request, future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
}

TEST_F(FakeRmadClientTest, ExtractExternalDiagnosticsApp_NotFound) {
  base::test::TestFuture<
      std::optional<rmad::ExtractExternalDiagnosticsAppReply>>
      future;
  client_->ExtractExternalDiagnosticsApp(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_DIAGNOSTICS_APP_NOT_FOUND);
}

TEST_F(FakeRmadClientTest, ExtractExternalDiagnosticsApp_Found) {
  fake_client_()->external_diag_app_path() =
      base::FilePath{"/example/diag_app"};

  base::test::TestFuture<
      std::optional<rmad::ExtractExternalDiagnosticsAppReply>>
      future;
  client_->ExtractExternalDiagnosticsApp(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
  EXPECT_EQ(response->diagnostics_app_swbn_path(), "/example/diag_app.swbn");
  EXPECT_EQ(response->diagnostics_app_crx_path(), "/example/diag_app.crx");
}

TEST_F(FakeRmadClientTest, InstallExtractedDiagnosticsApp_NotFound) {
  base::test::TestFuture<
      std::optional<rmad::InstallExtractedDiagnosticsAppReply>>
      future;
  client_->InstallExtractedDiagnosticsApp(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_DIAGNOSTICS_APP_NOT_FOUND);
}

TEST_F(FakeRmadClientTest, InstallExtractedDiagnosticsApp_Found) {
  fake_client_()->external_diag_app_path() =
      base::FilePath{"/example/diag_app"};

  base::test::TestFuture<
      std::optional<rmad::InstallExtractedDiagnosticsAppReply>>
      future;
  client_->InstallExtractedDiagnosticsApp(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
  // The installed path was set so the next `GetInstalledDiagnosticsApp` will
  // get the installed package path.
  EXPECT_EQ(fake_client_()->installed_diag_app_path(),
            fake_client_()->external_diag_app_path());
}

TEST_F(FakeRmadClientTest, GetInstalledDiagnosticsApp_NotFound) {
  base::test::TestFuture<std::optional<rmad::GetInstalledDiagnosticsAppReply>>
      future;
  client_->GetInstalledDiagnosticsApp(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_DIAGNOSTICS_APP_NOT_FOUND);
}

TEST_F(FakeRmadClientTest, GetInstalledDiagnosticsApp_Found) {
  fake_client_()->installed_diag_app_path() =
      base::FilePath{"/example/diag_app"};

  base::test::TestFuture<std::optional<rmad::GetInstalledDiagnosticsAppReply>>
      future;
  client_->GetInstalledDiagnosticsApp(future.GetCallback());
  const auto& response = future.Get();
  EXPECT_TRUE(response.has_value());
  EXPECT_EQ(response->error(), rmad::RMAD_ERROR_OK);
  EXPECT_EQ(response->diagnostics_app_swbn_path(), "/example/diag_app.swbn");
  EXPECT_EQ(response->diagnostics_app_crx_path(), "/example/diag_app.crx");
}

// Tests that synchronous observers are notified about errors that occur outside
// of state transitions.
TEST_F(FakeRmadClientTest, ErrorObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerErrorObservation(
      rmad::RmadErrorCode::RMAD_ERROR_REIMAGING_UNKNOWN_FAILURE);
  EXPECT_EQ(observer_1.num_error(), 1);
  EXPECT_EQ(observer_1.last_error(),
            rmad::RmadErrorCode::RMAD_ERROR_REIMAGING_UNKNOWN_FAILURE);
}

// Tests that synchronous observers are notified about component calibration
// progress.
TEST_F(FakeRmadClientTest, CalibrationProgressObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerCalibrationProgressObservation(
      rmad::RmadComponent::RMAD_COMPONENT_LID_ACCELEROMETER,
      rmad::CalibrationComponentStatus::RMAD_CALIBRATION_IN_PROGRESS, 0.5);
  EXPECT_EQ(observer_1.num_calibration_progress(), 1);
  EXPECT_EQ(observer_1.last_calibration_component_status().component(),
            rmad::RmadComponent::RMAD_COMPONENT_LID_ACCELEROMETER);
  EXPECT_EQ(observer_1.last_calibration_component_status().status(),
            rmad::CalibrationComponentStatus::RMAD_CALIBRATION_IN_PROGRESS);
  EXPECT_EQ(observer_1.last_calibration_component_status().progress(), 0.5);
}

// Tests that synchronous observers are notified about overall calibration
// progress.
TEST_F(FakeRmadClientTest, CalibrationOverallProgressObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerCalibrationOverallProgressObservation(
      rmad::CalibrationOverallStatus::
          RMAD_CALIBRATION_OVERALL_CURRENT_ROUND_COMPLETE);
  EXPECT_EQ(observer_1.num_calibration_overall_progress(), 1);
  EXPECT_EQ(observer_1.last_calibration_overall_status(),
            rmad::CalibrationOverallStatus::
                RMAD_CALIBRATION_OVERALL_CURRENT_ROUND_COMPLETE);
}

// Tests that synchronous observers are notified about provisioning progress.
TEST_F(FakeRmadClientTest, ProvisioningProgressObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerProvisioningProgressObservation(
      rmad::ProvisionStatus::RMAD_PROVISION_STATUS_IN_PROGRESS, 0.25,
      rmad::ProvisionStatus::RMAD_PROVISION_ERROR_WP_ENABLED);
  EXPECT_EQ(observer_1.num_provisioning_progress(), 1);
  EXPECT_EQ(observer_1.last_provisioning_status().status(),
            rmad::ProvisionStatus::RMAD_PROVISION_STATUS_IN_PROGRESS);
  EXPECT_EQ(observer_1.last_provisioning_status().progress(), 0.25);
  EXPECT_EQ(observer_1.last_provisioning_status().error(),
            rmad::ProvisionStatus::RMAD_PROVISION_ERROR_WP_ENABLED);
}

// Tests that synchronous observers are notified about hardware write protection
// state.
TEST_F(FakeRmadClientTest, HardwareWriteProtectionStateObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerHardwareWriteProtectionStateObservation(false);
  EXPECT_EQ(observer_1.num_hardware_write_protection_state(), 1);
  EXPECT_FALSE(observer_1.last_hardware_write_protection_state());

  fake_client_()->TriggerHardwareWriteProtectionStateObservation(true);
  EXPECT_EQ(observer_1.num_hardware_write_protection_state(), 2);
  EXPECT_TRUE(observer_1.last_hardware_write_protection_state());
}

// Tests that synchronous observers are notified about power cable state.
TEST_F(FakeRmadClientTest, PowerCableStateObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerPowerCableStateObservation(false);
  EXPECT_EQ(observer_1.num_power_cable_state(), 1);
  EXPECT_FALSE(observer_1.last_power_cable_state());

  fake_client_()->TriggerPowerCableStateObservation(true);
  EXPECT_EQ(observer_1.num_power_cable_state(), 2);
  EXPECT_TRUE(observer_1.last_power_cable_state());
}

// Tests that synchronous observers are notified about external disk state.
TEST_F(FakeRmadClientTest, ExternalDiskStateObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerExternalDiskStateObservation(false);
  EXPECT_EQ(observer_1.num_external_disk_state(), 1);
  EXPECT_FALSE(observer_1.last_external_disk_state());

  fake_client_()->TriggerExternalDiskStateObservation(true);
  EXPECT_EQ(observer_1.num_external_disk_state(), 2);
  EXPECT_TRUE(observer_1.last_external_disk_state());
}

// Tests that synchronous observers are notified about hardware verification
// status.
TEST_F(FakeRmadClientTest, HardwareVerificationResultObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerHardwareVerificationResultObservation(
      false, "fatal error", false);
  EXPECT_EQ(observer_1.num_hardware_verification_result(), 1);
  EXPECT_FALSE(observer_1.last_hardware_verification_result().is_compliant());
  EXPECT_EQ(observer_1.last_hardware_verification_result().error_str(),
            "fatal error");
  EXPECT_FALSE(observer_1.last_hardware_verification_result().is_skipped());

  fake_client_()->TriggerHardwareVerificationResultObservation(true, "ok",
                                                               true);
  EXPECT_EQ(observer_1.num_hardware_verification_result(), 2);
  EXPECT_TRUE(observer_1.last_hardware_verification_result().is_compliant());
  EXPECT_EQ(observer_1.last_hardware_verification_result().error_str(), "ok");
  EXPECT_TRUE(observer_1.last_hardware_verification_result().is_skipped());
}

// Tests that synchronous observers are notified about ro firmware update
// progress.
TEST_F(FakeRmadClientTest, RoFirmwareUpdateProgressObservation) {
  TestObserver observer_1(client_);

  fake_client_()->TriggerRoFirmwareUpdateProgressObservation(
      rmad::UpdateRoFirmwareStatus::RMAD_UPDATE_RO_FIRMWARE_UPDATING);
  EXPECT_EQ(observer_1.num_ro_firmware_update_progress(), 1);
  EXPECT_EQ(observer_1.last_ro_firmware_update_progress(),
            rmad::UpdateRoFirmwareStatus::RMAD_UPDATE_RO_FIRMWARE_UPDATING);

  fake_client_()->TriggerRoFirmwareUpdateProgressObservation(
      rmad::UpdateRoFirmwareStatus::RMAD_UPDATE_RO_FIRMWARE_REBOOTING);
  EXPECT_EQ(observer_1.num_ro_firmware_update_progress(), 2);
  EXPECT_EQ(observer_1.last_ro_firmware_update_progress(),
            rmad::UpdateRoFirmwareStatus::RMAD_UPDATE_RO_FIRMWARE_REBOOTING);
}

}  // namespace
}  // namespace ash