| 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/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/test/histogram_tester.h" | 18 #include "base/test/histogram_tester.h" |
| 19 #include "base/test/simple_test_tick_clock.h" | 19 #include "base/test/simple_test_tick_clock.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "chrome/browser/android/data_usage/data_use_matcher.h" | 21 #include "chrome/browser/android/data_usage/data_use_matcher.h" |
| 22 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" |
| 22 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" | 23 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" |
| 23 #include "components/data_usage/core/data_use_aggregator.h" | 24 #include "components/data_usage/core/data_use_aggregator.h" |
| 24 #include "components/data_usage/core/data_use_amortizer.h" | 25 #include "components/data_usage/core/data_use_amortizer.h" |
| 25 #include "components/data_usage/core/data_use_annotator.h" | 26 #include "components/data_usage/core/data_use_annotator.h" |
| 26 #include "components/sessions/core/session_id.h" | 27 #include "components/sessions/core/session_id.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/test/test_browser_thread_bundle.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
| 29 #include "net/base/network_change_notifier.h" | 30 #include "net/base/network_change_notifier.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 60 }; | 61 }; |
| 61 | 62 |
| 62 // Mock observer to track the calls to start and end tracking events. | 63 // Mock observer to track the calls to start and end tracking events. |
| 63 class MockTabDataUseObserver | 64 class MockTabDataUseObserver |
| 64 : public chrome::android::DataUseTabModel::TabDataUseObserver { | 65 : public chrome::android::DataUseTabModel::TabDataUseObserver { |
| 65 public: | 66 public: |
| 66 MOCK_METHOD1(NotifyTrackingStarting, void(SessionID::id_type tab_id)); | 67 MOCK_METHOD1(NotifyTrackingStarting, void(SessionID::id_type tab_id)); |
| 67 MOCK_METHOD1(NotifyTrackingEnding, void(SessionID::id_type tab_id)); | 68 MOCK_METHOD1(NotifyTrackingEnding, void(SessionID::id_type tab_id)); |
| 68 }; | 69 }; |
| 69 | 70 |
| 71 class TestExternalDataUseObserverBridge |
| 72 : public chrome::android::ExternalDataUseObserverBridge { |
| 73 public: |
| 74 TestExternalDataUseObserverBridge() {} |
| 75 void FetchMatchingRules() const override {} |
| 76 void ShouldRegisterAsDataUseObserver(bool should_register) const override{}; |
| 77 }; |
| 78 |
| 70 } // namespace | 79 } // namespace |
| 71 | 80 |
| 72 namespace chrome { | 81 namespace chrome { |
| 73 | 82 |
| 74 namespace android { | 83 namespace android { |
| 75 | 84 |
| 76 class ExternalDataUseObserver; | |
| 77 | |
| 78 class DataUseTabModelTest : public testing::Test { | 85 class DataUseTabModelTest : public testing::Test { |
| 79 public: | 86 public: |
| 80 DataUseTabModelTest() | 87 DataUseTabModelTest() |
| 81 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 88 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 89 external_data_use_observer_bridge_( |
| 90 new TestExternalDataUseObserverBridge()) {} |
| 82 | 91 |
| 83 protected: | 92 protected: |
| 84 void SetUp() override { | 93 void SetUp() override { |
| 85 base::RunLoop().RunUntilIdle(); | 94 base::RunLoop().RunUntilIdle(); |
| 86 data_use_tab_model_.reset(new DataUseTabModel()); | 95 data_use_tab_model_.reset(new DataUseTabModel()); |
| 87 data_use_tab_model_->InitOnUIThread( | 96 data_use_tab_model_->InitOnUIThread( |
| 88 content::BrowserThread::GetMessageLoopProxyForThread( | 97 external_data_use_observer_bridge_.get()); |
| 89 content::BrowserThread::IO), | |
| 90 base::WeakPtr<ExternalDataUseObserver>()); | |
| 91 | 98 |
| 92 tick_clock_ = new base::SimpleTestTickClock(); | 99 tick_clock_ = new base::SimpleTestTickClock(); |
| 93 | 100 |
| 94 // Advance to non nil time. | 101 // Advance to non nil time. |
| 95 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); | 102 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); |
| 96 | 103 |
| 97 // |tick_clock_| will be owned by |data_use_tab_model_|. | 104 // |tick_clock_| will be owned by |data_use_tab_model_|. |
| 98 data_use_tab_model_->tick_clock_.reset(tick_clock_); | 105 data_use_tab_model_->tick_clock_.reset(tick_clock_); |
| 99 data_use_tab_model_->OnControlAppInstallStateChange(true); | 106 data_use_tab_model_->OnControlAppInstallStateChange(true); |
| 100 } | 107 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 labels); | 189 labels); |
| 183 } | 190 } |
| 184 | 191 |
| 185 // Pointer to the tick clock owned by |data_use_tab_model_|. | 192 // Pointer to the tick clock owned by |data_use_tab_model_|. |
| 186 base::SimpleTestTickClock* tick_clock_; | 193 base::SimpleTestTickClock* tick_clock_; |
| 187 | 194 |
| 188 scoped_ptr<DataUseTabModel> data_use_tab_model_; | 195 scoped_ptr<DataUseTabModel> data_use_tab_model_; |
| 189 | 196 |
| 190 private: | 197 private: |
| 191 content::TestBrowserThreadBundle thread_bundle_; | 198 content::TestBrowserThreadBundle thread_bundle_; |
| 199 scoped_ptr<ExternalDataUseObserverBridge> external_data_use_observer_bridge_; |
| 192 | 200 |
| 193 DISALLOW_COPY_AND_ASSIGN(DataUseTabModelTest); | 201 DISALLOW_COPY_AND_ASSIGN(DataUseTabModelTest); |
| 194 }; | 202 }; |
| 195 | 203 |
| 196 // Starts and ends tracking a single tab and checks if its label is returned | 204 // Starts and ends tracking a single tab and checks if its label is returned |
| 197 // correctly for DataUse objects. | 205 // correctly for DataUse objects. |
| 198 TEST_F(DataUseTabModelTest, SingleTabTracking) { | 206 TEST_F(DataUseTabModelTest, SingleTabTracking) { |
| 199 ExpectTabEntrySize(TabEntrySize::ZERO); | 207 ExpectTabEntrySize(TabEntrySize::ZERO); |
| 200 | 208 |
| 201 // No label is applied initially. | 209 // No label is applied initially. |
| (...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 | 916 |
| 909 data_use_tab_model_->OnControlAppInstallStateChange(false); | 917 data_use_tab_model_->OnControlAppInstallStateChange(false); |
| 910 | 918 |
| 911 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); | 919 EXPECT_FALSE(IsTrackingDataUse(kTabID1)); |
| 912 EXPECT_FALSE(data_use_tab_model_->data_use_matcher_->HasValidRules()); | 920 EXPECT_FALSE(data_use_tab_model_->data_use_matcher_->HasValidRules()); |
| 913 } | 921 } |
| 914 | 922 |
| 915 } // namespace android | 923 } // namespace android |
| 916 | 924 |
| 917 } // namespace chrome | 925 } // namespace chrome |
| OLD | NEW |