GINDurableDeepLinkServiceReceivingTests.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <UIKit/UIKit.h>
  17. #import <XCTest/XCTest.h>
  18. #import "DynamicLinks/GINDurableDeepLinkServiceReceiving+Private.h"
  19. #import "OCMock.h"
  20. @interface GINDurableDeepLinkServiceReceivingTests : XCTestCase
  21. @end
  22. @implementation GINDurableDeepLinkServiceReceivingTests
  23. - (void)testGINGetMainWindowRetrievesWindowWhenWindowIsKey {
  24. UIWindow *window = [[UIWindow alloc] init];
  25. id application = OCMPartialMock([UIApplication sharedApplication]);
  26. [[[application stub] andReturn:window] keyWindow];
  27. id returnedObject = GINGetMainWindow(application);
  28. XCTAssertEqual(returnedObject, window);
  29. }
  30. - (void)testGINGetMainWindowRetrievesWindowWhenWindowIsDelegateWindowAndNotKey {
  31. UIWindow *window = [[UIWindow alloc] init];
  32. id appDelegate = OCMProtocolMock(@protocol(UIApplicationDelegate));
  33. [[[appDelegate stub] andReturn:window] window];
  34. id application = OCMPartialMock([UIApplication sharedApplication]);
  35. [[[application stub] andReturn:nil] keyWindow];
  36. [[[application stub] andReturn:appDelegate] delegate];
  37. id returnedObject = GINGetMainWindow(application);
  38. XCTAssertEqual(returnedObject, window);
  39. }
  40. - (void)testGINGetMainWindowRetrievesNilWhenNoKeyWindowAndNoDelegateWindow {
  41. id application = OCMPartialMock([UIApplication sharedApplication]);
  42. [[[application stub] andReturn:nil] keyWindow];
  43. [[[application stub] andReturn:OCMOCK_ANY] delegate];
  44. id returnedObject = GINGetMainWindow(application);
  45. XCTAssertNil(returnedObject);
  46. }
  47. - (void)testGINGetTopViewControllerFromViewControllerReturnsNilWithNilVC {
  48. id returnedObject = GINGetTopViewControllerFromViewController(nil);
  49. XCTAssertNil(returnedObject);
  50. }
  51. - (void)testGINGetTopViewControllerFromViewControllerReturnsSameVCWhenNotAContainerVC {
  52. UIViewController *viewController = [[UIViewController alloc] init];
  53. id returnedObject = GINGetTopViewControllerFromViewController(viewController);
  54. XCTAssertEqual(viewController, returnedObject);
  55. }
  56. - (void)testGINGetTopViewControllerFromViewControllerReturnsTopVCOfNavVC {
  57. UIViewController *topViewController = [[UIViewController alloc] init];
  58. UINavigationController *navViewController =
  59. [[UINavigationController alloc] initWithRootViewController:topViewController];
  60. id returnedObject = GINGetTopViewControllerFromViewController(navViewController);
  61. XCTAssertEqual(topViewController, returnedObject);
  62. }
  63. - (void)testGINGetTopViewControllerFromViewControllerReturnsFocusOfTabVC {
  64. UIViewController *tabVC = [[UIViewController alloc] init];
  65. UITabBarController *tabBarController = [[UITabBarController alloc] init];
  66. tabBarController.viewControllers = @[ tabVC ];
  67. id returnedObject = GINGetTopViewControllerFromViewController(tabBarController);
  68. XCTAssertEqual(tabVC, returnedObject);
  69. }
  70. - (void)testGINGetTopViewControllerFromViewControllerRetunsPresentedViewController {
  71. UIViewController *presentedViewController = [[UIViewController alloc] init];
  72. id presentingViewController = OCMPartialMock([[UIViewController alloc] init]);
  73. [[[presentingViewController stub] andReturn:presentedViewController] presentedViewController];
  74. id returnedObject = GINGetTopViewControllerFromViewController(presentingViewController);
  75. XCTAssertEqual(presentedViewController, returnedObject);
  76. }
  77. - (void)testGINRemoveViewControllerFromHierarchyRemovesFromParent {
  78. id viewController = OCMClassMock([UIViewController class]);
  79. [[[viewController stub] andReturn:OCMOCK_ANY] parentViewController];
  80. [OCMStub([viewController removeFromParentViewController]) andDo:nil];
  81. GINRemoveViewControllerFromHierarchy(viewController);
  82. OCMVerify([viewController removeFromParentViewController]);
  83. }
  84. - (void)testGINRemoveViewControllerFromHierarchyRemovesViewFromSuperview {
  85. id view = OCMClassMock([UIView class]);
  86. [[[view stub] andReturn:OCMOCK_ANY] superview];
  87. [OCMStub([view removeFromSuperview]) andDo:nil];
  88. id viewController = OCMPartialMock([[UIViewController alloc] init]);
  89. [[[viewController stub] andReturn:view] view];
  90. GINRemoveViewControllerFromHierarchy(viewController);
  91. OCMVerify([view removeFromSuperview]);
  92. }
  93. - (void)testGINRemoveViewControllerFromHierarchyDoesntRemoveFromParentIfNoParent {
  94. id viewController = OCMClassMock([UIViewController class]);
  95. [[[viewController stub] andReturn:nil] parentViewController];
  96. [[viewController reject] removeFromParentViewController];
  97. GINRemoveViewControllerFromHierarchy(viewController);
  98. [viewController verify];
  99. }
  100. - (void)testGINRemoveViewControllerFromHierarchyDoesntRemoveViewFromSuperviewIfNoSuperview {
  101. id view = OCMClassMock([UIView class]);
  102. [[[view stub] andReturn:nil] superview];
  103. [[view reject] removeFromSuperview];
  104. id viewController = OCMPartialMock([[UIViewController alloc] init]);
  105. [[[viewController stub] andReturn:view] view];
  106. GINRemoveViewControllerFromHierarchy(viewController);
  107. [view verify];
  108. }
  109. @end