selected_keyword_decoration_unittest.mm revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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 <Cocoa/Cocoa.h>
6
7#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
8#import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
9#include "testing/gtest/include/gtest/gtest.h"
10#import "testing/gtest_mac.h"
11
12namespace {
13
14// A wide width which should fit everything.
15const CGFloat kWidth(300.0);
16
17// A narrow width for tests which test things that don't fit.
18const CGFloat kNarrowWidth(5.0);
19
20}  // namespace
21
22class SelectedKeywordDecorationTest : public CocoaTest {
23 public:
24  SelectedKeywordDecorationTest()
25      : decoration_([NSFont userFontOfSize:12]) {
26  }
27
28  SelectedKeywordDecoration decoration_;
29};
30
31// Test that the cell correctly chooses the partial keyword if there's
32// not enough room.
33TEST_F(SelectedKeywordDecorationTest, UsesPartialKeywordIfNarrow) {
34
35  const std::wstring kKeyword(L"Engine");
36  NSString* const kFullString = @"Search Engine:";
37  NSString* const kPartialString = @"Search En\u2026:";  // ellipses
38
39  decoration_.SetKeyword(kKeyword, false);
40
41  // Wide width chooses the full string and image.
42  const CGFloat all_width = decoration_.GetWidthForSpace(kWidth);
43  EXPECT_TRUE(decoration_.image_);
44  EXPECT_NSEQ(kFullString, decoration_.label_);
45
46  // If not enough space to include the image, uses exactly the full
47  // string.
48  const CGFloat full_width = decoration_.GetWidthForSpace(all_width - 5.0);
49  EXPECT_LT(full_width, all_width);
50  EXPECT_FALSE(decoration_.image_);
51  EXPECT_NSEQ(kFullString, decoration_.label_);
52
53  // Narrow width chooses the partial string.
54  const CGFloat partial_width = decoration_.GetWidthForSpace(kNarrowWidth);
55  EXPECT_LT(partial_width, full_width);
56  EXPECT_FALSE(decoration_.image_);
57  EXPECT_NSEQ(kPartialString, decoration_.label_);
58
59  // Narrow doesn't choose partial string if there is not one.
60  decoration_.partial_string_.reset();
61  decoration_.GetWidthForSpace(kNarrowWidth);
62  EXPECT_FALSE(decoration_.image_);
63  EXPECT_NSEQ(kFullString, decoration_.label_);
64}
65