1 // Copyright 2014 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.
5 #include "chrome/browser/apps/drive/drive_app_mapping.h"
7 #include "base/macros.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/test/base/testing_pref_service_syncable.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 class DriveAppMappingTest
: public testing::Test
{
14 DriveAppMappingTest() {}
15 ~DriveAppMappingTest() override
{}
18 void SetUp() override
{
19 pref_service_
.reset(new TestingPrefServiceSyncable
);
20 DriveAppMapping::RegisterProfilePrefs(pref_service_
->registry());
22 mapping_
.reset(new DriveAppMapping(pref_service_
.get()));
25 DriveAppMapping
* mapping() { return mapping_
.get(); }
28 scoped_ptr
<TestingPrefServiceSyncable
> pref_service_
;
29 scoped_ptr
<DriveAppMapping
> mapping_
;
31 DISALLOW_COPY_AND_ASSIGN(DriveAppMappingTest
);
34 TEST_F(DriveAppMappingTest
, Empty
) {
35 EXPECT_EQ("", mapping()->GetChromeApp(""));
36 EXPECT_EQ("", mapping()->GetDriveApp(""));
37 EXPECT_EQ("", mapping()->GetChromeApp("non-existent-drive-app"));
38 EXPECT_EQ("", mapping()->GetDriveApp("non-existent-chrome-app"));
39 EXPECT_EQ(0u, mapping()->GetDriveAppIds().size());
42 TEST_F(DriveAppMappingTest
, Add
) {
43 std::set
<std::string
> drive_app_ids
;
46 mapping()->Add("drive", "chrome", false);
47 EXPECT_EQ("chrome", mapping()->GetChromeApp("drive"));
48 EXPECT_EQ("drive", mapping()->GetDriveApp("chrome"));
49 EXPECT_FALSE(mapping()->IsChromeAppGenerated("chrome"));
50 drive_app_ids
= mapping()->GetDriveAppIds();
51 EXPECT_EQ(1u, drive_app_ids
.size());
52 EXPECT_EQ(1u, drive_app_ids
.count("drive"));
54 // Overwrite previous mapping if added under the same key.
55 mapping()->Add("drive", "another-chrome-app", true);
56 EXPECT_EQ("", mapping()->GetDriveApp("chrome"));
57 EXPECT_FALSE(mapping()->IsChromeAppGenerated("chrome"));
58 EXPECT_EQ("another-chrome-app", mapping()->GetChromeApp("drive"));
59 EXPECT_EQ("drive", mapping()->GetDriveApp("another-chrome-app"));
60 EXPECT_TRUE(mapping()->IsChromeAppGenerated("another-chrome-app"));
61 drive_app_ids
= mapping()->GetDriveAppIds();
62 EXPECT_EQ(1u, drive_app_ids
.size());
63 EXPECT_EQ(1u, drive_app_ids
.count("drive"));
66 mapping()->Add("drive-1", "chrome-1", false);
67 EXPECT_EQ("chrome-1", mapping()->GetChromeApp("drive-1"));
68 EXPECT_EQ("drive-1", mapping()->GetDriveApp("chrome-1"));
69 drive_app_ids
= mapping()->GetDriveAppIds();
70 EXPECT_EQ(2u, drive_app_ids
.size());
71 EXPECT_EQ(1u, drive_app_ids
.count("drive"));
72 EXPECT_EQ(1u, drive_app_ids
.count("drive-1"));
74 // Previous mapping should not be affected by new mapping.
75 EXPECT_EQ("another-chrome-app", mapping()->GetChromeApp("drive"));
76 EXPECT_EQ("drive", mapping()->GetDriveApp("another-chrome-app"));
77 EXPECT_TRUE(mapping()->IsChromeAppGenerated("another-chrome-app"));
79 // Non-existent value returns empty string.
80 EXPECT_EQ("", mapping()->GetChromeApp("non-existent-drive-app"));
81 EXPECT_EQ("", mapping()->GetDriveApp("non-existent-chrome-app"));
84 TEST_F(DriveAppMappingTest
, Remove
) {
85 std::set
<std::string
> drive_app_ids
;
88 mapping()->Add("drive-1", "chrome-1", false);
89 mapping()->Add("drive-2", "chrome-2", false);
90 drive_app_ids
= mapping()->GetDriveAppIds();
91 EXPECT_EQ(2u, drive_app_ids
.size());
93 // Remove non-existent.
94 mapping()->Remove("non-existent-drive-app");
95 drive_app_ids
= mapping()->GetDriveAppIds();
96 EXPECT_EQ(2u, drive_app_ids
.size());
99 EXPECT_EQ("chrome-1", mapping()->GetChromeApp("drive-1"));
100 mapping()->Remove("drive-1");
101 EXPECT_EQ("", mapping()->GetChromeApp("drive-1"));
102 drive_app_ids
= mapping()->GetDriveAppIds();
103 EXPECT_EQ(1u, drive_app_ids
.size());
105 // Remove it again has no effect.
106 mapping()->Remove("drive-1");
107 drive_app_ids
= mapping()->GetDriveAppIds();
108 EXPECT_EQ(1u, drive_app_ids
.size());
110 // Remove another one.
111 EXPECT_EQ("chrome-2", mapping()->GetChromeApp("drive-2"));
112 mapping()->Remove("drive-2");
113 EXPECT_EQ("", mapping()->GetChromeApp("drive-2"));
114 drive_app_ids
= mapping()->GetDriveAppIds();
115 EXPECT_EQ(0u, drive_app_ids
.size());
118 TEST_F(DriveAppMappingTest
, TrackUninstall
) {
119 const std::string drive_app_id
= "drive-1";
121 mapping()->AddUninstalledDriveApp(drive_app_id
);
122 EXPECT_TRUE(mapping()->IsUninstalledDriveApp(drive_app_id
));
124 mapping()->RemoveUninstalledDriveApp(drive_app_id
);
125 EXPECT_FALSE(mapping()->IsUninstalledDriveApp(drive_app_id
));