summaryrefslogtreecommitdiffstats
path: root/chrome/browser/instant/instant_loader_manager_unittest.cc
blob: d76f8f39d981396ac5eedac53b552cbb28e0fa9a (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2010 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 "base/scoped_ptr.h"
#include "chrome/browser/instant/instant_loader.h"
#include "chrome/browser/instant/instant_loader_delegate.h"
#include "chrome/browser/instant/instant_loader_manager.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class InstantLoaderDelegateImpl : public InstantLoaderDelegate {
 public:
  InstantLoaderDelegateImpl() {}

  virtual void ShowInstantLoader(InstantLoader* loader) {}

  virtual void SetSuggestedTextFor(InstantLoader* loader,
                                   const string16& text) {}

  virtual gfx::Rect GetInstantBounds() {
    return gfx::Rect();
  }

  virtual bool ShouldCommitInstantOnMouseUp() {
    return false;
  }

  virtual void CommitInstantLoader(InstantLoader* loader) {
  }

  virtual void InstantLoaderDoesntSupportInstant(InstantLoader* loader,
                                                 bool needs_reload,
                                                 const GURL& url_to_load) {
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(InstantLoaderDelegateImpl);
};

}

class InstantLoaderManagerTest : public testing::Test {
 public:
  InstantLoaderManagerTest() {}

  void MarkReady(InstantLoader* loader) {
    loader->ready_ = true;
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(InstantLoaderManagerTest);
};

// Makes sure UpdateLoader works when invoked once.
TEST_F(InstantLoaderManagerTest, Basic) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(0, &loader);
  EXPECT_EQ(NULL, loader.get());
  EXPECT_TRUE(manager.current_loader());
  EXPECT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(0, manager.current_loader()->template_url_id());
}

// Make sure invoking update twice for non-instant results keeps the same
// loader.
TEST_F(InstantLoaderManagerTest, UpdateTwice) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(0, &loader);
  InstantLoader* current_loader = manager.current_loader();
  manager.UpdateLoader(0, &loader);
  EXPECT_EQ(NULL, loader.get());
  EXPECT_EQ(current_loader, manager.current_loader());
  EXPECT_EQ(NULL, manager.pending_loader());
}

// Make sure invoking update twice for instant results keeps the same loader.
TEST_F(InstantLoaderManagerTest, UpdateInstantTwice) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(1, &loader);
  InstantLoader* current_loader = manager.current_loader();
  manager.UpdateLoader(1, &loader);
  EXPECT_EQ(NULL, loader.get());
  EXPECT_EQ(current_loader, manager.current_loader());
  EXPECT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(1u, manager.num_instant_loaders());
}

// Makes sure transitioning from non-instant to instant works.
TEST_F(InstantLoaderManagerTest, NonInstantToInstant) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(0, &loader);
  InstantLoader* current_loader = manager.current_loader();
  manager.UpdateLoader(1, &loader);
  EXPECT_TRUE(loader.get() != NULL);
  EXPECT_NE(current_loader, manager.current_loader());
  EXPECT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(1u, manager.num_instant_loaders());
}

// Makes sure instant loaders aren't deleted when invoking update with different
// ids.
TEST_F(InstantLoaderManagerTest, DontDeleteInstantLoaders) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(1, &loader);
  InstantLoader* current_loader = manager.current_loader();
  manager.UpdateLoader(2, &loader);
  EXPECT_EQ(NULL, loader.get());
  EXPECT_NE(current_loader, manager.current_loader());
  EXPECT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(2u, manager.num_instant_loaders());
}

// Makes sure a new loader is created and assigned to secondary when
// transitioning from a ready non-instant to instant.
TEST_F(InstantLoaderManagerTest, CreateSecondaryWhenReady) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(0, &loader);
  InstantLoader* current_loader = manager.current_loader();
  ASSERT_TRUE(current_loader);
  MarkReady(current_loader);

  manager.UpdateLoader(1, &loader);
  EXPECT_EQ(NULL, loader.get());
  EXPECT_EQ(current_loader, manager.current_loader());
  EXPECT_TRUE(manager.pending_loader());
  EXPECT_NE(current_loader, manager.pending_loader());
  EXPECT_EQ(1u, manager.num_instant_loaders());

  // Make the pending loader current.
  InstantLoader* pending_loader = manager.pending_loader();
  manager.MakePendingCurrent(&loader);
  EXPECT_TRUE(loader.get());
  EXPECT_EQ(pending_loader, manager.current_loader());
  EXPECT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(1u, manager.num_instant_loaders());
}

