This source file includes following definitions.
- AppendURLToListValue
- AppendInstantURLToVector
- AppendMostVisitedURLToVector
- SetUpMaybeShuffle
- field_trial_list_
- SetUp
- ValidateMetrics
- GetHistogram
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/history/most_visited_tiles_experiment.h"
#include <algorithm>
#include <sstream>
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/common/instant_types.h"
#include "components/variations/entropy_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace history {
namespace {
const char kMostVisitedFieldTrialName[] = "MostVisitedTilePlacement";
const char kOneEightAGroupName[] = "OneEight_A_Flipped";
const char kOneFourAGroupName[] = "OneFour_A_Flipped";
const char kDontShowOpenURLsGroupName[] = "DontShowOpenTabs";
const char kGmailURL[] = "http://www.gmail.com/";
const char kMostVisitedExperimentHistogramName[] =
"NewTabPage.MostVisitedTilePlacementExperiment";
const size_t kMinUrlSuggestions = 8;
enum FlippedIndexes {
TILE_ONE = 0,
TILE_FOUR = 3,
TILE_EIGHT = 7
};
void AppendURLToListValue(const std::string& url_string,
base::ListValue* list) {
base::DictionaryValue* page_value = new base::DictionaryValue();
page_value->SetString("url", url_string);
list->Append(page_value);
}
void AppendInstantURLToVector(const std::string& url_string,
std::vector<InstantMostVisitedItem>* list) {
InstantMostVisitedItem item;
item.url = GURL(url_string);
list->push_back(item);
}
void AppendMostVisitedURLToVector(const std::string& url_string,
std::vector<history::MostVisitedURL>* list) {
history::MostVisitedURL most_visited;
most_visited.url = GURL(url_string);
list->push_back(most_visited);
}
void SetUpMaybeShuffle(const int& max_urls,
MostVisitedURLList* most_visited_urls,
MostVisitedURLList* test_urls) {
for (int i = 0; i < max_urls; ++i) {
std::string url;
base::SStringPrintf(&url, "http://www.test%d.com", i);
AppendMostVisitedURLToVector(url, most_visited_urls);
AppendMostVisitedURLToVector(url, test_urls);
}
}
}
class MostVisitedTilesExperimentTest : public testing::Test {
public:
MostVisitedTilesExperimentTest()
: histogram_(NULL),
field_trial_list_(new metrics::SHA1EntropyProvider("foo")) {}
virtual ~MostVisitedTilesExperimentTest() {}
protected:
virtual void SetUp() OVERRIDE {
base::StatisticsRecorder::Initialize();
previous_metrics_count_.resize(NUM_NTP_TILE_EXPERIMENT_ACTIONS, 0);
base::HistogramBase* histogram = GetHistogram();
if (histogram) {
scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
if (samples.get()) {
for (int state = NTP_TILE_EXPERIMENT_ACTION_REMOVED_URL;
state < NUM_NTP_TILE_EXPERIMENT_ACTIONS;
++state) {
previous_metrics_count_[state] = samples->GetCount(state);
}
}
}
}
void ValidateMetrics(const base::HistogramBase::Sample& value) {
base::HistogramBase* histogram = GetHistogram();
ASSERT_TRUE(histogram != NULL);
scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
if (samples.get()) {
for (int state = NTP_TILE_EXPERIMENT_ACTION_REMOVED_URL;
state < NUM_NTP_TILE_EXPERIMENT_ACTIONS;
++state) {
if (state == value) {
EXPECT_EQ(previous_metrics_count_[state] + 1,
samples->GetCount(state));
} else {
EXPECT_EQ(previous_metrics_count_[state], samples->GetCount(state));
}
}
}
}
private:
base::HistogramBase* GetHistogram() {
if (!histogram_) {
histogram_ = base::StatisticsRecorder::FindHistogram(
kMostVisitedExperimentHistogramName);
}
return histogram_;
}
base::HistogramBase* histogram_;
base::FieldTrialList field_trial_list_;
std::vector<int> previous_metrics_count_;
DISALLOW_COPY_AND_ASSIGN(MostVisitedTilesExperimentTest);
};
TEST_F(MostVisitedTilesExperimentTest,
RemovePageValuesMatchingOpenTabsTooFewURLs) {
base::FieldTrialList::CreateFieldTrial(kMostVisitedFieldTrialName,
kDontShowOpenURLsGroupName);
EXPECT_TRUE(MostVisitedTilesExperiment::IsDontShowOpenURLsEnabled());
std::set<std::string> open_urls;
open_urls.insert(kGmailURL);
base::ListValue pages_value;
AppendURLToListValue(kGmailURL, &pages_value);
MostVisitedTilesExperiment::RemovePageValuesMatchingOpenTabs(
open_urls, &pages_value);
base::DictionaryValue gmail_value;
gmail_value.SetString("url", kGmailURL);
EXPECT_NE(pages_value.end(), pages_value.Find(gmail_value));
EXPECT_NO_FATAL_FAILURE(
ValidateMetrics(NTP_TILE_EXPERIMENT_ACTION_DID_NOT_REMOVE_URL));
}
TEST_F(MostVisitedTilesExperimentTest, RemovePageValuesMatchingOpenTabs) {
base::FieldTrialList::CreateFieldTrial(kMostVisitedFieldTrialName,
kDontShowOpenURLsGroupName);
EXPECT_TRUE(MostVisitedTilesExperiment::IsDontShowOpenURLsEnabled());
std::set<std::string> open_urls;
open_urls.insert(kGmailURL);
base::ListValue pages_value;
AppendURLToListValue(kGmailURL, &pages_value);
for (size_t i = 0; i < kMinUrlSuggestions; ++i) {
std::string url;
base::SStringPrintf(&url, "http://www.test%d.com", static_cast<int>(i));
AppendURLToListValue(url, &pages_value);
}
MostVisitedTilesExperiment::RemovePageValuesMatchingOpenTabs(
open_urls, &pages_value);
base::DictionaryValue gmail_value;
gmail_value.SetString("url", kGmailURL);
EXPECT_EQ(pages_value.end(), pages_value.Find(gmail_value));
EXPECT_NO_FATAL_FAILURE(
ValidateMetrics(NTP_TILE_EXPERIMENT_ACTION_REMOVED_URL));
}
TEST_F(MostVisitedTilesExperimentTest, RemoveItemsMatchingOpenTabsTooFewURLs) {
base::FieldTrialList::CreateFieldTrial(kMostVisitedFieldTrialName,
kDontShowOpenURLsGroupName);
EXPECT_TRUE(MostVisitedTilesExperiment::IsDontShowOpenURLsEnabled());
std::set<std::string> open_urls;
open_urls.insert(kGmailURL);
std::vector<InstantMostVisitedItem> items;
AppendInstantURLToVector(kGmailURL, &items);
MostVisitedTilesExperiment::RemoveItemsMatchingOpenTabs(open_urls, &items);
for (size_t i = 0; i < items.size(); i++) {
const std::string& item_url = items[i].url.spec();
EXPECT_NE(0u, open_urls.count(item_url));
}
EXPECT_NO_FATAL_FAILURE(
ValidateMetrics(NTP_TILE_EXPERIMENT_ACTION_DID_NOT_REMOVE_URL));
}
TEST_F(MostVisitedTilesExperimentTest, RemoveItemsMatchingOpenTabs) {
base::FieldTrialList::CreateFieldTrial(
kMostVisitedFieldTrialName,
kDontShowOpenURLsGroupName);
EXPECT_TRUE(MostVisitedTilesExperiment::IsDontShowOpenURLsEnabled());
std::set<std::string> open_urls;
open_urls.insert(kGmailURL);
std::vector<InstantMostVisitedItem> items;
AppendInstantURLToVector(kGmailURL, &items);
for (size_t i = 0; i < kMinUrlSuggestions; ++i) {
std::string url;
base::SStringPrintf(&url, "http://www.test%d.com", static_cast<int>(i));
AppendInstantURLToVector(url, &items);
}
MostVisitedTilesExperiment::RemoveItemsMatchingOpenTabs(open_urls, &items);
for (size_t i = 0; i < items.size(); i++) {
const std::string& item_url = items[i].url.spec();
EXPECT_EQ(0u, open_urls.count(item_url));
}
EXPECT_NO_FATAL_FAILURE(
ValidateMetrics(NTP_TILE_EXPERIMENT_ACTION_REMOVED_URL));
}
TEST_F(MostVisitedTilesExperimentTest, MaybeShuffleOneEight) {
base::FieldTrialList::CreateFieldTrial(kMostVisitedFieldTrialName,
kOneEightAGroupName);
EXPECT_EQ(kOneEightAGroupName,
base::FieldTrialList::FindFullName(kMostVisitedFieldTrialName));
MostVisitedURLList most_visited_urls;
MostVisitedURLList test_urls;
SetUpMaybeShuffle(kMinUrlSuggestions, &most_visited_urls, &test_urls);
history::MostVisitedTilesExperiment::MaybeShuffle(&most_visited_urls);
EXPECT_EQ(most_visited_urls[TILE_ONE].url.spec(),
test_urls[TILE_EIGHT].url.spec());
}
TEST_F(MostVisitedTilesExperimentTest, MaybeShuffleOneEightTooFewURLs) {
base::FieldTrialList::CreateFieldTrial(kMostVisitedFieldTrialName,
kOneEightAGroupName);
EXPECT_EQ(kOneEightAGroupName,
base::FieldTrialList::FindFullName(kMostVisitedFieldTrialName));
MostVisitedURLList most_visited_urls;
MostVisitedURLList test_urls;
SetUpMaybeShuffle(kMinUrlSuggestions - 1, &most_visited_urls, &test_urls);
history::MostVisitedTilesExperiment::MaybeShuffle(&most_visited_urls);
EXPECT_EQ(most_visited_urls[TILE_ONE].url.spec(),
test_urls[TILE_ONE].url.spec());
EXPECT_EQ(most_visited_urls[TILE_EIGHT - 1].url.spec(),
test_urls[TILE_EIGHT - 1].url.spec());
EXPECT_NO_FATAL_FAILURE(
ValidateMetrics(NTP_TILE_EXPERIMENT_ACTION_TOO_FEW_URLS_TILES_1_8));
}
TEST_F(MostVisitedTilesExperimentTest, MaybeShuffleOneFour) {
base::FieldTrialList::CreateFieldTrial(kMostVisitedFieldTrialName,
kOneFourAGroupName);
EXPECT_EQ(kOneFourAGroupName,
base::FieldTrialList::FindFullName(kMostVisitedFieldTrialName));
MostVisitedURLList most_visited_urls;
MostVisitedURLList test_urls;
SetUpMaybeShuffle(kMinUrlSuggestions, &most_visited_urls, &test_urls);
history::MostVisitedTilesExperiment::MaybeShuffle(&most_visited_urls);
EXPECT_EQ(most_visited_urls[TILE_ONE].url.spec(),
test_urls[TILE_FOUR].url.spec());
}
TEST_F(MostVisitedTilesExperimentTest, MaybeShuffleOneFourTooFewURLs) {
base::FieldTrialList::CreateFieldTrial(
kMostVisitedFieldTrialName,
kOneFourAGroupName);
EXPECT_EQ(kOneFourAGroupName,
base::FieldTrialList::FindFullName(kMostVisitedFieldTrialName));
MostVisitedURLList most_visited_urls;
MostVisitedURLList test_urls;
SetUpMaybeShuffle(kMinUrlSuggestions - 5, &most_visited_urls, &test_urls);
history::MostVisitedTilesExperiment::MaybeShuffle(&most_visited_urls);
EXPECT_EQ(most_visited_urls[TILE_ONE].url.spec(),
test_urls[TILE_ONE].url.spec());
EXPECT_EQ(most_visited_urls[TILE_FOUR-1].url.spec(),
test_urls[TILE_FOUR-1].url.spec());
EXPECT_NO_FATAL_FAILURE(
ValidateMetrics(NTP_TILE_EXPERIMENT_ACTION_TOO_FEW_URLS_TILES_1_4));
}
}