summaryrefslogtreecommitdiff
path: root/chromium/components/ntp_snippets/logger_unittest.cc
blob: 595415abd7d4cb9e52256408c47af8ec1415a3b9 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ntp_snippets/logger.h"

#include "base/location.h"
#include "base/strings/string_number_conversions.h"
#include "components/ntp_snippets/features.h"
#include "components/variations/variations_params_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ntp_snippets {

namespace {

const int kMaxItemsCount = 1000;

}  // namespace

class LoggerTest : public ::testing::Test {
 public:
  LoggerTest() = default;
  ~LoggerTest() override = default;

  void EnableFeature() {
    params_manager_.SetVariationParamsWithFeatureAssociations(
        kContentSuggestionsDebugLog.name,
        {{"max_items_count", base::IntToString(kMaxItemsCount)}},
        {kContentSuggestionsDebugLog.name});
  }

 private:
  variations::testing::VariationParamsManager params_manager_;
};

TEST_F(LoggerTest, ShouldNotLogWhenNotEnabled) {
  Logger logger;
  logger.Log(FROM_HERE, /*message=*/"test");
  EXPECT_EQ("", logger.GetHumanReadableLog());
}

TEST_F(LoggerTest, ShouldLogInRightOrder) {
  EnableFeature();
  Logger logger;

  logger.Log(FROM_HERE, /*message=*/"first");
  logger.Log(FROM_HERE, /*message=*/"second");

  const std::string log = logger.GetHumanReadableLog();
  ASSERT_NE(std::string::npos, log.find("first"));
  ASSERT_NE(std::string::npos, log.find("second"));
  EXPECT_LT(log.find("first"), log.find("second"));
}

TEST_F(LoggerTest, ShouldNotResetLogWhenDumping) {
  EnableFeature();
  Logger logger;

  logger.Log(FROM_HERE, /*message=*/"first");
  logger.Log(FROM_HERE, /*message=*/"second");

  const std::string first_call = logger.GetHumanReadableLog();
  const std::string second_call = logger.GetHumanReadableLog();
  EXPECT_EQ(first_call, second_call);
}

TEST_F(LoggerTest, ShouldLimitLogItems) {
  EnableFeature();
  Logger logger;

  logger.Log(FROM_HERE, /*message=*/"first_event");
  for (int i = 0; i < kMaxItemsCount - 1; ++i) {
    logger.Log(FROM_HERE, /*message=*/"other_event");
  }
  // The first event still should be in the log.
  ASSERT_NE(std::string::npos,
            logger.GetHumanReadableLog().find("first_event"));

  logger.Log(FROM_HERE, /*message=*/"other_event");
  // The first event should not be in the log anymore.
  EXPECT_EQ(std::string::npos,
            logger.GetHumanReadableLog().find("first_event"));
}

}  // namespace ntp_snippets