resource_request_allowed_notifier_unittest.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2013 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 "base/memory/ptr_util.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include "base/run_loop.h"
  7. #include "base/test/task_environment.h"
  8. #include "components/prefs/testing_pref_service.h"
  9. #include "components/web_resource/eula_accepted_notifier.h"
  10. #include "components/web_resource/resource_request_allowed_notifier_test_util.h"
  11. #include "services/network/test/test_network_connection_tracker.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace web_resource {
  14. // EulaAcceptedNotifier test class that allows mocking the EULA accepted state
  15. // and issuing simulated notifications.
  16. class TestEulaAcceptedNotifier : public EulaAcceptedNotifier {
  17. public:
  18. TestEulaAcceptedNotifier()
  19. : EulaAcceptedNotifier(nullptr),
  20. eula_accepted_(false) {
  21. }
  22. TestEulaAcceptedNotifier(const TestEulaAcceptedNotifier&) = delete;
  23. TestEulaAcceptedNotifier& operator=(const TestEulaAcceptedNotifier&) = delete;
  24. ~TestEulaAcceptedNotifier() override {}
  25. bool IsEulaAccepted() override { return eula_accepted_; }
  26. void SetEulaAcceptedForTesting(bool eula_accepted) {
  27. eula_accepted_ = eula_accepted;
  28. }
  29. void SimulateEulaAccepted() {
  30. NotifyObserver();
  31. }
  32. private:
  33. bool eula_accepted_;
  34. };
  35. enum class ConnectionTrackerResponseMode {
  36. kSynchronous,
  37. kAsynchronous,
  38. };
  39. // A test fixture class for ResourceRequestAllowedNotifier tests that require
  40. // network state simulations. This also acts as the service implementing the
  41. // ResourceRequestAllowedNotifier::Observer interface.
  42. class ResourceRequestAllowedNotifierTest
  43. : public testing::Test,
  44. public ResourceRequestAllowedNotifier::Observer,
  45. public testing::WithParamInterface<ConnectionTrackerResponseMode> {
  46. public:
  47. ResourceRequestAllowedNotifierTest()
  48. : resource_request_allowed_notifier_(
  49. &prefs_,
  50. network::TestNetworkConnectionTracker::GetInstance()),
  51. eula_notifier_(new TestEulaAcceptedNotifier),
  52. was_notified_(false) {
  53. auto* tracker = network::TestNetworkConnectionTracker::GetInstance();
  54. tracker->SetRespondSynchronously(
  55. GetParam() == ConnectionTrackerResponseMode::kSynchronous);
  56. tracker->SetConnectionType(network::mojom::ConnectionType::CONNECTION_WIFI);
  57. resource_request_allowed_notifier_.InitWithEulaAcceptNotifier(
  58. this, base::WrapUnique(eula_notifier_.get()));
  59. }
  60. ResourceRequestAllowedNotifierTest(
  61. const ResourceRequestAllowedNotifierTest&) = delete;
  62. ResourceRequestAllowedNotifierTest& operator=(
  63. const ResourceRequestAllowedNotifierTest&) = delete;
  64. ~ResourceRequestAllowedNotifierTest() override {}
  65. bool was_notified() const { return was_notified_; }
  66. // ResourceRequestAllowedNotifier::Observer override:
  67. void OnResourceRequestsAllowed() override { was_notified_ = true; }
  68. void SimulateNetworkConnectionChange(network::mojom::ConnectionType type) {
  69. network::TestNetworkConnectionTracker::GetInstance()->SetConnectionType(
  70. type);
  71. base::RunLoop().RunUntilIdle();
  72. }
  73. // Simulate a resource request from the test service. It returns true if
  74. // resource request is allowed. Otherwise returns false and will change the
  75. // result of was_notified() to true when the request is allowed.
  76. bool SimulateResourceRequest() {
  77. return resource_request_allowed_notifier_.ResourceRequestsAllowed();
  78. }
  79. void SimulateEulaAccepted() {
  80. eula_notifier_->SimulateEulaAccepted();
  81. }
  82. // Eula manipulation methods:
  83. void SetNeedsEulaAcceptance(bool needs_acceptance) {
  84. eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance);
  85. }
  86. void SetWaitingForEula(bool waiting) {
  87. resource_request_allowed_notifier_.SetWaitingForEulaForTesting(waiting);
  88. }
  89. // Used in tests involving the EULA. Disables both the EULA accepted state
  90. // and the network.
  91. void DisableEulaAndNetwork() {
  92. SimulateNetworkConnectionChange(
  93. network::mojom::ConnectionType::CONNECTION_NONE);
  94. SetWaitingForEula(true);
  95. SetNeedsEulaAcceptance(true);
  96. }
  97. void SetUp() override {
  98. // Assume the test service has already requested permission, as all tests
  99. // just test that criteria changes notify the server.
  100. // Set default EULA state to done (not waiting and EULA accepted) to
  101. // simplify non-ChromeOS tests.
  102. SetWaitingForEula(false);
  103. SetNeedsEulaAcceptance(false);
  104. }
  105. private:
  106. base::test::SingleThreadTaskEnvironment task_environment_{
  107. base::test::SingleThreadTaskEnvironment::MainThreadType::UI};
  108. TestRequestAllowedNotifier resource_request_allowed_notifier_;
  109. TestingPrefServiceSimple prefs_;
  110. raw_ptr<TestEulaAcceptedNotifier> eula_notifier_; // Weak, owned by RRAN.
  111. bool was_notified_;
  112. };
  113. TEST_P(ResourceRequestAllowedNotifierTest, NotifyOnInitialNetworkState) {
  114. if (GetParam() == ConnectionTrackerResponseMode::kSynchronous) {
  115. EXPECT_TRUE(SimulateResourceRequest());
  116. } else {
  117. EXPECT_FALSE(SimulateResourceRequest());
  118. base::RunLoop().RunUntilIdle();
  119. EXPECT_TRUE(was_notified());
  120. }
  121. }
  122. TEST_P(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) {
  123. SimulateNetworkConnectionChange(
  124. network::mojom::ConnectionType::CONNECTION_NONE);
  125. EXPECT_FALSE(SimulateResourceRequest());
  126. SimulateNetworkConnectionChange(
  127. network::mojom::ConnectionType::CONNECTION_NONE);
  128. EXPECT_FALSE(was_notified());
  129. }
  130. TEST_P(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) {
  131. SimulateNetworkConnectionChange(
  132. network::mojom::ConnectionType::CONNECTION_WIFI);
  133. EXPECT_TRUE(SimulateResourceRequest());
  134. SimulateNetworkConnectionChange(
  135. network::mojom::ConnectionType::CONNECTION_ETHERNET);
  136. EXPECT_FALSE(was_notified());
  137. }
  138. TEST_P(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) {
  139. SimulateNetworkConnectionChange(
  140. network::mojom::ConnectionType::CONNECTION_NONE);
  141. EXPECT_FALSE(SimulateResourceRequest());
  142. SimulateNetworkConnectionChange(
  143. network::mojom::ConnectionType::CONNECTION_ETHERNET);
  144. EXPECT_TRUE(was_notified());
  145. }
  146. TEST_P(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) {
  147. SimulateNetworkConnectionChange(
  148. network::mojom::ConnectionType::CONNECTION_WIFI);
  149. EXPECT_TRUE(SimulateResourceRequest());
  150. SimulateNetworkConnectionChange(
  151. network::mojom::ConnectionType::CONNECTION_WIFI);
  152. EXPECT_FALSE(was_notified());
  153. SimulateNetworkConnectionChange(
  154. network::mojom::ConnectionType::CONNECTION_3G);
  155. EXPECT_FALSE(was_notified());
  156. SimulateNetworkConnectionChange(
  157. network::mojom::ConnectionType::CONNECTION_4G);
  158. EXPECT_FALSE(was_notified());
  159. SimulateNetworkConnectionChange(
  160. network::mojom::ConnectionType::CONNECTION_WIFI);
  161. EXPECT_FALSE(was_notified());
  162. }
  163. TEST_P(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) {
  164. SimulateNetworkConnectionChange(
  165. network::mojom::ConnectionType::CONNECTION_WIFI);
  166. EXPECT_TRUE(SimulateResourceRequest());
  167. SimulateNetworkConnectionChange(
  168. network::mojom::ConnectionType::CONNECTION_WIFI);
  169. EXPECT_FALSE(was_notified());
  170. SimulateNetworkConnectionChange(
  171. network::mojom::ConnectionType::CONNECTION_NONE);
  172. EXPECT_FALSE(was_notified());
  173. SimulateNetworkConnectionChange(
  174. network::mojom::ConnectionType::CONNECTION_WIFI);
  175. EXPECT_FALSE(was_notified());
  176. }
  177. TEST_P(ResourceRequestAllowedNotifierTest, NotifyOnFlakyConnection) {
  178. // First, the observer queries the state while the network is connected.
  179. SimulateNetworkConnectionChange(
  180. network::mojom::ConnectionType::CONNECTION_WIFI);
  181. EXPECT_TRUE(SimulateResourceRequest());
  182. SimulateNetworkConnectionChange(
  183. network::mojom::ConnectionType::CONNECTION_WIFI);
  184. EXPECT_FALSE(was_notified());
  185. SimulateNetworkConnectionChange(
  186. network::mojom::ConnectionType::CONNECTION_NONE);
  187. EXPECT_FALSE(was_notified());
  188. // Now, the observer queries the state while the network is disconnected.
  189. EXPECT_FALSE(SimulateResourceRequest());
  190. SimulateNetworkConnectionChange(
  191. network::mojom::ConnectionType::CONNECTION_WIFI);
  192. EXPECT_TRUE(was_notified());
  193. }
  194. TEST_P(ResourceRequestAllowedNotifierTest, NoNotifyOnEulaAfterGoOffline) {
  195. DisableEulaAndNetwork();
  196. EXPECT_FALSE(SimulateResourceRequest());
  197. SimulateNetworkConnectionChange(
  198. network::mojom::ConnectionType::CONNECTION_WIFI);
  199. EXPECT_FALSE(was_notified());
  200. SimulateNetworkConnectionChange(
  201. network::mojom::ConnectionType::CONNECTION_NONE);
  202. EXPECT_FALSE(was_notified());
  203. SimulateEulaAccepted();
  204. EXPECT_FALSE(was_notified());
  205. }
  206. TEST_P(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) {
  207. // Ensure that if the observing service does not request access, it does not
  208. // get notified, even if the criteria are met. Note that this is done by not
  209. // calling SimulateResourceRequest here.
  210. SimulateNetworkConnectionChange(
  211. network::mojom::ConnectionType::CONNECTION_NONE);
  212. SimulateNetworkConnectionChange(
  213. network::mojom::ConnectionType::CONNECTION_ETHERNET);
  214. EXPECT_FALSE(was_notified());
  215. }
  216. TEST_P(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) {
  217. DisableEulaAndNetwork();
  218. EXPECT_FALSE(SimulateResourceRequest());
  219. SimulateEulaAccepted();
  220. EXPECT_FALSE(was_notified());
  221. }
  222. TEST_P(ResourceRequestAllowedNotifierTest, EulaFirst) {
  223. DisableEulaAndNetwork();
  224. EXPECT_FALSE(SimulateResourceRequest());
  225. SimulateEulaAccepted();
  226. EXPECT_FALSE(was_notified());
  227. SimulateNetworkConnectionChange(
  228. network::mojom::ConnectionType::CONNECTION_WIFI);
  229. EXPECT_TRUE(was_notified());
  230. }
  231. TEST_P(ResourceRequestAllowedNotifierTest, NetworkFirst) {
  232. DisableEulaAndNetwork();
  233. EXPECT_FALSE(SimulateResourceRequest());
  234. SimulateNetworkConnectionChange(
  235. network::mojom::ConnectionType::CONNECTION_WIFI);
  236. EXPECT_FALSE(was_notified());
  237. SimulateEulaAccepted();
  238. EXPECT_TRUE(was_notified());
  239. }
  240. TEST_P(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) {
  241. // Ensure that if the observing service does not request access, it does not
  242. // get notified, even if the criteria are met. Note that this is done by not
  243. // calling SimulateResourceRequest here.
  244. DisableEulaAndNetwork();
  245. SimulateNetworkConnectionChange(
  246. network::mojom::ConnectionType::CONNECTION_WIFI);
  247. EXPECT_FALSE(was_notified());
  248. SimulateEulaAccepted();
  249. EXPECT_FALSE(was_notified());
  250. }
  251. INSTANTIATE_TEST_SUITE_P(
  252. All,
  253. ResourceRequestAllowedNotifierTest,
  254. testing::Values(ConnectionTrackerResponseMode::kSynchronous,
  255. ConnectionTrackerResponseMode::kAsynchronous));
  256. } // namespace web_resource