| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 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 "components/feature_engagement_tracker/internal/init_aware_model.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/optional.h" |
| 13 #include "components/feature_engagement_tracker/internal/proto/event.pb.h" |
| 14 #include "components/feature_engagement_tracker/internal/test/event_util.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using testing::_; |
| 19 using testing::Return; |
| 20 using testing::SaveArg; |
| 21 using testing::Sequence; |
| 22 |
| 23 namespace feature_engagement_tracker { |
| 24 |
| 25 namespace { |
| 26 |
| 27 class MockModel : public Model { |
| 28 public: |
| 29 MockModel() = default; |
| 30 ~MockModel() override = default; |
| 31 |
| 32 // Model implementation. |
| 33 MOCK_METHOD2(Initialize, |
| 34 void(const OnModelInitializationFinished&, uint32_t)); |
| 35 MOCK_CONST_METHOD0(IsReady, bool()); |
| 36 MOCK_CONST_METHOD1(GetEvent, Event*(const std::string&)); |
| 37 MOCK_METHOD2(IncrementEvent, void(const std::string&, uint32_t)); |
| 38 |
| 39 private: |
| 40 DISALLOW_COPY_AND_ASSIGN(MockModel); |
| 41 }; |
| 42 |
| 43 class InitAwareModelTest : public testing::Test { |
| 44 public: |
| 45 InitAwareModelTest() : mocked_model_(nullptr) { |
| 46 load_callback_ = base::Bind(&InitAwareModelTest::OnModelInitialized, |
| 47 base::Unretained(this)); |
| 48 } |
| 49 |
| 50 ~InitAwareModelTest() override = default; |
| 51 |
| 52 void SetUp() override { |
| 53 auto mocked_model = base::MakeUnique<MockModel>(); |
| 54 mocked_model_ = mocked_model.get(); |
| 55 model_ = base::MakeUnique<InitAwareModel>(std::move(mocked_model)); |
| 56 } |
| 57 |
| 58 protected: |
| 59 void OnModelInitialized(bool success) { load_success_ = success; } |
| 60 |
| 61 std::unique_ptr<InitAwareModel> model_; |
| 62 MockModel* mocked_model_; |
| 63 |
| 64 // Load callback tracking. |
| 65 base::Optional<bool> load_success_; |
| 66 Model::OnModelInitializationFinished load_callback_; |
| 67 |
| 68 private: |
| 69 DISALLOW_COPY_AND_ASSIGN(InitAwareModelTest); |
| 70 }; |
| 71 |
| 72 } // namespace |
| 73 |
| 74 TEST_F(InitAwareModelTest, PassThroughIsReady) { |
| 75 EXPECT_CALL(*mocked_model_, IsReady()).Times(1); |
| 76 model_->IsReady(); |
| 77 } |
| 78 |
| 79 TEST_F(InitAwareModelTest, PassThroughGetEvent) { |
| 80 Event foo; |
| 81 foo.set_name("foo"); |
| 82 test::SetEventCountForDay(&foo, 1, 1); |
| 83 |
| 84 EXPECT_CALL(*mocked_model_, GetEvent(foo.name())) |
| 85 .WillRepeatedly(Return(&foo)); |
| 86 EXPECT_CALL(*mocked_model_, GetEvent("bar")).WillRepeatedly(Return(nullptr)); |
| 87 |
| 88 test::VerifyEventsEqual(&foo, model_->GetEvent(foo.name())); |
| 89 EXPECT_EQ(nullptr, model_->GetEvent("bar")); |
| 90 } |
| 91 |
| 92 TEST_F(InitAwareModelTest, PassThroughIncrementEvent) { |
| 93 EXPECT_CALL(*mocked_model_, IsReady()).WillRepeatedly(Return(true)); |
| 94 |
| 95 Sequence sequence; |
| 96 EXPECT_CALL(*mocked_model_, IncrementEvent("foo", 0U)).InSequence(sequence); |
| 97 EXPECT_CALL(*mocked_model_, IncrementEvent("bar", 1U)).InSequence(sequence); |
| 98 |
| 99 model_->IncrementEvent("foo", 0U); |
| 100 model_->IncrementEvent("bar", 1U); |
| 101 } |
| 102 |
| 103 TEST_F(InitAwareModelTest, QueuedIncrementEvent) { |
| 104 { |
| 105 EXPECT_CALL(*mocked_model_, IsReady()).WillRepeatedly(Return(false)); |
| 106 EXPECT_CALL(*mocked_model_, IncrementEvent(_, _)).Times(0); |
| 107 |
| 108 model_->IncrementEvent("foo", 0U); |
| 109 model_->IncrementEvent("bar", 1U); |
| 110 } |
| 111 |
| 112 Model::OnModelInitializationFinished callback; |
| 113 EXPECT_CALL(*mocked_model_, Initialize(_, 2U)) |
| 114 .WillOnce(SaveArg<0>(&callback)); |
| 115 model_->Initialize(load_callback_, 2U); |
| 116 |
| 117 { |
| 118 Sequence sequence; |
| 119 EXPECT_CALL(*mocked_model_, IncrementEvent("foo", 0U)) |
| 120 .Times(1) |
| 121 .InSequence(sequence); |
| 122 EXPECT_CALL(*mocked_model_, IncrementEvent("bar", 1U)) |
| 123 .Times(1) |
| 124 .InSequence(sequence); |
| 125 |
| 126 callback.Run(true); |
| 127 EXPECT_TRUE(load_success_.value()); |
| 128 } |
| 129 |
| 130 EXPECT_CALL(*mocked_model_, IsReady()).WillRepeatedly(Return(true)); |
| 131 EXPECT_CALL(*mocked_model_, IncrementEvent("qux", 3U)).Times(1); |
| 132 model_->IncrementEvent("qux", 3U); |
| 133 } |
| 134 |
| 135 } // namespace feature_engagement_tracker |
| OLD | NEW |