This source file includes following definitions.
- BookmarkModelChanged
- BookmarkModelLoaded
- AddNodesFromString
- WaitForBookmarkModelToLoad
- WaitForBookmarkModelToLoad
- ModelStringFromNode
- AddNodesFromModelString
#include "chrome/browser/bookmarks/bookmark_test_helpers.h"
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "content/public/test/test_utils.h"
#include "url/gurl.h"
namespace {
class BookmarkLoadObserver : public BaseBookmarkModelObserver {
public:
explicit BookmarkLoadObserver(const base::Closure& quit_task);
virtual ~BookmarkLoadObserver();
private:
virtual void BookmarkModelChanged() OVERRIDE;
virtual void BookmarkModelLoaded(BookmarkModel* model,
bool ids_reassigned) OVERRIDE;
base::Closure quit_task_;
DISALLOW_COPY_AND_ASSIGN(BookmarkLoadObserver);
};
BookmarkLoadObserver::BookmarkLoadObserver(const base::Closure& quit_task)
: quit_task_(quit_task) {}
BookmarkLoadObserver::~BookmarkLoadObserver() {}
void BookmarkLoadObserver::BookmarkModelChanged() {}
void BookmarkLoadObserver::BookmarkModelLoaded(BookmarkModel* model,
bool ids_reassigned) {
quit_task_.Run();
}
std::string::size_type AddNodesFromString(BookmarkModel* model,
const BookmarkNode* node,
const std::string& model_string,
std::string::size_type start_pos) {
DCHECK(node);
int index = node->child_count();
static const std::string folder_tell(":[");
std::string::size_type end_pos = model_string.find(' ', start_pos);
while (end_pos != std::string::npos) {
std::string::size_type part_length = end_pos - start_pos;
std::string node_name = model_string.substr(start_pos, part_length);
if (node_name != "]") {
std::string tell;
if (part_length > 2)
tell = node_name.substr(part_length - 2, 2);
if (tell == folder_tell) {
node_name = node_name.substr(0, part_length - 2);
const BookmarkNode* new_node =
model->AddFolder(node, index, base::UTF8ToUTF16(node_name));
end_pos = AddNodesFromString(model, new_node, model_string,
end_pos + 1);
} else {
std::string url_string("http://");
url_string += std::string(node_name.begin(), node_name.end());
url_string += ".com";
model->AddURL(
node, index, base::UTF8ToUTF16(node_name), GURL(url_string));
++end_pos;
}
++index;
start_pos = end_pos;
end_pos = model_string.find(' ', start_pos);
} else {
++end_pos;
break;
}
}
return end_pos;
}
}
namespace test {
void WaitForBookmarkModelToLoad(BookmarkModel* model) {
if (model->loaded())
return;
base::RunLoop run_loop;
BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
model->AddObserver(&observer);
content::RunThisRunLoop(&run_loop);
model->RemoveObserver(&observer);
DCHECK(model->loaded());
}
void WaitForBookmarkModelToLoad(Profile* profile) {
WaitForBookmarkModelToLoad(BookmarkModelFactory::GetForProfile(profile));
}
std::string ModelStringFromNode(const BookmarkNode* node) {
int child_count = node->child_count();
std::string child_string;
for (int i = 0; i < child_count; ++i) {
const BookmarkNode* child = node->GetChild(i);
if (child->is_folder()) {
child_string += base::UTF16ToUTF8(child->GetTitle()) + ":[ " +
ModelStringFromNode(child) + "] ";
} else {
child_string += base::UTF16ToUTF8(child->GetTitle()) + " ";
}
}
return child_string;
}
void AddNodesFromModelString(BookmarkModel* model,
const BookmarkNode* node,
const std::string& model_string) {
DCHECK(node);
std::string::size_type start_pos = 0;
std::string::size_type end_pos =
AddNodesFromString(model, node, model_string, start_pos);
DCHECK(end_pos == std::string::npos);
}
}