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. 4 5#include "chrome/browser/apps/drive/drive_app_mapping.h" 6 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" 11 12class DriveAppMappingTest : public testing::Test { 13 public: 14 DriveAppMappingTest() {} 15 virtual ~DriveAppMappingTest() {} 16 17 // testing::Test: 18 virtual void SetUp() OVERRIDE { 19 pref_service_.reset(new TestingPrefServiceSyncable); 20 DriveAppMapping::RegisterProfilePrefs(pref_service_->registry()); 21 22 mapping_.reset(new DriveAppMapping(pref_service_.get())); 23 } 24 25 DriveAppMapping* mapping() { return mapping_.get(); } 26 27 private: 28 scoped_ptr<TestingPrefServiceSyncable> pref_service_; 29 scoped_ptr<DriveAppMapping> mapping_; 30 31 DISALLOW_COPY_AND_ASSIGN(DriveAppMappingTest); 32}; 33 34TEST_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()); 40} 41 42TEST_F(DriveAppMappingTest, Add) { 43 std::set<std::string> drive_app_ids; 44 45 // Add one. 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")); 53 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")); 64 65 // Add another one. 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")); 73 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")); 78 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")); 82} 83 84TEST_F(DriveAppMappingTest, Remove) { 85 std::set<std::string> drive_app_ids; 86 87 // Prepare data. 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()); 92 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()); 97 98 // Remove one. 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()); 104 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()); 109 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()); 116} 117