File: instant_service_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 3,734 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/search/instant_service.h"

#include <vector>

#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/mock_callback.h"
#include "build/build_config.h"
#include "chrome/browser/search/instant_service_observer.h"
#include "chrome/browser/search/instant_unittest_base.h"
#include "chrome/browser/themes/test/theme_service_changed_waiter.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/search/instant_types.h"
#include "chrome/common/url_constants.h"
#include "components/ntp_tiles/features.h"
#include "components/ntp_tiles/ntp_tile.h"
#include "components/ntp_tiles/section_type.h"
#include "components/search/ntp_features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/native_theme/test_native_theme.h"
#include "url/gurl.h"

namespace {

class MockInstantServiceObserver : public InstantServiceObserver {
 public:
  MOCK_METHOD1(NtpThemeChanged, void(NtpTheme));
  MOCK_METHOD1(MostVisitedInfoChanged, void(const InstantMostVisitedInfo&));
};

class MockInstantService : public InstantService {
 public:
  explicit MockInstantService(Profile* profile) : InstantService(profile) {}
  ~MockInstantService() override = default;

  MOCK_METHOD0(ResetCustomBackgroundNtpTheme, void());
};
}  // namespace

using InstantServiceTest = InstantUnitTestBase;

TEST_F(InstantServiceTest, GetNTPTileSuggestion) {
  ntp_tiles::NTPTile some_tile;
  some_tile.url = GURL("https://foo.com");
  some_tile.title = u"Foo";
  some_tile.favicon_url = GURL("https://foo.com/favicon");
  ntp_tiles::NTPTilesVector suggestions{some_tile};

  std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector> suggestions_map;
  suggestions_map[ntp_tiles::SectionType::PERSONALIZED] = suggestions;

  instant_service_->OnURLsAvailable(false, suggestions_map);

  auto items = instant_service_->most_visited_info_->items;
  ASSERT_EQ(1, (int)items.size());
  EXPECT_EQ("https://foo.com/", items[0].url);
  EXPECT_EQ(u"Foo", items[0].title);
  EXPECT_EQ("https://foo.com/favicon", items[0].favicon);
}

TEST_F(InstantServiceTest, TestNoNtpTheme) {
  instant_service_->theme_ = nullptr;
  EXPECT_NE(nullptr, instant_service_->GetInitializedNtpTheme());
}

class InstantServiceThemeTest : public InstantServiceTest {
 public:
  InstantServiceThemeTest() = default;

  InstantServiceThemeTest(const InstantServiceThemeTest&) = delete;
  InstantServiceThemeTest& operator=(const InstantServiceThemeTest&) = delete;

  ~InstantServiceThemeTest() override = default;

  ui::TestNativeTheme* theme() { return &theme_; }

 private:
  ui::TestNativeTheme theme_;
};

TEST_F(InstantServiceTest, SetNTPElementsNtpTheme) {
  // Check defaults when no theme and no custom backgrounds is set.
  NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
  EXPECT_FALSE(theme->logo_alternate);

  // Install colors, theme update should trigger SetNTPElementsNtpTheme() and
  // update NTP themed elements info.
  ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile());
  test::ThemeServiceChangedWaiter waiter(theme_service);
  theme_service->BuildAutogeneratedThemeFromColor(SK_ColorRED);
  waiter.WaitForThemeChanged();

  theme = instant_service_->GetInitializedNtpTheme();
  EXPECT_TRUE(theme->logo_alternate);
}