1 // Copyright 2013 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/search/search_tab_helper.h"
6 
7 #include <stdint.h>
8 
9 #include <memory>
10 #include <string>
11 #include <utility>
12 
13 #include "base/bind.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/sync/profile_sync_service_factory.h"
16 #include "chrome/browser/ui/search/search_ipc_router.h"
17 #include "chrome/common/search/mock_embedded_search_client.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "chrome/test/base/browser_with_test_window_test.h"
21 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "components/strings/grit/components_strings.h"
24 #include "components/sync/driver/test_sync_service.h"
25 #include "content/public/browser/web_contents.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/gfx/vector_icon_types.h"
30 #include "url/gurl.h"
31 
32 using testing::_;
33 using testing::NiceMock;
34 using testing::Return;
35 
36 namespace {
37 
38 class MockEmbeddedSearchClientFactory
39     : public SearchIPCRouter::EmbeddedSearchClientFactory {
40  public:
41   MOCK_METHOD0(GetEmbeddedSearchClient,
42                search::mojom::EmbeddedSearchClient*(void));
43 };
44 
45 }  // namespace
46 
47 class SearchTabHelperTest : public ChromeRenderViewHostTestHarness {
48  public:
SearchTabHelperTest()49   SearchTabHelperTest() {}
50 
SetUp()51   void SetUp() override {
52     ChromeRenderViewHostTestHarness::SetUp();
53     SearchTabHelper::CreateForWebContents(web_contents());
54     auto* search_tab = SearchTabHelper::FromWebContents(web_contents());
55     auto factory =
56         std::make_unique<NiceMock<MockEmbeddedSearchClientFactory>>();
57     ON_CALL(*factory, GetEmbeddedSearchClient())
58         .WillByDefault(Return(&mock_embedded_search_client_));
59     search_tab->ipc_router_for_testing()
60         .set_embedded_search_client_factory_for_testing(std::move(factory));
61   }
62 
GetTestingFactories() const63   TestingProfile::TestingFactories GetTestingFactories() const override {
64     return {{ProfileSyncServiceFactory::GetInstance(),
65              base::BindRepeating(
66                  [](content::BrowserContext*) -> std::unique_ptr<KeyedService> {
67                    return std::make_unique<syncer::TestSyncService>();
68                  })}};
69   }
70 
71   // Configure the account to |sync_history| or not.
SetHistorySync(bool sync_history)72   void SetHistorySync(bool sync_history) {
73     syncer::TestSyncService* sync_service =
74         static_cast<syncer::TestSyncService*>(
75             ProfileSyncServiceFactory::GetForProfile(profile()));
76 
77     sync_service->SetFirstSetupComplete(true);
78     syncer::ModelTypeSet types;
79     if (sync_history) {
80       types.Put(syncer::TYPED_URLS);
81     }
82     sync_service->SetPreferredDataTypes(types);
83   }
84 
85  private:
86   NiceMock<MockEmbeddedSearchClient> mock_embedded_search_client_;
87 };
88 
TEST_F(SearchTabHelperTest,FileSelectedUpdatesLastSelectedDirectory)89 TEST_F(SearchTabHelperTest, FileSelectedUpdatesLastSelectedDirectory) {
90   NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl));
91   SearchTabHelper* search_tab_helper =
92       SearchTabHelper::FromWebContents(web_contents());
93   ASSERT_NE(nullptr, search_tab_helper);
94 
95   base::FilePath filePath =
96       base::FilePath::FromUTF8Unsafe("a/b/c/Picture/kitten.png");
97   search_tab_helper->FileSelected(filePath, 0, {});
98   Profile* profile = search_tab_helper->profile();
99   EXPECT_EQ(filePath.DirName(), profile->last_selected_directory());
100 }
101 
TEST_F(SearchTabHelperTest,TitleIsSetForNTP)102 TEST_F(SearchTabHelperTest, TitleIsSetForNTP) {
103   NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl));
104   EXPECT_EQ(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE),
105             web_contents()->GetTitle());
106 }
107