Enable Enhanced Bookmark on Android Tablet
[chromium-blink-merge.git] / athena / content / content_proxy_browsertest.cc
blobd9601b5bef8634029620b0ebea9b5f883269162e
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 "athena/activity/public/activity.h"
6 #include "athena/activity/public/activity_view_model.h"
7 #include "athena/resource_manager/public/resource_manager.h"
8 #include "athena/test/base/test_util.h"
9 #include "athena/test/chrome/athena_chrome_browser_test.h"
10 #include "base/command_line.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "content/public/browser/web_contents.h"
15 #include "ui/compositor/compositor_switches.h"
16 #include "ui/gfx/image/image_skia.h"
17 #include "ui/gfx/size.h"
18 #include "url/gurl.h"
20 namespace athena {
22 namespace {
23 // The test URL to navigate to.
24 const char kTestUrl[] = "chrome:about";
26 const int kTimeoutMS = 12000; // The timeout: 2 seconds.
27 const int kIterationSleepMS = 5; // The wait time in ms per iteration.
30 // Need to override the test class to make the test always draw its content.
31 class ContentProxyBrowserTest : public AthenaChromeBrowserTest {
32 public:
33 ContentProxyBrowserTest() {}
34 ~ContentProxyBrowserTest() override {}
36 // Make sure that we have rendered the page that a read back will succeed.
37 void WaitForRendererToBeFinished(Activity* activity) {
38 int timeout_counter = kTimeoutMS / kIterationSleepMS;
39 do {
40 content::RenderViewHost* host =
41 activity->GetWebContents()->GetRenderViewHost();
43 if (host && host->GetView() &&
44 host->GetView()->IsSurfaceAvailableForCopy())
45 return;
47 usleep(kIterationSleepMS * 1000);
48 test_util::WaitUntilIdle();
49 } while (--timeout_counter);
51 NOTREACHED() << "Renderer did not get finished rendering.";
54 // AthenaBrowserTest:
55 void SetUpCommandLine(base::CommandLine* command_line) override {
56 // Make sure that we draw the output - it's required for this test.
57 command_line->AppendSwitch(switches::kEnablePixelOutputInTests);
58 AthenaChromeBrowserTest::SetUpCommandLine(command_line);
61 private:
62 DISALLOW_COPY_AND_ASSIGN(ContentProxyBrowserTest);
65 IN_PROC_BROWSER_TEST_F(ContentProxyBrowserTest, CreateContent) {
66 const GURL gurl(kTestUrl);
67 content::BrowserContext* context = GetBrowserContext();
68 // Create an activity (and wait until it is loaded).
69 // The size of its overview image should be empty since it is visible.
70 Activity* activity1 =
71 test_util::CreateTestWebActivity(context,
72 base::UTF8ToUTF16("App1"),
73 gurl);
75 DCHECK_EQ(activity1->GetActivityViewModel()->GetOverviewModeImage()
76 .size().ToString(),
77 gfx::Size().ToString());
79 // Allow the activity time to start the renderer. Locally this was not a
80 // problem in over 150 runs, but the try server shows flakiness.
81 WaitForRendererToBeFinished(activity1);
83 // Create another activity. The size of all overview images should be empty
84 // since they have the visible state.
85 Activity* activity2 =
86 test_util::CreateTestWebActivity(context,
87 base::UTF8ToUTF16("App2"),
88 gurl);
89 DCHECK_EQ(activity1->GetActivityViewModel()->GetOverviewModeImage()
90 .size().ToString(),
91 gfx::Size().ToString());
92 DCHECK_EQ(activity2->GetActivityViewModel()->GetOverviewModeImage()
93 .size().ToString(),
94 gfx::Size().ToString());
96 // As above.
97 WaitForRendererToBeFinished(activity2);
99 // Turn the activity invisible which should create the ContentProxy.
100 activity1->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
101 test_util::WaitUntilIdle();
102 DCHECK_EQ(activity1->GetCurrentState(), Activity::ACTIVITY_INVISIBLE);
104 // Wait until an image is loaded, but do not give more then 2 seconds.
105 gfx::ImageSkia image;
106 int iteration = 0;
107 while (image.isNull()) {
108 // TODO(skuhne): If we add an observer to track the creation, we should add
109 // its use here.
110 image = activity1->GetActivityViewModel()->GetOverviewModeImage();
111 if (++iteration > kTimeoutMS / kIterationSleepMS) {
112 LOG(ERROR) << "Timout on reading back the content image.";
113 break;
115 test_util::WaitUntilIdle();
116 usleep(1000 * kIterationSleepMS);
119 // Check that the image of the old activity has now a usable size.
120 DCHECK_NE(activity1->GetActivityViewModel()->GetOverviewModeImage()
121 .size().ToString(),
122 gfx::Size().ToString());
123 DCHECK_EQ(activity2->GetActivityViewModel()->GetOverviewModeImage()
124 .size().ToString(),
125 gfx::Size().ToString());
127 // After the Activity gets entirely unloaded, the image should still be there.
128 activity1->SetCurrentState(Activity::ACTIVITY_UNLOADED);
129 test_util::WaitUntilIdle();
130 DCHECK_EQ(activity1->GetCurrentState(), Activity::ACTIVITY_UNLOADED);
132 DCHECK_NE(activity1->GetActivityViewModel()->GetOverviewModeImage()
133 .size().ToString(),
134 gfx::Size().ToString());
136 // When it then becomes visible again, the image will be gone.
137 activity1->SetCurrentState(Activity::ACTIVITY_VISIBLE);
138 test_util::WaitUntilIdle();
139 DCHECK_EQ(activity1->GetCurrentState(), Activity::ACTIVITY_VISIBLE);
141 DCHECK_EQ(activity1->GetActivityViewModel()->GetOverviewModeImage()
142 .size().ToString(),
143 gfx::Size().ToString());
146 } // namespace athena