File: content_lofi_ui_service_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (112 lines) | stat: -rw-r--r-- 3,964 bytes parent folder | download
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
// Copyright 2015 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 "components/data_reduction_proxy/content/browser/content_lofi_ui_service.h"

#include <stddef.h>

#include <memory>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/previews_state.h"
#include "content/public/test/test_renderer_host.h"
#include "net/socket/socket_test_util.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace data_reduction_proxy {

class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness {
 public:
  ContentLoFiUIServiceTest() : callback_called_(false) {
    // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness.
    SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD);
  }

  void RunTestOnIOThread(base::RunLoop* ui_run_loop) {
    ASSERT_TRUE(ui_run_loop);
    EXPECT_TRUE(
        content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

    net::TestURLRequestContext context(true);
    net::MockClientSocketFactory mock_socket_factory;
    net::TestDelegate delegate;
    context.set_client_socket_factory(&mock_socket_factory);
    context.Init();

    content_lofi_ui_service_.reset(new ContentLoFiUIService(
        content::BrowserThread::GetTaskRunnerForThread(
            content::BrowserThread::UI),
        base::Bind(&ContentLoFiUIServiceTest::OnLoFiResponseReceivedCallback,
                   base::Unretained(this))));

    std::unique_ptr<net::URLRequest> request =
        CreateRequest(context, &delegate);

    content_lofi_ui_service_->OnLoFiReponseReceived(*request);

    content::BrowserThread::PostTask(
        content::BrowserThread::UI, FROM_HERE,
        base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop)));
  }

  std::unique_ptr<net::URLRequest> CreateRequest(
      const net::TestURLRequestContext& context,
      net::TestDelegate* delegate) {
    EXPECT_TRUE(
        content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

    std::unique_ptr<net::URLRequest> request = context.CreateRequest(
        GURL("http://www.google.com/"), net::IDLE, delegate);

    content::ResourceRequestInfo::AllocateForTesting(
        request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL,
        web_contents()->GetMainFrame()->GetProcess()->GetID(), -1,
        web_contents()->GetMainFrame()->GetRoutingID(),
        /*is_main_frame=*/false,
        /*parent_is_main_frame=*/false,
        /*allow_download=*/false,
        /*is_async=*/false, content::SERVER_LOFI_ON);

    return request;
  }

  void OnLoFiResponseReceivedCallback(content::WebContents* web_contents) {
    EXPECT_TRUE(
        content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
    callback_called_ = true;
  }

  void VerifyOnLoFiResponseReceivedCallback() {
    EXPECT_TRUE(
        content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
    EXPECT_TRUE(callback_called_);
  }

 private:
  std::unique_ptr<ContentLoFiUIService> content_lofi_ui_service_;
  bool callback_called_;
};

TEST_F(ContentLoFiUIServiceTest, OnLoFiResponseReceived) {
  base::RunLoop ui_run_loop;
  content::BrowserThread::PostTask(
      content::BrowserThread::IO, FROM_HERE,
      base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread,
                 base::Unretained(this), &ui_run_loop));
  ui_run_loop.Run();
  base::RunLoop().RunUntilIdle();
  VerifyOnLoFiResponseReceivedCallback();
}

}  // namespace data_reduction_proxy