1 // Copyright (c) 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/omnibox/omnibox_popup_view_mac.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" 11 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" 12 #include "chrome/test/base/testing_profile.h" 13 #include "ui/gfx/font_list.h" 14 #include "ui/gfx/rect.h" 15 #include "ui/gfx/text_elider.h" 16 17 namespace { 18 19 class MockOmniboxPopupViewMac : public OmniboxPopupViewMac { 20 public: 21 MockOmniboxPopupViewMac(OmniboxView* omnibox_view, 22 OmniboxEditModel* edit_model, 23 NSTextField* field) 24 : OmniboxPopupViewMac(omnibox_view, edit_model, field) { 25 } 26 27 void SetResultCount(size_t count) { 28 ACMatches matches; 29 for (size_t i = 0; i < count; ++i) 30 matches.push_back(AutocompleteMatch()); 31 result_.Reset(); 32 result_.AppendMatches(matches); 33 } 34 35 protected: 36 virtual const AutocompleteResult& GetResult() const OVERRIDE { 37 return result_; 38 } 39 40 private: 41 AutocompleteResult result_; 42 }; 43 44 class OmniboxPopupViewMacTest : public CocoaProfileTest { 45 public: 46 OmniboxPopupViewMacTest() {} 47 48 private: 49 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupViewMacTest); 50 }; 51 52 TEST_F(OmniboxPopupViewMacTest, UpdatePopupAppearance) { 53 base::scoped_nsobject<NSTextField> field( 54 [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 20)]); 55 [[test_window() contentView] addSubview:field]; 56 57 OmniboxViewMac view(NULL, profile(), NULL, NULL); 58 MockOmniboxPopupViewMac popup_view(&view, view.model(), field); 59 60 popup_view.UpdatePopupAppearance(); 61 EXPECT_FALSE(popup_view.IsOpen()); 62 EXPECT_EQ(0, [popup_view.matrix() numberOfRows]); 63 64 popup_view.SetResultCount(3); 65 popup_view.UpdatePopupAppearance(); 66 EXPECT_TRUE(popup_view.IsOpen()); 67 EXPECT_EQ(3, [popup_view.matrix() numberOfRows]); 68 69 int old_height = popup_view.GetTargetBounds().height(); 70 popup_view.SetResultCount(5); 71 popup_view.UpdatePopupAppearance(); 72 EXPECT_GT(popup_view.GetTargetBounds().height(), old_height); 73 EXPECT_EQ(5, [popup_view.matrix() numberOfRows]); 74 75 popup_view.SetResultCount(0); 76 popup_view.UpdatePopupAppearance(); 77 EXPECT_FALSE(popup_view.IsOpen()); 78 EXPECT_EQ(0, [popup_view.matrix() numberOfRows]); 79 } 80 81 } // namespace 82