OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/values.h" | 5 #include "base/values.h" |
6 #include "build/build_config.h" | 6 #include "build/build_config.h" |
7 #include "chrome/browser/extensions/api/top_sites/top_sites_api.h" | 7 #include "chrome/browser/extensions/api/top_sites/top_sites_api.h" |
8 #include "chrome/browser/extensions/extension_function_test_utils.h" | 8 #include "chrome/browser/extensions/extension_function_test_utils.h" |
9 #include "chrome/browser/history/top_sites.h" | |
10 #include "chrome/browser/history/top_sites_factory.h" | 9 #include "chrome/browser/history/top_sites_factory.h" |
11 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "components/history/core/browser/top_sites.h" |
14 | 14 |
15 namespace utils = extension_function_test_utils; | 15 namespace utils = extension_function_test_utils; |
16 | 16 |
17 namespace extensions { | 17 namespace extensions { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class TopSitesExtensionTest : public InProcessBrowserTest { | 21 class TopSitesExtensionTest : public InProcessBrowserTest { |
22 public: | 22 public: |
23 TopSitesExtensionTest() : top_sites_inited_(false), waiting_(false) { | 23 TopSitesExtensionTest() |
24 } | 24 : top_sites_prepopulated_pages_size_(0), |
| 25 top_sites_inited_(false), |
| 26 waiting_(false) {} |
25 | 27 |
26 void SetUpOnMainThread() override { | 28 void SetUpOnMainThread() override { |
27 scoped_refptr<history::TopSites> top_sites = | 29 scoped_refptr<history::TopSites> top_sites = |
28 TopSitesFactory::GetForProfile(browser()->profile()); | 30 TopSitesFactory::GetForProfile(browser()->profile()); |
29 | 31 |
| 32 top_sites_prepopulated_pages_size_ = |
| 33 top_sites->GetPrepopulatedPages().size(); |
| 34 |
30 // This may return async or sync. If sync, top_sites_inited_ will be true | 35 // This may return async or sync. If sync, top_sites_inited_ will be true |
31 // before we get to the conditional below. Otherwise, we'll run a nested | 36 // before we get to the conditional below. Otherwise, we'll run a nested |
32 // message loop until the async callback. | 37 // message loop until the async callback. |
33 top_sites->GetMostVisitedURLs( | 38 top_sites->GetMostVisitedURLs( |
34 base::Bind(&TopSitesExtensionTest::OnTopSitesAvailable, this), false); | 39 base::Bind(&TopSitesExtensionTest::OnTopSitesAvailable, this), false); |
35 | 40 |
36 if (!top_sites_inited_) { | 41 if (!top_sites_inited_) { |
37 waiting_ = true; | 42 waiting_ = true; |
38 base::MessageLoop::current()->Run(); | 43 base::MessageLoop::current()->Run(); |
39 } | 44 } |
40 | 45 |
41 // By this point, we know topsites has loaded. We can run the tests now. | 46 // By this point, we know topsites has loaded. We can run the tests now. |
42 } | 47 } |
43 | 48 |
| 49 size_t top_sites_prepopulated_pages_size() const { |
| 50 return top_sites_prepopulated_pages_size_; |
| 51 } |
| 52 |
44 private: | 53 private: |
45 void OnTopSitesAvailable(const history::MostVisitedURLList& data) { | 54 void OnTopSitesAvailable(const history::MostVisitedURLList& data) { |
46 if (waiting_) { | 55 if (waiting_) { |
47 base::MessageLoop::current()->Quit(); | 56 base::MessageLoop::current()->Quit(); |
48 waiting_ = false; | 57 waiting_ = false; |
49 } | 58 } |
50 top_sites_inited_ = true; | 59 top_sites_inited_ = true; |
51 } | 60 } |
52 | 61 |
| 62 size_t top_sites_prepopulated_pages_size_; |
53 bool top_sites_inited_; | 63 bool top_sites_inited_; |
54 bool waiting_; | 64 bool waiting_; |
55 }; | 65 }; |
56 | 66 |
57 } // namespace | 67 } // namespace |
58 | 68 |
59 IN_PROC_BROWSER_TEST_F(TopSitesExtensionTest, GetTopSites) { | 69 IN_PROC_BROWSER_TEST_F(TopSitesExtensionTest, GetTopSites) { |
60 scoped_refptr<TopSitesGetFunction> get_top_sites_function( | 70 scoped_refptr<TopSitesGetFunction> get_top_sites_function( |
61 new TopSitesGetFunction()); | 71 new TopSitesGetFunction()); |
62 // Without a callback the function will not generate a result. | 72 // Without a callback the function will not generate a result. |
63 get_top_sites_function->set_has_callback(true); | 73 get_top_sites_function->set_has_callback(true); |
64 | 74 |
65 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | 75 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( |
66 get_top_sites_function.get(), "[]", browser())); | 76 get_top_sites_function.get(), "[]", browser())); |
67 base::ListValue* list; | 77 base::ListValue* list; |
68 ASSERT_TRUE(result->GetAsList(&list)); | 78 ASSERT_TRUE(result->GetAsList(&list)); |
69 EXPECT_GE(list->GetSize(), arraysize(history::kPrepopulatedPages)); | 79 EXPECT_GE(list->GetSize(), top_sites_prepopulated_pages_size()); |
70 } | 80 } |
71 | 81 |
72 } // namespace extensions | 82 } // namespace extensions |
OLD | NEW |