summaryrefslogtreecommitdiffstats
path: root/ios/consumer/base/supports_user_data_unittest.cc
blob: b38c2db922d93d632c365c8377fbfa6e9cb16a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/consumer/public/base/supports_user_data.h"

#include "base/memory/scoped_ptr.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

namespace ios {

namespace {
  const char* kTestData1Key = "test_data1";
  const char* kTestData2Key = "test_data2";
}  // namespace

class TestData : public SupportsUserData::Data {
 public:
  TestData(bool* was_destroyed)
      : was_destroyed_(was_destroyed) {
    *was_destroyed_ = false;
  }
  virtual ~TestData() {
    *was_destroyed_ = true;
  }

 private:
  bool* was_destroyed_;
};

class TestSupportsUserData : public SupportsUserData {
 public:
  TestSupportsUserData() {}
  virtual ~TestSupportsUserData() {}
};

class SupportsUserDataTest : public PlatformTest {
 public:
  virtual void SetUp() OVERRIDE {
    PlatformTest::SetUp();

    test_data1_was_destroyed_ = false;
    test_data1_ = new TestData(&test_data1_was_destroyed_);
    test_data2_was_destroyed_ = false;
    test_data2_ = new TestData(&test_data2_was_destroyed_);
    supports_user_data_.reset(new TestSupportsUserData());
  }

  virtual void TearDown() OVERRIDE {
    if (!test_data1_was_destroyed_ &&
        supports_user_data_ &&
        supports_user_data_->GetUserData(kTestData1Key) != test_data1_)
      delete test_data1_;
    if (!test_data2_was_destroyed_ &&
        supports_user_data_ &&
        supports_user_data_->GetUserData(kTestData2Key) != test_data2_)
      delete test_data2_;

    PlatformTest::TearDown();
  }

 protected:
  scoped_ptr<TestSupportsUserData> supports_user_data_;
  bool test_data1_was_destroyed_;
  TestData* test_data1_;
  bool test_data2_was_destroyed_;
  TestData* test_data2_;
};

TEST_F(SupportsUserDataTest, SetAndGetData) {
  EXPECT_FALSE(supports_user_data_->GetUserData(kTestData1Key));
  supports_user_data_->SetUserData(kTestData1Key, test_data1_);
  EXPECT_EQ(supports_user_data_->GetUserData(kTestData1Key), test_data1_);
}

TEST_F(SupportsUserDataTest, DataDestroyedOnDestruction) {
  EXPECT_FALSE(supports_user_data_->GetUserData(kTestData1Key));
  supports_user_data_->SetUserData(kTestData1Key, test_data1_);
  EXPECT_FALSE(test_data1_was_destroyed_);
  supports_user_data_.reset();
  EXPECT_TRUE(test_data1_was_destroyed_);
}

TEST_F(SupportsUserDataTest, DataDestroyedOnRemoval) {
  EXPECT_FALSE(supports_user_data_->GetUserData(kTestData1Key));
  supports_user_data_->SetUserData(kTestData1Key, test_data1_);
  EXPECT_FALSE(test_data1_was_destroyed_);
  supports_user_data_->RemoveUserData(kTestData1Key);
  EXPECT_TRUE(test_data1_was_destroyed_);
}

TEST_F(SupportsUserDataTest, DistinctDataStoredSeparately) {
  EXPECT_FALSE(supports_user_data_->GetUserData(kTestData2Key));
  supports_user_data_->SetUserData(kTestData1Key, test_data1_);
  EXPECT_FALSE(supports_user_data_->GetUserData(kTestData2Key));
  supports_user_data_->SetUserData(kTestData2Key, test_data2_);
  EXPECT_EQ(supports_user_data_->GetUserData(kTestData2Key), test_data2_);
}

TEST_F(SupportsUserDataTest, DistinctDataDestroyedSeparately) {
  supports_user_data_->SetUserData(kTestData1Key, test_data1_);
  supports_user_data_->SetUserData(kTestData2Key, test_data2_);
  EXPECT_FALSE(test_data1_was_destroyed_);
  EXPECT_FALSE(test_data2_was_destroyed_);

  supports_user_data_->RemoveUserData(kTestData2Key);
  EXPECT_FALSE(test_data1_was_destroyed_);
  EXPECT_TRUE(test_data2_was_destroyed_);

  supports_user_data_.reset();
  EXPECT_TRUE(test_data1_was_destroyed_);
}

}  // namespace ios