Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / app_list / views / folder_header_view_unittest.cc
blob94a70d6376bc7777768305e2f8d482b498226028
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"
7 #include <string>
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"
23 namespace app_list {
24 namespace test {
26 namespace {
28 class TestFolderHeaderViewDelegate : public FolderHeaderViewDelegate {
29 public:
30 TestFolderHeaderViewDelegate() {}
31 ~TestFolderHeaderViewDelegate() override {}
33 // FolderHeaderViewDelegate
34 void NavigateBack(AppListFolderItem* item,
35 const ui::Event& event_flags) override {}
37 void GiveBackFocusToSearchBox() override {}
39 void SetItemName(AppListFolderItem* item, const std::string& name) override {
40 folder_name_ = name;
43 const std::string& folder_name() const { return folder_name_; }
45 private:
46 std::string folder_name_;
48 DISALLOW_COPY_AND_ASSIGN(TestFolderHeaderViewDelegate);
51 } // namespace
53 class FolderHeaderViewTest : public views::ViewsTestBase {
54 public:
55 FolderHeaderViewTest() {}
56 ~FolderHeaderViewTest() override {}
58 // testing::Test overrides:
59 void SetUp() override {
60 views::ViewsTestBase::SetUp();
61 model_.reset(new AppListTestModel);
62 model_->SetFoldersEnabled(true);
64 delegate_.reset(new TestFolderHeaderViewDelegate);
65 folder_header_view_.reset(new FolderHeaderView(delegate_.get()));
68 void TearDown() override {
69 folder_header_view_.reset(); // Release apps grid view before models.
70 delegate_.reset();
71 views::ViewsTestBase::TearDown();
74 protected:
75 void UpdateFolderName(const std::string& name) {
76 base::string16 folder_name = base::UTF8ToUTF16(name);
77 folder_header_view_->SetFolderNameForTest(folder_name);
78 folder_header_view_->ContentsChanged(NULL, folder_name);
81 const std::string GetFolderNameFromUI() {
82 return base::UTF16ToUTF8(folder_header_view_->GetFolderNameForTest());
85 bool CanEditFolderName() {
86 return folder_header_view_->IsFolderNameEnabledForTest();
89 scoped_ptr<AppListTestModel> model_;
90 scoped_ptr<FolderHeaderView> folder_header_view_;
91 scoped_ptr<TestFolderHeaderViewDelegate> delegate_;
93 private:
94 DISALLOW_COPY_AND_ASSIGN(FolderHeaderViewTest);
97 TEST_F(FolderHeaderViewTest, SetFolderName) {
98 // Creating a folder with empty folder name.
99 AppListFolderItem* folder_item = model_->CreateAndPopulateFolderWithApps(2);
100 folder_header_view_->SetFolderItem(folder_item);
101 EXPECT_EQ("", GetFolderNameFromUI());
102 EXPECT_TRUE(CanEditFolderName());
104 // Update UI to set folder name to "test folder".
105 UpdateFolderName("test folder");
106 EXPECT_EQ("test folder", delegate_->folder_name());
109 TEST_F(FolderHeaderViewTest, MaxFoldernNameLength) {
110 // Creating a folder with empty folder name.
111 AppListFolderItem* folder_item = model_->CreateAndPopulateFolderWithApps(2);
112 folder_header_view_->SetFolderItem(folder_item);
113 EXPECT_EQ("", GetFolderNameFromUI());
114 EXPECT_TRUE(CanEditFolderName());
116 // Update UI to set folder name to really long one beyond its maxium limit,
117 // The folder name should be trucated to the maximum length.
118 std::string max_len_name;
119 for (size_t i = 0; i < kMaxFolderNameChars; ++i)
120 max_len_name += "a";
121 UpdateFolderName(max_len_name);
122 EXPECT_EQ(max_len_name, delegate_->folder_name());
123 std::string too_long_name = max_len_name + "a";
124 UpdateFolderName(too_long_name);
125 EXPECT_EQ(max_len_name, delegate_->folder_name());
128 TEST_F(FolderHeaderViewTest, OemFolderNameNotEditable) {
129 AppListFolderItem* folder_item = model_->CreateAndAddOemFolder("oem folder");
130 folder_header_view_->SetFolderItem(folder_item);
131 EXPECT_EQ("", GetFolderNameFromUI());
132 EXPECT_FALSE(CanEditFolderName());
135 } // namespace test
136 } // namespace app_list