summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/text_input_client_mac_unittest.mm
blob: 6870064e81360133b829b80ae52a0d8db039a655 (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
// Copyright (c) 2011 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.

#import "chrome/browser/renderer_host/text_input_client_mac.h"

#include "base/bind.h"
#include "base/message_loop.h"
#include "base/threading/thread.h"
#include "chrome/common/text_input_client_messages.h"
#include "chrome/test/testing_profile.h"
#include "content/browser/renderer_host/mock_render_process_host.h"
#include "content/browser/renderer_host/render_process_host.h"
#include "content/browser/renderer_host/render_widget_host.h"
#include "ipc/ipc_test_sink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"

namespace {

// This test does not test the WebKit side of the dictionary system (which
// performs the actual data fetching), but rather this just tests that the
// service's signaling system works.
class TextInputClientMacTest : public testing::Test {
 public:
  TextInputClientMacTest()
      : message_loop_(MessageLoop::TYPE_UI),
        profile_(),
        process_factory_(),
        widget_(process_factory_.CreateRenderProcessHost(&profile_), 1),
        thread_("TextInputClientMacTestThread") {}

  // Accessor for the TextInputClientMac instance.
  TextInputClientMac* service() {
    return TextInputClientMac::GetInstance();
  }

  // Helper method to post a task on the testing thread's MessageLoop after
  // a short delay.
  void PostTask(const base::Closure& task) {
    const int64 kTaskDelayMs = 200;
    thread_.message_loop()->PostDelayedTask(FROM_HERE, task, kTaskDelayMs);
  }

  RenderWidgetHost* widget() {
    return &widget_;
  }

  IPC::TestSink& ipc_sink() {
    return static_cast<MockRenderProcessHost*>(widget()->process())->sink();
  }

 private:
  friend class ScopedTestingThread;

  MessageLoop message_loop_;
  TestingProfile profile_;

  // Gets deleted when the last RWH in the "process" gets destroyed.
  MockRenderProcessHostFactory process_factory_;
  RenderWidgetHost widget_;

  base::Thread thread_;
};

////////////////////////////////////////////////////////////////////////////////

// Helper class that Start()s and Stop()s a thread according to the scope of the
// object.
class ScopedTestingThread {
 public:
  ScopedTestingThread(TextInputClientMacTest* test) : thread_(test->thread_) {
    thread_.Start();
  }
  ~ScopedTestingThread() {
    thread_.Stop();
  }

 private:
  base::Thread& thread_;
};

// Test Cases //////////////////////////////////////////////////////////////////

TEST_F(TextInputClientMacTest, GetCharacterIndex) {
  ScopedTestingThread thread(this);
  const NSUInteger kSuccessValue = 42;

  PostTask(base::Bind(&TextInputClientMac::SetCharacterIndexAndSignal,
      base::Unretained(service()), kSuccessValue));
  NSUInteger index = service()->GetCharacterIndexAtPoint(
      widget(), gfx::Point(2, 2));

  EXPECT_EQ(1U, ipc_sink().message_count());
  EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching(
      TextInputClientMsg_CharacterIndexForPoint::ID));
  EXPECT_EQ(kSuccessValue, index);
}

TEST_F(TextInputClientMacTest, TimeoutCharacterIndex) {
  NSUInteger index = service()->GetCharacterIndexAtPoint(
      widget(), gfx::Point(2, 2));
  EXPECT_EQ(1U, ipc_sink().message_count());
  EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching(
      TextInputClientMsg_CharacterIndexForPoint::ID));
  EXPECT_EQ(NSNotFound, index);
}

TEST_F(TextInputClientMacTest, GetRectForRange) {
  ScopedTestingThread thread(this);
  const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45);

  PostTask(base::Bind(&TextInputClientMac::SetFirstRectAndSignal,
      base::Unretained(service()), kSuccessValue));
  NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32));

  EXPECT_EQ(1U, ipc_sink().message_count());
  EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching(
      TextInputClientMsg_FirstRectForCharacterRange::ID));
  EXPECT_TRUE(NSEqualRects(kSuccessValue, rect));
}

TEST_F(TextInputClientMacTest, TimeoutRectForRange) {
  NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32));
  EXPECT_EQ(1U, ipc_sink().message_count());
  EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching(
      TextInputClientMsg_FirstRectForCharacterRange::ID));
  EXPECT_TRUE(NSEqualRects(NSZeroRect, rect));
}

TEST_F(TextInputClientMacTest, GetSubstring) {
  ScopedTestingThread thread(this);
  NSDictionary* attributes =
      [NSDictionary dictionaryWithObject:[NSColor purpleColor]
                                  forKey:NSForegroundColorAttributeName];
  scoped_nsobject<NSAttributedString> kSuccessValue(
      [[NSAttributedString alloc] initWithString:@"Barney is a purple dinosaur"
                                      attributes:attributes]);

  PostTask(base::Bind(&TextInputClientMac::SetSubstringAndSignal,
      base::Unretained(service()), base::Unretained(kSuccessValue.get())));
  NSAttributedString* string = service()->GetAttributedSubstringFromRange(
      widget(), NSMakeRange(0, 32));

  EXPECT_NSEQ(kSuccessValue, string);
  EXPECT_NE(kSuccessValue.get(), string);  // |string| should be a copy.
  EXPECT_EQ(1U, ipc_sink().message_count());
  EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching(
      TextInputClientMsg_StringForRange::ID));
}

TEST_F(TextInputClientMacTest, TimeoutSubstring) {
  NSAttributedString* string = service()->GetAttributedSubstringFromRange(
      widget(), NSMakeRange(0, 32));
  EXPECT_EQ(nil, string);
  EXPECT_EQ(1U, ipc_sink().message_count());
  EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching(
      TextInputClientMsg_StringForRange::ID));
}

}  // namespace