OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
bengr
2015/11/05 00:00:38
"(c)" -> ""
sclittle
2015/11/05 01:22:48
Done.
| |
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 "chrome/browser/data_usage/tab_id_provider.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/callback.h" | |
11 #include "base/location.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/run_loop.h" | |
17 #include "base/single_thread_task_runner.h" | |
18 #include "base/task_runner.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace chrome_browser_data_usage { | |
22 | |
23 namespace { | |
24 | |
25 const int32_t kTabId = 10; | |
26 | |
27 class TabIdProviderTest : public testing::Test { | |
28 public: | |
29 TabIdProviderTest() | |
30 : task_runner_(base::MessageLoop::current()->task_runner()), | |
31 tab_id_getter_call_count_(0) {} | |
32 | |
33 ~TabIdProviderTest() override {} | |
34 | |
35 base::Callback<int32_t(void)> TabIdGetterCallback() { | |
36 return base::Bind(&TabIdProviderTest::GetTabId, base::Unretained(this)); | |
37 } | |
38 | |
39 base::TaskRunner* task_runner() { return task_runner_.get(); } | |
40 base::RunLoop* run_loop() { return &run_loop_; } | |
41 int tab_id_getter_call_count() const { return tab_id_getter_call_count_; } | |
42 | |
43 private: | |
44 int32_t GetTabId() { | |
45 ++tab_id_getter_call_count_; | |
46 return kTabId; | |
47 } | |
48 | |
49 base::MessageLoop message_loop_; | |
50 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
51 base::RunLoop run_loop_; | |
52 int tab_id_getter_call_count_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(TabIdProviderTest); | |
55 }; | |
56 | |
57 // Copies |tab_id| into |capture|. | |
58 void CaptureTabId(int32_t* capture, int32_t tab_id) { | |
59 *capture = tab_id; | |
60 } | |
61 | |
62 TEST_F(TabIdProviderTest, ProvideTabId) { | |
63 TabIdProvider provider(task_runner(), FROM_HERE, TabIdGetterCallback()); | |
64 | |
65 int32_t tab_id = -1; | |
66 provider.ProvideTabId(base::Bind(&CaptureTabId, &tab_id)); | |
67 run_loop()->RunUntilIdle(); | |
68 | |
69 EXPECT_EQ(1, tab_id_getter_call_count()); | |
70 EXPECT_EQ(kTabId, tab_id); | |
71 } | |
72 | |
73 TEST_F(TabIdProviderTest, ProvideTabIdPiggyback) { | |
74 TabIdProvider provider(task_runner(), FROM_HERE, TabIdGetterCallback()); | |
75 | |
76 // First, ask for the first tab ID to kick things off. | |
77 int32_t first_tab_id = -1; | |
78 provider.ProvideTabId(base::Bind(&CaptureTabId, &first_tab_id)); | |
79 | |
80 // The first tab ID callback should still be pending, with the tab ID not | |
81 // available yet, so this second callback should piggyback off of the first | |
82 // callback. | |
83 int32_t piggyback_tab_id = -1; | |
84 provider.ProvideTabId(base::Bind(&CaptureTabId, &piggyback_tab_id)); | |
85 | |
86 run_loop()->RunUntilIdle(); | |
87 | |
88 // The tab ID getter callback should only have been called once. | |
89 EXPECT_EQ(1, tab_id_getter_call_count()); | |
90 EXPECT_EQ(kTabId, first_tab_id); | |
91 EXPECT_EQ(kTabId, piggyback_tab_id); | |
92 } | |
93 | |
94 TEST_F(TabIdProviderTest, ProvideTabIdCacheHit) { | |
95 TabIdProvider provider(task_runner(), FROM_HERE, TabIdGetterCallback()); | |
96 | |
97 // First, ask for the first tab ID to kick things off. | |
98 int32_t first_tab_id = -1; | |
99 provider.ProvideTabId(base::Bind(&CaptureTabId, &first_tab_id)); | |
100 | |
101 // Wait for the first tab ID callback to finish. | |
102 run_loop()->RunUntilIdle(); | |
103 | |
104 EXPECT_EQ(1, tab_id_getter_call_count()); | |
105 EXPECT_EQ(kTabId, first_tab_id); | |
106 | |
107 // Ask for another tab ID, which should be satisfied by the cached tab ID from | |
108 // the first callback. | |
109 int32_t cache_hit_tab_id = -1; | |
110 provider.ProvideTabId(base::Bind(&CaptureTabId, &cache_hit_tab_id)); | |
111 | |
112 // This cache hit callback should run synchronously, without causing the tab | |
113 // ID getter callback to run again. | |
114 EXPECT_EQ(1, tab_id_getter_call_count()); | |
115 EXPECT_EQ(kTabId, cache_hit_tab_id); | |
116 } | |
117 | |
118 TEST_F(TabIdProviderTest, ProvideTabIdAfterProviderDestroyed) { | |
119 scoped_ptr<TabIdProvider> provider( | |
120 new TabIdProvider(task_runner(), FROM_HERE, TabIdGetterCallback())); | |
121 | |
122 // Ask for two tab IDs. | |
123 int32_t first_tab_id = -1, second_tab_id = -1; | |
124 provider->ProvideTabId(base::Bind(&CaptureTabId, &first_tab_id)); | |
125 provider->ProvideTabId(base::Bind(&CaptureTabId, &second_tab_id)); | |
126 | |
127 // Then, destroy the |provider| before the tab ID getter callback or any other | |
128 // callbacks are run. | |
129 provider.reset(); | |
130 | |
131 // The callbacks should still complete successfully later even though the | |
132 // |provider| no longer exists. | |
133 run_loop()->RunUntilIdle(); | |
134 EXPECT_EQ(1, tab_id_getter_call_count()); | |
135 EXPECT_EQ(kTabId, first_tab_id); | |
136 EXPECT_EQ(kTabId, second_tab_id); | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 } // namespace chrome_browser_data_usage | |
OLD | NEW |