This source file includes following definitions.
- debug_
- Done
- SetDebug
- GetWeight
- GetItemCount
- PopItem
- FetchItems
- AddSuggestion
- SetCombiner
- Reset
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
#include <deque>
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/ui/webui/ntp/suggestions_combiner.h"
#include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h"
#include "chrome/browser/ui/webui/ntp/suggestions_source.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
struct SourceInfo {
int weight;
const char* source_name;
int number_of_suggestions;
};
struct TestDescription {
SourceInfo sources[3];
const char* results[8];
} test_suite[] = {
{
{{1, "A", 10}},
{"A 0", "A 1", "A 2", "A 3", "A 4", "A 5", "A 6", "A 7"}
},
{
{{1, "A", 8}},
{"A 0", "A 1", "A 2", "A 3", "A 4", "A 5", "A 6", "A 7"}
},
{
{{1, "A", 3}},
{"A 0", "A 1", "A 2"}
},
{
{{1, "A", 0}},
{}
},
{
{{1, "A", 10}, {1, "B", 10}},
{"A 0", "A 1", "A 2", "A 3", "B 0", "B 1", "B 2", "B 3"}
},
{
{{1, "A", 4}, {1, "B", 4}},
{"A 0", "A 1", "A 2", "A 3", "B 0", "B 1", "B 2", "B 3"}
},
{
{{1, "A", 5}, {1, "B", 3}},
{"A 0", "A 1", "A 2", "A 3", "A 4", "B 0", "B 1", "B 2"}
},
{
{{1, "A", 2}, {1, "B", 6}},
{"A 0", "A 1", "B 0", "B 1", "B 2", "B 3", "B 4", "B 5"}
},
{
{{1, "A", 0}, {1, "B", 8}},
{"B 0", "B 1", "B 2", "B 3", "B 4", "B 5", "B 6", "B 7"}
},
{
{{1, "A", 8}, {1, "B", 0}},
{"A 0", "A 1", "A 2", "A 3", "A 4", "A 5", "A 6", "A 7"}
},
{
{{1, "A", 3}, {1, "B", 3}},
{"A 0", "A 1", "A 2", "B 0", "B 1", "B 2"}
},
{
{{1, "A", 4}, {1, "B", 3}},
{"A 0", "A 1", "A 2", "A 3", "B 0", "B 1", "B 2"}
},
{
{{1, "A", 1}, {1, "B", 3}},
{"A 0", "B 0", "B 1", "B 2"}
},
{
{{3, "A", 10}, {1, "B", 10}},
{"A 0", "A 1", "A 2", "A 3", "A 4", "A 5", "B 0", "B 1"}
},
{
{{1, "A", 10}, {7, "B", 10}},
{"A 0", "B 0", "B 1", "B 2", "B 3", "B 4", "B 5", "B 6"}
},
{
{{1, "A", 10}, {2, "B", 10}},
{"A 0", "A 1", "B 0", "B 1", "B 2", "B 3", "B 4", "B 5"}
},
{
{{2, "A", 10}, {1, "B", 10}, {1, "C", 10}},
{"A 0", "A 1", "A 2", "A 3", "B 0", "B 1", "C 0", "C 1"}
},
{
{{1, "A", 10}, {1, "B", 10}, {1, "C", 10}},
{"A 0", "A 1", "B 0", "B 1", "B 2", "C 0", "C 1", "C 2"}
},
{
{{1, "A", 3}, {1, "B", 4}, {10, "C", 1}},
{"A 0", "A 1", "A 2", "B 0", "B 1", "B 2", "B 3", "C 0"}
}
};
}
class SuggestionsSourceStub : public SuggestionsSource {
public:
explicit SuggestionsSourceStub(int weight,
const std::string& source_name, int number_of_suggestions)
: combiner_(NULL),
weight_(weight),
source_name_(source_name),
number_of_suggestions_(number_of_suggestions),
debug_(false) {
}
virtual ~SuggestionsSourceStub() {
STLDeleteElements(&items_);
}
void Done() {
combiner_->OnItemsReady();
}
private:
virtual void SetDebug(bool enable) OVERRIDE {
debug_ = enable;
}
virtual int GetWeight() OVERRIDE {
return weight_;
}
virtual int GetItemCount() OVERRIDE {
return items_.size();
}
virtual base::DictionaryValue* PopItem() OVERRIDE {
if (items_.empty())
return NULL;
base::DictionaryValue* item = items_.front();
items_.pop_front();
return item;
}
virtual void FetchItems(Profile* profile) OVERRIDE {
char num_str[21];
for (int i = 0; i < number_of_suggestions_; ++i) {
base::snprintf(num_str, sizeof(num_str), "%d", i);
AddSuggestion(source_name_ + ' ' + num_str);
}
}
void AddSuggestion(const std::string& title) {
base::DictionaryValue* item = new base::DictionaryValue();
item->SetString("title", title);
items_.push_back(item);
}
virtual void SetCombiner(SuggestionsCombiner* combiner) OVERRIDE {
DCHECK(!combiner_);
combiner_ = combiner;
}
SuggestionsCombiner* combiner_;
int weight_;
std::string source_name_;
int number_of_suggestions_;
bool debug_;
std::deque<base::DictionaryValue*> items_;
DISALLOW_COPY_AND_ASSIGN(SuggestionsSourceStub);
};
class SuggestionsCombinerTest : public testing::Test {
public:
SuggestionsCombinerTest() {
}
protected:
Profile* profile_;
SuggestionsHandler* suggestions_handler_;
SuggestionsCombiner* combiner_;
void Reset() {
delete combiner_;
combiner_ = new SuggestionsCombiner(suggestions_handler_, profile_);
}
private:
virtual void SetUp() {
profile_ = new TestingProfile();
suggestions_handler_ = new SuggestionsHandler();
combiner_ = new SuggestionsCombiner(suggestions_handler_, profile_);
}
virtual void TearDown() {
delete combiner_;
delete suggestions_handler_;
delete profile_;
}
DISALLOW_COPY_AND_ASSIGN(SuggestionsCombinerTest);
};
TEST_F(SuggestionsCombinerTest, NoSource) {
combiner_->FetchItems(NULL);
EXPECT_EQ(0UL, combiner_->GetPageValues()->GetSize());
}
TEST_F(SuggestionsCombinerTest, SourcesAreNotDoneFetching) {
combiner_->AddSource(new SuggestionsSourceStub(1, "sourceA", 10));
combiner_->AddSource(new SuggestionsSourceStub(1, "sourceB", 10));
combiner_->FetchItems(NULL);
EXPECT_EQ(0UL, combiner_->GetPageValues()->GetSize());
}
TEST_F(SuggestionsCombinerTest, TestSuite) {
size_t test_count = arraysize(test_suite);
for (size_t i = 0; i < test_count; ++i) {
const TestDescription& description = test_suite[i];
size_t source_count = arraysize(description.sources);
scoped_ptr<SuggestionsSourceStub*[]> sources(
new SuggestionsSourceStub*[source_count]);
for (size_t j = 0; j < source_count; ++j) {
const SourceInfo& source_info = description.sources[j];
if (source_info.source_name) {
sources[j] = new SuggestionsSourceStub(source_info.weight,
source_info.source_name, source_info.number_of_suggestions);
combiner_->AddSource(sources[j]);
} else {
sources[j] = NULL;
}
}
combiner_->FetchItems(NULL);
for (size_t j = 0; j < source_count; ++j) {
if (sources[j])
sources[j]->Done();
}
base::ListValue* results = combiner_->GetPageValues();
size_t result_count = results->GetSize();
EXPECT_LE(result_count, 8UL);
for (size_t j = 0; j < 8; ++j) {
if (j < result_count) {
std::string value;
base::DictionaryValue* dictionary;
results->GetDictionary(j, &dictionary);
dictionary->GetString("title", &value);
EXPECT_STREQ(description.results[j], value.c_str()) <<
" test index:" << i;
} else {
EXPECT_EQ(description.results[j], static_cast<const char*>(NULL)) <<
" test index:" << i;
}
}
Reset();
}
}