Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include <memory> | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #import "ios/web/public/test/http_server.h" | |
| 9 #include "ios/web/public/test/http_server_util.h" | |
| 10 #include "ios/web/public/web_state/navigation_handle.h" | |
| 11 #include "ios/web/public/web_state/web_state_observer.h" | |
| 12 #include "ios/web/test/test_url_constants.h" | |
| 13 #import "ios/web/test/web_int_test.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "url/gurl.h" | |
| 17 #include "url/scheme_host_port.h" | |
| 18 | |
| 19 namespace web { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Verifies correctness of |NavigationHandle| for new page navigation passed to | |
| 24 // |DidFinishNavigation|. | |
| 25 ACTION_P2(VerifyNewPageHandle, web_state, url) { | |
|
kkhorimoto
2017/02/22 01:42:38
s/Page/Document
Eugene But (OOO till 7-30)
2017/02/22 17:54:33
Acknowledged.
| |
| 26 NavigationHandle* handle = arg0; | |
| 27 ASSERT_TRUE(handle); | |
| 28 EXPECT_EQ(web_state, handle->GetWebState()); | |
| 29 EXPECT_EQ(url, handle->GetUrl()); | |
| 30 EXPECT_FALSE(handle->IsSamePage()); | |
| 31 EXPECT_FALSE(handle->IsErrorPage()); | |
| 32 } | |
| 33 | |
| 34 // Verifies correctness of |NavigationHandle| for same page navigation passed to | |
| 35 // |DidFinishNavigation|. | |
| 36 ACTION_P2(VerifySamePageHandle, web_state, url) { | |
|
kkhorimoto
2017/02/22 01:42:38
s/Page/Document
Eugene But (OOO till 7-30)
2017/02/22 17:54:33
Acknowledged.
| |
| 37 NavigationHandle* handle = arg0; | |
| 38 ASSERT_TRUE(handle); | |
| 39 EXPECT_EQ(web_state, handle->GetWebState()); | |
| 40 EXPECT_EQ(url, handle->GetUrl()); | |
| 41 EXPECT_TRUE(handle->IsSamePage()); | |
| 42 EXPECT_FALSE(handle->IsErrorPage()); | |
| 43 } | |
| 44 | |
| 45 // Mocks DidFinishNavigation navigation callback. | |
| 46 class WebStateObserverMock : public WebStateObserver { | |
| 47 public: | |
| 48 WebStateObserverMock(WebState* web_state) : WebStateObserver(web_state) {} | |
| 49 MOCK_METHOD1(DidFinishNavigation, void(NavigationHandle* handle)); | |
| 50 }; | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 using test::HttpServer; | |
| 55 using testing::_; | |
| 56 | |
| 57 // Test fixture for WebStateDelegate::DidFinishNavigation integration tests. | |
| 58 class DidFinishNavigationTest : public WebIntTest { | |
| 59 void SetUp() override { | |
| 60 WebIntTest::SetUp(); | |
| 61 observer_ = base::MakeUnique<WebStateObserverMock>(web_state()); | |
| 62 } | |
| 63 | |
| 64 protected: | |
| 65 std::unique_ptr<WebStateObserverMock> observer_; | |
| 66 }; | |
| 67 | |
| 68 // Tests successful navigation to a new page. | |
| 69 TEST_F(DidFinishNavigationTest, NewPageNavigation) { | |
| 70 const GURL url = HttpServer::MakeUrl("http://chromium.test"); | |
| 71 std::map<GURL, std::string> responses; | |
| 72 responses[url] = "Chromium Test"; | |
| 73 web::test::SetUpSimpleHttpServer(responses); | |
| 74 | |
| 75 // Perform new page navigation. | |
| 76 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 77 .WillOnce(VerifyNewPageHandle(web_state(), url)); | |
| 78 LoadUrl(url); | |
| 79 } | |
| 80 | |
| 81 // Tests user-initiated hash change. | |
| 82 TEST_F(DidFinishNavigationTest, UserInitiatedHashChangeNavigation) { | |
| 83 const GURL url = HttpServer::MakeUrl("http://chromium.test"); | |
| 84 std::map<GURL, std::string> responses; | |
| 85 responses[url] = "Chromium Test"; | |
| 86 web::test::SetUpSimpleHttpServer(responses); | |
| 87 | |
| 88 // Perform new page navigation. | |
| 89 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 90 .WillOnce(VerifyNewPageHandle(web_state(), url)); | |
| 91 LoadUrl(url); | |
| 92 | |
| 93 // Perform same-page navigation. | |
| 94 const GURL hash_url = HttpServer::MakeUrl("http://chromium.test#1"); | |
| 95 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 96 .WillOnce(VerifySamePageHandle(web_state(), hash_url)); | |
| 97 LoadUrl(hash_url); | |
| 98 | |
| 99 // Perform same-page navigation by going back. | |
| 100 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 101 .WillOnce(VerifySamePageHandle(web_state(), url)); | |
| 102 ExecuteBlockAndWaitForLoad(url, ^{ | |
| 103 navigation_manager()->GoBack(); | |
| 104 }); | |
| 105 } | |
| 106 | |
| 107 // Tests renderer-initiated hash change. | |
| 108 TEST_F(DidFinishNavigationTest, RendererInitiatedHashChangeNavigation) { | |
| 109 const GURL url = HttpServer::MakeUrl("http://chromium.test"); | |
| 110 std::map<GURL, std::string> responses; | |
| 111 responses[url] = "Chromium Test"; | |
| 112 web::test::SetUpSimpleHttpServer(responses); | |
| 113 | |
| 114 // Perform new page navigation. | |
| 115 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 116 .WillOnce(VerifyNewPageHandle(web_state(), url)); | |
| 117 LoadUrl(url); | |
| 118 | |
| 119 // Perform same-page navigation using JavaScript. | |
| 120 const GURL hash_url = HttpServer::MakeUrl("http://chromium.test#1"); | |
| 121 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 122 .WillOnce(VerifySamePageHandle(web_state(), hash_url)); | |
| 123 ExecuteJavaScript(@"window.location.hash = '#1'"); | |
| 124 } | |
| 125 | |
| 126 // Tests state change. | |
| 127 TEST_F(DidFinishNavigationTest, StateNavigation) { | |
| 128 const GURL url = HttpServer::MakeUrl("http://chromium.test"); | |
| 129 std::map<GURL, std::string> responses; | |
| 130 responses[url] = "Chromium Test"; | |
| 131 web::test::SetUpSimpleHttpServer(responses); | |
| 132 | |
| 133 // Perform new page navigation. | |
| 134 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 135 .WillOnce(VerifyNewPageHandle(web_state(), url)); | |
| 136 LoadUrl(url); | |
| 137 | |
| 138 // Perform push state using JavaScript. | |
| 139 const GURL push_url = HttpServer::MakeUrl("http://chromium.test/test.html"); | |
| 140 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 141 .WillOnce(VerifySamePageHandle(web_state(), push_url)); | |
| 142 ExecuteJavaScript(@"window.history.pushState('', 'Test', 'test.html')"); | |
| 143 | |
| 144 // Perform replace state using JavaScript. | |
| 145 const GURL replace_url = HttpServer::MakeUrl("http://chromium.test/1.html"); | |
| 146 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 147 .WillOnce(VerifySamePageHandle(web_state(), replace_url)); | |
| 148 ExecuteJavaScript(@"window.history.replaceState('', 'Test', '1.html')"); | |
| 149 } | |
| 150 | |
| 151 // Tests native content navigation. | |
| 152 TEST_F(DidFinishNavigationTest, NativeContentNavigation) { | |
| 153 GURL url(url::SchemeHostPort(kTestNativeContentScheme, "ui", 0).Serialize()); | |
| 154 EXPECT_CALL(*observer_, DidFinishNavigation(_)) | |
| 155 .WillOnce(VerifyNewPageHandle(web_state(), url)); | |
| 156 LoadUrl(url); | |
| 157 } | |
| 158 | |
|
Eugene But (OOO till 7-30)
2017/02/21 23:31:45
I could not write tests for error page navigation,
| |
| 159 } // namespace web | |
| OLD | NEW |