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/ui/crw_web_controller_container_view.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "testing/gtest_mac.h"
9 #include "testing/platform_test.h"
12 // The frame of CRWWebControllerContainerViewTest's |container_view_|.
13 const CGRect kContainerViewFrame = CGRectMake(0.0f, 0.0f, 200.0f, 300.0f);
14 // TestToolbarView's frame height.
15 const CGFloat kTestToolbarViewHeight = 50.0f;
18 // Test view to use as a toolbar.
19 @interface TestToolbarView : UIView
22 @implementation TestToolbarView
23 - (CGSize)sizeThatFits:(CGSize)size {
24 return CGSizeMake(size.width, kTestToolbarViewHeight);
28 #pragma mark - CRWWebControllerContainerViewTest
30 class CRWWebControllerContainerViewTest : public PlatformTest {
32 void SetUp() override {
33 PlatformTest::SetUp();
34 container_view_.reset([[CRWWebControllerContainerView alloc]
35 initWithFrame:kContainerViewFrame]);
38 // The container view being tested.
39 base::scoped_nsobject<CRWWebControllerContainerView> container_view_;
42 // Tests that |-addToolbar:| will successfully add the passed-in toolbar to the
43 // container view and will correctly reset its frame to be bottom-aligned with
44 // the container's width.
45 TEST_F(CRWWebControllerContainerViewTest, AddToolbar) {
46 base::scoped_nsobject<TestToolbarView> toolbar(
47 [[TestToolbarView alloc] initWithFrame:CGRectZero]);
48 [container_view_ addToolbar:toolbar];
49 [container_view_ layoutIfNeeded];
50 // Check that the toolbar has been added to the container view.
51 EXPECT_TRUE([toolbar isDescendantOfView:container_view_]);
52 // Check the toolbar's frame.
53 CGRect expected_toolbar_frame = CGRectMake(
54 CGRectGetMinX([container_view_ bounds]),
55 CGRectGetMaxY([container_view_ bounds]) - kTestToolbarViewHeight,
56 CGRectGetWidth(kContainerViewFrame), kTestToolbarViewHeight);
57 CGRect toolbar_container_frame =
58 [container_view_ convertRect:[toolbar bounds] fromView:toolbar];
59 EXPECT_TRUE(CGRectEqualToRect(expected_toolbar_frame,
60 toolbar_container_frame));