| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ash/common/system/chromeos/network/vpn_delegate.h" | 5 #include "ash/common/system/chromeos/network/vpn_list.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/public/interfaces/vpn_list.mojom.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 14 using ash::mojom::ThirdPartyVpnProvider; |
| 15 using ash::mojom::ThirdPartyVpnProviderPtr; |
| 16 |
| 13 namespace ash { | 17 namespace ash { |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 | 20 |
| 17 class TestVpnDelegate : public VPNDelegate { | 21 class TestVpnListObserver : public VpnList::Observer { |
| 18 public: | 22 public: |
| 19 TestVpnDelegate() {} | 23 TestVpnListObserver() {} |
| 20 ~TestVpnDelegate() override {} | 24 ~TestVpnListObserver() override {} |
| 21 | 25 |
| 22 // VPNDelegate: | 26 // VpnList::Observer: |
| 23 void ShowAddPage(const std::string& extension_id) override {} | 27 void OnVPNProvidersChanged() override { change_count_++; } |
| 24 | 28 |
| 25 private: | 29 int change_count_ = 0; |
| 26 DISALLOW_COPY_AND_ASSIGN(TestVpnDelegate); | |
| 27 }; | 30 }; |
| 28 | 31 |
| 29 } // namespace | 32 } // namespace |
| 30 | 33 |
| 31 using VpnDelegateTest = testing::Test; | 34 using VpnListTest = testing::Test; |
| 32 | 35 |
| 33 TEST_F(VpnDelegateTest, BuiltInProvider) { | 36 TEST_F(VpnListTest, BuiltInProvider) { |
| 34 TestVpnDelegate delegate; | 37 VpnList vpn_list; |
| 35 | 38 |
| 36 // The VPN list should only contain the built-in provider. | 39 // The VPN list should only contain the built-in provider. |
| 37 ASSERT_EQ(1u, delegate.vpn_providers().size()); | 40 ASSERT_EQ(1u, vpn_list.vpn_providers().size()); |
| 38 VPNProvider provider = delegate.vpn_providers()[0]; | 41 VPNProvider provider = vpn_list.vpn_providers()[0]; |
| 39 EXPECT_FALSE(provider.third_party); | 42 EXPECT_FALSE(provider.third_party); |
| 40 EXPECT_TRUE(provider.extension_id.empty()); | 43 EXPECT_TRUE(provider.extension_id.empty()); |
| 41 } | 44 } |
| 42 | 45 |
| 43 TEST_F(VpnDelegateTest, ThirdPartyProviders) { | 46 TEST_F(VpnListTest, ThirdPartyProviders) { |
| 44 TestVpnDelegate delegate; | 47 VpnList vpn_list; |
| 45 | 48 |
| 46 // The VPN list should only contain the built-in provider. | 49 // The VPN list should only contain the built-in provider. |
| 47 EXPECT_EQ(1u, delegate.vpn_providers().size()); | 50 EXPECT_EQ(1u, vpn_list.vpn_providers().size()); |
| 48 | 51 |
| 49 // Add some third party (extension-backed) providers. | 52 // Add some third party (extension-backed) providers. |
| 53 std::vector<ThirdPartyVpnProviderPtr> third_party_providers; |
| 54 ThirdPartyVpnProviderPtr third_party1 = ThirdPartyVpnProvider::New(); |
| 55 third_party1->name = "name1"; |
| 56 third_party1->extension_id = "extension_id1"; |
| 57 third_party_providers.push_back(std::move(third_party1)); |
| 58 |
| 59 ThirdPartyVpnProviderPtr third_party2 = ThirdPartyVpnProvider::New(); |
| 60 third_party2->name = "name2"; |
| 61 third_party2->extension_id = "extension_id2"; |
| 62 third_party_providers.push_back(std::move(third_party2)); |
| 63 |
| 64 vpn_list.SetThirdPartyVpnProviders(std::move(third_party_providers)); |
| 65 |
| 66 // Mojo types will be converted to internal ash types. |
| 50 VPNProvider provider1("extension_id1", "name1"); | 67 VPNProvider provider1("extension_id1", "name1"); |
| 51 VPNProvider provider2("extension_id2", "name2"); | 68 VPNProvider provider2("extension_id2", "name2"); |
| 52 std::vector<VPNProvider> third_party_providers = {provider1, provider2}; | |
| 53 delegate.SetThirdPartyVpnProviders(third_party_providers); | |
| 54 | 69 |
| 55 // List contains the extension-backed providers. Order doesn't matter. | 70 // List contains the extension-backed providers. Order doesn't matter. |
| 56 std::vector<VPNProvider> providers = delegate.vpn_providers(); | 71 std::vector<VPNProvider> providers = vpn_list.vpn_providers(); |
| 57 EXPECT_EQ(3u, providers.size()); | 72 EXPECT_EQ(3u, providers.size()); |
| 58 EXPECT_EQ(1u, std::count(providers.begin(), providers.end(), provider1)); | 73 EXPECT_EQ(1u, std::count(providers.begin(), providers.end(), provider1)); |
| 59 EXPECT_EQ(1u, std::count(providers.begin(), providers.end(), provider2)); | 74 EXPECT_EQ(1u, std::count(providers.begin(), providers.end(), provider2)); |
| 60 } | 75 } |
| 61 | 76 |
| 77 TEST_F(VpnListTest, Observers) { |
| 78 VpnList vpn_list; |
| 79 |
| 80 // Observers are not notified when they are added. |
| 81 TestVpnListObserver observer; |
| 82 vpn_list.AddObserver(&observer); |
| 83 EXPECT_EQ(0, observer.change_count_); |
| 84 |
| 85 // Add a third party (extension-backed) provider. |
| 86 std::vector<ThirdPartyVpnProviderPtr> third_party_providers; |
| 87 ThirdPartyVpnProviderPtr third_party1 = ThirdPartyVpnProvider::New(); |
| 88 third_party1->name = "name1"; |
| 89 third_party1->extension_id = "extension_id1"; |
| 90 third_party_providers.push_back(std::move(third_party1)); |
| 91 vpn_list.SetThirdPartyVpnProviders(std::move(third_party_providers)); |
| 92 |
| 93 // Observer was notified. |
| 94 EXPECT_EQ(1, observer.change_count_); |
| 95 |
| 96 vpn_list.RemoveObserver(&observer); |
| 97 } |
| 98 |
| 62 } // namespace ash | 99 } // namespace ash |
| OLD | NEW |