// Makes sure releasing an instant updates maps currectly.
TEST_F(InstantLoaderManagerTest, ReleaseInstant) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(1, &loader);
  scoped_ptr<InstantLoader> current_loader(manager.ReleaseCurrentLoader());
  EXPECT_TRUE(current_loader.get());
  EXPECT_EQ(NULL, manager.current_loader());
  EXPECT_EQ(0u, manager.num_instant_loaders());
}

// Tests transitioning from a non-instant ready loader to an instant ready
// loader is immediate.
TEST_F(InstantLoaderManagerTest, NonInstantToInstantWhenReady) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(1, &loader);
  ASSERT_TRUE(manager.current_loader());
  EXPECT_EQ(1, manager.current_loader()->template_url_id());
  InstantLoader* instant_loader = manager.current_loader();

  manager.UpdateLoader(0, &loader);
  InstantLoader* non_instant_loader = manager.current_loader();
  ASSERT_TRUE(non_instant_loader);
  MarkReady(non_instant_loader);
  EXPECT_NE(non_instant_loader, instant_loader);

  MarkReady(instant_loader);
  manager.UpdateLoader(1, &loader);
  EXPECT_EQ(non_instant_loader, loader.get());
  EXPECT_EQ(instant_loader, manager.current_loader());
  EXPECT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(1u, manager.num_instant_loaders());
}

// Tests transitioning between 3 instant loaders, all ready.
TEST_F(InstantLoaderManagerTest, ThreeInstant) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(1, &loader);
  ASSERT_TRUE(manager.current_loader());
  EXPECT_EQ(1, manager.current_loader()->template_url_id());
  InstantLoader* instant_loader1 = manager.current_loader();
  MarkReady(instant_loader1);

  manager.UpdateLoader(2, &loader);
  InstantLoader* instant_loader2 = manager.pending_loader();
  ASSERT_TRUE(instant_loader2);
  EXPECT_EQ(2, instant_loader2->template_url_id());
  EXPECT_NE(instant_loader1, instant_loader2);
  EXPECT_EQ(instant_loader1, manager.current_loader());

  manager.UpdateLoader(3, &loader);
  InstantLoader* instant_loader3 = manager.pending_loader();
  ASSERT_TRUE(instant_loader3);
  EXPECT_EQ(3, instant_loader3->template_url_id());
  EXPECT_NE(instant_loader1, instant_loader3);
  EXPECT_NE(instant_loader2, instant_loader3);
  EXPECT_EQ(instant_loader1, manager.current_loader());
}

// Tests DestroyLoader with an instant loader.
TEST_F(InstantLoaderManagerTest, DestroyInstantLoader) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(1, &loader);
  ASSERT_TRUE(manager.current_loader());
  EXPECT_EQ(1, manager.current_loader()->template_url_id());
  // Now destroy it.
  manager.DestroyLoader(manager.current_loader());

  // There should be no current, pending and 0 instant loaders.
  ASSERT_EQ(NULL, manager.current_loader());
  ASSERT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(0u, manager.num_instant_loaders());
}

// Tests DestroyLoader when the loader is pending.
TEST_F(InstantLoaderManagerTest, DestroyPendingLoader) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;
  manager.UpdateLoader(1, &loader);
  InstantLoader* first_loader = manager.active_loader();
  MarkReady(first_loader);

  // Create another loader.
  manager.UpdateLoader(0, &loader);
  InstantLoader* second_loader = manager.pending_loader();
  ASSERT_TRUE(second_loader);
  ASSERT_NE(second_loader, first_loader);

  // Destroy it.
  manager.DestroyLoader(second_loader);
  EXPECT_EQ(NULL, manager.pending_loader());
  EXPECT_EQ(first_loader, manager.current_loader());
}

// Makes sure WillUpateChangeActiveLoader works.
TEST_F(InstantLoaderManagerTest, WillUpateChangeActiveLoader) {
  InstantLoaderDelegateImpl delegate;
  InstantLoaderManager manager(&delegate);
  scoped_ptr<InstantLoader> loader;

  // When there is no loader WillUpateChangeActiveLoader should return true.
  EXPECT_TRUE(manager.WillUpateChangeActiveLoader(0));
  EXPECT_TRUE(manager.WillUpateChangeActiveLoader(1));

  // Add a loder with id 0 and test again.
  manager.UpdateLoader(0, &loader);
  EXPECT_FALSE(manager.WillUpateChangeActiveLoader(0));
  EXPECT_TRUE(manager.WillUpateChangeActiveLoader(1));
  ASSERT_TRUE(manager.active_loader());
  MarkReady(manager.active_loader());

  // Add a loader with id 1 and test again.
  manager.UpdateLoader(1, &loader);
  EXPECT_TRUE(manager.WillUpateChangeActiveLoader(0));
  EXPECT_FALSE(manager.WillUpateChangeActiveLoader(1));
}