OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/values.h" | |
6 #include "build/build_config.h" | |
7 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
8 #include "chrome/browser/history/top_sites.h" | |
9 #include "chrome/browser/history/top_sites_extension_api.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 | |
14 namespace utils = extension_function_test_utils; | |
15 | |
16 namespace { | |
17 | |
18 class TopSitesExtensionTest : public InProcessBrowserTest { | |
19 public: | |
20 TopSitesExtensionTest() : top_sites_inited_(false), waiting_(false) { | |
21 } | |
22 | |
23 void SetUpOnMainThread() { | |
24 history::TopSites* top_sites = browser()->profile()->GetTopSites(); | |
25 | |
26 // This may return async or sync. If sync, top_sites_inited_ will be true | |
27 // before we get to the conditional below. Otherwise, we'll run a nested | |
28 // message loop until the async callback. | |
29 top_sites->GetMostVisitedURLs( | |
30 base::Bind(&TopSitesExtensionTest::OnTopSitesAvailable, this)); | |
31 | |
32 if (!top_sites_inited_) { | |
33 waiting_ = true; | |
34 MessageLoop::current()->Run(); | |
35 } | |
36 | |
37 // By this point, we know topsites has loaded. We can run the tests now. | |
38 } | |
39 | |
40 private: | |
41 void OnTopSitesAvailable(const history::MostVisitedURLList& data) { | |
42 if (waiting_) { | |
43 MessageLoop::current()->Quit(); | |
44 waiting_ = false; | |
45 } | |
46 top_sites_inited_ = true; | |
47 } | |
48 | |
49 bool top_sites_inited_; | |
50 bool waiting_; | |
51 }; | |
52 | |
53 } // namespace | |
54 | |
55 IN_PROC_BROWSER_TEST_F(TopSitesExtensionTest, GetTopSites) { | |
56 scoped_refptr<GetTopSitesFunction> get_top_sites_function( | |
57 new GetTopSitesFunction()); | |
58 // Without a callback the function will not generate a result. | |
59 get_top_sites_function->set_has_callback(true); | |
60 | |
61 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
62 get_top_sites_function.get(), "[]", browser())); | |
63 base::ListValue* list; | |
64 ASSERT_TRUE(result->GetAsList(&list)); | |
65 EXPECT_GE(list->GetSize(), arraysize(history::kPrepopulatedPages)); | |
66 } | |
OLD | NEW |