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