summaryrefslogtreecommitdiffstats
path: root/webkit/appcache/appcache_group_unittest.cc
blob: bcfbce05499ccfb796fe92859d1f20ae3ba6ac76 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2009 The Chromium Authos. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/appcache/appcache.h"
#include "webkit/appcache/appcache_group.h"
#include "webkit/appcache/appcache_host.h"
#include "webkit/appcache/appcache_service.h"

namespace {

class TestAppCacheFrontend : public appcache::AppCacheFrontend {
 public:
  TestAppCacheFrontend()
      : last_host_id_(-1), last_cache_id_(-1),
        last_status_(appcache::OBSOLETE) {
  }

  virtual void OnCacheSelected(int host_id, int64 cache_id ,
                               appcache::Status status) {
    last_host_id_ = host_id;
    last_cache_id_ = cache_id;
    last_status_ = status;
  }

  virtual void OnStatusChanged(const std::vector<int>& host_ids,
                               appcache::Status status) {
  }

  virtual void OnEventRaised(const std::vector<int>& host_ids,
                             appcache::EventID event_id) {
  }

  int last_host_id_;
  int64 last_cache_id_;
  appcache::Status last_status_;
};

}  // namespace anon

namespace appcache {

class AppCacheGroupTest : public testing::Test {
};

TEST(AppCacheGroupTest, AddRemoveCache) {
  AppCacheService service;
  scoped_refptr<AppCacheGroup> group =
      new AppCacheGroup(&service, GURL::EmptyGURL());

  base::TimeTicks ticks = base::TimeTicks::Now();

  AppCache* cache1 = new AppCache(&service, 111);
  cache1->set_complete(true);
  cache1->set_update_time(ticks);
  cache1->set_owning_group(group);
  group->AddCache(cache1);
  EXPECT_EQ(cache1, group->newest_complete_cache());

  // Adding older cache does not change newest complete cache.
  AppCache* cache2 = new AppCache(&service, 222);
  cache2->set_complete(true);
  cache2->set_update_time(ticks - base::TimeDelta::FromDays(1));
  cache2->set_owning_group(group);
  group->AddCache(cache2);
  EXPECT_EQ(cache1, group->newest_complete_cache());

  // Adding newer cache does change newest complete cache.
  AppCache* cache3 = new AppCache(&service, 333);
  cache3->set_complete(true);
  cache3->set_update_time(ticks + base::TimeDelta::FromDays(1));
  cache3->set_owning_group(group);
  group->AddCache(cache3);
  EXPECT_EQ(cache3, group->newest_complete_cache());

  // Old caches can always be removed.
  EXPECT_TRUE(group->RemoveCache(cache1));
  EXPECT_EQ(cache3, group->newest_complete_cache());  // newest unchanged

  // Cannot remove newest cache if there are older caches.
  EXPECT_FALSE(group->RemoveCache(cache3));
  EXPECT_EQ(cache3, group->newest_complete_cache());  // newest unchanged

  // Can remove newest cache after all older caches are removed.
  EXPECT_TRUE(group->RemoveCache(cache2));
  EXPECT_EQ(cache3, group->newest_complete_cache());  // newest unchanged
  EXPECT_TRUE(group->RemoveCache(cache3));
}

TEST(AppCacheGroupTest, CleanupUnusedGroup) {
  AppCacheService service;
  TestAppCacheFrontend frontend;
  AppCacheGroup* group = new AppCacheGroup(&service, GURL::EmptyGURL());

  AppCacheHost host1(1, &frontend, &service);
  AppCacheHost host2(2, &frontend, &service);

  base::TimeTicks ticks = base::TimeTicks::Now();

  AppCache* cache1 = new AppCache(&service, 111);
  cache1->set_complete(true);
  cache1->set_update_time(ticks);
  cache1->set_owning_group(group);
  group->AddCache(cache1);
  EXPECT_EQ(cache1, group->newest_complete_cache());

  host1.AssociateCache(cache1);
  EXPECT_EQ(frontend.last_host_id_, host1.host_id());
  EXPECT_EQ(frontend.last_cache_id_, cache1->cache_id());
  EXPECT_EQ(frontend.last_status_, appcache::IDLE);

  host2.AssociateCache(cache1);
  EXPECT_EQ(frontend.last_host_id_, host2.host_id());
  EXPECT_EQ(frontend.last_cache_id_, cache1->cache_id());
  EXPECT_EQ(frontend.last_status_, appcache::IDLE);

  AppCache* cache2 = new AppCache(&service, 222);
  cache2->set_complete(true);
  cache2->set_update_time(ticks + base::TimeDelta::FromDays(1));
  cache2->set_owning_group(group);
  group->AddCache(cache2);
  EXPECT_EQ(cache2, group->newest_complete_cache());

  // Unassociate all hosts from older cache.
  host1.AssociateCache(NULL);
  host2.AssociateCache(NULL);
  EXPECT_EQ(frontend.last_host_id_, host2.host_id());
  EXPECT_EQ(frontend.last_cache_id_, appcache::kNoCacheId);
  EXPECT_EQ(frontend.last_status_, appcache::UNCACHED);
}

}  // namespace appcache