1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/values.h"
6 #include "build/build_config.h"
7 #include "chrome/browser/extensions/api/top_sites/top_sites_api.h"
8 #include "chrome/browser/extensions/extension_function_test_utils.h"
9 #include "chrome/browser/history/top_sites.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/test/base/in_process_browser_test.h"
14 namespace utils = extension_function_test_utils;
16 namespace extensions {
20 class TopSitesExtensionTest : public InProcessBrowserTest {
22 TopSitesExtensionTest() : top_sites_inited_(false), waiting_(false) {
25 void SetUpOnMainThread() override {
26 history::TopSites* top_sites = browser()->profile()->GetTopSites();
28 // This may return async or sync. If sync, top_sites_inited_ will be true
29 // before we get to the conditional below. Otherwise, we'll run a nested
30 // message loop until the async callback.
31 top_sites->GetMostVisitedURLs(
32 base::Bind(&TopSitesExtensionTest::OnTopSitesAvailable, this), false);
34 if (!top_sites_inited_) {
36 base::MessageLoop::current()->Run();
39 // By this point, we know topsites has loaded. We can run the tests now.
43 void OnTopSitesAvailable(const history::MostVisitedURLList& data) {
45 base::MessageLoop::current()->Quit();
48 top_sites_inited_ = true;
51 bool top_sites_inited_;
57 IN_PROC_BROWSER_TEST_F(TopSitesExtensionTest, GetTopSites) {
58 scoped_refptr<TopSitesGetFunction> get_top_sites_function(
59 new TopSitesGetFunction());
60 // Without a callback the function will not generate a result.
61 get_top_sites_function->set_has_callback(true);
63 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
64 get_top_sites_function.get(), "[]", browser()));
65 base::ListValue* list;
66 ASSERT_TRUE(result->GetAsList(&list));
67 EXPECT_GE(list->GetSize(), arraysize(history::kPrepopulatedPages));
70 } // namespace extensions