web_state_observer_bridge_unittest.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright 2017 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. #import "ios/web/public/web_state_observer_bridge.h"
  5. #include "base/memory/ptr_util.h"
  6. #include "base/scoped_observation.h"
  7. #import "ios/web/navigation/navigation_context_impl.h"
  8. #include "ios/web/public/favicon/favicon_url.h"
  9. #import "ios/web/public/test/fakes/crw_fake_web_state_observer.h"
  10. #import "ios/web/public/test/fakes/fake_web_state.h"
  11. #include "net/http/http_response_headers.h"
  12. #include "testing/platform_test.h"
  13. #if !defined(__has_feature) || !__has_feature(objc_arc)
  14. #error "This file requires ARC support."
  15. #endif
  16. namespace web {
  17. namespace {
  18. const char kRawResponseHeaders[] =
  19. "HTTP/1.1 200 OK\0"
  20. "Content-Length: 450\0"
  21. "Connection: keep-alive\0";
  22. } // namespace
  23. // Test fixture to test WebStateObserverBridge class.
  24. class WebStateObserverBridgeTest : public PlatformTest {
  25. protected:
  26. WebStateObserverBridgeTest()
  27. : observer_([[CRWFakeWebStateObserver alloc] init]),
  28. observer_bridge_(observer_),
  29. response_headers_(new net::HttpResponseHeaders(
  30. std::string(kRawResponseHeaders, sizeof(kRawResponseHeaders)))) {
  31. scoped_observation_.Observe(&fake_web_state_);
  32. }
  33. web::FakeWebState fake_web_state_;
  34. CRWFakeWebStateObserver* observer_;
  35. WebStateObserverBridge observer_bridge_;
  36. base::ScopedObservation<WebState, WebStateObserver> scoped_observation_{
  37. &observer_bridge_};
  38. scoped_refptr<net::HttpResponseHeaders> response_headers_;
  39. };
  40. // Tests |webStateWasShown:| forwarding.
  41. TEST_F(WebStateObserverBridgeTest, WasShown) {
  42. ASSERT_FALSE([observer_ wasShownInfo]);
  43. observer_bridge_.WasShown(&fake_web_state_);
  44. ASSERT_TRUE([observer_ wasShownInfo]);
  45. EXPECT_EQ(&fake_web_state_, [observer_ wasShownInfo]->web_state);
  46. }
  47. // Tests |webStateWasHidden:| forwarding.
  48. TEST_F(WebStateObserverBridgeTest, WasHidden) {
  49. ASSERT_FALSE([observer_ wasHiddenInfo]);
  50. observer_bridge_.WasHidden(&fake_web_state_);
  51. ASSERT_TRUE([observer_ wasHiddenInfo]);
  52. EXPECT_EQ(&fake_web_state_, [observer_ wasHiddenInfo]->web_state);
  53. }
  54. // Tests |webState:didStartNavigation:| forwarding.
  55. TEST_F(WebStateObserverBridgeTest, DidStartNavigation) {
  56. ASSERT_FALSE([observer_ didStartNavigationInfo]);
  57. GURL url("https://chromium.test/");
  58. std::unique_ptr<web::NavigationContext> context =
  59. web::NavigationContextImpl::CreateNavigationContext(
  60. &fake_web_state_, url, /*has_user_gesture=*/true,
  61. ui::PageTransition::PAGE_TRANSITION_FORWARD_BACK,
  62. /*is_renderer_initiated=*/false);
  63. observer_bridge_.DidStartNavigation(&fake_web_state_, context.get());
  64. ASSERT_TRUE([observer_ didStartNavigationInfo]);
  65. EXPECT_EQ(&fake_web_state_, [observer_ didStartNavigationInfo]->web_state);
  66. web::NavigationContext* actual_context =
  67. [observer_ didStartNavigationInfo]->context.get();
  68. ASSERT_TRUE(actual_context);
  69. EXPECT_EQ(&fake_web_state_, actual_context->GetWebState());
  70. EXPECT_EQ(context->IsSameDocument(), actual_context->IsSameDocument());
  71. EXPECT_EQ(context->GetError(), actual_context->GetError());
  72. EXPECT_EQ(context->GetUrl(), actual_context->GetUrl());
  73. EXPECT_EQ(context->HasUserGesture(), actual_context->HasUserGesture());
  74. EXPECT_TRUE(PageTransitionTypeIncludingQualifiersIs(
  75. ui::PageTransition::PAGE_TRANSITION_FORWARD_BACK,
  76. actual_context->GetPageTransition()));
  77. EXPECT_EQ(context->GetResponseHeaders(),
  78. actual_context->GetResponseHeaders());
  79. }
  80. // Tests |webState:didRedirectNavigation:| forwarding.
  81. TEST_F(WebStateObserverBridgeTest, DidRedirectNavigation) {
  82. ASSERT_FALSE([observer_ didRedirectNavigationInfo]);
  83. GURL url("https://chromium.test/");
  84. std::unique_ptr<web::NavigationContext> context =
  85. web::NavigationContextImpl::CreateNavigationContext(
  86. &fake_web_state_, url, /*has_user_gesture=*/true,
  87. ui::PageTransition::PAGE_TRANSITION_FORWARD_BACK,
  88. /*is_renderer_initiated=*/false);
  89. observer_bridge_.DidRedirectNavigation(&fake_web_state_, context.get());
  90. ASSERT_TRUE([observer_ didRedirectNavigationInfo]);
  91. EXPECT_EQ(&fake_web_state_, [observer_ didRedirectNavigationInfo]->web_state);
  92. web::NavigationContext* actual_context =
  93. [observer_ didRedirectNavigationInfo]->context.get();
  94. ASSERT_TRUE(actual_context);
  95. EXPECT_EQ(&fake_web_state_, actual_context->GetWebState());
  96. EXPECT_EQ(context->IsSameDocument(), actual_context->IsSameDocument());
  97. EXPECT_EQ(context->GetError(), actual_context->GetError());
  98. EXPECT_EQ(context->GetUrl(), actual_context->GetUrl());
  99. EXPECT_EQ(context->HasUserGesture(), actual_context->HasUserGesture());
  100. EXPECT_TRUE(PageTransitionTypeIncludingQualifiersIs(
  101. ui::PageTransition::PAGE_TRANSITION_FORWARD_BACK,
  102. actual_context->GetPageTransition()));
  103. EXPECT_EQ(context->GetResponseHeaders(),
  104. actual_context->GetResponseHeaders());
  105. }
  106. // Tests |webState:didFinishNavigation:| forwarding.
  107. TEST_F(WebStateObserverBridgeTest, DidFinishNavigation) {
  108. ASSERT_FALSE([observer_ didFinishNavigationInfo]);
  109. GURL url("https://chromium.test/");
  110. std::unique_ptr<web::NavigationContext> context =
  111. web::NavigationContextImpl::CreateNavigationContext(
  112. &fake_web_state_, url, /*has_user_gesture=*/true,
  113. ui::PageTransition::PAGE_TRANSITION_FROM_ADDRESS_BAR,
  114. /*is_renderer_initiated=*/false);
  115. observer_bridge_.DidFinishNavigation(&fake_web_state_, context.get());
  116. ASSERT_TRUE([observer_ didFinishNavigationInfo]);
  117. EXPECT_EQ(&fake_web_state_, [observer_ didFinishNavigationInfo]->web_state);
  118. web::NavigationContext* actual_context =
  119. [observer_ didFinishNavigationInfo]->context.get();
  120. ASSERT_TRUE(actual_context);
  121. EXPECT_EQ(&fake_web_state_, actual_context->GetWebState());
  122. EXPECT_EQ(context->IsSameDocument(), actual_context->IsSameDocument());
  123. EXPECT_EQ(context->GetError(), actual_context->GetError());
  124. EXPECT_EQ(context->GetUrl(), actual_context->GetUrl());
  125. EXPECT_EQ(context->HasUserGesture(), actual_context->HasUserGesture());
  126. EXPECT_TRUE(PageTransitionTypeIncludingQualifiersIs(
  127. ui::PageTransition::PAGE_TRANSITION_FROM_ADDRESS_BAR,
  128. actual_context->GetPageTransition()));
  129. EXPECT_EQ(context->GetResponseHeaders(),
  130. actual_context->GetResponseHeaders());
  131. }
  132. // Tests |webState:webStateDidStartLoading:| forwarding.
  133. TEST_F(WebStateObserverBridgeTest, DidStartLoading) {
  134. ASSERT_FALSE([observer_ startLoadingInfo]);
  135. observer_bridge_.DidStartLoading(&fake_web_state_);
  136. ASSERT_TRUE([observer_ startLoadingInfo]);
  137. EXPECT_EQ(&fake_web_state_, [observer_ startLoadingInfo]->web_state);
  138. }
  139. // Tests |webState:webStateDidStopLoading:| forwarding.
  140. TEST_F(WebStateObserverBridgeTest, DidStopLoading) {
  141. ASSERT_FALSE([observer_ stopLoadingInfo]);
  142. observer_bridge_.DidStopLoading(&fake_web_state_);
  143. ASSERT_TRUE([observer_ stopLoadingInfo]);
  144. EXPECT_EQ(&fake_web_state_, [observer_ stopLoadingInfo]->web_state);
  145. }
  146. // Tests |webState:didLoadPageWithSuccess:| forwarding.
  147. TEST_F(WebStateObserverBridgeTest, PageLoaded) {
  148. ASSERT_FALSE([observer_ loadPageInfo]);
  149. // Forwarding PageLoadCompletionStatus::SUCCESS.
  150. observer_bridge_.PageLoaded(&fake_web_state_,
  151. PageLoadCompletionStatus::SUCCESS);
  152. ASSERT_TRUE([observer_ loadPageInfo]);
  153. EXPECT_EQ(&fake_web_state_, [observer_ loadPageInfo]->web_state);
  154. EXPECT_TRUE([observer_ loadPageInfo]->success);
  155. // Forwarding PageLoadCompletionStatus::FAILURE.
  156. observer_bridge_.PageLoaded(&fake_web_state_,
  157. PageLoadCompletionStatus::FAILURE);
  158. ASSERT_TRUE([observer_ loadPageInfo]);
  159. EXPECT_EQ(&fake_web_state_, [observer_ loadPageInfo]->web_state);
  160. EXPECT_FALSE([observer_ loadPageInfo]->success);
  161. }
  162. // Tests |webState:didChangeLoadingProgress:| forwarding.
  163. TEST_F(WebStateObserverBridgeTest, LoadProgressChanged) {
  164. ASSERT_FALSE([observer_ changeLoadingProgressInfo]);
  165. const double kTestLoadProgress = 0.75;
  166. observer_bridge_.LoadProgressChanged(&fake_web_state_, kTestLoadProgress);
  167. ASSERT_TRUE([observer_ changeLoadingProgressInfo]);
  168. EXPECT_EQ(&fake_web_state_, [observer_ changeLoadingProgressInfo]->web_state);
  169. EXPECT_EQ(kTestLoadProgress, [observer_ changeLoadingProgressInfo]->progress);
  170. }
  171. // Tests |webStateDidChangeBackForwardState:| forwarding.
  172. TEST_F(WebStateObserverBridgeTest, DidChangeBackForwardState) {
  173. ASSERT_FALSE([observer_ changeBackForwardStateInfo]);
  174. observer_bridge_.DidChangeBackForwardState(&fake_web_state_);
  175. ASSERT_TRUE([observer_ changeBackForwardStateInfo]);
  176. EXPECT_EQ(&fake_web_state_,
  177. [observer_ changeBackForwardStateInfo]->web_state);
  178. }
  179. // Tests |webStateDidChangeTitle:| forwarding.
  180. TEST_F(WebStateObserverBridgeTest, TitleWasSet) {
  181. ASSERT_FALSE([observer_ titleWasSetInfo]);
  182. observer_bridge_.TitleWasSet(&fake_web_state_);
  183. ASSERT_TRUE([observer_ titleWasSetInfo]);
  184. EXPECT_EQ(&fake_web_state_, [observer_ titleWasSetInfo]->web_state);
  185. }
  186. // Tests |webStateDidChangeVisibleSecurityState:| forwarding.
  187. TEST_F(WebStateObserverBridgeTest, DidChangeVisibleSecurityState) {
  188. ASSERT_FALSE([observer_ didChangeVisibleSecurityStateInfo]);
  189. observer_bridge_.DidChangeVisibleSecurityState(&fake_web_state_);
  190. ASSERT_TRUE([observer_ didChangeVisibleSecurityStateInfo]);
  191. EXPECT_EQ(&fake_web_state_,
  192. [observer_ didChangeVisibleSecurityStateInfo]->web_state);
  193. }
  194. // Tests |webState:didUpdateFaviconURLCandidates:| forwarding.
  195. TEST_F(WebStateObserverBridgeTest, FaviconUrlUpdated) {
  196. ASSERT_FALSE([observer_ updateFaviconUrlCandidatesInfo]);
  197. web::FaviconURL url(GURL("https://chromium.test/"),
  198. web::FaviconURL::IconType::kTouchIcon, {gfx::Size(5, 6)});
  199. observer_bridge_.FaviconUrlUpdated(&fake_web_state_, {url});
  200. ASSERT_TRUE([observer_ updateFaviconUrlCandidatesInfo]);
  201. EXPECT_EQ(&fake_web_state_,
  202. [observer_ updateFaviconUrlCandidatesInfo]->web_state);
  203. ASSERT_EQ(1U, [observer_ updateFaviconUrlCandidatesInfo]->candidates.size());
  204. const web::FaviconURL& actual_url =
  205. [observer_ updateFaviconUrlCandidatesInfo]->candidates[0];
  206. EXPECT_EQ(url.icon_url, actual_url.icon_url);
  207. EXPECT_EQ(url.icon_type, actual_url.icon_type);
  208. ASSERT_EQ(url.icon_sizes.size(), actual_url.icon_sizes.size());
  209. EXPECT_EQ(url.icon_sizes[0].width(), actual_url.icon_sizes[0].width());
  210. EXPECT_EQ(url.icon_sizes[0].height(), actual_url.icon_sizes[0].height());
  211. }
  212. // Tests |webState:didChangeStateForPermission:| forwarding.
  213. TEST_F(WebStateObserverBridgeTest, PermissionStateChanged) {
  214. if (@available(iOS 15.0, *)) {
  215. ASSERT_FALSE([observer_ permissionStateChangedInfo]);
  216. // Test PermissionMicrophone state changed.
  217. observer_bridge_.PermissionStateChanged(&fake_web_state_,
  218. web::PermissionMicrophone);
  219. ASSERT_TRUE([observer_ permissionStateChangedInfo]);
  220. EXPECT_EQ(&fake_web_state_,
  221. [observer_ permissionStateChangedInfo]->web_state);
  222. EXPECT_EQ(web::PermissionMicrophone,
  223. [observer_ permissionStateChangedInfo]->permission);
  224. // Test PermissionCamera state changed.
  225. observer_bridge_.PermissionStateChanged(&fake_web_state_,
  226. web::PermissionCamera);
  227. EXPECT_EQ(web::PermissionCamera,
  228. [observer_ permissionStateChangedInfo]->permission);
  229. }
  230. }
  231. // Tests |renderProcessGoneForWebState:| forwarding.
  232. TEST_F(WebStateObserverBridgeTest, RenderProcessGone) {
  233. ASSERT_FALSE([observer_ renderProcessGoneInfo]);
  234. observer_bridge_.RenderProcessGone(&fake_web_state_);
  235. ASSERT_TRUE([observer_ renderProcessGoneInfo]);
  236. EXPECT_EQ(&fake_web_state_, [observer_ renderProcessGoneInfo]->web_state);
  237. }
  238. // Tests |webStateRealized:| forwarding.
  239. TEST_F(WebStateObserverBridgeTest, WebStateRealized) {
  240. ASSERT_FALSE([observer_ webStateRealizedInfo]);
  241. observer_bridge_.WebStateRealized(&fake_web_state_);
  242. ASSERT_TRUE([observer_ webStateRealizedInfo]);
  243. EXPECT_EQ(&fake_web_state_, [observer_ webStateRealizedInfo]->web_state);
  244. }
  245. // Tests |webStateDestroyed:| forwarding.
  246. TEST_F(WebStateObserverBridgeTest, WebStateDestroyed) {
  247. ASSERT_FALSE([observer_ webStateDestroyedInfo]);
  248. observer_bridge_.WebStateDestroyed(&fake_web_state_);
  249. ASSERT_TRUE([observer_ webStateDestroyedInfo]);
  250. EXPECT_EQ(&fake_web_state_, [observer_ webStateDestroyedInfo]->web_state);
  251. }
  252. } // namespace web