| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #import <UIKit/UIWebView.h> | 6 #import <Foundation/Foundation.h> |
| 7 | 7 |
| 8 #include "ios/web/public/test/web_test_util.h" | |
| 9 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | 8 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" |
| 10 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 9 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 11 #import "ios/web/test/web_test.h" | 10 #import "ios/web/test/web_test.h" |
| 12 #import "testing/gtest_mac.h" | 11 #import "testing/gtest_mac.h" |
| 13 | 12 |
| 14 // Testing class of JsInjectioManager that has no dependencies. | 13 // Testing class of JsInjectioManager that has no dependencies. |
| 15 @interface TestingCRWJSBaseManager : CRWJSInjectionManager | 14 @interface TestingCRWJSBaseManager : CRWJSInjectionManager |
| 16 @end | 15 @end |
| 17 | 16 |
| 18 @implementation TestingCRWJSBaseManager | 17 @implementation TestingCRWJSBaseManager |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 [TestingJsManagerWithNestedDependencies class], | 146 [TestingJsManagerWithNestedDependencies class], |
| 148 [TestingAnotherJsManager class], | 147 [TestingAnotherJsManager class], |
| 149 [TestingAnotherCRWJSBaseManager class], | 148 [TestingAnotherCRWJSBaseManager class], |
| 150 ]; | 149 ]; |
| 151 } | 150 } |
| 152 | 151 |
| 153 @end | 152 @end |
| 154 | 153 |
| 155 #pragma mark - | 154 #pragma mark - |
| 156 | 155 |
| 157 namespace { | 156 namespace web { |
| 158 | 157 |
| 159 // A mixin class for testing with CRWWKWebViewWebController or | 158 // Test fixture to test web controller injection. |
| 160 // CRWUIWebViewWebController. | 159 class JsInjectionManagerTest : public web::WebTestWithWKWebViewWebController { |
| 161 template <typename WebTestT> | |
| 162 class JsInjectionManagerTest : public WebTestT { | |
| 163 protected: | 160 protected: |
| 164 virtual void SetUp() override { | 161 void SetUp() override { |
| 165 WebTestT::SetUp(); | 162 web::WebTestWithWKWebViewWebController::SetUp(); |
| 166 // Loads a dummy page to prepare JavaScript evaluation. | 163 // Loads a dummy page to prepare JavaScript evaluation. |
| 167 NSString* const kPageContent = @"<html><body><div></div></body></html>"; | 164 NSString* const kPageContent = @"<html><body><div></div></body></html>"; |
| 168 WebTestT::LoadHtml(kPageContent); | 165 LoadHtml(kPageContent); |
| 169 } | 166 } |
| 170 // Returns the manager of the given class. | 167 // Returns the manager of the given class. |
| 171 CRWJSInjectionManager* GetInstanceOfClass(Class jsInjectionManagerClass); | 168 CRWJSInjectionManager* GetInstanceOfClass(Class jsInjectionManagerClass); |
| 172 // Returns true if the receiver_ has all the managers in |managers|. | 169 // Returns true if the receiver_ has all the managers in |managers|. |
| 173 bool HasReceiverManagers(NSArray* managers); | 170 bool HasReceiverManagers(NSArray* managers); |
| 174 // EXPECTs that |actual| consists of the CRWJSInjectionManagers of the | 171 // EXPECTs that |actual| consists of the CRWJSInjectionManagers of the |
| 175 // expected classes in a correct order. | 172 // expected classes in a correct order. |
| 176 void TestAllDependencies(NSArray* expected, NSArray* actual); | 173 void TestAllDependencies(NSArray* expected, NSArray* actual); |
| 177 }; | 174 }; |
| 178 | 175 |
| 179 // Concrete test fixture to test UIWebView-based web controller injection. | 176 bool JsInjectionManagerTest::HasReceiverManagers(NSArray* manager_classes) { |
| 180 typedef JsInjectionManagerTest<web::WebTestWithUIWebViewWebController> | |
| 181 JsInjectionManagerUIWebViewTest; | |
| 182 | |
| 183 // Concrete test fixture to test WKWebView-based web controller injection. | |
| 184 class JsInjectionManagerWKWebViewTest | |
| 185 : public JsInjectionManagerTest<web::WebTestWithWKWebViewWebController> { | |
| 186 protected: | |
| 187 void SetUp() override { | |
| 188 // SetUp crashes on WKWebView creation if running on iOS7. | |
| 189 CR_TEST_REQUIRES_WK_WEB_VIEW(); | |
| 190 JsInjectionManagerTest<web::WebTestWithWKWebViewWebController>::SetUp(); | |
| 191 } | |
| 192 }; | |
| 193 | |
| 194 template <typename WebTestT> | |
| 195 bool JsInjectionManagerTest<WebTestT>::HasReceiverManagers( | |
| 196 NSArray* manager_classes) { | |
| 197 NSDictionary* receiver_managers = | 177 NSDictionary* receiver_managers = |
| 198 [[WebTestT::webController_ jsInjectionReceiver] managers]; | 178 [[webController_ jsInjectionReceiver] managers]; |
| 199 for (Class manager_class in manager_classes) { | 179 for (Class manager_class in manager_classes) { |
| 200 if (![receiver_managers objectForKey:manager_class]) | 180 if (![receiver_managers objectForKey:manager_class]) |
| 201 return false; | 181 return false; |
| 202 } | 182 } |
| 203 return true; | 183 return true; |
| 204 } | 184 } |
| 205 | 185 |
| 206 template <typename WebTestT> | 186 void JsInjectionManagerTest::TestAllDependencies(NSArray* expected_classes, |
| 207 void JsInjectionManagerTest<WebTestT>::TestAllDependencies( | 187 NSArray* actual) { |
| 208 NSArray* expected_classes, | |
| 209 NSArray* actual) { | |
| 210 EXPECT_EQ([expected_classes count], [actual count]); | 188 EXPECT_EQ([expected_classes count], [actual count]); |
| 211 | 189 |
| 212 for (Class manager_class in expected_classes) { | 190 for (Class manager_class in expected_classes) { |
| 213 CRWJSInjectionManager* expected_manager = GetInstanceOfClass(manager_class); | 191 CRWJSInjectionManager* expected_manager = GetInstanceOfClass(manager_class); |
| 214 EXPECT_TRUE([actual containsObject:expected_manager]); | 192 EXPECT_TRUE([actual containsObject:expected_manager]); |
| 215 } | 193 } |
| 216 | 194 |
| 217 for (size_t index = 0; index < [actual count]; ++ index) { | 195 for (size_t index = 0; index < [actual count]; ++ index) { |
| 218 CRWJSInjectionManager* manager = [actual objectAtIndex:index]; | 196 CRWJSInjectionManager* manager = [actual objectAtIndex:index]; |
| 219 for (Class manager_class in [manager directDependencies]) { | 197 for (Class manager_class in [manager directDependencies]) { |
| 220 CRWJSInjectionManager* dependency = GetInstanceOfClass(manager_class); | 198 CRWJSInjectionManager* dependency = GetInstanceOfClass(manager_class); |
| 221 size_t dependency_index = [actual indexOfObject:dependency]; | 199 size_t dependency_index = [actual indexOfObject:dependency]; |
| 222 EXPECT_TRUE(index > dependency_index); | 200 EXPECT_TRUE(index > dependency_index); |
| 223 } | 201 } |
| 224 } | 202 } |
| 225 } | 203 } |
| 226 | 204 |
| 227 template <typename WebTestT> | 205 CRWJSInjectionManager* JsInjectionManagerTest::GetInstanceOfClass( |
| 228 CRWJSInjectionManager* JsInjectionManagerTest<WebTestT>::GetInstanceOfClass( | |
| 229 Class jsInjectionManagerClass) { | 206 Class jsInjectionManagerClass) { |
| 230 return [[WebTestT::webController_ jsInjectionReceiver] | 207 return [[webController_ jsInjectionReceiver] |
| 231 instanceOfClass:jsInjectionManagerClass]; | 208 instanceOfClass:jsInjectionManagerClass]; |
| 232 } | 209 } |
| 233 | 210 |
| 234 WEB_TEST_F(JsInjectionManagerUIWebViewTest, | 211 TEST_F(JsInjectionManagerTest, NoDependencies) { |
| 235 JsInjectionManagerWKWebViewTest, | |
| 236 NoDependencies) { | |
| 237 NSUInteger originalCount = | 212 NSUInteger originalCount = |
| 238 [[[this->webController_ jsInjectionReceiver] managers] count]; | 213 [[[webController_ jsInjectionReceiver] managers] count]; |
| 239 CRWJSInjectionManager* manager = | 214 CRWJSInjectionManager* manager = |
| 240 this->GetInstanceOfClass([TestingCRWJSBaseManager class]); | 215 GetInstanceOfClass([TestingCRWJSBaseManager class]); |
| 241 EXPECT_TRUE(manager); | 216 EXPECT_TRUE(manager); |
| 242 EXPECT_EQ(originalCount + 1U, | 217 EXPECT_EQ(originalCount + 1U, |
| 243 [[[this->webController_ jsInjectionReceiver] managers] count]); | 218 [[[webController_ jsInjectionReceiver] managers] count]); |
| 244 EXPECT_TRUE(this->HasReceiverManagers(@[ [TestingCRWJSBaseManager class] ])); | 219 EXPECT_TRUE(HasReceiverManagers(@[ [TestingCRWJSBaseManager class] ])); |
| 245 EXPECT_FALSE([manager hasBeenInjected]); | 220 EXPECT_FALSE([manager hasBeenInjected]); |
| 246 | 221 |
| 247 [manager inject]; | 222 [manager inject]; |
| 248 EXPECT_TRUE([manager hasBeenInjected]); | 223 EXPECT_TRUE([manager hasBeenInjected]); |
| 249 } | 224 } |
| 250 | 225 |
| 251 WEB_TEST_F(JsInjectionManagerUIWebViewTest, | 226 TEST_F(JsInjectionManagerTest, HasDependencies) { |
| 252 JsInjectionManagerWKWebViewTest, | |
| 253 HasDependencies) { | |
| 254 NSUInteger originalCount = | 227 NSUInteger originalCount = |
| 255 [[[this->webController_ jsInjectionReceiver] managers] count]; | 228 [[[webController_ jsInjectionReceiver] managers] count]; |
| 256 CRWJSInjectionManager* manager = | 229 CRWJSInjectionManager* manager = GetInstanceOfClass([TestingJsManager class]); |
| 257 this->GetInstanceOfClass([TestingJsManager class]); | |
| 258 EXPECT_TRUE(manager); | 230 EXPECT_TRUE(manager); |
| 259 EXPECT_EQ(originalCount + 2U, | 231 EXPECT_EQ(originalCount + 2U, |
| 260 [[[this->webController_ jsInjectionReceiver] managers] count]) | 232 [[[webController_ jsInjectionReceiver] managers] count]) |
| 261 << "Two more CRWJSInjectionManagers should be created."; | 233 << "Two more CRWJSInjectionManagers should be created."; |
| 262 EXPECT_TRUE(this->HasReceiverManagers( | 234 EXPECT_TRUE(HasReceiverManagers( |
| 263 @[ [TestingCRWJSBaseManager class], [TestingCRWJSBaseManager class] ])); | 235 @[ [TestingCRWJSBaseManager class], [TestingCRWJSBaseManager class] ])); |
| 264 | 236 |
| 265 EXPECT_FALSE([manager hasBeenInjected]); | 237 EXPECT_FALSE([manager hasBeenInjected]); |
| 266 | 238 |
| 267 [manager inject]; | 239 [manager inject]; |
| 268 EXPECT_TRUE([manager hasBeenInjected]); | 240 EXPECT_TRUE([manager hasBeenInjected]); |
| 269 EXPECT_TRUE([this->GetInstanceOfClass([TestingCRWJSBaseManager class]) | 241 EXPECT_TRUE( |
| 270 hasBeenInjected]); | 242 [GetInstanceOfClass([TestingCRWJSBaseManager class]) hasBeenInjected]); |
| 271 } | 243 } |
| 272 | 244 |
| 273 WEB_TEST_F(JsInjectionManagerUIWebViewTest, | 245 TEST_F(JsInjectionManagerTest, Dynamic) { |
| 274 JsInjectionManagerWKWebViewTest, | |
| 275 Dynamic) { | |
| 276 CRWJSInjectionManager* manager = | 246 CRWJSInjectionManager* manager = |
| 277 this->GetInstanceOfClass([TestingDynamicJsManager class]); | 247 GetInstanceOfClass([TestingDynamicJsManager class]); |
| 278 EXPECT_TRUE(manager); | 248 EXPECT_TRUE(manager); |
| 279 | 249 |
| 280 EXPECT_FALSE([manager hasBeenInjected]); | 250 EXPECT_FALSE([manager hasBeenInjected]); |
| 281 [manager inject]; | 251 [manager inject]; |
| 282 EXPECT_TRUE([manager hasBeenInjected]); | 252 EXPECT_TRUE([manager hasBeenInjected]); |
| 283 // Ensure that content isn't cached. | 253 // Ensure that content isn't cached. |
| 284 EXPECT_NSNE([manager injectionContent], [manager injectionContent]); | 254 EXPECT_NSNE([manager injectionContent], [manager injectionContent]); |
| 285 } | 255 } |
| 286 | 256 |
| 287 WEB_TEST_F(JsInjectionManagerUIWebViewTest, | 257 TEST_F(JsInjectionManagerTest, HasNestedDependencies) { |
| 288 JsInjectionManagerWKWebViewTest, | |
| 289 HasNestedDependencies) { | |
| 290 NSUInteger originalCount = | 258 NSUInteger originalCount = |
| 291 [[[this->webController_ jsInjectionReceiver] managers] count]; | 259 [[[webController_ jsInjectionReceiver] managers] count]; |
| 292 CRWJSInjectionManager* manager = | 260 CRWJSInjectionManager* manager = |
| 293 this->GetInstanceOfClass([TestingJsManagerWithNestedDependencies class]); | 261 GetInstanceOfClass([TestingJsManagerWithNestedDependencies class]); |
| 294 EXPECT_TRUE(manager); | 262 EXPECT_TRUE(manager); |
| 295 EXPECT_EQ(originalCount + 3U, | 263 EXPECT_EQ(originalCount + 3U, |
| 296 [[[this->webController_ jsInjectionReceiver] managers] count]) | 264 [[[webController_ jsInjectionReceiver] managers] count]) |
| 297 << "Three more CRWJSInjectionManagers should be created."; | 265 << "Three more CRWJSInjectionManagers should be created."; |
| 298 EXPECT_TRUE(this->HasReceiverManagers(@[ | 266 EXPECT_TRUE(HasReceiverManagers(@[ |
| 299 [TestingJsManagerWithNestedDependencies class], | 267 [TestingJsManagerWithNestedDependencies class], |
| 300 [TestingCRWJSBaseManager class], | 268 [TestingCRWJSBaseManager class], [TestingCRWJSBaseManager class] |
| 301 [TestingCRWJSBaseManager class] | |
| 302 ])); | 269 ])); |
| 303 | 270 |
| 304 EXPECT_FALSE([manager hasBeenInjected]); | 271 EXPECT_FALSE([manager hasBeenInjected]); |
| 305 | 272 |
| 306 [manager inject]; | 273 [manager inject]; |
| 307 EXPECT_TRUE([manager hasBeenInjected]); | 274 EXPECT_TRUE([manager hasBeenInjected]); |
| 275 EXPECT_TRUE([GetInstanceOfClass([TestingJsManager class]) hasBeenInjected]); |
| 308 EXPECT_TRUE( | 276 EXPECT_TRUE( |
| 309 [this->GetInstanceOfClass([TestingJsManager class]) hasBeenInjected]); | 277 [GetInstanceOfClass([TestingCRWJSBaseManager class]) hasBeenInjected]); |
| 310 EXPECT_TRUE([this->GetInstanceOfClass([TestingCRWJSBaseManager class]) | |
| 311 hasBeenInjected]); | |
| 312 | 278 |
| 313 NSArray* list = [manager allDependencies]; | 279 NSArray* list = [manager allDependencies]; |
| 314 this->TestAllDependencies( | 280 TestAllDependencies( |
| 315 @[ | 281 @[ |
| 316 [TestingCRWJSBaseManager class], | 282 [TestingCRWJSBaseManager class], [TestingJsManager class], |
| 317 [TestingJsManager class], | |
| 318 [TestingJsManagerWithNestedDependencies class] | 283 [TestingJsManagerWithNestedDependencies class] |
| 319 ], | 284 ], |
| 320 list); | 285 list); |
| 321 } | 286 } |
| 322 | 287 |
| 323 // Tests that checking for an uninjected presence beacon returns false. | 288 // Tests that checking for an uninjected presence beacon returns false. |
| 324 WEB_TEST_F(JsInjectionManagerUIWebViewTest, | 289 TEST_F(JsInjectionManagerTest, WebControllerCheckForUninjectedScript) { |
| 325 JsInjectionManagerWKWebViewTest, | 290 EXPECT_FALSE([webController_ |
| 326 WebControllerCheckForUninjectedScript) { | |
| 327 EXPECT_FALSE([this->webController_ | |
| 328 scriptHasBeenInjectedForClass:Nil | 291 scriptHasBeenInjectedForClass:Nil |
| 329 presenceBeacon:@"__gCrWeb.dummyBeacon"]); | 292 presenceBeacon:@"__gCrWeb.dummyBeacon"]); |
| 330 } | 293 } |
| 331 | 294 |
| 332 WEB_TEST_F(JsInjectionManagerUIWebViewTest, | 295 TEST_F(JsInjectionManagerTest, AllDependencies) { |
| 333 JsInjectionManagerWKWebViewTest, | |
| 334 AllDependencies) { | |
| 335 CRWJSInjectionManager* manager = | 296 CRWJSInjectionManager* manager = |
| 336 this->GetInstanceOfClass([TestingJsManagerComplex class]); | 297 GetInstanceOfClass([TestingJsManagerComplex class]); |
| 337 NSArray* list = [manager allDependencies]; | 298 NSArray* list = [manager allDependencies]; |
| 338 this->TestAllDependencies( | 299 TestAllDependencies( |
| 339 @[ | 300 @[ |
| 340 [TestingCRWJSBaseManager class], | 301 [TestingCRWJSBaseManager class], [TestingAnotherCRWJSBaseManager class], |
| 341 [TestingAnotherCRWJSBaseManager class], | 302 [TestingJsManager class], [TestingAnotherJsManager class], |
| 342 [TestingJsManager class], | |
| 343 [TestingAnotherJsManager class], | |
| 344 [TestingJsManagerWithNestedDependencies class], | 303 [TestingJsManagerWithNestedDependencies class], |
| 345 [TestingJsManagerComplex class] | 304 [TestingJsManagerComplex class] |
| 346 ], | 305 ], |
| 347 list); | 306 list); |
| 348 } | 307 } |
| 349 | 308 |
| 350 } // namespace | 309 } // namespace web |
| OLD | NEW |