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.
5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h"
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 "components/omnibox/browser/autocomplete_input.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/text_elider.h"
20 class MockOmniboxPopupViewMac : public OmniboxPopupViewMac {
22 MockOmniboxPopupViewMac(OmniboxView* omnibox_view,
23 OmniboxEditModel* edit_model,
25 : OmniboxPopupViewMac(omnibox_view, edit_model, field) {
28 void SetResultCount(size_t count) {
30 for (size_t i = 0; i < count; ++i)
31 matches.push_back(AutocompleteMatch());
33 result_.AppendMatches(AutocompleteInput(), matches);
37 const AutocompleteResult& GetResult() const override { return result_; }
40 AutocompleteResult result_;
43 class OmniboxPopupViewMacTest : public CocoaProfileTest {
45 OmniboxPopupViewMacTest() {}
48 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupViewMacTest);
51 TEST_F(OmniboxPopupViewMacTest, UpdatePopupAppearance) {
52 base::scoped_nsobject<NSTextField> field(
53 [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 20)]);
54 [[test_window() contentView] addSubview:field];
56 OmniboxViewMac view(NULL, profile(), NULL, NULL);
57 MockOmniboxPopupViewMac popup_view(&view, view.model(), field);
59 popup_view.UpdatePopupAppearance();
60 EXPECT_FALSE(popup_view.IsOpen());
61 EXPECT_EQ(0, [popup_view.matrix() numberOfRows]);
63 popup_view.SetResultCount(3);
64 popup_view.UpdatePopupAppearance();
65 EXPECT_TRUE(popup_view.IsOpen());
66 EXPECT_EQ(3, [popup_view.matrix() numberOfRows]);
68 int old_height = popup_view.GetTargetBounds().height();
69 popup_view.SetResultCount(5);
70 popup_view.UpdatePopupAppearance();
71 EXPECT_GT(popup_view.GetTargetBounds().height(), old_height);
72 EXPECT_EQ(5, [popup_view.matrix() numberOfRows]);
74 popup_view.SetResultCount(0);
75 popup_view.UpdatePopupAppearance();
76 EXPECT_FALSE(popup_view.IsOpen());
77 EXPECT_EQ(0, [popup_view.matrix() numberOfRows]);