| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "chrome/browser/media/router/issue_manager.h" | 9 #include "chrome/browser/media/router/issue_manager.h" |
| 10 #include "chrome/browser/media/router/mock_media_router.h" |
| 11 #include "chrome/browser/media/router/test_helper.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 12 | 14 |
| 15 using testing::_; |
| 16 using testing::SaveArg; |
| 17 |
| 13 namespace media_router { | 18 namespace media_router { |
| 14 namespace { | 19 namespace { |
| 15 | 20 |
| 16 const char kTestRouteId[] = "routeId"; | 21 IssueInfo CreateTestIssue(IssueInfo::Severity severity) { |
| 17 | 22 IssueInfo issue("title", IssueInfo::Action::DISMISS, severity); |
| 18 Issue CreateTestIssue(const std::string& route_id) { | 23 issue.message = "message"; |
| 19 return Issue("title", "message", IssueAction(IssueAction::TYPE_DISMISS), | 24 issue.help_page_id = 12345; |
| 20 std::vector<IssueAction>(), route_id, Issue::WARNING, false, | 25 return issue; |
| 21 12345); | |
| 22 } | 26 } |
| 23 | 27 |
| 24 class IssueManagerUnitTest : public ::testing::Test { | 28 } // namespace |
| 29 |
| 30 class IssueManagerTest : public ::testing::Test { |
| 25 protected: | 31 protected: |
| 26 IssueManagerUnitTest() {} | 32 IssueManagerTest() {} |
| 27 ~IssueManagerUnitTest() override {} | 33 ~IssueManagerTest() override {} |
| 28 | 34 |
| 29 content::TestBrowserThreadBundle thread_bundle_; | 35 content::TestBrowserThreadBundle thread_bundle_; |
| 30 IssueManager manager_; | 36 IssueManager manager_; |
| 37 MockMediaRouter router_; |
| 31 | 38 |
| 32 private: | 39 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(IssueManagerUnitTest); | 40 DISALLOW_COPY_AND_ASSIGN(IssueManagerTest); |
| 34 }; | 41 }; |
| 35 | 42 |
| 36 TEST_F(IssueManagerUnitTest, InitializeManager) { | 43 TEST_F(IssueManagerTest, AddAndClearIssue) { |
| 37 // Before anything is done to the manager, it should hold no issues. | 44 IssueInfo issue_info1 = CreateTestIssue(IssueInfo::Severity::WARNING); |
| 38 EXPECT_EQ(0u, manager_.GetIssueCount()); | 45 |
| 46 // Add initial issue. |
| 47 manager_.AddIssue(issue_info1); |
| 48 |
| 49 Issue issue1((IssueInfo())); |
| 50 MockIssuesObserver observer(&router_); |
| 51 EXPECT_CALL(observer, OnIssue(_)).WillOnce(SaveArg<0>(&issue1)); |
| 52 manager_.RegisterObserver(&observer); |
| 53 EXPECT_EQ(issue_info1, issue1.info()); |
| 54 Issue::Id issue1_id = issue1.id(); |
| 55 |
| 56 IssueInfo issue_info2 = CreateTestIssue(IssueInfo::Severity::FATAL); |
| 57 EXPECT_TRUE(issue_info2.is_blocking); |
| 58 |
| 59 // Blocking issue takes precedence. |
| 60 Issue issue2((IssueInfo())); |
| 61 EXPECT_CALL(observer, OnIssue(_)).WillOnce(SaveArg<0>(&issue2)); |
| 62 manager_.AddIssue(issue_info2); |
| 63 EXPECT_EQ(issue_info2, issue2.info()); |
| 64 |
| 65 // Clear |issue2|. Observer will be notified with |issue1| again as it is now |
| 66 // the top issue. |
| 67 EXPECT_CALL(observer, OnIssue(_)).WillOnce(SaveArg<0>(&issue1)); |
| 68 manager_.ClearIssue(issue2.id()); |
| 69 EXPECT_EQ(issue1_id, issue1.id()); |
| 70 EXPECT_EQ(issue_info1, issue1.info()); |
| 71 |
| 72 // All issues cleared. Observer will be notified with |nullptr| that there are |
| 73 // no more issues. |
| 74 EXPECT_CALL(observer, OnIssuesCleared()); |
| 75 manager_.ClearIssue(issue1.id()); |
| 76 |
| 77 manager_.UnregisterObserver(&observer); |
| 39 } | 78 } |
| 40 | 79 |
| 41 TEST_F(IssueManagerUnitTest, AddIssue) { | 80 TEST_F(IssueManagerTest, AddSameIssueInfoHasNoEffect) { |
| 42 Issue issue = CreateTestIssue(kTestRouteId); | 81 IssueInfo issue_info = CreateTestIssue(IssueInfo::Severity::WARNING); |
| 43 | 82 |
| 44 // Add initial issue. | 83 MockIssuesObserver observer(&router_); |
| 45 manager_.AddIssue(issue); | 84 manager_.RegisterObserver(&observer); |
| 46 EXPECT_EQ(1u, manager_.GetIssueCount()); | |
| 47 | 85 |
| 48 // Attempt to add the same issue. Duplicates should not be inserted. | 86 Issue issue((IssueInfo())); |
| 49 manager_.AddIssue(issue); | 87 EXPECT_CALL(observer, OnIssue(_)).WillOnce(SaveArg<0>(&issue)); |
| 50 EXPECT_EQ(1u, manager_.GetIssueCount()); | 88 manager_.AddIssue(issue_info); |
| 89 EXPECT_EQ(issue_info, issue.info()); |
| 90 |
| 91 // Adding the same IssueInfo has no effect. |
| 92 manager_.AddIssue(issue_info); |
| 93 |
| 94 EXPECT_CALL(observer, OnIssuesCleared()); |
| 95 manager_.ClearIssue(issue.id()); |
| 96 manager_.UnregisterObserver(&observer); |
| 51 } | 97 } |
| 52 | 98 |
| 53 TEST_F(IssueManagerUnitTest, ClearIssue) { | |
| 54 Issue issue = CreateTestIssue(kTestRouteId); | |
| 55 | |
| 56 // Remove an issue that doesn't exist. | |
| 57 manager_.ClearIssue("id"); | |
| 58 | |
| 59 // Add initial issue. | |
| 60 manager_.AddIssue(issue); | |
| 61 EXPECT_EQ(1u, manager_.GetIssueCount()); | |
| 62 | |
| 63 // Remove the only issue. | |
| 64 manager_.ClearIssue(issue.id()); | |
| 65 EXPECT_EQ(0u, manager_.GetIssueCount()); | |
| 66 | |
| 67 // Remove an issue that doesn't exist. | |
| 68 manager_.ClearIssue("id"); | |
| 69 EXPECT_EQ(0u, manager_.GetIssueCount()); | |
| 70 } | |
| 71 | |
| 72 TEST_F(IssueManagerUnitTest, ClearAllIssues) { | |
| 73 // Add ten issues. | |
| 74 for (int i = 0; i < 10; i++) { | |
| 75 manager_.AddIssue(CreateTestIssue(kTestRouteId)); | |
| 76 } | |
| 77 | |
| 78 // Check that the issues were added. | |
| 79 EXPECT_EQ(10u, manager_.GetIssueCount()); | |
| 80 | |
| 81 // Remove all the issues. | |
| 82 manager_.ClearAllIssues(); | |
| 83 EXPECT_EQ(0u, manager_.GetIssueCount()); | |
| 84 } | |
| 85 | |
| 86 TEST_F(IssueManagerUnitTest, ClearGlobalIssues) { | |
| 87 // Add ten non-global issues. | |
| 88 for (int i = 0; i < 10; i++) { | |
| 89 manager_.AddIssue(CreateTestIssue(kTestRouteId)); | |
| 90 } | |
| 91 | |
| 92 // Check that the issues were added. | |
| 93 EXPECT_EQ(10u, manager_.GetIssueCount()); | |
| 94 | |
| 95 // Add five global issues. | |
| 96 for (int i = 0; i < 5; i++) { | |
| 97 manager_.AddIssue(CreateTestIssue("")); | |
| 98 } | |
| 99 | |
| 100 // Check that the issues were added. | |
| 101 EXPECT_EQ(15u, manager_.GetIssueCount()); | |
| 102 | |
| 103 // Remove all the global issues. | |
| 104 manager_.ClearGlobalIssues(); | |
| 105 EXPECT_EQ(10u, manager_.GetIssueCount()); | |
| 106 } | |
| 107 | |
| 108 TEST_F(IssueManagerUnitTest, ClearIssuesWithRouteId) { | |
| 109 const std::string route_id_one = "route_id1"; | |
| 110 const std::string route_id_two = "route_id2"; | |
| 111 | |
| 112 // Add ten issues with the same route. | |
| 113 for (int i = 0; i < 10; i++) { | |
| 114 manager_.AddIssue(CreateTestIssue(route_id_one)); | |
| 115 } | |
| 116 | |
| 117 // Check that the issues were added. | |
| 118 EXPECT_EQ(10u, manager_.GetIssueCount()); | |
| 119 | |
| 120 // Add ten issues with a different route. | |
| 121 for (int i = 0; i < 10; i++) { | |
| 122 manager_.AddIssue(CreateTestIssue(route_id_two)); | |
| 123 } | |
| 124 | |
| 125 // Check that the issues were added. | |
| 126 EXPECT_EQ(20u, manager_.GetIssueCount()); | |
| 127 | |
| 128 // Add ten global issues. | |
| 129 for (int i = 0; i < 10; i++) { | |
| 130 manager_.AddIssue(CreateTestIssue("")); | |
| 131 } | |
| 132 | |
| 133 // Check that the issues were added. | |
| 134 EXPECT_EQ(30u, manager_.GetIssueCount()); | |
| 135 | |
| 136 // Remove all routes with route_id_one. | |
| 137 manager_.ClearIssuesWithRouteId(route_id_one); | |
| 138 EXPECT_EQ(20u, manager_.GetIssueCount()); | |
| 139 | |
| 140 // Remove all routes with route_id_two. | |
| 141 manager_.ClearIssuesWithRouteId(route_id_two); | |
| 142 EXPECT_EQ(10u, manager_.GetIssueCount()); | |
| 143 } | |
| 144 | |
| 145 } // namespace | |
| 146 } // namespace media_router | 99 } // namespace media_router |
| OLD | NEW |