1// Copyright 2014 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#include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
6#include "testing/gtest/include/gtest/gtest.h"
7
8TEST(OmniboxResultViewTest, CheckComputeMatchWidths) {
9    int contents_max_width, description_max_width;
10    const int separator_width = 10;
11
12    // Both contents and description fit fine.
13    OmniboxResultView::ComputeMatchMaxWidths(
14        100, separator_width, 50, 200, true, &contents_max_width,
15        &description_max_width);
16    EXPECT_EQ(-1, contents_max_width);
17    EXPECT_EQ(-1, description_max_width);
18
19    // Contents should be given as much space as it wants up to 300 pixels.
20    OmniboxResultView::ComputeMatchMaxWidths(
21        100, separator_width, 50, 100, true, &contents_max_width,
22        &description_max_width);
23    EXPECT_EQ(-1, contents_max_width);
24    EXPECT_EQ(0, description_max_width);
25
26    // Description should be hidden if it's at least 75 pixels wide but doesn't
27    // get 75 pixels of space.
28    OmniboxResultView::ComputeMatchMaxWidths(
29        300, separator_width, 75, 384, true, &contents_max_width,
30        &description_max_width);
31    EXPECT_EQ(-1, contents_max_width);
32    EXPECT_EQ(0, description_max_width);
33
34    // Both contents and description will be limited.
35    OmniboxResultView::ComputeMatchMaxWidths(
36        310, separator_width, 150, 400, true, &contents_max_width,
37        &description_max_width);
38    EXPECT_EQ(300, contents_max_width);
39    EXPECT_EQ(400 - 300 - separator_width, description_max_width);
40
41    // Contents takes all available space.
42    OmniboxResultView::ComputeMatchMaxWidths(
43        400, separator_width, 0, 200, true, &contents_max_width,
44        &description_max_width);
45    EXPECT_EQ(-1, contents_max_width);
46    EXPECT_EQ(0, description_max_width);
47
48    // Half and half.
49    OmniboxResultView::ComputeMatchMaxWidths(
50        395, separator_width, 395, 700, true, &contents_max_width,
51        &description_max_width);
52    EXPECT_EQ((700 - separator_width) / 2, contents_max_width);
53    EXPECT_EQ((700 - separator_width) / 2, description_max_width);
54
55    // When we disallow shrinking the contents, it should get as much space as
56    // it wants.
57    OmniboxResultView::ComputeMatchMaxWidths(
58        395, separator_width, 395, 700, false, &contents_max_width,
59        &description_max_width);
60    EXPECT_EQ(-1, contents_max_width);
61    EXPECT_EQ(295, description_max_width);
62
63    // (available_width - separator_width) is odd, so contents gets the extra
64    // pixel.
65    OmniboxResultView::ComputeMatchMaxWidths(
66        395, separator_width, 395, 699, true, &contents_max_width,
67        &description_max_width);
68    EXPECT_EQ((700 - separator_width) / 2, contents_max_width);
69    EXPECT_EQ((700 - separator_width) / 2 - 1, description_max_width);
70
71    // Not enough space to draw anything.
72    OmniboxResultView::ComputeMatchMaxWidths(
73        1, 1, 1, 0, true, &contents_max_width, &description_max_width);
74    EXPECT_EQ(0, contents_max_width);
75    EXPECT_EQ(0, description_max_width);
76}
77