File: ntp_features_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,269 bytes parent folder | download | duplicates (4)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/search/ntp_features.h"

#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"

namespace {

const char kMobilePromoQRCodeURL[] =
    "https://apps.apple.com/app/apple-store/"
    "id535886823?pt=9008&ct=desktop-chr-ntp&mt=8";

}

namespace ntp_features {

using testing::ElementsAre;

TEST(NTPFeaturesTest, ModulesLoadTimeout) {
  base::test::ScopedFeatureList scoped_feature_list_;

  // The default value can be overridden.
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpModulesLoadTimeoutMilliseconds,
        {{kNtpModulesLoadTimeoutMillisecondsParam, "123"}}}},
      {});
  base::TimeDelta timeout = GetModulesLoadTimeout();
  EXPECT_EQ(123, timeout.InMilliseconds());

  // If the timeout is not parsable to an unsigned integer, the default value is
  // used.
  scoped_feature_list_.Reset();
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpModulesLoadTimeoutMilliseconds,
        {{kNtpModulesLoadTimeoutMillisecondsParam, "j"}}}},
      {});
  timeout = GetModulesLoadTimeout();
  EXPECT_EQ(3, timeout.InSeconds());
}

TEST(NTPFeaturesTest, ModulesOrder) {
  base::test::ScopedFeatureList scoped_feature_list_;

  // Can process list.
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpModulesOrder, {{kNtpModulesOrderParam, "foo,bar"}}}}, {});
  EXPECT_THAT(GetModulesOrder(), ElementsAre("foo", "bar"));

  // Can process empty param.
  scoped_feature_list_.Reset();
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpModulesOrder, {{kNtpModulesOrderParam, ""}}}}, {});
  EXPECT_TRUE(GetModulesOrder().empty());
}

TEST(NTPFeaturesTest, WallpaperSearchButtonAnimationShownThreshold) {
  base::test::ScopedFeatureList scoped_feature_list_;

  // If the param is unset, the default value is used.
  int threshold = GetWallpaperSearchButtonAnimationShownThreshold();
  EXPECT_EQ(15, threshold);

  // Unsigned integers override the default value.
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpWallpaperSearchButtonAnimationShownThreshold,
        {{kNtpWallpaperSearchButtonAnimationShownThresholdParam, "20"}}}},
      {});
  threshold = GetWallpaperSearchButtonAnimationShownThreshold();
  EXPECT_EQ(20, threshold);

  // Signed integers override the default value.
  scoped_feature_list_.Reset();
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpWallpaperSearchButtonAnimationShownThreshold,
        {{kNtpWallpaperSearchButtonAnimationShownThresholdParam, "-20"}}}},
      {});
  threshold = GetWallpaperSearchButtonAnimationShownThreshold();
  EXPECT_EQ(-20, threshold);

  // If the param is not parsable to an integer, the default value is
  // used.
  scoped_feature_list_.Reset();
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpWallpaperSearchButtonAnimationShownThreshold,
        {{kNtpWallpaperSearchButtonAnimationShownThresholdParam, "j"}}}},
      {});
  threshold = GetWallpaperSearchButtonAnimationShownThreshold();
  EXPECT_EQ(15, threshold);
}

TEST(NTPFeaturesTest, MobilePromoTargetURL) {
  base::test::ScopedFeatureList scoped_feature_list_;

  // If the param is unset, the default value is used.
  std::string target_url = GetMobilePromoTargetURL();
  EXPECT_EQ(kMobilePromoQRCodeURL, target_url);

  // If the param is empty, the default value is still used.
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpMobilePromo, {{kNtpMobilePromoTargetUrlParam, ""}}}}, {});
  target_url = GetMobilePromoTargetURL();
  EXPECT_EQ(kMobilePromoQRCodeURL, target_url);

  // Some alternate url will override the default.
  std::string test_url = "http://www.google.com";
  scoped_feature_list_.Reset();
  scoped_feature_list_.InitWithFeaturesAndParameters(
      {{kNtpMobilePromo, {{kNtpMobilePromoTargetUrlParam, test_url}}}}, {});
  target_url = GetMobilePromoTargetURL();
  EXPECT_EQ(test_url, target_url);
}
}  // namespace ntp_features