OLD | NEW |
| (Empty) |
1 // Copyright 2012 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 "chrome/browser/ui/cocoa/tab_contents/overlayable_contents_controller.h" | |
6 | |
7 #include "chrome/browser/profiles/profile.h" | |
8 #include "chrome/browser/ui/browser.h" | |
9 #include "chrome/browser/ui/browser_window.h" | |
10 #include "chrome/browser/ui/cocoa/tab_contents/instant_overlay_controller_mac.h" | |
11 #include "chrome/browser/ui/search/instant_overlay_model.h" | |
12 #include "chrome/test/base/in_process_browser_test.h" | |
13 #include "content/public/browser/notification_source.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "content/public/browser/web_contents_view.h" | |
16 #import "testing/gtest_mac.h" | |
17 | |
18 class OverlayableContentsControllerTest : public InProcessBrowserTest, | |
19 public content::NotificationObserver { | |
20 public: | |
21 OverlayableContentsControllerTest() : instant_overlay_model_(NULL), | |
22 visibility_changed_count_(0) { | |
23 } | |
24 | |
25 virtual void SetUpOnMainThread() OVERRIDE { | |
26 web_contents_.reset(content::WebContents::Create( | |
27 content::WebContents::CreateParams(browser()->profile()))); | |
28 instant_overlay_model_.SetOverlayContents(web_contents_.get()); | |
29 | |
30 controller_.reset([[OverlayableContentsController alloc] | |
31 initWithBrowser:browser()]); | |
32 [[controller_ view] setFrame:NSMakeRect(0, 0, 100, 200)]; | |
33 instant_overlay_model_.AddObserver([controller_ instantOverlayController]); | |
34 } | |
35 | |
36 virtual void CleanUpOnMainThread() OVERRIDE { | |
37 instant_overlay_model_.RemoveObserver( | |
38 [controller_ instantOverlayController]); | |
39 instant_overlay_model_.SetOverlayContents(NULL); | |
40 controller_.reset(); | |
41 web_contents_.reset(); | |
42 } | |
43 | |
44 void VerifyOverlayFrame(CGFloat expected_height, | |
45 InstantSizeUnits units) { | |
46 NSRect container_bounds = [[controller_ view] bounds]; | |
47 NSRect overlay_frame = | |
48 [web_contents_->GetView()->GetNativeView() frame]; | |
49 | |
50 EXPECT_EQ(NSMinX(container_bounds), NSMinX(overlay_frame)); | |
51 EXPECT_EQ(NSWidth(container_bounds), NSWidth(overlay_frame)); | |
52 switch (units) { | |
53 case INSTANT_SIZE_PIXELS: | |
54 EXPECT_EQ(expected_height, NSHeight(overlay_frame)); | |
55 EXPECT_EQ(NSMaxY(container_bounds), NSMaxY(overlay_frame)); | |
56 break; | |
57 case INSTANT_SIZE_PERCENT: | |
58 EXPECT_EQ((expected_height * NSHeight(container_bounds)) / 100, | |
59 NSHeight(overlay_frame)); | |
60 EXPECT_EQ(NSMaxY(container_bounds), NSMaxY(overlay_frame)); | |
61 } | |
62 } | |
63 | |
64 virtual void Observe(int type, | |
65 const content::NotificationSource& source, | |
66 const content::NotificationDetails& details) OVERRIDE { | |
67 if (type == content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED) | |
68 ++visibility_changed_count_; | |
69 } | |
70 | |
71 protected: | |
72 InstantOverlayModel instant_overlay_model_; | |
73 scoped_ptr<content::WebContents> web_contents_; | |
74 base::scoped_nsobject<OverlayableContentsController> controller_; | |
75 content::NotificationRegistrar registrar_; | |
76 int visibility_changed_count_; | |
77 }; | |
78 | |
79 // Verify that the view is correctly laid out when size is specified in percent. | |
80 IN_PROC_BROWSER_TEST_F(OverlayableContentsControllerTest, SizePerecent) { | |
81 SearchMode mode; | |
82 mode.mode = SearchMode::MODE_NTP; | |
83 CGFloat expected_height = 30; | |
84 InstantSizeUnits units = INSTANT_SIZE_PERCENT; | |
85 instant_overlay_model_.SetOverlayState(mode, expected_height, units); | |
86 | |
87 EXPECT_NSEQ([web_contents_->GetView()->GetNativeView() superview], | |
88 [controller_ view]); | |
89 VerifyOverlayFrame(expected_height, units); | |
90 | |
91 // Resize the view and verify that the overlay is also resized. | |
92 [[controller_ view] setFrameSize:NSMakeSize(300, 400)]; | |
93 VerifyOverlayFrame(expected_height, units); | |
94 } | |
95 | |
96 // Verify that the view is correctly laid out when size is specified in pixels. | |
97 IN_PROC_BROWSER_TEST_F(OverlayableContentsControllerTest, SizePixels) { | |
98 SearchMode mode; | |
99 mode.mode = SearchMode::MODE_NTP; | |
100 CGFloat expected_height = 30; | |
101 InstantSizeUnits units = INSTANT_SIZE_PIXELS; | |
102 instant_overlay_model_.SetOverlayState(mode, expected_height, units); | |
103 | |
104 EXPECT_NSEQ([web_contents_->GetView()->GetNativeView() superview], | |
105 [controller_ view]); | |
106 VerifyOverlayFrame(expected_height, units); | |
107 | |
108 // Resize the view and verify that the overlay is also resized. | |
109 [[controller_ view] setFrameSize:NSMakeSize(300, 400)]; | |
110 VerifyOverlayFrame(expected_height, units); | |
111 } | |
112 | |
113 // Verify that the web contents is not hidden when just the height changes. | |
114 IN_PROC_BROWSER_TEST_F(OverlayableContentsControllerTest, HeightChangeNoHide) { | |
115 SearchMode mode; | |
116 mode.mode = SearchMode::MODE_SEARCH_SUGGESTIONS; | |
117 instant_overlay_model_.SetOverlayState(mode, 10, INSTANT_SIZE_PERCENT); | |
118 | |
119 registrar_.Add(this, | |
120 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
121 content::Source<content::WebContents>(web_contents_.get())); | |
122 EXPECT_EQ(0, visibility_changed_count_); | |
123 instant_overlay_model_.SetOverlayState(mode, 11, INSTANT_SIZE_PERCENT); | |
124 EXPECT_EQ(1, visibility_changed_count_); | |
125 } | |
OLD | NEW |