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 "base/files/scoped_temp_dir.h"
6 #include "base/path_service.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/time/time.h"
9 #include "components/signin/core/browser/webdata/token_service_table.h"
10 #include "components/webdata/common/web_database.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class TokenServiceTableTest : public testing::Test {
17 TokenServiceTableTest() {}
18 virtual ~TokenServiceTableTest() {}
21 virtual void SetUp() {
22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
23 file_ = temp_dir_.path().AppendASCII("TestWebDatabase");
25 table_.reset(new TokenServiceTable);
26 db_.reset(new WebDatabase);
27 db_->AddTable(table_.get());
28 ASSERT_EQ(sql::INIT_OK, db_->Init(file_));
32 base::ScopedTempDir temp_dir_;
33 scoped_ptr<TokenServiceTable> table_;
34 scoped_ptr<WebDatabase> db_;
36 DISALLOW_COPY_AND_ASSIGN(TokenServiceTableTest);
39 // Flaky on mac_rel. See http://crbug.com/228943
40 #if defined(OS_MACOSX)
41 #define MAYBE_TokenServiceGetAllRemoveAll DISABLED_TokenServiceGetAllRemoveAll
42 #define MAYBE_TokenServiceGetSet DISABLED_TokenServiceGetSet
43 #define MAYBE_TokenServiceRemove DISABLED_TokenServiceRemove
45 #define MAYBE_TokenServiceGetAllRemoveAll TokenServiceGetAllRemoveAll
46 #define MAYBE_TokenServiceGetSet TokenServiceGetSet
47 #define MAYBE_TokenServiceRemove TokenServiceRemove
50 TEST_F(TokenServiceTableTest, MAYBE_TokenServiceGetAllRemoveAll) {
51 std::map<std::string, std::string> out_map;
54 service = "testservice";
55 service2 = "othertestservice";
57 EXPECT_TRUE(table_->GetAllTokens(&out_map));
58 EXPECT_TRUE(out_map.empty());
60 // Check that get all tokens works
61 EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni"));
62 EXPECT_TRUE(table_->SetTokenForService(service2, "steak"));
63 EXPECT_TRUE(table_->GetAllTokens(&out_map));
64 EXPECT_EQ("pepperoni", out_map.find(service)->second);
65 EXPECT_EQ("steak", out_map.find(service2)->second);
69 EXPECT_TRUE(table_->RemoveAllTokens());
70 EXPECT_TRUE(table_->GetAllTokens(&out_map));
71 EXPECT_TRUE(out_map.empty());
73 // Check that you can still add it back in
74 EXPECT_TRUE(table_->SetTokenForService(service, "cheese"));
75 EXPECT_TRUE(table_->GetAllTokens(&out_map));
76 EXPECT_EQ("cheese", out_map.find(service)->second);
79 TEST_F(TokenServiceTableTest, MAYBE_TokenServiceGetSet) {
80 std::map<std::string, std::string> out_map;
82 service = "testservice";
84 EXPECT_TRUE(table_->GetAllTokens(&out_map));
85 EXPECT_TRUE(out_map.empty());
87 EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni"));
88 EXPECT_TRUE(table_->GetAllTokens(&out_map));
89 EXPECT_EQ("pepperoni", out_map.find(service)->second);
92 // try blanking it - won't remove it from the db though!
93 EXPECT_TRUE(table_->SetTokenForService(service, std::string()));
94 EXPECT_TRUE(table_->GetAllTokens(&out_map));
95 EXPECT_EQ("", out_map.find(service)->second);
99 EXPECT_TRUE(table_->SetTokenForService(service, "ham"));
100 EXPECT_TRUE(table_->GetAllTokens(&out_map));
101 EXPECT_EQ("ham", out_map.find(service)->second);
104 TEST_F(TokenServiceTableTest, MAYBE_TokenServiceRemove) {
105 std::map<std::string, std::string> out_map;
107 std::string service2;
108 service = "testservice";
109 service2 = "othertestservice";
111 EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni"));
112 EXPECT_TRUE(table_->SetTokenForService(service2, "steak"));
113 EXPECT_TRUE(table_->RemoveTokenForService(service));
114 EXPECT_TRUE(table_->GetAllTokens(&out_map));
115 EXPECT_EQ(0u, out_map.count(service));
116 EXPECT_EQ("steak", out_map.find(service2)->second);