blob: 197a31833324d4e3b61792bcdf06c8f072041099 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "base/mac/scoped_nsobject.h"
#import "ios/web/web_state/crw_web_view_proxy_impl.h"
#import "ios/web/web_state/ui/crw_web_controller_container_view.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "third_party/ocmock/gtest_support.h"
#import "third_party/ocmock/OCMock/OCMock.h"
namespace {
// The frame of CRWWebControllerContainerViewTest's |container_view_|.
const CGRect kContainerViewFrame = CGRectMake(0.0f, 0.0f, 200.0f, 300.0f);
// TestToolbarView's frame height.
const CGFloat kTestToolbarViewHeight = 50.0f;
}
// Test view to use as a toolbar.
@interface TestToolbarView : UIView
@end
@implementation TestToolbarView
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(size.width, kTestToolbarViewHeight);
}
@end
// Test CRWWebControllerContainerViewDelegate implementation.
@interface TestWebControllerContainerViewDelegate
: NSObject<CRWWebControllerContainerViewDelegate> {
base::scoped_nsobject<CRWWebViewProxyImpl> _proxy;
}
- (instancetype)initWithContentViewProxy:(CRWWebViewProxyImpl*)proxy;
@end
@implementation TestWebControllerContainerViewDelegate
- (instancetype)initWithContentViewProxy:(CRWWebViewProxyImpl*)proxy {
if ((self = [super init]))
_proxy.reset([proxy retain]);
return self;
}
- (CRWWebViewProxyImpl*)contentViewProxyForContainerView:
(CRWWebControllerContainerView*)containerView {
return _proxy.get();
}
- (CGFloat)headerHeightForContainerView:
(CRWWebControllerContainerView*)containerView {
return 0;
}
@end
#pragma mark - CRWWebControllerContainerViewTest
class CRWWebControllerContainerViewTest : public PlatformTest {
protected:
void SetUp() override {
PlatformTest::SetUp();
CRWWebViewProxyImpl* proxy =
[OCMockObject niceMockForClass:[CRWWebViewProxyImpl class]];
delegate_.reset([[TestWebControllerContainerViewDelegate alloc]
initWithContentViewProxy:proxy]);
container_view_.reset(
[[CRWWebControllerContainerView alloc] initWithDelegate:delegate_]);
[container_view_ setFrame:kContainerViewFrame];
}
// The CRWWebControllerContainerViewDelegate (required for designated
// initializer).
base::scoped_nsobject<TestWebControllerContainerViewDelegate> delegate_;
// The container view being tested.
base::scoped_nsobject<CRWWebControllerContainerView> container_view_;
};
// Tests that |-addToolbar:| will successfully add the passed-in toolbar to the
// container view and will correctly reset its frame to be bottom-aligned with
// the container's width.
TEST_F(CRWWebControllerContainerViewTest, AddToolbar) {
base::scoped_nsobject<TestToolbarView> toolbar(
[[TestToolbarView alloc] initWithFrame:CGRectZero]);
[container_view_ addToolbar:toolbar];
[container_view_ layoutIfNeeded];
// Check that the toolbar has been added to the container view.
EXPECT_TRUE([toolbar isDescendantOfView:container_view_]);
// Check the toolbar's frame.
CGRect expected_toolbar_frame = CGRectMake(
CGRectGetMinX([container_view_ bounds]),
CGRectGetMaxY([container_view_ bounds]) - kTestToolbarViewHeight,
CGRectGetWidth(kContainerViewFrame), kTestToolbarViewHeight);
CGRect toolbar_container_frame =
[container_view_ convertRect:[toolbar bounds] fromView:toolbar];
EXPECT_TRUE(CGRectEqualToRect(expected_toolbar_frame,
toolbar_container_frame));
}