cellular_setup_service_unittest.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2019 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 <memory>
  5. #include <utility>
  6. #include "ash/services/cellular_setup/cellular_setup_impl.h"
  7. #include "ash/services/cellular_setup/public/cpp/fake_activation_delegate.h"
  8. #include "ash/services/cellular_setup/public/cpp/fake_carrier_portal_handler.h"
  9. #include "ash/services/cellular_setup/public/cpp/fake_cellular_setup.h"
  10. #include "ash/services/cellular_setup/public/mojom/cellular_setup.mojom.h"
  11. #include "base/bind.h"
  12. #include "base/callback_helpers.h"
  13. #include "base/run_loop.h"
  14. #include "base/test/task_environment.h"
  15. #include "mojo/public/cpp/bindings/pending_remote.h"
  16. #include "mojo/public/cpp/bindings/remote.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace ash::cellular_setup {
  20. namespace {
  21. using CarrierPortalHandlerPair =
  22. std::pair<mojo::Remote<mojom::CarrierPortalHandler>,
  23. FakeCarrierPortalHandler*>;
  24. const char kTestPaymentUrl[] = "testPaymentUrl";
  25. const char kTestPaymentPostData[] = "testPaymentPostData";
  26. const char kTestCarrier[] = "testCarrier";
  27. const char kTestMeid[] = "testMeid";
  28. const char kTestImei[] = "testImei";
  29. const char kTestMdn[] = "testMdn";
  30. } // namespace
  31. class CellularSetupServiceTest : public testing::Test {
  32. public:
  33. CellularSetupServiceTest(const CellularSetupServiceTest&) = delete;
  34. CellularSetupServiceTest& operator=(const CellularSetupServiceTest&) = delete;
  35. protected:
  36. CellularSetupServiceTest() = default;
  37. ~CellularSetupServiceTest() override = default;
  38. // testing::Test:
  39. void SetUp() override {
  40. service_ = std::make_unique<FakeCellularSetup>();
  41. service_->BindReceiver(cellular_setup_remote_.BindNewPipeAndPassReceiver());
  42. cellular_setup_remote_.FlushForTesting();
  43. }
  44. // Calls StartActivation() and returns the fake CarrierPortalHandler and its
  45. // associated mojo::Remote<>.
  46. CarrierPortalHandlerPair CallStartActivation(
  47. FakeActivationDelegate* fake_activation_delegate) {
  48. std::vector<std::unique_ptr<FakeCellularSetup::StartActivationInvocation>>&
  49. start_activation_invocations =
  50. fake_cellular_setup()->start_activation_invocations();
  51. size_t num_args_before_call = start_activation_invocations.size();
  52. base::RunLoop run_loop;
  53. // Make StartActivation() call and propagate it to the service.
  54. cellular_setup_remote_->StartActivation(
  55. fake_activation_delegate->GenerateRemote(),
  56. base::BindOnce(&CellularSetupServiceTest::OnStartActivationResult,
  57. base::Unretained(this), run_loop.QuitClosure()));
  58. cellular_setup_remote_.FlushForTesting();
  59. // Verify that the call was made successfully.
  60. EXPECT_EQ(num_args_before_call + 1u, start_activation_invocations.size());
  61. // Execute the callback and retrieve the returned CarrierPortalHandler.
  62. FakeCarrierPortalHandler* fake_carrier_portal_observer =
  63. start_activation_invocations.back()->ExecuteCallback();
  64. run_loop.RunUntilIdle();
  65. EXPECT_TRUE(last_carrier_portal_observer_);
  66. CarrierPortalHandlerPair observer_pair = std::make_pair(
  67. std::move(last_carrier_portal_observer_), fake_carrier_portal_observer);
  68. last_carrier_portal_observer_.reset();
  69. return observer_pair;
  70. }
  71. // Calls OnActivationStarted() for the provided ActivationDelegate, passing
  72. // test metadata to represent the device. |fake_activation_delegate| must
  73. // correspond to the delegate provided to the most recent call to
  74. // CallStartActivation().
  75. void NotifyLastDelegateThatActivationStarted(
  76. FakeActivationDelegate* fake_activation_delegate) {
  77. const std::vector<mojom::CellularMetadataPtr>& cellular_metadata_list =
  78. fake_activation_delegate->cellular_metadata_list();
  79. size_t num_elements_before_call = cellular_metadata_list.size();
  80. GetLastActivationDelegate()->OnActivationStarted(
  81. mojom::CellularMetadata::New(GURL(kTestPaymentUrl),
  82. kTestPaymentPostData, kTestCarrier,
  83. kTestMeid, kTestImei, kTestMdn));
  84. GetLastActivationDelegate().FlushForTesting();
  85. ASSERT_EQ(num_elements_before_call + 1u, cellular_metadata_list.size());
  86. EXPECT_EQ(GURL(kTestPaymentUrl),
  87. cellular_metadata_list.back()->payment_url);
  88. EXPECT_EQ(kTestCarrier, cellular_metadata_list.back()->carrier);
  89. EXPECT_EQ(kTestMeid, cellular_metadata_list.back()->meid);
  90. EXPECT_EQ(kTestImei, cellular_metadata_list.back()->imei);
  91. EXPECT_EQ(kTestMdn, cellular_metadata_list.back()->mdn);
  92. }
  93. // Calls OnActivationFinished() for the provided ActivationDelegate, passing
  94. // |activation_result| to the callback. |fake_activation_delegate| must
  95. // correspond to the delegate provided to the most recent call to
  96. // CallStartActivation().
  97. void NotifyLastDelegateThatActivationFinished(
  98. mojom::ActivationResult activation_result,
  99. FakeActivationDelegate* fake_activation_delegate) {
  100. const std::vector<mojom::ActivationResult>& activation_results =
  101. fake_activation_delegate->activation_results();
  102. size_t num_results_before_call = activation_results.size();
  103. GetLastActivationDelegate()->OnActivationFinished(activation_result);
  104. GetLastActivationDelegate().FlushForTesting();
  105. ASSERT_EQ(num_results_before_call + 1u, activation_results.size());
  106. EXPECT_EQ(activation_result, activation_results.back());
  107. }
  108. // Calls OnCarrierPortalStatusChanged() for the provided
  109. // CarrierPortalStatusObserver and verifies that the status was received.
  110. void SendCarrierPortalStatusUpdate(
  111. mojom::CarrierPortalStatus carrier_portal_status,
  112. CarrierPortalHandlerPair* pair) {
  113. const std::vector<mojom::CarrierPortalStatus>& status_updates =
  114. pair->second->status_updates();
  115. size_t num_updates_before_call = status_updates.size();
  116. pair->first->OnCarrierPortalStatusChange(carrier_portal_status);
  117. pair->first.FlushForTesting();
  118. ASSERT_EQ(num_updates_before_call + 1u, status_updates.size());
  119. EXPECT_EQ(carrier_portal_status, status_updates.back());
  120. }
  121. private:
  122. void OnStartActivationResult(base::OnceClosure quit_closure,
  123. mojo::PendingRemote<mojom::CarrierPortalHandler>
  124. carrier_portal_observer) {
  125. EXPECT_FALSE(last_carrier_portal_observer_);
  126. last_carrier_portal_observer_.Bind(std::move(carrier_portal_observer));
  127. std::move(quit_closure).Run();
  128. }
  129. FakeCellularSetup* fake_cellular_setup() { return service_.get(); }
  130. mojo::Remote<mojom::ActivationDelegate>& GetLastActivationDelegate() {
  131. return fake_cellular_setup()
  132. ->start_activation_invocations()
  133. .back()
  134. ->activation_delegate();
  135. }
  136. base::test::TaskEnvironment task_environment_;
  137. std::unique_ptr<FakeCellularSetup> service_;
  138. mojo::Remote<mojom::CarrierPortalHandler> last_carrier_portal_observer_;
  139. mojo::Remote<mojom::CellularSetup> cellular_setup_remote_;
  140. };
  141. TEST_F(CellularSetupServiceTest, StartActivation_Success) {
  142. auto fake_activation_delegate = std::make_unique<FakeActivationDelegate>();
  143. CarrierPortalHandlerPair pair =
  144. CallStartActivation(fake_activation_delegate.get());
  145. NotifyLastDelegateThatActivationStarted(fake_activation_delegate.get());
  146. SendCarrierPortalStatusUpdate(
  147. mojom::CarrierPortalStatus::kPortalLoadedWithoutPaidUser, &pair);
  148. SendCarrierPortalStatusUpdate(
  149. mojom::CarrierPortalStatus::kPortalLoadedAndUserCompletedPayment, &pair);
  150. NotifyLastDelegateThatActivationFinished(
  151. mojom::ActivationResult::kSuccessfullyStartedActivation,
  152. fake_activation_delegate.get());
  153. }
  154. TEST_F(CellularSetupServiceTest, StartActivation_PortalFailsToLoad) {
  155. auto fake_activation_delegate = std::make_unique<FakeActivationDelegate>();
  156. CarrierPortalHandlerPair pair =
  157. CallStartActivation(fake_activation_delegate.get());
  158. NotifyLastDelegateThatActivationStarted(fake_activation_delegate.get());
  159. SendCarrierPortalStatusUpdate(mojom::CarrierPortalStatus::kPortalFailedToLoad,
  160. &pair);
  161. NotifyLastDelegateThatActivationFinished(
  162. mojom::ActivationResult::kFailedToActivate,
  163. fake_activation_delegate.get());
  164. }
  165. TEST_F(CellularSetupServiceTest, StartActivation_ErrorDuringPayment) {
  166. auto fake_activation_delegate = std::make_unique<FakeActivationDelegate>();
  167. CarrierPortalHandlerPair pair =
  168. CallStartActivation(fake_activation_delegate.get());
  169. NotifyLastDelegateThatActivationStarted(fake_activation_delegate.get());
  170. SendCarrierPortalStatusUpdate(
  171. mojom::CarrierPortalStatus::kPortalLoadedWithoutPaidUser, &pair);
  172. SendCarrierPortalStatusUpdate(
  173. mojom::CarrierPortalStatus::kPortalLoadedButErrorOccurredDuringPayment,
  174. &pair);
  175. NotifyLastDelegateThatActivationFinished(
  176. mojom::ActivationResult::kFailedToActivate,
  177. fake_activation_delegate.get());
  178. }
  179. } // namespace ash::cellular_setup