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_factory.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"
13 #include "components/history/core/browser/top_sites.h"
15 namespace utils
= extension_function_test_utils
;
17 namespace extensions
{
21 class TopSitesExtensionTest
: public InProcessBrowserTest
{
23 TopSitesExtensionTest()
24 : top_sites_prepopulated_pages_size_(0),
25 top_sites_inited_(false),
28 void SetUpOnMainThread() override
{
29 scoped_refptr
<history::TopSites
> top_sites
=
30 TopSitesFactory::GetForProfile(browser()->profile());
32 top_sites_prepopulated_pages_size_
=
33 top_sites
->GetPrepopulatedPages().size();
35 // This may return async or sync. If sync, top_sites_inited_ will be true
36 // before we get to the conditional below. Otherwise, we'll run a nested
37 // message loop until the async callback.
38 top_sites
->GetMostVisitedURLs(
39 base::Bind(&TopSitesExtensionTest::OnTopSitesAvailable
, this), false);
41 if (!top_sites_inited_
) {
43 base::MessageLoop::current()->Run();
46 // By this point, we know topsites has loaded. We can run the tests now.
49 size_t top_sites_prepopulated_pages_size() const {
50 return top_sites_prepopulated_pages_size_
;
54 void OnTopSitesAvailable(const history::MostVisitedURLList
& data
) {
56 base::MessageLoop::current()->Quit();
59 top_sites_inited_
= true;
62 size_t top_sites_prepopulated_pages_size_
;
63 bool top_sites_inited_
;
69 IN_PROC_BROWSER_TEST_F(TopSitesExtensionTest
, GetTopSites
) {
70 scoped_refptr
<TopSitesGetFunction
> get_top_sites_function(
71 new TopSitesGetFunction());
72 // Without a callback the function will not generate a result.
73 get_top_sites_function
->set_has_callback(true);
75 scoped_ptr
<base::Value
> result(utils::RunFunctionAndReturnSingleResult(
76 get_top_sites_function
.get(), "[]", browser()));
77 base::ListValue
* list
;
78 ASSERT_TRUE(result
->GetAsList(&list
));
79 EXPECT_GE(list
->GetSize(), top_sites_prepopulated_pages_size());
82 } // namespace extensions