1 // Copyright (c) 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.
5 #include "ui/app_list/views/folder_header_view.h"
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/app_list/app_list_constants.h"
16 #include "ui/app_list/app_list_folder_item.h"
17 #include "ui/app_list/app_list_item.h"
18 #include "ui/app_list/app_list_model.h"
19 #include "ui/app_list/test/app_list_test_model.h"
20 #include "ui/app_list/views/folder_header_view_delegate.h"
21 #include "ui/views/test/views_test_base.h"
28 class TestFolderHeaderViewDelegate
: public FolderHeaderViewDelegate
{
30 TestFolderHeaderViewDelegate() {}
31 virtual ~TestFolderHeaderViewDelegate() {}
33 // FolderHeaderViewDelegate
34 virtual void NavigateBack(AppListFolderItem
* item
,
35 const ui::Event
& event_flags
) OVERRIDE
{}
37 virtual void GiveBackFocusToSearchBox() OVERRIDE
{}
39 virtual void SetItemName(AppListFolderItem
* item
,
40 const std::string
& name
) OVERRIDE
{
44 const std::string
& folder_name() const { return folder_name_
; }
47 std::string folder_name_
;
49 DISALLOW_COPY_AND_ASSIGN(TestFolderHeaderViewDelegate
);
54 class FolderHeaderViewTest
: public views::ViewsTestBase
{
56 FolderHeaderViewTest() {}
57 virtual ~FolderHeaderViewTest() {}
59 // testing::Test overrides:
60 virtual void SetUp() OVERRIDE
{
61 views::ViewsTestBase::SetUp();
62 model_
.reset(new AppListTestModel
);
63 model_
->SetFoldersEnabled(true);
65 delegate_
.reset(new TestFolderHeaderViewDelegate
);
66 folder_header_view_
.reset(new FolderHeaderView(delegate_
.get()));
69 virtual void TearDown() OVERRIDE
{
70 folder_header_view_
.reset(); // Release apps grid view before models.
72 views::ViewsTestBase::TearDown();
76 void UpdateFolderName(const std::string
& name
) {
77 base::string16 folder_name
= base::UTF8ToUTF16(name
);
78 folder_header_view_
->SetFolderNameForTest(folder_name
);
79 folder_header_view_
->ContentsChanged(NULL
, folder_name
);
82 const std::string
GetFolderNameFromUI() {
83 return base::UTF16ToUTF8(folder_header_view_
->GetFolderNameForTest());
86 bool CanEditFolderName() {
87 return folder_header_view_
->IsFolderNameEnabledForTest();
90 scoped_ptr
<AppListTestModel
> model_
;
91 scoped_ptr
<FolderHeaderView
> folder_header_view_
;
92 scoped_ptr
<TestFolderHeaderViewDelegate
> delegate_
;
95 DISALLOW_COPY_AND_ASSIGN(FolderHeaderViewTest
);
98 TEST_F(FolderHeaderViewTest
, SetFolderName
) {
99 // Creating a folder with empty folder name.
100 AppListFolderItem
* folder_item
= model_
->CreateAndPopulateFolderWithApps(2);
101 folder_header_view_
->SetFolderItem(folder_item
);
102 EXPECT_EQ("", GetFolderNameFromUI());
103 EXPECT_TRUE(CanEditFolderName());
105 // Update UI to set folder name to "test folder".
106 UpdateFolderName("test folder");
107 EXPECT_EQ("test folder", delegate_
->folder_name());
110 TEST_F(FolderHeaderViewTest
, MaxFoldernNameLength
) {
111 // Creating a folder with empty folder name.
112 AppListFolderItem
* folder_item
= model_
->CreateAndPopulateFolderWithApps(2);
113 folder_header_view_
->SetFolderItem(folder_item
);
114 EXPECT_EQ("", GetFolderNameFromUI());
115 EXPECT_TRUE(CanEditFolderName());
117 // Update UI to set folder name to really long one beyond its maxium limit,
118 // The folder name should be trucated to the maximum length.
119 std::string max_len_name
;
120 for (size_t i
= 0; i
< kMaxFolderNameChars
; ++i
)
122 UpdateFolderName(max_len_name
);
123 EXPECT_EQ(max_len_name
, delegate_
->folder_name());
124 std::string too_long_name
= max_len_name
+ "a";
125 UpdateFolderName(too_long_name
);
126 EXPECT_EQ(max_len_name
, delegate_
->folder_name());
129 TEST_F(FolderHeaderViewTest
, OemFolderNameNotEditable
) {
130 AppListFolderItem
* folder_item
= model_
->CreateAndAddOemFolder("oem folder");
131 folder_header_view_
->SetFolderItem(folder_item
);
132 EXPECT_EQ("", GetFolderNameFromUI());
133 EXPECT_FALSE(CanEditFolderName());
137 } // namespace app_list