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 #import "ios/clean/chrome/browser/browser_coordinator.h" | |
| 6 #import "ios/clean/chrome/browser/browser_coordinator+internal.h" | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "testing/gtest_mac.h" | |
| 11 #include "testing/platform_test.h" | |
| 12 | |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 14 #error "This file requires ARC support." | |
| 15 #endif | |
| 16 | |
| 17 @interface TestCoordinator : BrowserCoordinator | |
| 18 @property(nonatomic) UIViewController* viewController; | |
| 19 @property(nonatomic, copy) void (^stopHandler)(); | |
| 20 @end | |
| 21 | |
| 22 @implementation TestCoordinator | |
| 23 @synthesize viewController = _viewController; | |
| 24 @synthesize stopHandler = _stopHandler; | |
| 25 | |
| 26 - (instancetype)init { | |
| 27 if (!(self = [super init])) | |
| 28 return nil; | |
| 29 | |
| 30 _viewController = [[UIViewController alloc] init]; | |
| 31 return self; | |
| 32 } | |
| 33 | |
| 34 - (void)stop { | |
| 35 if (self.stopHandler) | |
| 36 self.stopHandler(); | |
| 37 } | |
| 38 | |
| 39 @end | |
| 40 | |
| 41 @interface NonOverlayableCoordinator : TestCoordinator | |
| 42 @end | |
| 43 | |
| 44 @implementation NonOverlayableCoordinator | |
| 45 | |
| 46 - (BOOL)canAddOverlayCoordinator:(BrowserCoordinator*)overlayCoordinator { | |
| 47 return NO; | |
| 48 } | |
| 49 | |
| 50 @end | |
| 51 | |
| 52 TEST(BrowserCoordinatorTest, TestStopOnDealloc) { | |
| 53 __block BOOL called = NO; | |
| 54 | |
| 55 @autoreleasepool { | |
|
lpromero
2017/01/11 13:29:25
Why autoreleasepool and not just braces?
marq (ping after 24h)
2017/01/11 14:06:25
Done.
| |
| 56 TestCoordinator* coordinator = [[TestCoordinator alloc] init]; | |
| 57 coordinator.stopHandler = ^{ | |
| 58 called = YES; | |
| 59 }; | |
| 60 } | |
| 61 | |
| 62 EXPECT_TRUE(called); | |
| 63 } | |
| 64 | |
| 65 TEST(BrowserCoordinatorTest, TestChildren) { | |
| 66 TestCoordinator* parent = [[TestCoordinator alloc] init]; | |
| 67 TestCoordinator* child = [[TestCoordinator alloc] init]; | |
| 68 | |
| 69 [parent addChildCoordinator:child]; | |
| 70 EXPECT_TRUE([parent.children containsObject:child]); | |
| 71 EXPECT_EQ(parent, child.parentCoordinator); | |
| 72 EXPECT_EQ(parent.viewController, child.rootViewController); | |
| 73 | |
| 74 [parent removeChildCoordinator:child]; | |
| 75 EXPECT_FALSE([parent.children containsObject:child]); | |
| 76 EXPECT_EQ(nil, child.parentCoordinator); | |
| 77 // Unparenting shouldn't change a child's rootViewController. | |
| 78 EXPECT_EQ(parent.viewController, child.rootViewController); | |
| 79 | |
| 80 TestCoordinator* otherParent = [[TestCoordinator alloc] init]; | |
| 81 TestCoordinator* otherChild = [[TestCoordinator alloc] init]; | |
| 82 [otherParent addChildCoordinator:otherChild]; | |
| 83 | |
| 84 // -removeChildCoordinator of a non-child should have no affect. | |
| 85 [parent removeChildCoordinator:otherChild]; | |
| 86 EXPECT_TRUE([otherParent.children containsObject:otherChild]); | |
| 87 EXPECT_EQ(otherParent, otherChild.parentCoordinator); | |
| 88 } | |
| 89 | |
| 90 TEST(BrowserCoordinatorTest, TestOverlay) { | |
| 91 TestCoordinator* parent = [[TestCoordinator alloc] init]; | |
| 92 TestCoordinator* child = [[TestCoordinator alloc] init]; | |
| 93 TestCoordinator* grandchild = [[TestCoordinator alloc] init]; | |
| 94 TestCoordinator* overlay = [[TestCoordinator alloc] init]; | |
| 95 TestCoordinator* secondOverlay = [[TestCoordinator alloc] init]; | |
| 96 | |
| 97 EXPECT_TRUE([parent canAddOverlayCoordinator:overlay]); | |
| 98 [parent addChildCoordinator:child]; | |
| 99 [child addChildCoordinator:grandchild]; | |
| 100 EXPECT_FALSE([parent canAddOverlayCoordinator:overlay]); | |
| 101 EXPECT_FALSE([child canAddOverlayCoordinator:overlay]); | |
| 102 EXPECT_TRUE([grandchild canAddOverlayCoordinator:overlay]); | |
| 103 EXPECT_FALSE([grandchild canAddOverlayCoordinator:child]); | |
| 104 | |
| 105 EXPECT_FALSE(overlay.overlaying); | |
| 106 [parent addOverlayCoordinator:overlay]; | |
| 107 EXPECT_TRUE(overlay.overlaying); | |
| 108 EXPECT_EQ(overlay, parent.overlayCoordinator); | |
| 109 EXPECT_EQ(overlay, child.overlayCoordinator); | |
| 110 EXPECT_EQ(overlay, grandchild.overlayCoordinator); | |
| 111 EXPECT_TRUE([grandchild.children containsObject:overlay]); | |
| 112 EXPECT_EQ(grandchild, overlay.parentCoordinator); | |
| 113 | |
| 114 // Shouldn't be able to add a second overlaying coordinator. | |
| 115 EXPECT_FALSE([grandchild canAddOverlayCoordinator:secondOverlay]); | |
| 116 EXPECT_FALSE(secondOverlay.overlaying); | |
| 117 [parent addOverlayCoordinator:secondOverlay]; | |
| 118 EXPECT_FALSE(secondOverlay.overlaying); | |
| 119 | |
| 120 [child removeOverlayCoordinator]; | |
| 121 EXPECT_FALSE(overlay.overlaying); | |
| 122 EXPECT_EQ(nil, parent.overlayCoordinator); | |
| 123 EXPECT_EQ(nil, child.overlayCoordinator); | |
| 124 EXPECT_EQ(nil, grandchild.overlayCoordinator); | |
| 125 EXPECT_FALSE([grandchild.children containsObject:overlay]); | |
| 126 EXPECT_EQ(nil, overlay.parentCoordinator); | |
| 127 | |
| 128 // An implementation that doesn't allow any overlays shouldn't get one. | |
| 129 NonOverlayableCoordinator* noOverlays = | |
| 130 [[NonOverlayableCoordinator alloc] init]; | |
| 131 TestCoordinator* thirdOverlay = [[TestCoordinator alloc] init]; | |
| 132 | |
| 133 EXPECT_FALSE([noOverlays canAddOverlayCoordinator:thirdOverlay]); | |
| 134 EXPECT_FALSE(thirdOverlay.overlaying); | |
| 135 [noOverlays addOverlayCoordinator:thirdOverlay]; | |
| 136 EXPECT_FALSE(thirdOverlay.overlaying); | |
| 137 } | |
| OLD | NEW |