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