Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h" | 5 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
| 10 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 11 #include "base/mac/scoped_nsobject.h" | |
| 12 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 13 #import "base/test/ios/wait_util.h" | 11 #import "base/test/ios/wait_util.h" |
| 14 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 12 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 15 #import "ios/chrome/browser/ui/url_loader.h" | 13 #import "ios/chrome/browser/ui/url_loader.h" |
| 16 #include "ios/chrome/grit/ios_strings.h" | 14 #include "ios/chrome/grit/ios_strings.h" |
| 17 #import "ios/testing/ocmock_complex_type_helper.h" | 15 #import "ios/testing/ocmock_complex_type_helper.h" |
| 18 #include "ios/web/public/referrer.h" | 16 #include "ios/web/public/referrer.h" |
| 19 #import "ios/web/public/test/fakes/test_web_client.h" | 17 #import "ios/web/public/test/fakes/test_web_client.h" |
| 20 #import "ios/web/public/test/js_test_util.h" | 18 #import "ios/web/public/test/js_test_util.h" |
| 21 #include "ios/web/public/test/scoped_testing_web_client.h" | 19 #include "ios/web/public/test/scoped_testing_web_client.h" |
| 22 #include "ios/web/public/test/test_web_thread_bundle.h" | 20 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 23 #import "ios/web/public/web_state/ui/crw_native_content.h" | 21 #import "ios/web/public/web_state/ui/crw_native_content.h" |
| 24 #import "net/base/mac/url_conversions.h" | 22 #import "net/base/mac/url_conversions.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "testing/gtest_mac.h" | 24 #include "testing/gtest_mac.h" |
| 27 #include "testing/platform_test.h" | 25 #include "testing/platform_test.h" |
| 28 #import "third_party/ocmock/OCMock/OCMock.h" | 26 #import "third_party/ocmock/OCMock/OCMock.h" |
| 29 #include "third_party/ocmock/gtest_support.h" | 27 #include "third_party/ocmock/gtest_support.h" |
| 30 #include "ui/base/l10n/l10n_util_mac.h" | 28 #include "ui/base/l10n/l10n_util_mac.h" |
| 31 | 29 |
| 30 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 31 #error "This file requires ARC support." | |
| 32 #endif | |
| 33 | |
| 32 // Generator that returns the value of a localized identifier. | 34 // Generator that returns the value of a localized identifier. |
| 33 @interface L10nHtmlGenerator : NSObject<HtmlGenerator> { | 35 @interface L10nHtmlGenerator : NSObject<HtmlGenerator> { |
| 34 @private | 36 @private |
| 35 int messageId_; | 37 int messageId_; |
| 36 } | 38 } |
| 37 - (id)initWithMessageId:(int)messageId; | 39 - (id)initWithMessageId:(int)messageId; |
| 38 @end | 40 @end |
| 39 | 41 |
| 40 @implementation L10nHtmlGenerator | 42 @implementation L10nHtmlGenerator |
| 41 - (id)initWithMessageId:(int)messageId { | 43 - (id)initWithMessageId:(int)messageId { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 : web_client_(base::MakeUnique<web::TestWebClient>()) {} | 102 : web_client_(base::MakeUnique<web::TestWebClient>()) {} |
| 101 | 103 |
| 102 protected: | 104 protected: |
| 103 void SetUp() override { | 105 void SetUp() override { |
| 104 PlatformTest::SetUp(); | 106 PlatformTest::SetUp(); |
| 105 chrome_browser_state_ = TestChromeBrowserState::Builder().Build(); | 107 chrome_browser_state_ = TestChromeBrowserState::Builder().Build(); |
| 106 } | 108 } |
| 107 | 109 |
| 108 web::TestWebThreadBundle thread_bundle_; | 110 web::TestWebThreadBundle thread_bundle_; |
| 109 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 111 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 110 // Private autorelease pool so objects are released as soon as possible. | |
| 111 base::mac::ScopedNSAutoreleasePool pool_; | |
| 112 web::ScopedTestingWebClient web_client_; | 112 web::ScopedTestingWebClient web_client_; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 // Tests the creation of a StaticHtmlViewController displaying a resource file. | 115 // Tests the creation of a StaticHtmlViewController displaying a resource file. |
| 116 TEST_F(StaticHtmlViewControllerTest, LoadResourceTest) { | 116 TEST_F(StaticHtmlViewControllerTest, LoadResourceTest) { |
| 117 base::scoped_nsobject<id> loader; | 117 id loader; |
|
sdefresne
2017/02/13 14:19:02
nit: no need for separate line for definition/init
| |
| 118 loader.reset([[LoadTestMockLoader alloc] | 118 loader = [[LoadTestMockLoader alloc] |
| 119 initWithRepresentedObject:[OCMockObject | 119 initWithRepresentedObject:[OCMockObject |
| 120 mockForProtocol:@protocol(UrlLoader)]]); | 120 mockForProtocol:@protocol(UrlLoader)]]; |
| 121 | 121 |
| 122 id<CRWNativeContentDelegate> delegate = | 122 id<CRWNativeContentDelegate> delegate = |
| 123 [OCMockObject mockForProtocol:@protocol(CRWNativeContentDelegate)]; | 123 [OCMockObject mockForProtocol:@protocol(CRWNativeContentDelegate)]; |
| 124 GURL referrer_url("chrome://foo"); | 124 GURL referrer_url("chrome://foo"); |
| 125 web::Referrer referrer(referrer_url, web::ReferrerPolicyDefault); | 125 web::Referrer referrer(referrer_url, web::ReferrerPolicyDefault); |
| 126 base::scoped_nsobject<StaticHtmlViewController> content( | 126 StaticHtmlViewController* content = [[StaticHtmlViewController alloc] |
| 127 [[StaticHtmlViewController alloc] | 127 initWithResource:@"terms_en.html" |
| 128 initWithResource:@"terms_en.html" | 128 browserState:chrome_browser_state_.get()]; |
| 129 browserState:chrome_browser_state_.get()]); | |
| 130 [content setLoader:loader referrer:referrer]; | 129 [content setLoader:loader referrer:referrer]; |
| 131 [content setDelegate:delegate]; | 130 [content setDelegate:delegate]; |
| 132 [[(OCMockObject*)delegate expect] | 131 [[(OCMockObject*)delegate expect] |
| 133 nativeContent:content.get() | 132 nativeContent:content |
| 134 titleDidChange:[OCMArg checkWithBlock:^BOOL(id value) { | 133 titleDidChange:[OCMArg checkWithBlock:^BOOL(id value) { |
| 135 isRunLoopDry = true; | 134 isRunLoopDry = true; |
| 136 return [@"Google Chrome Terms of Service" | 135 return [@"Google Chrome Terms of Service" |
| 137 isEqualToString:(NSString*)value]; | 136 isEqualToString:(NSString*)value]; |
| 138 }]]; | 137 }]]; |
| 139 [content triggerPendingLoad]; | 138 [content triggerPendingLoad]; |
| 140 DryRunLoop(false); | 139 DryRunLoop(false); |
| 141 ASSERT_OCMOCK_VERIFY(loader.get()); | 140 ASSERT_OCMOCK_VERIFY(loader); |
| 142 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); | 141 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); |
| 143 base::scoped_nsobject<id> block( | 142 id block = [(id) ^ (const GURL& url, const web::Referrer& referrer, |
| 144 [(id) ^ (const GURL& url, const web::Referrer& referrer, | 143 ui::PageTransition transition, BOOL rendererInitiated) { |
| 145 ui::PageTransition transition, BOOL rendererInitiated) { | 144 EXPECT_EQ(url, GURL()); |
| 146 EXPECT_EQ(url, GURL()); | 145 EXPECT_EQ(referrer.url, referrer_url); |
| 147 EXPECT_EQ(referrer.url, referrer_url); | 146 EXPECT_EQ(referrer.policy, web::ReferrerPolicyDefault); |
| 148 EXPECT_EQ(referrer.policy, web::ReferrerPolicyDefault); | 147 EXPECT_TRUE(PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)); |
| 149 EXPECT_TRUE( | 148 EXPECT_TRUE(rendererInitiated); |
| 150 PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)); | 149 } copy]; |
| 151 EXPECT_TRUE(rendererInitiated); | |
| 152 } copy]); | |
| 153 | 150 |
| 154 [loader onSelector:@selector(loadURL:referrer:transition:rendererInitiated:) | 151 [loader onSelector:@selector(loadURL:referrer:transition:rendererInitiated:) |
| 155 callBlockExpectation:block]; | 152 callBlockExpectation:block]; |
| 156 | 153 |
| 157 DryRunLoop(true); | 154 DryRunLoop(true); |
| 158 ASSERT_OCMOCK_VERIFY(loader.get()); | 155 ASSERT_OCMOCK_VERIFY(loader); |
| 159 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); | 156 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); |
| 160 } | 157 } |
| 161 | 158 |
| 162 // Tests the creation of a StaticHtmlViewController displaying a local file. | 159 // Tests the creation of a StaticHtmlViewController displaying a local file. |
| 163 TEST_F(StaticHtmlViewControllerTest, LoadFileURLTest) { | 160 TEST_F(StaticHtmlViewControllerTest, LoadFileURLTest) { |
| 164 base::scoped_nsobject<id> loader; | 161 id loader; |
|
sdefresne
2017/02/13 14:19:02
ditto, single line.
| |
| 165 loader.reset([[LoadTestMockLoader alloc] | 162 loader = [[LoadTestMockLoader alloc] |
| 166 initWithRepresentedObject:[OCMockObject | 163 initWithRepresentedObject:[OCMockObject |
| 167 mockForProtocol:@protocol(UrlLoader)]]); | 164 mockForProtocol:@protocol(UrlLoader)]]; |
| 168 | 165 |
| 169 id<CRWNativeContentDelegate> delegate = | 166 id<CRWNativeContentDelegate> delegate = |
| 170 [OCMockObject mockForProtocol:@protocol(CRWNativeContentDelegate)]; | 167 [OCMockObject mockForProtocol:@protocol(CRWNativeContentDelegate)]; |
| 171 GURL referrer_url("chrome://foo"); | 168 GURL referrer_url("chrome://foo"); |
| 172 web::Referrer referrer(referrer_url, web::ReferrerPolicyDefault); | 169 web::Referrer referrer(referrer_url, web::ReferrerPolicyDefault); |
| 173 NSURL* fileURL = [NSURL | 170 NSURL* fileURL = [NSURL |
| 174 fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"terms_en.html" | 171 fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"terms_en.html" |
| 175 ofType:nil | 172 ofType:nil |
| 176 inDirectory:nil]]; | 173 inDirectory:nil]]; |
| 177 base::scoped_nsobject<StaticHtmlViewController> content( | 174 StaticHtmlViewController* content = [[StaticHtmlViewController alloc] |
| 178 [[StaticHtmlViewController alloc] | 175 initWithFileURL:net::GURLWithNSURL(fileURL) |
| 179 initWithFileURL:net::GURLWithNSURL(fileURL) | 176 allowingReadAccessToURL:net::GURLWithNSURL( |
| 180 allowingReadAccessToURL:net::GURLWithNSURL( | 177 [fileURL URLByDeletingLastPathComponent]) |
| 181 [fileURL URLByDeletingLastPathComponent]) | 178 browserState:chrome_browser_state_.get()]; |
| 182 browserState:chrome_browser_state_.get()]); | |
| 183 [content setLoader:loader referrer:referrer]; | 179 [content setLoader:loader referrer:referrer]; |
| 184 [content setDelegate:delegate]; | 180 [content setDelegate:delegate]; |
| 185 [[(OCMockObject*)delegate expect] | 181 [[(OCMockObject*)delegate expect] |
| 186 nativeContent:content.get() | 182 nativeContent:content |
| 187 titleDidChange:[OCMArg checkWithBlock:^BOOL(id value) { | 183 titleDidChange:[OCMArg checkWithBlock:^BOOL(id value) { |
| 188 isRunLoopDry = true; | 184 isRunLoopDry = true; |
| 189 return [@"Google Chrome Terms of Service" | 185 return [@"Google Chrome Terms of Service" |
| 190 isEqualToString:(NSString*)value]; | 186 isEqualToString:(NSString*)value]; |
| 191 }]]; | 187 }]]; |
| 192 [content triggerPendingLoad]; | 188 [content triggerPendingLoad]; |
| 193 DryRunLoop(false); | 189 DryRunLoop(false); |
| 194 ASSERT_OCMOCK_VERIFY(loader.get()); | 190 ASSERT_OCMOCK_VERIFY(loader); |
| 195 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); | 191 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); |
| 196 base::scoped_nsobject<id> block( | 192 id block = [(id) ^ (const GURL& url, const web::Referrer& referrer, |
| 197 [(id) ^ (const GURL& url, const web::Referrer& referrer, | 193 ui::PageTransition transition, BOOL rendererInitiated) { |
| 198 ui::PageTransition transition, BOOL rendererInitiated) { | 194 EXPECT_EQ(url, GURL()); |
| 199 EXPECT_EQ(url, GURL()); | 195 EXPECT_EQ(referrer.url, referrer_url); |
| 200 EXPECT_EQ(referrer.url, referrer_url); | 196 EXPECT_EQ(referrer.policy, web::ReferrerPolicyDefault); |
| 201 EXPECT_EQ(referrer.policy, web::ReferrerPolicyDefault); | 197 EXPECT_TRUE(PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)); |
| 202 EXPECT_TRUE( | 198 EXPECT_TRUE(rendererInitiated); |
| 203 PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)); | 199 } copy]; |
| 204 EXPECT_TRUE(rendererInitiated); | |
| 205 } copy]); | |
| 206 | 200 |
| 207 [loader onSelector:@selector(loadURL:referrer:transition:rendererInitiated:) | 201 [loader onSelector:@selector(loadURL:referrer:transition:rendererInitiated:) |
| 208 callBlockExpectation:block]; | 202 callBlockExpectation:block]; |
| 209 | 203 |
| 210 DryRunLoop(true); | 204 DryRunLoop(true); |
| 211 ASSERT_OCMOCK_VERIFY(loader.get()); | 205 ASSERT_OCMOCK_VERIFY(loader); |
| 212 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); | 206 ASSERT_OCMOCK_VERIFY((OCMockObject*)delegate); |
| 213 } | 207 } |
| 214 | 208 |
| 215 // Tests that -[StaticHtmlViewController webView] returns a non-nil view. | 209 // Tests that -[StaticHtmlViewController webView] returns a non-nil view. |
| 216 TEST_F(StaticHtmlViewControllerTest, WebViewNonNil) { | 210 TEST_F(StaticHtmlViewControllerTest, WebViewNonNil) { |
| 217 base::scoped_nsobject<L10nHtmlGenerator> generator( | 211 L10nHtmlGenerator* generator = |
| 218 [[L10nHtmlGenerator alloc] initWithMessageId:IDS_IOS_TOOLS_MENU]); | 212 [[L10nHtmlGenerator alloc] initWithMessageId:IDS_IOS_TOOLS_MENU]; |
| 219 base::scoped_nsobject<StaticHtmlViewController> staticHtmlViewController( | 213 StaticHtmlViewController* staticHtmlViewController = |
| 220 [[StaticHtmlViewController alloc] | 214 [[StaticHtmlViewController alloc] |
| 221 initWithGenerator:generator | 215 initWithGenerator:generator |
| 222 browserState:chrome_browser_state_.get()]); | 216 browserState:chrome_browser_state_.get()]; |
| 223 EXPECT_TRUE([staticHtmlViewController webView]); | 217 EXPECT_TRUE([staticHtmlViewController webView]); |
| 224 } | 218 } |
| 225 | 219 |
| 226 // Tests the generated HTML is localized. | 220 // Tests the generated HTML is localized. |
| 227 TEST_F(StaticHtmlViewControllerTest, L10NTest) { | 221 TEST_F(StaticHtmlViewControllerTest, L10NTest) { |
| 228 base::scoped_nsobject<L10nHtmlGenerator> generator( | 222 L10nHtmlGenerator* generator = |
| 229 [[L10nHtmlGenerator alloc] initWithMessageId:IDS_IOS_TOOLS_MENU]); | 223 [[L10nHtmlGenerator alloc] initWithMessageId:IDS_IOS_TOOLS_MENU]; |
| 230 base::scoped_nsobject<StaticHtmlViewController> content( | 224 StaticHtmlViewController* content = [[StaticHtmlViewController alloc] |
| 231 [[StaticHtmlViewController alloc] | 225 initWithGenerator:generator |
| 232 initWithGenerator:generator | 226 browserState:chrome_browser_state_.get()]; |
| 233 browserState:chrome_browser_state_.get()]); | |
| 234 id<UrlLoader> loader = [OCMockObject mockForProtocol:@protocol(UrlLoader)]; | 227 id<UrlLoader> loader = [OCMockObject mockForProtocol:@protocol(UrlLoader)]; |
| 235 [content setLoader:loader | 228 [content setLoader:loader |
| 236 referrer:web::Referrer(GURL("chrome://foo"), | 229 referrer:web::Referrer(GURL("chrome://foo"), |
| 237 web::ReferrerPolicyDefault)]; | 230 web::ReferrerPolicyDefault)]; |
| 238 [content triggerPendingLoad]; | 231 [content triggerPendingLoad]; |
| 239 ASSERT_OCMOCK_VERIFY((OCMockObject*)loader); | 232 ASSERT_OCMOCK_VERIFY((OCMockObject*)loader); |
| 240 __block id string_in_page = nil; | 233 __block id string_in_page = nil; |
| 241 base::test::ios::WaitUntilCondition(^bool { | 234 base::test::ios::WaitUntilCondition(^bool { |
| 242 string_in_page = | 235 string_in_page = |
| 243 web::ExecuteJavaScript([content webView], @"document.body.innerHTML"); | 236 web::ExecuteJavaScript([content webView], @"document.body.innerHTML"); |
| 244 return ![string_in_page isEqual:@""]; | 237 return ![string_in_page isEqual:@""]; |
| 245 }); | 238 }); |
| 246 EXPECT_TRUE([string_in_page isKindOfClass:[NSString class]]); | 239 EXPECT_TRUE([string_in_page isKindOfClass:[NSString class]]); |
| 247 NSString* to_find = l10n_util::GetNSString(IDS_IOS_TOOLS_MENU); | 240 NSString* to_find = l10n_util::GetNSString(IDS_IOS_TOOLS_MENU); |
| 248 EXPECT_TRUE([string_in_page rangeOfString:to_find].length); | 241 EXPECT_TRUE([string_in_page rangeOfString:to_find].length); |
| 249 } | 242 } |
| 250 | 243 |
| 251 } // namespace | 244 } // namespace |
| OLD | NEW |