| 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 #import <ChromeWebView/ChromeWebView.h> | |
| 6 #import <Foundation/Foundation.h> | |
| 7 | |
| 8 #include "base/strings/stringprintf.h" | |
| 9 #import "base/strings/sys_string_conversions.h" | |
| 10 #import "ios/testing/wait_util.h" | |
| 11 #import "ios/web_view/test/chrome_web_view_test.h" | |
| 12 #import "ios/web_view/test/observer.h" | |
| 13 #import "ios/web_view/test/web_view_test_util.h" | |
| 14 #import "net/base/mac/url_conversions.h" | |
| 15 #include "testing/gtest_mac.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 19 #error "This file requires ARC support." | |
| 20 #endif | |
| 21 | |
| 22 namespace ios_web_view { | |
| 23 | |
| 24 // Tests that the KVO compliant properties of CWVWebView correctly report | |
| 25 // changes. | |
| 26 typedef ios_web_view::ChromeWebViewTest ChromeWebViewKvoTest; | |
| 27 | |
| 28 // Tests that CWVWebView correctly reports |canGoBack| and |canGoForward| state. | |
| 29 TEST_F(ChromeWebViewKvoTest, CanGoBackForward) { | |
| 30 Observer* back_observer = [[Observer alloc] init]; | |
| 31 [back_observer setObservedObject:web_view_ keyPath:@"canGoBack"]; | |
| 32 | |
| 33 Observer* forward_observer = [[Observer alloc] init]; | |
| 34 [forward_observer setObservedObject:web_view_ keyPath:@"canGoForward"]; | |
| 35 | |
| 36 ASSERT_FALSE(back_observer.lastValue); | |
| 37 ASSERT_FALSE(forward_observer.lastValue); | |
| 38 | |
| 39 // Define pages in reverse order so the links can reference the "next" page. | |
| 40 GURL page_3_url = GetUrlForPageWithTitle("Page 3"); | |
| 41 | |
| 42 std::string page_2_html = | |
| 43 "<a id='link_2' href='" + page_3_url.spec() + "'>Link 2</a>"; | |
| 44 GURL page_2_url = GetUrlForPageWithHtmlBody(page_2_html); | |
| 45 | |
| 46 std::string page_1_html = | |
| 47 "<a id='link_1' href='" + page_2_url.spec() + "'>Link 1</a>"; | |
| 48 GURL page_1_url = GetUrlForPageWithHtmlBody(page_1_html); | |
| 49 | |
| 50 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(page_1_url))); | |
| 51 // Loading initial URL should not affect back/forward navigation state. | |
| 52 EXPECT_FALSE([back_observer.lastValue boolValue]); | |
| 53 EXPECT_FALSE([forward_observer.lastValue boolValue]); | |
| 54 | |
| 55 // Navigate to page 2. | |
| 56 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); | |
| 57 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 58 EXPECT_TRUE([back_observer.lastValue boolValue]); | |
| 59 EXPECT_FALSE([forward_observer.lastValue boolValue]); | |
| 60 | |
| 61 // Navigate back to page 1. | |
| 62 [web_view_ goBack]; | |
| 63 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 64 EXPECT_FALSE([back_observer.lastValue boolValue]); | |
| 65 EXPECT_TRUE([forward_observer.lastValue boolValue]); | |
| 66 | |
| 67 // Navigate forward to page 2. | |
| 68 [web_view_ goForward]; | |
| 69 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 70 EXPECT_TRUE([back_observer.lastValue boolValue]); | |
| 71 EXPECT_FALSE([forward_observer.lastValue boolValue]); | |
| 72 | |
| 73 // Navigate to page 3. | |
| 74 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_2")); | |
| 75 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 76 EXPECT_TRUE([back_observer.lastValue boolValue]); | |
| 77 EXPECT_FALSE([forward_observer.lastValue boolValue]); | |
| 78 | |
| 79 // Navigate back to page 2. | |
| 80 [web_view_ goBack]; | |
| 81 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 82 EXPECT_TRUE([back_observer.lastValue boolValue]); | |
| 83 EXPECT_TRUE([forward_observer.lastValue boolValue]); | |
| 84 } | |
| 85 | |
| 86 // Tests that CWVWebView correctly reports current |title|. | |
| 87 TEST_F(ChromeWebViewKvoTest, Title) { | |
| 88 Observer* observer = [[Observer alloc] init]; | |
| 89 [observer setObservedObject:web_view_ keyPath:@"title"]; | |
| 90 | |
| 91 NSString* page_2_title = @"Page 2"; | |
| 92 GURL page_2_url = | |
| 93 GetUrlForPageWithTitle(base::SysNSStringToUTF8(page_2_title)); | |
| 94 | |
| 95 NSString* page_1_title = @"Page 1"; | |
| 96 std::string page_1_html = base::StringPrintf( | |
| 97 "<a id='link_1' href='%s'>Link 1</a>", page_2_url.spec().c_str()); | |
| 98 GURL page_1_url = GetUrlForPageWithTitleAndBody( | |
| 99 base::SysNSStringToUTF8(page_1_title), page_1_html); | |
| 100 | |
| 101 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(page_1_url))); | |
| 102 EXPECT_NSEQ(page_1_title, observer.lastValue); | |
| 103 | |
| 104 // Navigate to page 2. | |
| 105 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); | |
| 106 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 107 EXPECT_NSEQ(page_2_title, observer.lastValue); | |
| 108 | |
| 109 // Navigate back to page 1. | |
| 110 [web_view_ goBack]; | |
| 111 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 112 EXPECT_NSEQ(page_1_title, observer.lastValue); | |
| 113 } | |
| 114 | |
| 115 // Tests that CWVWebView correctly reports |isLoading| value. | |
| 116 TEST_F(ChromeWebViewKvoTest, Loading) { | |
| 117 Observer* observer = [[Observer alloc] init]; | |
| 118 [observer setObservedObject:web_view_ keyPath:@"loading"]; | |
| 119 | |
| 120 GURL page_2_url = GetUrlForPageWithTitle("Page 2"); | |
| 121 | |
| 122 std::string page_1_html = base::StringPrintf( | |
| 123 "<a id='link_1' href='%s'>Link 1</a>", page_2_url.spec().c_str()); | |
| 124 GURL page_1_url = GetUrlForPageWithTitleAndBody("Page 1", page_1_html); | |
| 125 | |
| 126 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(page_1_url))); | |
| 127 EXPECT_TRUE([observer.previousValue boolValue]); | |
| 128 EXPECT_FALSE([observer.lastValue boolValue]); | |
| 129 | |
| 130 // Navigate to page 2. | |
| 131 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); | |
| 132 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 133 EXPECT_TRUE([observer.previousValue boolValue]); | |
| 134 EXPECT_FALSE([observer.lastValue boolValue]); | |
| 135 | |
| 136 // Navigate back to page 1. | |
| 137 [web_view_ goBack]; | |
| 138 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 139 EXPECT_TRUE([observer.previousValue boolValue]); | |
| 140 EXPECT_FALSE([observer.lastValue boolValue]); | |
| 141 } | |
| 142 | |
| 143 // Tests that CWVWebView correctly reports |visibleURL| and |lastCommittedURL|. | |
| 144 TEST_F(ChromeWebViewKvoTest, URLs) { | |
| 145 Observer* last_committed_url_observer = [[Observer alloc] init]; | |
| 146 [last_committed_url_observer setObservedObject:web_view_ | |
| 147 keyPath:@"lastCommittedURL"]; | |
| 148 | |
| 149 Observer* visible_url_observer = [[Observer alloc] init]; | |
| 150 [visible_url_observer setObservedObject:web_view_ keyPath:@"visibleURL"]; | |
| 151 | |
| 152 GURL page_2 = GetUrlForPageWithTitle("Page 2"); | |
| 153 NSURL* page_2_url = net::NSURLWithGURL(page_2); | |
| 154 | |
| 155 std::string page_1_html = base::StringPrintf( | |
| 156 "<a id='link_1' href='%s'>Link 1</a>", page_2.spec().c_str()); | |
| 157 NSURL* page_1_url = | |
| 158 net::NSURLWithGURL(GetUrlForPageWithTitleAndBody("Page 1", page_1_html)); | |
| 159 | |
| 160 [web_view_ loadRequest:[NSURLRequest requestWithURL:page_1_url]]; | |
| 161 | |
| 162 // |visibleURL| will update immediately | |
| 163 EXPECT_NSEQ(page_1_url, visible_url_observer.lastValue); | |
| 164 | |
| 165 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 166 EXPECT_NSEQ(page_1_url, last_committed_url_observer.lastValue); | |
| 167 EXPECT_NSEQ(page_1_url, visible_url_observer.lastValue); | |
| 168 | |
| 169 // Navigate to page 2. | |
| 170 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); | |
| 171 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 172 EXPECT_NSEQ(page_2_url, last_committed_url_observer.lastValue); | |
| 173 EXPECT_NSEQ(page_2_url, visible_url_observer.lastValue); | |
| 174 | |
| 175 // Navigate back to page 1. | |
| 176 [web_view_ goBack]; | |
| 177 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | |
| 178 EXPECT_NSEQ(page_1_url, last_committed_url_observer.lastValue); | |
| 179 EXPECT_NSEQ(page_1_url, visible_url_observer.lastValue); | |
| 180 } | |
| 181 | |
| 182 } // namespace ios_web_view | |
| OLD | NEW |