Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/android/data_usage/data_use_tab_model.h" | 5 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/test/histogram_tester.h" | |
| 13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 14 #include "chrome/browser/android/data_usage/external_data_use_observer.h" | 16 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
| 15 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" | 17 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" |
| 16 #include "net/base/network_change_notifier.h" | 18 #include "net/base/network_change_notifier.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // Tracking labels for tests. | 24 // Tracking labels for tests. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 35 } // namespace | 37 } // namespace |
| 36 | 38 |
| 37 namespace base { | 39 namespace base { |
| 38 class SingleThreadTaskRunner; | 40 class SingleThreadTaskRunner; |
| 39 } | 41 } |
| 40 | 42 |
| 41 namespace chrome { | 43 namespace chrome { |
| 42 | 44 |
| 43 namespace android { | 45 namespace android { |
| 44 | 46 |
| 47 // Test version of |DataUseTabModel|, which permits overriding of calls to Now. | |
| 48 class TestDataUseTabModel : public DataUseTabModel { | |
| 49 public: | |
| 50 TestDataUseTabModel( | |
| 51 const ExternalDataUseObserver* data_use_observer, | |
| 52 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) | |
| 53 : DataUseTabModel(data_use_observer, ui_task_runner) {} | |
| 54 | |
| 55 ~TestDataUseTabModel() override {} | |
| 56 | |
| 57 void AdvanceTime(base::TimeDelta now_offset) { now_offset_ = now_offset; } | |
| 58 | |
| 59 private: | |
| 60 // Returns the fake time as Now. | |
|
tbansal1
2015/11/16 21:09:13
s/Returns the fake time as Now./Returns the curren
Raj
2015/11/17 07:04:03
Done.
| |
| 61 base::TimeTicks Now() const override { | |
| 62 return base::TimeTicks::Now() + now_offset_; | |
| 63 } | |
| 64 | |
| 65 // Represents the delta offset to be added to current time that is returned by | |
| 66 // Now. | |
| 67 base::TimeDelta now_offset_; | |
| 68 }; | |
| 69 | |
| 45 class DataUseTabModelTest : public testing::Test { | 70 class DataUseTabModelTest : public testing::Test { |
| 46 public: | 71 public: |
| 47 DataUseTabModelTest() : data_use_tab_model_(nullptr, task_runner_.get()) { | 72 DataUseTabModelTest() {} |
| 73 | |
| 74 protected: | |
| 75 void SetUp() override { | |
| 48 // TODO(rajendrant): Create a mock class for ExternalDataUseObserver to | 76 // TODO(rajendrant): Create a mock class for ExternalDataUseObserver to |
| 49 // spoof the Matches call and test the OnNavigationEvent. | 77 // spoof the Matches call and test the OnNavigationEvent. |
| 78 test_data_use_tab_model_ = | |
| 79 new TestDataUseTabModel(nullptr, task_runner_.get()); | |
| 80 data_use_tab_model_.reset(test_data_use_tab_model_); | |
| 50 } | 81 } |
| 51 | 82 |
| 52 void SetUp() override {} | |
| 53 | |
| 54 base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); } | 83 base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); } |
| 55 | 84 |
| 56 // Returns true if tab entry for |tab_id| exists in |active_tabs_|. | 85 // Returns true if tab entry for |tab_id| exists in |active_tabs_|. |
| 57 bool IsTabEntryExists(int32_t tab_id) const { | 86 bool IsTabEntryExists(int32_t tab_id) const { |
| 58 return data_use_tab_model_.active_tabs_.find(tab_id) != | 87 return data_use_tab_model_->active_tabs_.find(tab_id) != |
| 59 data_use_tab_model_.active_tabs_.end(); | 88 data_use_tab_model_->active_tabs_.end(); |
| 60 } | 89 } |
| 61 | 90 |
| 62 // Checks if there are |expected_size| tab entries being tracked in | 91 // Checks if there are |expected_size| tab entries being tracked in |
| 63 // |active_tabs_|. | 92 // |active_tabs_|. |
| 64 void ExpectTabEntrySize(uint32_t expected_size) const { | 93 void ExpectTabEntrySize(uint32_t expected_size) const { |
| 65 EXPECT_EQ(expected_size, data_use_tab_model_.active_tabs_.size()); | 94 EXPECT_EQ(expected_size, data_use_tab_model_->active_tabs_.size()); |
| 66 } | 95 } |
| 67 | 96 |
| 68 // Returns true if the tracking session for tab with id |tab_id| is currently | 97 // Returns true if the tracking session for tab with id |tab_id| is currently |
| 69 // active. | 98 // active. |
| 70 bool IsTrackingDataUse(int32_t tab_id) const { | 99 bool IsTrackingDataUse(int32_t tab_id) const { |
| 71 auto tab_entry_iterator = data_use_tab_model_.active_tabs_.find(tab_id); | 100 auto tab_entry_iterator = data_use_tab_model_->active_tabs_.find(tab_id); |
| 72 if (tab_entry_iterator == data_use_tab_model_.active_tabs_.end()) | 101 if (tab_entry_iterator == data_use_tab_model_->active_tabs_.end()) |
| 73 return false; | 102 return false; |
| 74 return tab_entry_iterator->second.IsTrackingDataUse(); | 103 return tab_entry_iterator->second.IsTrackingDataUse(); |
| 75 } | 104 } |
| 76 | 105 |
| 77 // Checks if the DataUse object for the given |tab_id| with request start time | 106 // Checks if the DataUse object for the given |tab_id| with request start time |
| 78 // |at_time| is labeled as an empty string. | 107 // |at_time| is labeled as an empty string. |
| 79 void ExpectEmptyDataUseLabelAtTime(int32_t tab_id, | 108 void ExpectEmptyDataUseLabelAtTime(int32_t tab_id, |
| 80 const base::TimeTicks& at_time) const { | 109 const base::TimeTicks& at_time) const { |
| 81 ExpectDataUseLabelAtTimeWithReturn(tab_id, at_time, false, std::string()); | 110 ExpectDataUseLabelAtTimeWithReturn(tab_id, at_time, false, std::string()); |
| 82 } | 111 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 103 int32_t tab_id, | 132 int32_t tab_id, |
| 104 const base::TimeTicks& at_time, | 133 const base::TimeTicks& at_time, |
| 105 bool expected_return, | 134 bool expected_return, |
| 106 const std::string& expected_label) const { | 135 const std::string& expected_label) const { |
| 107 data_usage::DataUse data_use(GURL("http://foo.com"), at_time, | 136 data_usage::DataUse data_use(GURL("http://foo.com"), at_time, |
| 108 GURL("http://foobar.com"), tab_id, | 137 GURL("http://foobar.com"), tab_id, |
| 109 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | 138 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| 110 std::string(), 1000, 1000); | 139 std::string(), 1000, 1000); |
| 111 std::string actual_label; | 140 std::string actual_label; |
| 112 bool actual_return = | 141 bool actual_return = |
| 113 data_use_tab_model_.GetLabelForDataUse(data_use, &actual_label); | 142 data_use_tab_model_->GetLabelForDataUse(data_use, &actual_label); |
| 114 EXPECT_EQ(expected_return, actual_return); | 143 EXPECT_EQ(expected_return, actual_return); |
| 115 EXPECT_EQ(expected_label, actual_label); | 144 EXPECT_EQ(expected_label, actual_label); |
| 116 } | 145 } |
| 117 | 146 |
| 147 void StartTrackingDataUse(int32_t tab_id, const std::string& label) { | |
| 148 data_use_tab_model_->StartTrackingDataUse(tab_id, label); | |
| 149 } | |
| 150 | |
| 151 void EndTrackingDataUse(int32_t tab_id) { | |
| 152 data_use_tab_model_->EndTrackingDataUse(tab_id); | |
| 153 } | |
| 154 | |
| 155 TestDataUseTabModel* test_data_use_tab_model_; | |
|
tbansal1
2015/11/16 21:09:13
Would this cause memory leak? Should this be a sco
Raj
2015/11/17 07:04:03
test_data_use_tab_model_ and data_use_tab_model_ p
| |
| 156 | |
| 118 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 157 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 119 DataUseTabModel data_use_tab_model_; | 158 scoped_ptr<DataUseTabModel> data_use_tab_model_; |
| 120 base::MessageLoop message_loop_; | 159 base::MessageLoop message_loop_; |
| 121 }; | 160 }; |
| 122 | 161 |
| 123 // Mock observer to track the calls to start and end tracking events. | 162 // Mock observer to track the calls to start and end tracking events. |
| 124 class MockTabDataUseObserver : public DataUseTabModel::TabDataUseObserver { | 163 class MockTabDataUseObserver : public DataUseTabModel::TabDataUseObserver { |
| 125 public: | 164 public: |
| 126 MOCK_METHOD1(NotifyTrackingStarting, void(int32_t tab_id)); | 165 MOCK_METHOD1(NotifyTrackingStarting, void(int32_t tab_id)); |
| 127 MOCK_METHOD1(NotifyTrackingEnding, void(int32_t tab_id)); | 166 MOCK_METHOD1(NotifyTrackingEnding, void(int32_t tab_id)); |
| 128 }; | 167 }; |
| 129 | 168 |
| 130 // Starts and ends tracking a single tab and checks if its label is returned | 169 // Starts and ends tracking a single tab and checks if its label is returned |
| 131 // correctly for DataUse objects. | 170 // correctly for DataUse objects. |
| 132 TEST_F(DataUseTabModelTest, SingleTabTracking) { | 171 TEST_F(DataUseTabModelTest, SingleTabTracking) { |
| 133 ExpectTabEntrySize(TabEntrySize::ZERO); | 172 ExpectTabEntrySize(TabEntrySize::ZERO); |
| 134 | 173 |
| 135 // No label is applied initially. | 174 // No label is applied initially. |
| 136 ExpectEmptyDataUseLabel(kTabID1); | 175 ExpectEmptyDataUseLabel(kTabID1); |
| 137 ExpectEmptyDataUseLabel(kTabID2); | 176 ExpectEmptyDataUseLabel(kTabID2); |
| 138 | 177 |
| 139 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 178 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 140 ExpectTabEntrySize(TabEntrySize::ONE); | 179 ExpectTabEntrySize(TabEntrySize::ONE); |
| 141 | 180 |
| 142 EXPECT_TRUE(IsTrackingDataUse(kTabID1)); | 181 EXPECT_TRUE(IsTrackingDataUse(kTabID1)); |
| 143 ExpectDataUseLabel(kTabID1, kTestLabel1); | 182 ExpectDataUseLabel(kTabID1, kTestLabel1); |
| 144 ExpectEmptyDataUseLabel(kTabID2); | 183 ExpectEmptyDataUseLabel(kTabID2); |
| 145 | 184 |
| 146 data_use_tab_model_.EndTrackingDataUse(kTabID1); | 185 EndTrackingDataUse(kTabID1); |
| 147 ExpectTabEntrySize(TabEntrySize::ONE); | 186 ExpectTabEntrySize(TabEntrySize::ONE); |
| 148 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); | 187 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); |
| 149 } | 188 } |
| 150 | 189 |
| 151 // Starts and ends tracking multiple tabs and checks if labels are returned | 190 // Starts and ends tracking multiple tabs and checks if labels are returned |
| 152 // correctly for DataUse objects corresponding to different tab ids. | 191 // correctly for DataUse objects corresponding to different tab ids. |
| 153 TEST_F(DataUseTabModelTest, MultipleTabTracking) { | 192 TEST_F(DataUseTabModelTest, MultipleTabTracking) { |
| 154 ExpectTabEntrySize(TabEntrySize::ZERO); | 193 ExpectTabEntrySize(TabEntrySize::ZERO); |
| 155 ExpectEmptyDataUseLabel(kTabID1); | 194 ExpectEmptyDataUseLabel(kTabID1); |
| 156 ExpectEmptyDataUseLabel(kTabID2); | 195 ExpectEmptyDataUseLabel(kTabID2); |
| 157 ExpectEmptyDataUseLabel(kTabID3); | 196 ExpectEmptyDataUseLabel(kTabID3); |
| 158 | 197 |
| 159 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 198 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 160 data_use_tab_model_.StartTrackingDataUse(kTabID2, kTestLabel2); | 199 StartTrackingDataUse(kTabID2, kTestLabel2); |
| 161 data_use_tab_model_.StartTrackingDataUse(kTabID3, kTestLabel3); | 200 StartTrackingDataUse(kTabID3, kTestLabel3); |
| 162 ExpectTabEntrySize(TabEntrySize::THREE); | 201 ExpectTabEntrySize(TabEntrySize::THREE); |
| 163 | 202 |
| 164 EXPECT_TRUE(IsTrackingDataUse(kTabID1)); | 203 EXPECT_TRUE(IsTrackingDataUse(kTabID1)); |
| 165 EXPECT_TRUE(IsTrackingDataUse(kTabID2)); | 204 EXPECT_TRUE(IsTrackingDataUse(kTabID2)); |
| 166 EXPECT_TRUE(IsTrackingDataUse(kTabID3)); | 205 EXPECT_TRUE(IsTrackingDataUse(kTabID3)); |
| 167 ExpectDataUseLabel(kTabID1, kTestLabel1); | 206 ExpectDataUseLabel(kTabID1, kTestLabel1); |
| 168 ExpectDataUseLabel(kTabID2, kTestLabel2); | 207 ExpectDataUseLabel(kTabID2, kTestLabel2); |
| 169 ExpectDataUseLabel(kTabID3, kTestLabel3); | 208 ExpectDataUseLabel(kTabID3, kTestLabel3); |
| 170 | 209 |
| 171 data_use_tab_model_.EndTrackingDataUse(kTabID1); | 210 EndTrackingDataUse(kTabID1); |
| 172 data_use_tab_model_.EndTrackingDataUse(kTabID2); | 211 EndTrackingDataUse(kTabID2); |
| 173 data_use_tab_model_.EndTrackingDataUse(kTabID3); | 212 EndTrackingDataUse(kTabID3); |
| 174 ExpectTabEntrySize(TabEntrySize::THREE); | 213 ExpectTabEntrySize(TabEntrySize::THREE); |
| 175 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); | 214 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); |
| 176 EXPECT_FALSE(IsTrackingDataUse(kTabID2)); | 215 EXPECT_FALSE(IsTrackingDataUse(kTabID2)); |
| 177 EXPECT_FALSE(IsTrackingDataUse(kTabID3)); | 216 EXPECT_FALSE(IsTrackingDataUse(kTabID3)); |
| 178 | 217 |
| 179 // Future data use object should be labeled as an empty string. | 218 // Future data use object should be labeled as an empty string. |
| 180 base::TimeTicks future_time = | 219 base::TimeTicks future_time = |
| 181 base::TimeTicks::Now() + base::TimeDelta::FromMilliseconds(20); | 220 base::TimeTicks::Now() + base::TimeDelta::FromMilliseconds(20); |
| 182 ExpectEmptyDataUseLabelAtTime(kTabID1, future_time); | 221 ExpectEmptyDataUseLabelAtTime(kTabID1, future_time); |
| 183 ExpectEmptyDataUseLabelAtTime(kTabID2, future_time); | 222 ExpectEmptyDataUseLabelAtTime(kTabID2, future_time); |
| 184 ExpectEmptyDataUseLabelAtTime(kTabID3, future_time); | 223 ExpectEmptyDataUseLabelAtTime(kTabID3, future_time); |
| 185 } | 224 } |
| 186 | 225 |
| 187 // Checks that the mock observer receives start and end tracking events for a | 226 // Checks that the mock observer receives start and end tracking events for a |
| 188 // single tab. | 227 // single tab. |
| 189 TEST_F(DataUseTabModelTest, ObserverStartEndEvents) { | 228 TEST_F(DataUseTabModelTest, ObserverStartEndEvents) { |
| 190 MockTabDataUseObserver mock_observer; | 229 MockTabDataUseObserver mock_observer; |
| 191 | 230 |
| 192 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(1); | 231 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(1); |
| 193 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(1); | 232 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(1); |
| 194 | 233 |
| 195 data_use_tab_model_.AddObserver(&mock_observer); | 234 data_use_tab_model_->AddObserver(&mock_observer); |
| 196 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 235 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 197 data_use_tab_model_.EndTrackingDataUse(kTabID1); | 236 EndTrackingDataUse(kTabID1); |
| 198 | 237 |
| 199 message_loop_.RunUntilIdle(); | 238 message_loop_.RunUntilIdle(); |
| 200 } | 239 } |
| 201 | 240 |
| 202 // Checks that multiple mock observers receive start and end tracking events for | 241 // Checks that multiple mock observers receive start and end tracking events for |
| 203 // multiple tabs. | 242 // multiple tabs. |
| 204 TEST_F(DataUseTabModelTest, MultipleObserverMultipleStartEndEvents) { | 243 TEST_F(DataUseTabModelTest, MultipleObserverMultipleStartEndEvents) { |
| 205 MockTabDataUseObserver mock_observers[kMaxMockObservers]; | 244 MockTabDataUseObserver mock_observers[kMaxMockObservers]; |
| 206 | 245 |
| 207 for (auto& mock_observer : mock_observers) { | 246 for (auto& mock_observer : mock_observers) { |
| 208 // Add the observer. | 247 // Add the observer. |
| 209 data_use_tab_model_.AddObserver(&mock_observer); | 248 data_use_tab_model_->AddObserver(&mock_observer); |
| 210 | 249 |
| 211 // Expect start and end events for tab ids 1-3. | 250 // Expect start and end events for tab ids 1-3. |
| 212 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(1); | 251 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(1); |
| 213 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(1); | 252 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(1); |
| 214 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID2)).Times(1); | 253 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID2)).Times(1); |
| 215 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID2)).Times(1); | 254 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID2)).Times(1); |
| 216 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID3)).Times(1); | 255 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID3)).Times(1); |
| 217 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID3)).Times(1); | 256 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID3)).Times(1); |
| 218 } | 257 } |
| 219 | 258 |
| 220 // Start and end tracking for tab ids 1-3. | 259 // Start and end tracking for tab ids 1-3. |
| 221 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 260 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 222 data_use_tab_model_.StartTrackingDataUse(kTabID2, kTestLabel2); | 261 StartTrackingDataUse(kTabID2, kTestLabel2); |
| 223 data_use_tab_model_.StartTrackingDataUse(kTabID3, kTestLabel3); | 262 StartTrackingDataUse(kTabID3, kTestLabel3); |
| 224 data_use_tab_model_.EndTrackingDataUse(kTabID1); | 263 EndTrackingDataUse(kTabID1); |
| 225 data_use_tab_model_.EndTrackingDataUse(kTabID2); | 264 EndTrackingDataUse(kTabID2); |
| 226 data_use_tab_model_.EndTrackingDataUse(kTabID3); | 265 EndTrackingDataUse(kTabID3); |
| 227 | 266 |
| 228 message_loop_.RunUntilIdle(); | 267 message_loop_.RunUntilIdle(); |
| 229 } | 268 } |
| 230 | 269 |
| 231 // Checks that the observer is not notified of start and end events after | 270 // Checks that the observer is not notified of start and end events after |
| 232 // RemoveObserver. | 271 // RemoveObserver. |
| 233 TEST_F(DataUseTabModelTest, ObserverNotNotifiedAfterRemove) { | 272 TEST_F(DataUseTabModelTest, ObserverNotNotifiedAfterRemove) { |
| 234 MockTabDataUseObserver mock_observer; | 273 MockTabDataUseObserver mock_observer; |
| 235 | 274 |
| 236 // Observer notified of start and end events. | 275 // Observer notified of start and end events. |
| 237 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(1); | 276 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(1); |
| 238 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(1); | 277 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(1); |
| 239 | 278 |
| 240 data_use_tab_model_.AddObserver(&mock_observer); | 279 data_use_tab_model_->AddObserver(&mock_observer); |
| 241 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 280 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 242 data_use_tab_model_.EndTrackingDataUse(kTabID1); | 281 EndTrackingDataUse(kTabID1); |
| 243 | 282 |
| 244 message_loop_.RunUntilIdle(); | 283 message_loop_.RunUntilIdle(); |
| 245 testing::Mock::VerifyAndClear(&mock_observer); | 284 testing::Mock::VerifyAndClear(&mock_observer); |
| 246 | 285 |
| 247 // Observer should not be notified after RemoveObserver. | 286 // Observer should not be notified after RemoveObserver. |
| 248 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(0); | 287 EXPECT_CALL(mock_observer, NotifyTrackingStarting(kTabID1)).Times(0); |
| 249 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(0); | 288 EXPECT_CALL(mock_observer, NotifyTrackingEnding(kTabID1)).Times(0); |
| 250 | 289 |
| 251 data_use_tab_model_.RemoveObserver(&mock_observer); | 290 data_use_tab_model_->RemoveObserver(&mock_observer); |
| 252 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 291 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 253 data_use_tab_model_.EndTrackingDataUse(kTabID1); | 292 EndTrackingDataUse(kTabID1); |
| 254 | 293 |
| 255 message_loop_.RunUntilIdle(); | 294 message_loop_.RunUntilIdle(); |
| 256 } | 295 } |
| 257 | 296 |
| 258 // Checks that tab close event updates the close time of the tab entry. | 297 // Checks that tab close event updates the close time of the tab entry. |
| 259 TEST_F(DataUseTabModelTest, TabCloseEvent) { | 298 TEST_F(DataUseTabModelTest, TabCloseEvent) { |
| 260 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 299 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 261 data_use_tab_model_.EndTrackingDataUse(kTabID1); | 300 EndTrackingDataUse(kTabID1); |
| 262 | 301 |
| 263 ExpectTabEntrySize(TabEntrySize::ONE); | 302 ExpectTabEntrySize(TabEntrySize::ONE); |
| 264 EXPECT_TRUE( | 303 EXPECT_TRUE( |
| 265 data_use_tab_model_.active_tabs_[kTabID1].tab_close_time_.is_null()); | 304 data_use_tab_model_->active_tabs_[kTabID1].tab_close_time_.is_null()); |
| 266 | 305 |
| 267 data_use_tab_model_.OnTabCloseEvent(kTabID1); | 306 data_use_tab_model_->OnTabCloseEvent(kTabID1); |
| 268 | 307 |
| 269 ExpectTabEntrySize(TabEntrySize::ONE); | 308 ExpectTabEntrySize(TabEntrySize::ONE); |
| 270 EXPECT_FALSE( | 309 EXPECT_FALSE( |
| 271 data_use_tab_model_.active_tabs_[kTabID1].tab_close_time_.is_null()); | 310 data_use_tab_model_->active_tabs_[kTabID1].tab_close_time_.is_null()); |
| 272 } | 311 } |
| 273 | 312 |
| 274 // Checks that tab close event ends the active tracking session for the tab. | 313 // Checks that tab close event ends the active tracking session for the tab. |
| 275 TEST_F(DataUseTabModelTest, TabCloseEventEndsTracking) { | 314 TEST_F(DataUseTabModelTest, TabCloseEventEndsTracking) { |
| 276 data_use_tab_model_.StartTrackingDataUse(kTabID1, kTestLabel1); | 315 StartTrackingDataUse(kTabID1, kTestLabel1); |
| 277 EXPECT_TRUE(IsTrackingDataUse(kTabID1)); | 316 EXPECT_TRUE(IsTrackingDataUse(kTabID1)); |
| 278 | 317 |
| 279 data_use_tab_model_.OnTabCloseEvent(kTabID1); | 318 data_use_tab_model_->OnTabCloseEvent(kTabID1); |
| 280 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); | 319 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); |
| 281 | 320 |
| 282 // Future data use object should be labeled as an empty string. | 321 // Future data use object should be labeled as an empty string. |
| 283 ExpectEmptyDataUseLabelAtTime( | 322 ExpectEmptyDataUseLabelAtTime( |
| 284 kTabID1, base::TimeTicks::Now() + base::TimeDelta::FromMilliseconds(20)); | 323 kTabID1, base::TimeTicks::Now() + base::TimeDelta::FromMilliseconds(20)); |
| 285 } | 324 } |
| 286 | 325 |
| 287 // Checks that |active_tabs_| does not grow beyond GetMaxTabEntriesForTests tab | 326 // Checks that |active_tabs_| does not grow beyond GetMaxTabEntriesForTests tab |
| 288 // entries. | 327 // entries. |
| 289 TEST_F(DataUseTabModelTest, CompactTabEntriesWithinMaxLimit) { | 328 TEST_F(DataUseTabModelTest, CompactTabEntriesWithinMaxLimit) { |
| 290 const size_t max_tab_entries = DataUseTabModel::GetMaxTabEntriesForTests(); | 329 const size_t max_tab_entries = DataUseTabModel::GetMaxTabEntriesForTests(); |
| 291 uint32_t tab_id = 1; | 330 uint32_t tab_id = 1; |
| 292 | 331 |
| 293 ExpectTabEntrySize(TabEntrySize::ZERO); | 332 ExpectTabEntrySize(TabEntrySize::ZERO); |
| 294 | 333 |
| 295 while (tab_id <= max_tab_entries) { | 334 while (tab_id <= max_tab_entries) { |
| 296 std::string tab_label = base::StringPrintf("track_label_%d", tab_id); | 335 std::string tab_label = base::StringPrintf("label_%d", tab_id); |
| 297 data_use_tab_model_.StartTrackingDataUse(tab_id, tab_label); | 336 StartTrackingDataUse(tab_id, tab_label); |
| 298 data_use_tab_model_.EndTrackingDataUse(tab_id); | 337 EndTrackingDataUse(tab_id); |
| 299 | 338 |
| 300 ExpectTabEntrySize(tab_id); | 339 ExpectTabEntrySize(tab_id); |
| 301 ++tab_id; | 340 ++tab_id; |
| 302 } | 341 } |
| 303 | 342 |
| 304 uint32_t oldest_tab_id = 1; // oldest tab entry that will be removed first. | 343 uint32_t oldest_tab_id = 1; // oldest tab entry that will be removed first. |
| 305 | 344 |
| 306 // Starting and ending more tracking tab entries does not increase the size of | 345 // Starting and ending more tracking tab entries does not increase the size of |
| 307 // |active_tabs_|. | 346 // |active_tabs_|. |
| 308 while (tab_id < max_tab_entries + 10) { | 347 while (tab_id < max_tab_entries + 10) { |
| 309 EXPECT_TRUE(IsTabEntryExists(oldest_tab_id)); | 348 EXPECT_TRUE(IsTabEntryExists(oldest_tab_id)); |
| 310 std::string tab_label = base::StringPrintf("label_%d", tab_id); | 349 std::string tab_label = base::StringPrintf("label_%d", tab_id); |
| 311 data_use_tab_model_.StartTrackingDataUse(tab_id, tab_label); | 350 StartTrackingDataUse(tab_id, tab_label); |
| 312 data_use_tab_model_.EndTrackingDataUse(tab_id); | 351 EndTrackingDataUse(tab_id); |
| 313 | 352 |
| 314 // Oldest entry got removed. | 353 // Oldest entry got removed. |
| 315 EXPECT_FALSE(IsTabEntryExists(oldest_tab_id)); | 354 EXPECT_FALSE(IsTabEntryExists(oldest_tab_id)); |
| 316 ExpectTabEntrySize(max_tab_entries); | 355 ExpectTabEntrySize(max_tab_entries); |
| 317 | 356 |
| 318 ++tab_id; | 357 ++tab_id; |
| 319 ++oldest_tab_id; // next oldest tab entry. | 358 ++oldest_tab_id; // next oldest tab entry. |
| 320 } | 359 } |
| 321 | 360 |
| 322 // Starting and not ending more tracking tab entries does not increase the | 361 // Starting and not ending more tracking tab entries does not increase the |
| 323 // size of |active_tabs_|. | 362 // size of |active_tabs_|. |
| 324 while (tab_id < max_tab_entries + 20) { | 363 while (tab_id < max_tab_entries + 20) { |
| 325 EXPECT_TRUE(IsTabEntryExists(oldest_tab_id)); | 364 EXPECT_TRUE(IsTabEntryExists(oldest_tab_id)); |
| 326 std::string tab_label = base::StringPrintf("label_%d", tab_id); | 365 std::string tab_label = base::StringPrintf("label_%d", tab_id); |
| 327 data_use_tab_model_.StartTrackingDataUse(tab_id, tab_label); | 366 StartTrackingDataUse(tab_id, tab_label); |
| 328 | 367 |
| 329 // Oldest entry got removed. | 368 // Oldest entry got removed. |
| 330 EXPECT_FALSE(IsTabEntryExists(oldest_tab_id)); | 369 EXPECT_FALSE(IsTabEntryExists(oldest_tab_id)); |
| 331 ExpectTabEntrySize(max_tab_entries); | 370 ExpectTabEntrySize(max_tab_entries); |
| 332 | 371 |
| 333 ++tab_id; | 372 ++tab_id; |
| 334 ++oldest_tab_id; // next oldest tab entry. | 373 ++oldest_tab_id; // next oldest tab entry. |
| 335 } | 374 } |
| 336 } | 375 } |
| 337 | 376 |
| 377 TEST_F(DataUseTabModelTest, ExpiredInactiveTabEntryRemovaltimeHistogram) { | |
| 378 const char kUMAExpiredInactiveTabEntryRemovalDurationSecondsHistogram[] = | |
| 379 "DataUse.TabModel.ExpiredInactiveTabEntryRemovalDuration"; | |
| 380 base::HistogramTester histogram_tester; | |
| 381 | |
| 382 StartTrackingDataUse(kTabID1, kTestLabel1); | |
| 383 EndTrackingDataUse(kTabID1); | |
| 384 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); | |
| 385 data_use_tab_model_->OnTabCloseEvent(kTabID1); | |
| 386 | |
| 387 // Fake tab close time to make it as expired. | |
| 388 EXPECT_TRUE(IsTabEntryExists(kTabID1)); | |
| 389 auto& tab_entry = data_use_tab_model_->active_tabs_[kTabID1]; | |
| 390 EXPECT_FALSE(tab_entry.tab_close_time_.is_null()); | |
| 391 tab_entry.tab_close_time_ -= base::TimeDelta::FromSeconds( | |
| 392 TabDataUseEntry::GetClosedTabExpirationDurationSecondsForTests() + 1); | |
| 393 EXPECT_TRUE(tab_entry.IsExpired()); | |
| 394 | |
| 395 // Fast forward 50 seconds. | |
| 396 test_data_use_tab_model_->AdvanceTime(base::TimeDelta::FromSeconds(50)); | |
| 397 | |
| 398 data_use_tab_model_->CompactTabEntries(); | |
| 399 EXPECT_FALSE(IsTabEntryExists(kTabID1)); | |
| 400 | |
| 401 histogram_tester.ExpectTotalCount( | |
| 402 kUMAExpiredInactiveTabEntryRemovalDurationSecondsHistogram, 1); | |
| 403 histogram_tester.ExpectBucketCount( | |
| 404 kUMAExpiredInactiveTabEntryRemovalDurationSecondsHistogram, 50, 1); | |
| 405 } | |
| 406 | |
| 407 TEST_F(DataUseTabModelTest, UnexpiredTabEntryRemovaltimeHistogram) { | |
| 408 const char kUMAUnexpiredTabEntryRemovalDurationMinutesHistogram[] = | |
| 409 "DataUse.TabModel.UnexpiredTabEntryRemovalDuration"; | |
| 410 base::HistogramTester histogram_tester; | |
| 411 const size_t max_tab_entries = DataUseTabModel::GetMaxTabEntriesForTests(); | |
| 412 uint32_t tab_id = 1; | |
| 413 | |
| 414 while (tab_id <= max_tab_entries) { | |
| 415 std::string tab_label = base::StringPrintf("label_%d", tab_id); | |
| 416 StartTrackingDataUse(tab_id, tab_label); | |
| 417 EndTrackingDataUse(tab_id); | |
| 418 ++tab_id; | |
| 419 } | |
| 420 | |
| 421 // Fast forward 10 minutes. | |
| 422 test_data_use_tab_model_->AdvanceTime(base::TimeDelta::FromMinutes(10)); | |
| 423 | |
| 424 // Adding another tab entry triggers CompactTabEntries. | |
| 425 std::string tab_label = base::StringPrintf("label_%d", tab_id); | |
| 426 StartTrackingDataUse(tab_id, tab_label); | |
| 427 EndTrackingDataUse(tab_id); | |
| 428 | |
| 429 histogram_tester.ExpectTotalCount( | |
| 430 kUMAUnexpiredTabEntryRemovalDurationMinutesHistogram, 1); | |
| 431 histogram_tester.ExpectBucketCount( | |
| 432 kUMAUnexpiredTabEntryRemovalDurationMinutesHistogram, 10, 1); | |
| 433 } | |
| 434 | |
| 338 } // namespace android | 435 } // namespace android |
| 339 | 436 |
| 340 } // namespace chrome | 437 } // namespace chrome |
| OLD | NEW |