invalid_connection_disconnector_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2020 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. #include "ash/components/phonehub/invalid_connection_disconnector.h"
  5. #include "ash/components/phonehub/mutable_phone_model.h"
  6. #include "ash/components/phonehub/phone_model_test_util.h"
  7. #include "ash/services/secure_channel/public/cpp/client/fake_connection_manager.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "base/timer/mock_timer.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace ash {
  12. namespace phonehub {
  13. class InvalidConnectionDisconnectorTest : public testing::Test {
  14. public:
  15. InvalidConnectionDisconnectorTest() = default;
  16. InvalidConnectionDisconnectorTest(const InvalidConnectionDisconnectorTest&) =
  17. delete;
  18. InvalidConnectionDisconnectorTest& operator=(
  19. const InvalidConnectionDisconnectorTest&) = delete;
  20. ~InvalidConnectionDisconnectorTest() override = default;
  21. // testing::Test:
  22. void SetUp() override {
  23. fake_connection_manager_ =
  24. std::make_unique<secure_channel::FakeConnectionManager>();
  25. auto timer = std::make_unique<base::MockOneShotTimer>();
  26. timer_ = timer.get();
  27. invalid_connection_disconnector_ =
  28. base::WrapUnique(new InvalidConnectionDisconnector(
  29. fake_connection_manager_.get(), &fake_phone_model_,
  30. std::move(timer)));
  31. }
  32. void FireTimer() { timer_->Fire(); }
  33. size_t num_disconnect_calls() const {
  34. return fake_connection_manager_->num_disconnect_calls();
  35. }
  36. void SetPhoneConnected() {
  37. fake_connection_manager_->SetStatus(
  38. secure_channel::ConnectionManager::Status::kConnected);
  39. }
  40. void SetPhoneDisconnected() {
  41. fake_connection_manager_->SetStatus(
  42. secure_channel::ConnectionManager::Status::kDisconnected);
  43. }
  44. void ClearPhoneModel() {
  45. fake_phone_model_.SetPhoneStatusModel(absl::nullopt);
  46. }
  47. void SetPhoneModel() {
  48. fake_phone_model_.SetPhoneStatusModel(CreateFakePhoneStatusModel());
  49. }
  50. bool IsTimerRunning() const { return timer_->IsRunning(); }
  51. private:
  52. std::unique_ptr<secure_channel::FakeConnectionManager>
  53. fake_connection_manager_;
  54. MutablePhoneModel fake_phone_model_;
  55. base::MockOneShotTimer* timer_;
  56. std::unique_ptr<InvalidConnectionDisconnector>
  57. invalid_connection_disconnector_;
  58. };
  59. TEST_F(InvalidConnectionDisconnectorTest, DisconnectFlows) {
  60. // A disconnection should occur when the phone becomes connected and phone
  61. // status model is empty after grace period.
  62. ClearPhoneModel();
  63. EXPECT_FALSE(IsTimerRunning());
  64. SetPhoneConnected();
  65. EXPECT_TRUE(IsTimerRunning());
  66. FireTimer();
  67. EXPECT_EQ(num_disconnect_calls(), 1U);
  68. // No disconnection should occur when the phone becomes connected and phone
  69. // status model is non-empty before grace period.
  70. EXPECT_FALSE(IsTimerRunning());
  71. SetPhoneConnected();
  72. EXPECT_TRUE(IsTimerRunning());
  73. SetPhoneModel();
  74. FireTimer();
  75. EXPECT_EQ(num_disconnect_calls(), 1U);
  76. // A change in connection status while the timer is running will cause the
  77. // timer to stop.
  78. ClearPhoneModel();
  79. SetPhoneDisconnected();
  80. EXPECT_FALSE(IsTimerRunning());
  81. SetPhoneConnected();
  82. EXPECT_TRUE(IsTimerRunning());
  83. SetPhoneDisconnected();
  84. EXPECT_FALSE(IsTimerRunning());
  85. }
  86. } // namespace phonehub
  87. } // namespace ash