1 // Copyright 2015 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.
5 #import "base/mac/scoped_nsobject.h"
6 #import "ios/web/web_state/crw_web_view_proxy_impl.h"
7 #import "ios/web/web_state/ui/crw_web_controller_container_view.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "testing/gtest_mac.h"
10 #include "testing/platform_test.h"
11 #include "third_party/ocmock/gtest_support.h"
12 #import "third_party/ocmock/OCMock/OCMock.h"
15 // The frame of CRWWebControllerContainerViewTest's |container_view_|.
16 const CGRect kContainerViewFrame = CGRectMake(0.0f, 0.0f, 200.0f, 300.0f);
17 // TestToolbarView's frame height.
18 const CGFloat kTestToolbarViewHeight = 50.0f;
21 // Test view to use as a toolbar.
22 @interface TestToolbarView : UIView
25 @implementation TestToolbarView
26 - (CGSize)sizeThatFits:(CGSize)size {
27 return CGSizeMake(size.width, kTestToolbarViewHeight);
31 #pragma mark - CRWWebControllerContainerViewTest
33 class CRWWebControllerContainerViewTest : public PlatformTest {
35 void SetUp() override {
36 PlatformTest::SetUp();
37 mock_web_view_proxy_.reset(
38 [[OCMockObject niceMockForClass:[CRWWebViewProxyImpl class]] retain]);
39 container_view_.reset([[CRWWebControllerContainerView alloc]
40 initWithContentViewProxy:mock_web_view_proxy_]);
41 [container_view_ setFrame:kContainerViewFrame];
44 // The web view proxy object (required for designated initializer).
45 base::scoped_nsobject<id> mock_web_view_proxy_;
46 // The container view being tested.
47 base::scoped_nsobject<CRWWebControllerContainerView> container_view_;
50 // Tests that |-addToolbar:| will successfully add the passed-in toolbar to the
51 // container view and will correctly reset its frame to be bottom-aligned with
52 // the container's width.
53 TEST_F(CRWWebControllerContainerViewTest, AddToolbar) {
54 base::scoped_nsobject<TestToolbarView> toolbar(
55 [[TestToolbarView alloc] initWithFrame:CGRectZero]);
56 [container_view_ addToolbar:toolbar];
57 [container_view_ layoutIfNeeded];
58 // Check that the toolbar has been added to the container view.
59 EXPECT_TRUE([toolbar isDescendantOfView:container_view_]);
60 // Check the toolbar's frame.
61 CGRect expected_toolbar_frame = CGRectMake(
62 CGRectGetMinX([container_view_ bounds]),
63 CGRectGetMaxY([container_view_ bounds]) - kTestToolbarViewHeight,
64 CGRectGetWidth(kContainerViewFrame), kTestToolbarViewHeight);
65 CGRect toolbar_container_frame =
66 [container_view_ convertRect:[toolbar bounds] fromView:toolbar];
67 EXPECT_TRUE(CGRectEqualToRect(expected_toolbar_frame,
68 toolbar_container_frame));